Skip to main content
This project is in beta. Found a bug? Open a GitHub issue. Question or idea? Ask in Discussions — we'd love your feedback!

OpenQASM 3 and the Qiskit SDK

Package versions

The code on this page was developed using the following requirements. We recommend using these versions or newer.

qiskit[all]~=2.5.2
# Added by doQumentation — installs the packages this notebook needs if they are missing
import importlib.util

_needed = {"qiskit": "qiskit"}
_missing = [pip for module, pip in _needed.items()
if importlib.util.find_spec(module) is None]
# One at a time, so a package that fails to install does not block the others
for _pip in _missing:
%pip install -q {_pip}
if not _missing:
print("\u2713 All required packages are installed")

The Qiskit SDK provides some tools for converting between OpenQASM representations of quantum programs, and the QuantumCircuit class. Note these tools are still in an exploratory phase of development and will continue to evolve as Qiskit's support for dynamic circuit capabilities expressed by OpenQASM 3 increases.

note

This function is still in the exploratory phase. Therefore, it is likely that the syntax and capabilities will evolve.

Import an OpenQASM 3 program into Qiskit​

You must install the package qiskit_qasm3_import to use this function. Install using the following command.

pip install qiskit-qasm3-import

Currently two high-level functions are available for importing from OpenQASM 3 into Qiskit. These functions are load(), which takes a file name, and loads(), which takes the program itself as a string:

import qiskit.qasm3
qiskit.qasm3.load(file_name)
qiskit.qasm3.loads(program_string)

In this example, we define a quantum program using OpenQASM 3, and use loads() to directly convert it into a QuantumCircuit:

import qiskit.qasm3

program = """
OPENQASM 3.0;
include "stdgates.inc";

input float[64] a;
qubit[3] q;
bit[2] mid;
bit[3] out;

let aliased = q[0:1];

gate my_gate(a) c, t {
gphase(a / 2);
ry(a) c;
cx c, t;
}
gate my_phase(a) c {
ctrl @ inv @ gphase(a) c;
}

my_gate(a * 2) aliased[0], q[{1, 2}][0];
measure q[0] -> mid[0];
measure q[1] -> mid[1];

while (mid == "00") {
reset q[0];
reset q[1];
my_gate(a) q[0], q[1];
my_phase(a - pi/2) q[1];
mid[0] = measure q[0];
mid[1] = measure q[1];
}

if (mid[0]) {
let inner_alias = q[{0, 1}];
reset inner_alias;
}

out = measure q;
"""
circuit = qiskit.qasm3.loads(program)
circuit.draw("mpl")

Output of the previous code cell

Export to OpenQASM 3​

You can export Qiskit code to OpenQASM 3 with dumps(), which exports to a string, or dump(), which exports to a file.

Example with dumps()​

from qiskit import QuantumCircuit
from qiskit.qasm3 import dumps

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()

dumps(qc)
'OPENQASM 3.0;\ninclude "stdgates.inc";\nbit[2] meas;\nqubit[2] q;\nh q[0];\ncx q[0], q[1];\nbarrier q[0], q[1];\nmeas[0] = measure q[0];\nmeas[1] = measure q[1];\n'

Example with dump()​

from qiskit import QuantumCircuit
from qiskit.qasm3 import dump

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()

f = open("my_file.txt", "w")
dump(qc, f)
f.close()

For more information, see the Exporting to OpenQASM 3 section of the API reference.

Next steps​

Recommendations