OpenQASM Import and Export
QiliSDK can bridge Circuit and both OpenQASM 2.0 and
OpenQASM 3.0 via the following entry-points in qilisdk.utils.openqasm:
OpenQASM 2.0
from_qasm2()— parse an OpenQASM 2.0 string into a Circuit.from_qasm2_file()— read a.qasmfile.to_qasm2()— serialize a Circuit to an OpenQASM 2.0 string.to_qasm2_file()— write a.qasmfile.
OpenQASM 3.0
from_qasm3()— parse an OpenQASM 3.0 string into a Circuit.from_qasm3_file()— read a.qasmfile.to_qasm3()— serialize a Circuit to an OpenQASM 3.0 string.to_qasm3_file()— write a.qasmfile.
The openqasm3 dependency is optional; install it with the openqasm extra:
pip install qilisdk[openqasm]
Quick start
from qilisdk.digital import CNOT, Circuit, H, M
from qilisdk.utils.openqasm import to_qasm2, from_qasm2, to_qasm3, from_qasm3
circuit = Circuit(2)
circuit.add(H(0))
circuit.add(CNOT(0, 1))
circuit.add(M(0, 1))
# OpenQASM 2.0
qasm2_text = to_qasm2(circuit)
print(qasm2_text)
reparsed = from_qasm2(qasm2_text)
# OpenQASM 3.0
qasm3_text = to_qasm3(circuit)
print(qasm3_text)
reparsed = from_qasm3(qasm3_text)
Reading and writing files:
from qilisdk.utils.openqasm import to_qasm2_file, from_qasm2_file
from qilisdk.utils.openqasm import to_qasm3_file, from_qasm3_file
to_qasm2_file(circuit, "bell.qasm")
same_circuit = from_qasm2_file("bell.qasm")
to_qasm3_file(circuit, "bell3.qasm")
same_circuit = from_qasm3_file("bell3.qasm")
OpenQASM 3.0 supported features
✅: The feature is fully supported
🟡: The feature is partially supported, see the note for explanation
❌: The feature is not supported
OpenQASM 3 feature |
Supported |
Notes |
|---|---|---|
comments |
✅ |
|
QASM version string |
✅ |
|
include |
✅ |
|
unicode names |
✅ |
|
qubit |
✅ |
|
bit |
🟡 |
1 |
bool |
🟡 |
1 |
int |
🟡 |
1 |
uint |
🟡 |
1 |
float |
🟡 |
1 |
angle |
🟡 |
1 |
complex |
🟡 |
1 |
const |
✅ |
|
pi/π/tau/τ/euler/ℇ |
✅ |
|
Aliasing: |
✅ |
|
register concatenation |
❌ |
|
casting ( |
✅ |
|
duration |
🟡 |
1 |
durationof |
❌ |
|
ns/μs/us/ms/s/dt |
✅ |
|
stretch ( |
🟡 |
1 |
delay |
❌ |
|
barrier |
❌ |
|
box |
❌ |
|
built-in |
✅ |
|
gate definition |
🟡 |
1 |
gphase |
❌ |
|
|
✅ |
|
|
🟡 |
1 |
|
✅ |
|
|
🟡 |
1, 2 |
reset |
❌ |
|
measure |
🟡 |
3 |
bit operations |
🟡 |
1 |
boolean operations |
🟡 |
1 |
arithmetic expressions |
🟡 |
1 |
comparisons |
🟡 |
1 |
|
🟡 |
1 |
|
🟡 |
1 |
|
🟡 |
1 |
for loops |
🟡 |
1 |
switch |
🟡 |
1 |
while loops |
🟡 |
1 |
|
🟡 |
1 |
|
🟡 |
1 |
extern |
❌ |
|
def subroutines |
🟡 |
1 |
return |
🟡 |
1 |
input |
🟡 |
1 |
output |
❌ |
Reading these constructs is fully supported, but the expressions are not stored in the
Circuitobject and will not be written back out when converting to OpenQASM. For example, declaringint x = 5;causesxto be evaluated and used during parsing, but the variable declaration will not appear in the resulting circuit object or in any re-exported QASM string.pow(k) @is only supported whenkis an integer; it is implemented by repeated gate application.Mid-circuit measurements are not supported in QiliSDK.