Skip to content

Commit 8bf4811

Browse files
feat: add PRX gate implementation and update gate imports
1 parent ba3648b commit 8bf4811

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

mpqp/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
Ry,
5353
Rz,
5454
Rzz,
55+
PRX,
5556
S,
5657
S_dagger,
5758
T,

mpqp/core/instruction/gates/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
Ry,
2222
Rz,
2323
Rzz,
24+
PRX,
2425
S,
2526
S_dagger,
2627
T,

mpqp/core/instruction/gates/native_gates.py

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,12 @@ class RotationGate(NativeGate, ParametrizedGate, SimpleClassReprABC):
217217
target: Index referring to the qubits on which the gate will be applied.
218218
"""
219219

220-
def __init__(self, theta: Expr | float, target: list[int] | int):
221-
self.parameters = [theta]
220+
def __init__(
221+
self, theta: list[Expr | float] | Expr | float, target: list[int] | int
222+
):
223+
if not isinstance(theta, list):
224+
theta = [theta]
225+
self.parameters = theta
222226
definition = UnitaryMatrix(self.to_canonical_matrix())
223227
if isinstance(target, int):
224228
target = [target]
@@ -1351,6 +1355,122 @@ def to_canonical_matrix(self):
13511355
)
13521356

13531357

1358+
class PRX(RotationGate, SingleQubitGate):
1359+
r"""Parametrized rotated-X gate.
1360+
1361+
PRX(θ, φ) = Rz(φ) Rx(θ) Rz(-φ)
1362+
1363+
Equivalent to a rotation of angle θ around
1364+
the axis (cos φ, sin φ, 0) in the XY plane.
1365+
1366+
Args:
1367+
theta: Rotation angle.
1368+
phi: Axis angle in XY plane.
1369+
target: Target qubit.
1370+
1371+
Matrix form:
1372+
Rz(φ) Rx(θ) Rz(-φ)
1373+
"""
1374+
1375+
qlm_aqasm_keyword = "PRX"
1376+
qiskit_string = "prx"
1377+
1378+
@classproperty
1379+
def braket_gate(cls):
1380+
from braket.circuits import gates
1381+
1382+
return gates.PRx
1383+
1384+
@classproperty
1385+
def qiskit_gate(cls):
1386+
from qiskit.circuit.library import UGate
1387+
1388+
return UGate
1389+
1390+
@classproperty
1391+
def cirq_gate(cls):
1392+
from cirq.ops.phased_x_gate import PhasedXPowGate
1393+
1394+
return PhasedXPowGate
1395+
1396+
def __init__(self, theta: Expr | float, phi: Expr | float, target: int):
1397+
super().__init__([theta, phi], target)
1398+
1399+
def to_canonical_matrix(self):
1400+
theta, phi = self.parameters
1401+
1402+
c = np.cos(theta / 2)
1403+
s = np.sin(theta / 2)
1404+
1405+
e_minus = np.exp(-1j * phi)
1406+
e_plus = np.exp(1j * phi)
1407+
1408+
return np.array(
1409+
[
1410+
[c, -1j * e_minus * s],
1411+
[-1j * e_plus * s, c],
1412+
],
1413+
dtype=complex,
1414+
)
1415+
1416+
def to_other_language(
1417+
self,
1418+
language: Language = Language.QISKIT,
1419+
qiskit_parameters: Optional[set["Parameter"]] = None,
1420+
):
1421+
1422+
theta, phi = self.parameters
1423+
try:
1424+
theta = float(theta)
1425+
except:
1426+
pass
1427+
try:
1428+
phi = float(phi)
1429+
except:
1430+
pass
1431+
1432+
if language == Language.QISKIT:
1433+
if qiskit_parameters is None:
1434+
qiskit_parameters = set()
1435+
return self.qiskit_gate(
1436+
_qiskit_parameter_adder(theta, qiskit_parameters),
1437+
_qiskit_parameter_adder(phi, qiskit_parameters),
1438+
_qiskit_parameter_adder(-phi, qiskit_parameters),
1439+
)
1440+
elif language == Language.BRAKET:
1441+
from braket.circuits import Instruction
1442+
1443+
connection = self.targets
1444+
if isinstance(self, ControlledGate):
1445+
connection += self.controls
1446+
return Instruction(
1447+
operator=self.braket_gate(
1448+
_sympy_to_braket_param(theta), _sympy_to_braket_param(phi)
1449+
),
1450+
target=connection,
1451+
)
1452+
elif language == Language.CIRQ:
1453+
return self.cirq_gate(theta, phi)
1454+
if language == Language.QASM2:
1455+
from mpqp.qasm.mpqp_to_qasm import float_to_qasm_str
1456+
1457+
instruction_str = self.qasm2_gate
1458+
instruction_str += (
1459+
"("
1460+
+ ",".join(float_to_qasm_str(float(param)) for param in self.parameters)
1461+
+ ")"
1462+
)
1463+
1464+
qubits = ""
1465+
if isinstance(self, ControlledGate):
1466+
qubits = ",".join([f"q[{j}]" for j in self.controls]) + ","
1467+
qubits += ",".join([f"q[{j}]" for j in self.targets])
1468+
1469+
return instruction_str + " " + qubits + ";"
1470+
else:
1471+
raise NotImplementedError(f"Error: {language} is not supported")
1472+
1473+
13541474
class Rk(RotationGate, SingleQubitGate):
13551475
r"""One qubit Phase gate of angle `\frac{2i\pi}{2^k}`.
13561476

0 commit comments

Comments
 (0)