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 2.

The `u` gate is in the header for OpenQASM2: https://github.com/Qiskit/qiskit/blob/main/qiskit/qasm/libs/qelib1.inc

In particular, it is produced by `qiskit.qasm2.dumps()` for circuits generated by `qiskit.circuit.random.random_circuit`.
  • Loading branch information
dlyongemallo committed May 4, 2024
1 parent 107c24e commit 6aa7296
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/notebooks/gates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"draw_zx_diagram(1, 'rz(0.125*pi) q;') # can also use 'p' or 'u1'\n",
"\n",
"draw_zx_diagram(1, 'u2(0.125*pi,0.125*pi) q[0];')\n",
"draw_zx_diagram(1, 'u3(0.125*pi,0.125*pi,0.125*pi) q[0];')"
"draw_zx_diagram(1, 'u3(0.125*pi,0.125*pi,0.125*pi) q[0];') # can also use 'u'"
]
},
{
Expand Down
2 changes: 2 additions & 0 deletions pyzx/circuit/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,7 @@ def to_graph(self, g, q_mapper, c_mapper):
"CCZ": CCZ,
"U2": U2,
"U3": U3,
"U": U3,
"CU3": CU3,
"CU": CU,
"CRX": CRX,
Expand Down Expand Up @@ -1286,6 +1287,7 @@ def to_graph(self, g, q_mapper, c_mapper):
"u1": ZPhase,
"u2": U2,
"u3": U3,
"u": U3,
"cu3": CU3,
"cu": CU,
"cx": CNOT,
Expand Down
4 changes: 2 additions & 2 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, XPhase, YPhase, ZPhase, NOT, U2, U3
from .gates import Gate, qasm_gate_table, U2, U3
from ..utils import settings


Expand Down Expand Up @@ -193,7 +193,7 @@ def parse_command(self, c: str, registers: Dict[str,Tuple[int,int]]) -> List[Gat
elif name == 'u2':
if len(phases) != 2: raise TypeError("Invalid specification {}".format(c))
gates.append(U2(argset[0],phases[0],phases[1]))
elif name == 'u3':
elif name in ('u3', 'u'):
if len(phases) != 3: raise TypeError("Invalid specification {}".format(c))
gates.append(U3(argset[0],phases[0],phases[1],phases[2]))
elif name in ('cx', 'CX', 'cy', 'cz', 'ch', 'csx', 'swap'):
Expand Down
7 changes: 4 additions & 3 deletions tests/test_qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def compare_gate_matrix_with_qiskit(gates, num_qubits: int, num_angles: int, qas
self.assertEqual(c.qubits, round_trip.qubits)
self.assertListEqual(c.gates, round_trip.gates)

# Test standard gates common to both OpenQASM 2 and 3.
# Test standard gates common to both qelib1.inc (OpenQASM 2) and stdgates.inc (OpenQASM 3).
compare_gate_matrix_with_qiskit(
['x', 'y', 'z', 'h', 's', 'sdg', 't', 'tdg', 'sx'], 1, 0)
compare_gate_matrix_with_qiskit(['u1', 'p', 'rx', 'ry', 'rz'], 1, 1)
Expand All @@ -209,14 +209,15 @@ def compare_gate_matrix_with_qiskit(gates, num_qubits: int, num_angles: int, qas
compare_gate_matrix_with_qiskit(['ccx', 'cswap'], 3, 0)
compare_gate_matrix_with_qiskit(['cu'], 2, 4)

# Test standard gates added to OpenQASM 3.
# Test standard gates added to stdgates.inc.
compare_gate_matrix_with_qiskit(['cphase'], 2, 1, [3])

# Test standard gates removed from OpenQASM 3.
# Test standard gates removed from stdgates.inc.
compare_gate_matrix_with_qiskit(['sxdg'], 1, 0, [2])
compare_gate_matrix_with_qiskit(['csx'], 2, 0, [2])
compare_gate_matrix_with_qiskit(['cu1', 'rxx', 'rzz'], 2, 1, [2])
compare_gate_matrix_with_qiskit(['cu3'], 2, 3, [2])
compare_gate_matrix_with_qiskit(['u'], 1, 3, [2])

@unittest.skipUnless(QuantumCircuit, "qiskit needs to be installed for this test")
def test_qiskit_transpile_pyzx_optimization_round_trip(self):
Expand Down

0 comments on commit 6aa7296

Please sign in to comment.