From 87aa89c19387ef7ff4177ef5144f60119da392e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= <57907331+ElePT@users.noreply.github.com> Date: Fri, 21 Jun 2024 17:25:59 +0200 Subject: [PATCH] Add Rust representation for `XXMinusYYGate` and `XXPlusYYGate` (#12606) * Add XXMinusYYGate and XXPlusYYGate, implement parameter multiplication function (naive approach). Co-authored by: Julien Gacon jul@zurich.ibm.com * * Format code * Use multiply_param in RZGate * Fix signs and indices --- crates/circuit/src/gate_matrix.rs | 46 +++++ crates/circuit/src/imports.rs | 14 +- crates/circuit/src/operations.rs | 167 ++++++++++++++---- .../library/standard_gates/xx_minus_yy.py | 3 + .../library/standard_gates/xx_plus_yy.py | 3 + 5 files changed, 194 insertions(+), 39 deletions(-) diff --git a/crates/circuit/src/gate_matrix.rs b/crates/circuit/src/gate_matrix.rs index ad8c918e73bc..23ce94869227 100644 --- a/crates/circuit/src/gate_matrix.rs +++ b/crates/circuit/src/gate_matrix.rs @@ -212,6 +212,7 @@ pub static ISWAP_GATE: [[Complex64; 4]; 4] = [ ]; pub static S_GATE: [[Complex64; 2]; 2] = [[c64(1., 0.), c64(0., 0.)], [c64(0., 0.), c64(0., 1.)]]; + pub static SDG_GATE: [[Complex64; 2]; 2] = [[c64(1., 0.), c64(0., 0.)], [c64(0., 0.), c64(0., -1.)]]; @@ -219,6 +220,7 @@ pub static T_GATE: [[Complex64; 2]; 2] = [ [c64(1., 0.), c64(0., 0.)], [c64(0., 0.), c64(FRAC_1_SQRT_2, FRAC_1_SQRT_2)], ]; + pub static TDG_GATE: [[Complex64; 2]; 2] = [ [c64(1., 0.), c64(0., 0.)], [c64(0., 0.), c64(FRAC_1_SQRT_2, -FRAC_1_SQRT_2)], @@ -246,3 +248,47 @@ pub fn u_gate(theta: f64, phi: f64, lam: f64) -> [[Complex64; 2]; 2] { [c64(0., phi).exp() * sin, c64(0., phi + lam).exp() * cos], ] } + +#[inline] +pub fn xx_minus_yy_gate(theta: f64, beta: f64) -> [[Complex64; 4]; 4] { + let cos = (theta / 2.).cos(); + let sin = (theta / 2.).sin(); + [ + [ + c64(cos, 0.), + c64(0., 0.), + c64(0., 0.), + c64(0., -sin) * c64(0., -beta).exp(), + ], + [c64(0., 0.), c64(1., 0.), c64(0., 0.), c64(0., 0.)], + [c64(0., 0.), c64(0., 0.), c64(1., 0.), c64(0., 0.)], + [ + c64(0., -sin) * c64(0., beta).exp(), + c64(0., 0.), + c64(0., 0.), + c64(cos, 0.), + ], + ] +} + +#[inline] +pub fn xx_plus_yy_gate(theta: f64, beta: f64) -> [[Complex64; 4]; 4] { + let cos = (theta / 2.).cos(); + let sin = (theta / 2.).sin(); + [ + [c64(1., 0.), c64(0., 0.), c64(0., 0.), c64(0., 0.)], + [ + c64(0., 0.), + c64(cos, 0.), + c64(0., -sin) * c64(0., -beta).exp(), + c64(0., 0.), + ], + [ + c64(0., 0.), + c64(0., -sin) * c64(0., beta).exp(), + c64(cos, 0.), + c64(0., 0.), + ], + [c64(0., 0.), c64(0., 0.), c64(0., 0.), c64(1., 0.)], + ] +} diff --git a/crates/circuit/src/imports.rs b/crates/circuit/src/imports.rs index 8db3b88fd7d2..3a9a942db8df 100644 --- a/crates/circuit/src/imports.rs +++ b/crates/circuit/src/imports.rs @@ -124,13 +124,23 @@ static STDGATE_IMPORT_PATHS: [[&str; 2]; STANDARD_GATE_SIZE] = [ // SdgGate = 19 ["qiskit.circuit.library.standard_gates.s", "SdgGate"], // TGate = 20 - ["qiskit.circuit.library.standard_gates.s", "TGate"], + ["qiskit.circuit.library.standard_gates.t", "TGate"], // TdgGate = 21 - ["qiskit.circuit.library.standard_gates.s", "TdgGate"], + ["qiskit.circuit.library.standard_gates.t", "TdgGate"], // SXdgGate = 22 ["qiskit.circuit.library.standard_gates.sx", "SXdgGate"], // iSWAPGate = 23 ["qiskit.circuit.library.standard_gates.iswap", "iSwapGate"], + //XXMinusYYGate = 24 + [ + "qiskit.circuit.library.standard_gates.xx_minus_yy", + "XXMinusYYGate", + ], + //XXPlusYYGate = 25 + [ + "qiskit.circuit.library.standard_gates.xx_plus_yy", + "XXPlusYYGate", + ], ]; /// A mapping from the enum variant in crate::operations::StandardGate to the python object for the diff --git a/crates/circuit/src/operations.rs b/crates/circuit/src/operations.rs index 9048c55d9d48..6dedd3ac2061 100644 --- a/crates/circuit/src/operations.rs +++ b/crates/circuit/src/operations.rs @@ -203,14 +203,16 @@ pub enum StandardGate { TdgGate = 21, SXdgGate = 22, ISwapGate = 23, + XXMinusYYGate = 24, + XXPlusYYGate = 25, } static STANDARD_GATE_NUM_QUBITS: [u32; STANDARD_GATE_SIZE] = [ - 1, 1, 1, 2, 2, 2, 3, 1, 1, 1, 2, 2, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, + 1, 1, 1, 2, 2, 2, 3, 1, 1, 1, 2, 2, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, ]; static STANDARD_GATE_NUM_PARAMS: [u32; STANDARD_GATE_SIZE] = [ - 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 2, 2, ]; static STANDARD_GATE_NAME: [&str; STANDARD_GATE_SIZE] = [ @@ -238,6 +240,8 @@ static STANDARD_GATE_NAME: [&str; STANDARD_GATE_SIZE] = [ "tdg", "sxdg", "iswap", + "xx_minus_yy", + "xx_plus_yy", ]; #[pymethods] @@ -287,7 +291,7 @@ impl StandardGate { // Remove this when std::mem::variant_count() is stabilized (see // https://github.com/rust-lang/rust/issues/73662 ) -pub const STANDARD_GATE_SIZE: usize = 24; +pub const STANDARD_GATE_SIZE: usize = 26; impl Operation for StandardGate { fn name(&self) -> &str { @@ -416,6 +420,18 @@ impl Operation for StandardGate { [] => Some(aview2(&gate_matrix::ISWAP_GATE).to_owned()), _ => None, }, + Self::XXMinusYYGate => match params { + [Param::Float(theta), Param::Float(beta)] => { + Some(aview2(&gate_matrix::xx_minus_yy_gate(*theta, *beta)).to_owned()) + } + _ => None, + }, + Self::XXPlusYYGate => match params { + [Param::Float(theta), Param::Float(beta)] => { + Some(aview2(&gate_matrix::xx_plus_yy_gate(*theta, *beta)).to_owned()) + } + _ => None, + }, } } @@ -502,6 +518,7 @@ impl Operation for StandardGate { }), Self::CXGate => None, Self::CCXGate => Python::with_gil(|py| -> Option { + let q0 = smallvec![Qubit(0)]; let q1 = smallvec![Qubit(1)]; let q2 = smallvec![Qubit(2)]; let q0_1 = smallvec![Qubit(0), Qubit(1)]; @@ -524,7 +541,7 @@ impl Operation for StandardGate { (Self::TGate, smallvec![], q2.clone()), (Self::HGate, smallvec![], q2), (Self::CXGate, smallvec![], q0_1.clone()), - (Self::TGate, smallvec![], smallvec![Qubit(0)]), + (Self::TGate, smallvec![], q0), (Self::TdgGate, smallvec![], q1), (Self::CXGate, smallvec![], q0_1), ], @@ -536,39 +553,20 @@ impl Operation for StandardGate { Self::RXGate => todo!("Add when we have R"), Self::RYGate => todo!("Add when we have R"), Self::RZGate => Python::with_gil(|py| -> Option { - match ¶ms[0] { - Param::Float(theta) => Some( - CircuitData::from_standard_gates( - py, - 1, - [( - Self::PhaseGate, - smallvec![Param::Float(*theta)], - smallvec![Qubit(0)], - )], - Param::Float(-0.5 * theta), - ) - .expect("Unexpected Qiskit python bug"), - ), - Param::ParameterExpression(theta) => Some( - CircuitData::from_standard_gates( - py, - 1, - [( - Self::PhaseGate, - smallvec![Param::ParameterExpression(theta.clone_ref(py))], - smallvec![Qubit(0)], - )], - Param::ParameterExpression( - theta - .call_method1(py, intern!(py, "__rmul__"), (-0.5,)) - .expect("Parameter expression for global phase failed"), - ), - ) - .expect("Unexpected Qiskit python bug"), - ), - Param::Obj(_) => unreachable!(), - } + let theta = ¶ms[0]; + Some( + CircuitData::from_standard_gates( + py, + 1, + [( + Self::PhaseGate, + smallvec![theta.clone()], + smallvec![Qubit(0)], + )], + multiply_param(theta, -0.5, py), + ) + .expect("Unexpected Qiskit python bug"), + ) }), Self::ECRGate => todo!("Add when we have RZX"), Self::SwapGate => Python::with_gil(|py| -> Option { @@ -732,6 +730,88 @@ impl Operation for StandardGate { .expect("Unexpected Qiskit python bug"), ) }), + Self::XXMinusYYGate => Python::with_gil(|py| -> Option { + let q0 = smallvec![Qubit(0)]; + let q1 = smallvec![Qubit(1)]; + let q0_1 = smallvec![Qubit(0), Qubit(1)]; + let theta = ¶ms[0]; + let beta = ¶ms[1]; + Some( + CircuitData::from_standard_gates( + py, + 2, + [ + ( + Self::RZGate, + smallvec![multiply_param(beta, -1.0, py)], + q1.clone(), + ), + (Self::RZGate, smallvec![Param::Float(-PI2)], q0.clone()), + (Self::SXGate, smallvec![], q0.clone()), + (Self::RZGate, smallvec![Param::Float(PI2)], q0.clone()), + (Self::SGate, smallvec![], q1.clone()), + (Self::CXGate, smallvec![], q0_1.clone()), + ( + Self::RYGate, + smallvec![multiply_param(theta, 0.5, py)], + q0.clone(), + ), + ( + Self::RYGate, + smallvec![multiply_param(theta, -0.5, py)], + q1.clone(), + ), + (Self::CXGate, smallvec![], q0_1), + (Self::SdgGate, smallvec![], q1.clone()), + (Self::RZGate, smallvec![Param::Float(-PI2)], q0.clone()), + (Self::SXdgGate, smallvec![], q0.clone()), + (Self::RZGate, smallvec![Param::Float(PI2)], q0), + (Self::RZGate, smallvec![beta.clone()], q1), + ], + FLOAT_ZERO, + ) + .expect("Unexpected Qiskit python bug"), + ) + }), + Self::XXPlusYYGate => Python::with_gil(|py| -> Option { + let q0 = smallvec![Qubit(0)]; + let q1 = smallvec![Qubit(1)]; + let q1_0 = smallvec![Qubit(1), Qubit(0)]; + let theta = ¶ms[0]; + let beta = ¶ms[1]; + Some( + CircuitData::from_standard_gates( + py, + 2, + [ + (Self::RZGate, smallvec![beta.clone()], q0.clone()), + (Self::RZGate, smallvec![Param::Float(-PI2)], q1.clone()), + (Self::SXGate, smallvec![], q1.clone()), + (Self::RZGate, smallvec![Param::Float(PI2)], q1.clone()), + (Self::SGate, smallvec![], q0.clone()), + (Self::CXGate, smallvec![], q1_0.clone()), + ( + Self::RYGate, + smallvec![multiply_param(theta, -0.5, py)], + q1.clone(), + ), + ( + Self::RYGate, + smallvec![multiply_param(theta, -0.5, py)], + q0.clone(), + ), + (Self::CXGate, smallvec![], q1_0), + (Self::SdgGate, smallvec![], q0.clone()), + (Self::RZGate, smallvec![Param::Float(-PI2)], q1.clone()), + (Self::SXdgGate, smallvec![], q1.clone()), + (Self::RZGate, smallvec![Param::Float(PI2)], q1), + (Self::RZGate, smallvec![multiply_param(beta, -1.0, py)], q0), + ], + FLOAT_ZERO, + ) + .expect("Unexpected Qiskit python bug"), + ) + }), } } @@ -742,6 +822,19 @@ impl Operation for StandardGate { const FLOAT_ZERO: Param = Param::Float(0.0); +fn multiply_param(param: &Param, mult: f64, py: Python) -> Param { + match param { + Param::Float(theta) => Param::Float(*theta * mult), + Param::ParameterExpression(theta) => Param::ParameterExpression( + theta + .clone_ref(py) + .call_method1(py, intern!(py, "__rmul__"), (mult,)) + .expect("Parameter expression for global phase failed"), + ), + Param::Obj(_) => unreachable!(), + } +} + /// This class is used to wrap a Python side Instruction that is not in the standard library #[derive(Clone, Debug)] #[pyclass(freelist = 20, module = "qiskit._accelerate.circuit")] diff --git a/qiskit/circuit/library/standard_gates/xx_minus_yy.py b/qiskit/circuit/library/standard_gates/xx_minus_yy.py index 4bf4ab80eca2..db3c3dc89153 100644 --- a/qiskit/circuit/library/standard_gates/xx_minus_yy.py +++ b/qiskit/circuit/library/standard_gates/xx_minus_yy.py @@ -27,6 +27,7 @@ from qiskit.circuit.parameterexpression import ParameterValueType from qiskit.circuit.quantumcircuit import QuantumCircuit from qiskit.circuit.quantumregister import QuantumRegister +from qiskit._accelerate.circuit import StandardGate class XXMinusYYGate(Gate): @@ -91,6 +92,8 @@ class XXMinusYYGate(Gate): \end{pmatrix} """ + _standard_gate = StandardGate.XXMinusYYGate + def __init__( self, theta: ParameterValueType, diff --git a/qiskit/circuit/library/standard_gates/xx_plus_yy.py b/qiskit/circuit/library/standard_gates/xx_plus_yy.py index a82316ed7b09..7920454d0b98 100644 --- a/qiskit/circuit/library/standard_gates/xx_plus_yy.py +++ b/qiskit/circuit/library/standard_gates/xx_plus_yy.py @@ -21,6 +21,7 @@ from qiskit.circuit.gate import Gate from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit.parameterexpression import ParameterValueType +from qiskit._accelerate.circuit import StandardGate class XXPlusYYGate(Gate): @@ -87,6 +88,8 @@ class XXPlusYYGate(Gate): \end{pmatrix} """ + _standard_gate = StandardGate.XXPlusYYGate + def __init__( self, theta: ParameterValueType,