diff --git a/.github/workflows/typecheck.yml b/.github/workflows/typecheck.yml index acd3790..7beedb0 100644 --- a/.github/workflows/typecheck.yml +++ b/.github/workflows/typecheck.yml @@ -12,7 +12,7 @@ concurrency: cancel-in-progress: true env: - python-version: "3.13" + python-version: "3.15" jobs: mypy-pyright: diff --git a/README.md b/README.md index 762820e..638e662 100644 --- a/README.md +++ b/README.md @@ -55,36 +55,43 @@ circuit = parser.parse_file("my_circuit.qasm") ### Supported Gates -| OpenQASM gate | Graphix instruction | -|--------------------------------------------------------------------|---------------------| -| [h](https://openqasm.com/language/standard_library.html#h) | H | -| [s](https://openqasm.com/language/standard_library.html#s) | S | -| [sdg](https://openqasm.com/language/standard_library.html#sdg) | SDG | -| [t](https://openqasm.com/language/standard_library.html#t) | T | -| [tdg](https://openqasm.com/language/standard_library.html#tdg) | TDG | -| [sx](https://openqasm.com/language/standard_library.html#sx) | SX | -| [sxdg](https://openqasm.com/language/standard_library.html#sxdg) | SXDG | -| [id](https://openqasm.com/language/standard_library.html#id) | I | -| [x](https://openqasm.com/language/standard_library.html#x) | X | -| [y](https://openqasm.com/language/standard_library.html#y) | Y | -| [z](https://openqasm.com/language/standard_library.html#z) | Z | -| [U](https://openqasm.com/language/gates.html#U) | U | -| [p](https://openqasm.com/language/standard_library.html#p) | P | -| [rx](https://openqasm.com/language/standard_library.html#rx) | RX | -| [ry](https://openqasm.com/language/standard_library.html#ry) | RY | -| [rz](https://openqasm.com/language/standard_library.html#rz) | RZ | -| [swap](https://openqasm.com/language/standard_library.html#swap) | SWAP | -| [cx](https://openqasm.com/language/standard_library.html#cx) | CNOT | -| [cy](https://openqasm.com/language/standard_library.html#cz) | CY | -| [cz](https://openqasm.com/language/standard_library.html#cz) | CZ | -| [cu](https://openqasm.com/language/standard_library.html#cu) | CU | -| [cp](https://openqasm.com/language/standard_library.html#cp) | CP | -| [crx](https://openqasm.com/language/standard_library.html#crx) | CRX | -| [cry](https://openqasm.com/language/standard_library.html#cry) | CRY | -| [crz](https://openqasm.com/language/standard_library.html#crz) | CRZ | -| [cswap](https://openqasm.com/language/standard_library.html#cswap) | CSWAP | -| [ccx](https://openqasm.com/language/standard_library.html#ccx) | CCX | -| [gphase](https://openqasm.com/language/gates.html#gphase) | GPHASE | +| OpenQASM gate | Graphix instruction | +|----------------------------------------------------------------------|---------------------| +| [h](https://openqasm.com/language/standard_library.html#h) | H | +| [s](https://openqasm.com/language/standard_library.html#s) | S | +| [sdg](https://openqasm.com/language/standard_library.html#sdg) | SDG | +| [t](https://openqasm.com/language/standard_library.html#t) | T | +| [tdg](https://openqasm.com/language/standard_library.html#tdg) | TDG | +| [sx](https://openqasm.com/language/standard_library.html#sx) | SX | +| [sxdg](https://openqasm.com/language/standard_library.html#sxdg) | SXDG | +| [id](https://openqasm.com/language/standard_library.html#id) | I | +| [x](https://openqasm.com/language/standard_library.html#x) | X | +| [y](https://openqasm.com/language/standard_library.html#y) | Y | +| [z](https://openqasm.com/language/standard_library.html#z) | Z | +| [U](https://openqasm.com/language/gates.html#U) | U | +| [p](https://openqasm.com/language/standard_library.html#p) | P | +| [phase](https://openqasm.com/language/standard_library.html#phase) | P | +| [rx](https://openqasm.com/language/standard_library.html#rx) | RX | +| [ry](https://openqasm.com/language/standard_library.html#ry) | RY | +| [rz](https://openqasm.com/language/standard_library.html#rz) | RZ | +| [swap](https://openqasm.com/language/standard_library.html#swap) | SWAP | +| [cx](https://openqasm.com/language/standard_library.html#cx) | CNOT | +| [cy](https://openqasm.com/language/standard_library.html#cz) | CY | +| [cz](https://openqasm.com/language/standard_library.html#cz) | CZ | +| [cu](https://openqasm.com/language/standard_library.html#cu) | CU | +| [cp](https://openqasm.com/language/standard_library.html#cp) | CP | +| [cphase](https://openqasm.com/language/standard_library.html#cphase) | CP | +| [crx](https://openqasm.com/language/standard_library.html#crx) | CRX | +| [cry](https://openqasm.com/language/standard_library.html#cry) | CRY | +| [crz](https://openqasm.com/language/standard_library.html#crz) | CRZ | +| [ch](https://openqasm.com/language/standard_library.html#ch) | CH | +| [cswap](https://openqasm.com/language/standard_library.html#cswap) | CSWAP | +| [ccx](https://openqasm.com/language/standard_library.html#ccx) | CCX | +| [gphase](https://openqasm.com/language/gates.html#gphase) | GPHASE | +| [u1](https://openqasm.com/language/standard_library.html#u1) | P | +| [u2](https://openqasm.com/language/standard_library.html#u2) | U + GPHASE | +| [u3](https://openqasm.com/language/standard_library.html#u3) | U + GPHASE | +| [CX](https://openqasm.com/language/standard_library.html#CX) | CNOT | ### Expressions diff --git a/graphix_qasm_parser/parser.py b/graphix_qasm_parser/parser.py index 74ff7d9..c6090f5 100644 --- a/graphix_qasm_parser/parser.py +++ b/graphix_qasm_parser/parser.py @@ -451,6 +451,11 @@ def _u3( qubit_count=2, instructions=(Instruction.CNOT(control=0, target=1),), ), + "CX": # https://openqasm.com/language/standard_library.html#CX + _Gate( + qubit_count=2, + instructions=(Instruction.CNOT(control=0, target=1),), + ), "cy": # https://openqasm.com/language/standard_library.html#cy _Gate( qubit_count=2, @@ -461,6 +466,11 @@ def _u3( qubit_count=2, instructions=(Instruction.CZ(control=0, target=1),), ), + "ch": # https://openqasm.com/language/standard_library.html#ch + _Gate( + qubit_count=2, + instructions=(Instruction.CH(control=0, target=1),), + ), "swap": # https://openqasm.com/language/standard_library.html#swap _Gate( qubit_count=2, @@ -532,6 +542,12 @@ def _u3( qubit_count=1, instructions=(Instruction.P(target=0, angle=rad_to_angle(_Theta)),), ), + "phase": # https://openqasm.com/language/standard_library.html#phase + _Gate( + params=(_Theta,), + qubit_count=1, + instructions=(Instruction.P(target=0, angle=rad_to_angle(_Theta)),), + ), "u1": # https://openqasm.com/language/standard_library.html#u1 _Gate( params=(_Theta,), @@ -570,6 +586,12 @@ def _u3( qubit_count=2, instructions=(Instruction.CP(control=0, target=1, angle=rad_to_angle(_Theta)),), ), + "cphase": # https://openqasm.com/language/standard_library.html#cphase + _Gate( + params=(_Theta,), + qubit_count=2, + instructions=(Instruction.CP(control=0, target=1, angle=rad_to_angle(_Theta)),), + ), "crx": # https://openqasm.com/language/standard_library.html#crx _Gate( params=(_Theta,), diff --git a/tests/test_parser.py b/tests/test_parser.py index a0f389d..afad38e 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -53,6 +53,7 @@ def test_parse_all_instructions() -> None: # noqa: PLR0915 cx q[0], q[1]; cy q[0], q[1]; cz q[0], q[1]; +ch q[0], q[1]; swap q[0], q[1]; cswap q[0], q[1], q[2]; h q[0]; @@ -98,6 +99,10 @@ def test_parse_all_instructions() -> None: # noqa: PLR0915 assert instruction.target == 1 assert instruction.control == 0 instruction = next(iterator) + assert isinstance(instruction, Instruction.CH) + assert instruction.target == 1 + assert instruction.control == 0 + instruction = next(iterator) assert isinstance(instruction, Instruction.SWAP) assert instruction.targets == (0, 1) instruction = next(iterator) @@ -206,6 +211,52 @@ def test_parse_all_instructions() -> None: # noqa: PLR0915 next(iterator) +def test_u2() -> None: + """Test parse compatibility gate u2.""" + s = """ +qubit q; +u2(pi/3, pi/4) q; +""" + parser = OpenQASMParser() + circuit = parser.parse_str(s) + assert circuit.width == 1 + assert circuit.instruction == [ + Instruction.U(target=0, theta=ANGLE_PI / 2, phi=ANGLE_PI / 3, lambda_=ANGLE_PI / 4), + Instruction.GPHASE(-ANGLE_PI / 4), + Instruction.GPHASE(-ANGLE_PI / 6), + Instruction.GPHASE(-ANGLE_PI / 8), + ] + + +def test_u3() -> None: + """Test parse compatibility gate u3.""" + s = """ +qubit q; +u3(pi/2, pi/3, pi/4) q; +""" + parser = OpenQASMParser() + circuit = parser.parse_str(s) + assert circuit.width == 1 + assert circuit.instruction == [ + Instruction.U(target=0, theta=ANGLE_PI / 2, phi=ANGLE_PI / 3, lambda_=ANGLE_PI / 4), + Instruction.GPHASE(-ANGLE_PI / 4), + Instruction.GPHASE(-ANGLE_PI / 6), + Instruction.GPHASE(-ANGLE_PI / 8), + ] + + +def test_legacy_cx() -> None: + """Test parse compatibility gate CX.""" + s = """ +qubit[2] q; +CX q[0], q[1]; +""" + parser = OpenQASMParser() + circuit = parser.parse_str(s) + assert circuit.width == 2 + assert circuit.instruction == [Instruction.CNOT(control=0, target=1)] + + def test_parse_all_expressions() -> None: """Test parse all expressions.""" s = """ @@ -296,15 +347,15 @@ def test_gate_definition() -> None: # Excerpt of https://openqasm.com/language/gates.html#defining-gates s = """ qubit[2] q; -gate cphase(θ) a, b +gate cphase2(θ) a, b { rz(θ / 2) a; - cx a, b; + CX a, b; rz(-θ / 2) b; - cx a, b; + CX a, b; rz(θ / 2) b; } -cphase(π / 2) q[0], q[1]; +cphase2(π / 2) q[0], q[1]; """ parser = OpenQASMParser() circuit = parser.parse_str(s)