Skip to content

Commit

Permalink
Add U as alias for u3 gate. Required for compatibility with OpenQ…
Browse files Browse the repository at this point in the history
…ASM 3.

Together with #218, fixes #227.
  • Loading branch information
dlyongemallo committed May 9, 2024
1 parent 0e843bd commit 4f98629
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions pyzx/circuit/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions pyzx/circuit/qasmparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/test_qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4f98629

Please sign in to comment.