From 4f986293ea0d13b187826b7ac4a63989b8b0cff6 Mon Sep 17 00:00:00 2001 From: David Yonge-Mallo Date: Thu, 9 May 2024 08:45:21 +0200 Subject: [PATCH] Add `U` as alias for `u3` gate. Required for compatibility with OpenQASM 3. Together with #218, fixes #227. --- pyzx/circuit/gates.py | 1 + pyzx/circuit/qasmparser.py | 8 ++++---- tests/test_qasm.py | 3 +++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pyzx/circuit/gates.py b/pyzx/circuit/gates.py index a6cf7de8..97c68b88 100644 --- a/pyzx/circuit/gates.py +++ b/pyzx/circuit/gates.py @@ -1288,6 +1288,7 @@ def to_graph(self, g, q_mapper, c_mapper): "u2": U2, "u3": U3, "u": U3, + "U": U3, # needed for OpenQASM 3 in Qiskit 1.0 and up "cu3": CU3, "cu": CU, "cx": CNOT, diff --git a/pyzx/circuit/qasmparser.py b/pyzx/circuit/qasmparser.py index 8f8f5448..a92341ea 100644 --- a/pyzx/circuit/qasmparser.py +++ b/pyzx/circuit/qasmparser.py @@ -21,7 +21,7 @@ from typing import List, Dict, Tuple, Optional from . import Circuit -from .gates import Gate, qasm_gate_table, U2, U3 +from .gates import Gate, qasm_gate_table from ..utils import settings @@ -193,10 +193,10 @@ def parse_command(self, c: str, registers: Dict[str,Tuple[int,int]]) -> List[Gat gates.append(g) elif name == 'u2': if len(phases) != 2: raise TypeError("Invalid specification {}".format(c)) - gates.append(U2(argset[0],phases[0],phases[1])) - elif name in ('u3', 'u'): + gates.append(qasm_gate_table[name](argset[0], phases[0], phases[1])) # type: ignore + elif name in ('u3', 'u', 'U'): if len(phases) != 3: raise TypeError("Invalid specification {}".format(c)) - gates.append(U3(argset[0],phases[0],phases[1],phases[2])) + gates.append(qasm_gate_table[name](argset[0], phases[0], phases[1], phases[2])) # type: ignore elif name in ('cx', 'CX', 'cy', 'cz', 'ch', 'csx', 'swap'): if len(phases) != 0: raise TypeError("Invalid specification {}".format(c)) g = qasm_gate_table[name](control=argset[0],target=argset[1]) # type: ignore diff --git a/tests/test_qasm.py b/tests/test_qasm.py index 07bd472c..128569f5 100644 --- a/tests/test_qasm.py +++ b/tests/test_qasm.py @@ -290,6 +290,9 @@ def compare_gate_matrix_with_qiskit(gates, num_qubits: int, num_angles: int, qas compare_gate_matrix_with_qiskit(['cu3'], 2, 3, [2]) compare_gate_matrix_with_qiskit(['u'], 1, 3, [2]) + # Test native OpenQASM 3 gate. + compare_gate_matrix_with_qiskit(['U'], 1, 3, [3]) + @unittest.skipUnless(QuantumCircuit, "qiskit needs to be installed for this test") def test_qiskit_transpile_pyzx_optimization_round_trip(self): """Regression test for issue #102.