Skip to content

Commit

Permalink
Add more cases to rust definition of RGate
Browse files Browse the repository at this point in the history
We have done all float parameters. This commit adds all expression (variable)
parameters. It remains to implement heterogeneous parameters.

* Run formatting tools on source
  • Loading branch information
jlapeyre committed Jun 5, 2024
1 parent d52943a commit b373e4d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
7 changes: 3 additions & 4 deletions crates/circuit/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ static mut STDGATE_PYTHON_GATES: GILOnceCell<[Option<PyObject>; STANDARD_GATE_SI
fn _array_of_option_pyobject() -> [Option<PyObject>; STANDARD_GATE_SIZE] {
use std::mem::{self, MaybeUninit};
let array = unsafe {
let mut data: [MaybeUninit<Option<PyObject>>; STANDARD_GATE_SIZE] = MaybeUninit::uninit().assume_init();
let mut data: [MaybeUninit<Option<PyObject>>; STANDARD_GATE_SIZE] =
MaybeUninit::uninit().assume_init();
for elem in &mut data[..] {
*elem = MaybeUninit::new(None);
}
Expand Down Expand Up @@ -135,9 +136,7 @@ pub fn populate_std_gate_map(py: Python, rs_gate: StandardGate, py_gate: PyObjec

#[inline]
pub fn get_std_gate_class(py: Python, rs_gate: StandardGate) -> PyResult<PyObject> {
let gate_map = unsafe {
STDGATE_PYTHON_GATES.get_or_init(py, _array_of_option_pyobject)
};
let gate_map = unsafe { STDGATE_PYTHON_GATES.get_or_init(py, _array_of_option_pyobject) };
let gate = &gate_map[rs_gate as usize];
let populate = gate.is_none();
let out_gate = match gate {
Expand Down
44 changes: 27 additions & 17 deletions crates/circuit/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,28 +627,38 @@ impl Operation for StandardGate {
Self::RGate => Python::with_gil(|py| -> Option<CircuitData> {
let params = params.unwrap();
let defparams = match (&params[0], &params[1]) {
(Param::Float(theta), Param::Float(phi)) =>
(Param::Float(theta), Param::Float(phi)) => [
Param::Float(*theta),
Param::Float(*phi - 1.0),
Param::Float(-*phi + 1.0),
],
(Param::ParameterExpression(theta), Param::ParameterExpression(phi)) => {
let thetaexpr = Param::ParameterExpression(theta.clone_ref(py));
let phiexpr1 = phi
.call_method1(py, intern!(py, "__add__"), ((-PI / 2.0),))
.expect("Unexpected Qiskit python bug");
let phiexpr2 = phiexpr1
.clone()
.call_method1(py, intern!(py, "__rmul__"), (-1.0,))
.expect("Unexpected Qiskit python bug");
[
Param::Float(*theta),
Param::Float(*phi - PI / 2.),
Param::Float(-*phi + PI / 2.),
],
thetaexpr,
Param::ParameterExpression(phiexpr1),
Param::ParameterExpression(phiexpr2),
]
}
_ => todo!(),
};
Some(
CircuitData::build_new_from(
py,
1,
0,
&[(
OperationType::Standard(Self::UGate),
&defparams,
&[0],
)],
FLOAT_ZERO,
)
.expect("Unexpected Qiskit python bug"),
CircuitData::build_new_from(
py,
1,
0,
&[(OperationType::Standard(Self::UGate), &defparams, &[0])],
FLOAT_ZERO,
)
.expect("Unexpected Qiskit python bug"),
)
}),
}
}
Expand Down
11 changes: 9 additions & 2 deletions qiskit/circuit/library/standard_gates/r.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from qiskit.circuit.parameterexpression import ParameterValueType
from qiskit._accelerate.circuit import StandardGate


class RGate(Gate):
r"""Rotation θ around the cos(φ)x + sin(φ)y axis.
Expand Down Expand Up @@ -61,8 +62,14 @@ def __init__(
unit="dt",
):
"""Create new r single-qubit gate."""
super().__init__("r", 1, [theta, phi], label=label, duration=duration, unit=unit,
)
super().__init__(
"r",
1,
[theta, phi],
label=label,
duration=duration,
unit=unit,
)

def _define(self):
"""
Expand Down

0 comments on commit b373e4d

Please sign in to comment.