Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/typecheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ concurrency:
cancel-in-progress: true

env:
python-version: "3.13"
python-version: "3.15"

jobs:
mypy-pyright:
Expand Down
67 changes: 37 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions graphix_qasm_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,),
Expand Down Expand Up @@ -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,),
Expand Down
59 changes: 55 additions & 4 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = """
Expand Down Expand Up @@ -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)
Expand Down
Loading