OpenQASM 3 & Intermediate Representations
The assembly language of quantum computers.
Every quantum SDK eventually emits something a control system can execute. OpenQASM 3 is the most widely used such language: a circuit format that grew classical control flow, real-time feed-forward, timing and pulse calibrations. With feed-forward, some states can be prepared in constant depth that provably need linear depth without it.
What you’ll learn
- Explain the role of intermediate representations (OpenQASM 2 and 3, Quil, QIR, MLIR dialects) in the compilation stack.
- Read and write OpenQASM 3: gates, modifiers, classical types, measurement, control flow, and
input/outputparameters. - Derive why global phase becomes observable under
ctrl @, and the OpenQASM 3 definition ofU. - Prove the deferred-measurement principle and a light-cone depth lower bound that dynamic circuits evade.
- Use
delay,box,stretchanddefcalto express timing and calibrations.
Why an intermediate representation?
A compiler stack needs a common language between front ends (Python SDKs, high-level languages) and back ends (simulators, control systems). Such an intermediate representation (IR) must be precise enough to execute, simple enough to analyze, and expressive enough for what hardware can actually do.
| IR | Origin | Character |
|---|---|---|
| OpenQASM 2 | IBM, 2017 | Flat gate lists with measurement and a simple classical if; became the de facto exchange format |
| OpenQASM 3 | Open specification, 2022 | Classical types and control flow, subroutines, real-time feed-forward, timing, and pulse-level calibrations |
| Quil | Rigetti, 2016 | An instruction set with a shared classical memory model and explicit quantum–classical interaction |
| QIR | Microsoft, then the QIR Alliance (2020s) | LLVM-based: quantum operations as function calls inside classical LLVM IR, reusing LLVM's tooling |
| MLIR dialects | Various (2020s) | Multi-level IRs for progressive lowering from algorithms toward pulses |
OPENQASM 3.0;
include "stdgates.inc";
input float[64] theta; // bound at run time, no recompilation
output bit[2] c;
qubit[2] q;
gate rzz(t) a, b { cx a, b; rz(t) b; cx a, b; }
ry(theta) q[0];
ry(theta) q[1];
rzz(pi / 4) q[0], q[1];
h q; // rotate both qubits to the X basis
c = measure q;input parameters let one compiled program be re-run with new angles, which is exactly what a variational loop needs.
Gates, modifiers and the phase you cannot ignore
OpenQASM 3 has only two built-in gates: the one-qubit and the global phase gphase(γ). Everything else, including the standard library stdgates.inc, is defined from them with gate declarations and modifiers: inv @ g is , pow(k) @ g is , ctrl @ g is controlled-, and negctrl @ g controls on .
Let , physically the same gate as . Control it:
Factor the phase out of the control- branch:
So
ctrl @turns a global phase into the phase gate on the control: a relative phase that interference can detect. A language withctrl @must therefore fix every gate's global phase, and it needsgphase, whose controlled version is a phase gate:
and differ only by a global phase. How do ctrl @ rz(pi) q[0], q[1]; and cz q[0], q[1]; compare?
- They are identical, because the gates differ only by a global phase
- They differ by an gate on the control qubit
- They differ by a global phase only
ctrl @ rz(pi)is not unitary
Show the answer
B. They differ by an gate on the control qubit
, so by the derivation . The extra phase sits on the control's branch: a relative phase, observable by interference.
Classical control and dynamic circuits
OpenQASM 3 treats measurement results as classical data that can steer the rest of a shot: if, while and for; arithmetic on bit, int, float and angle types; reset; and extern calls to classical functions. Computation that must finish within the qubits' coherence time is real-time; computation between shots, such as an optimizer choosing new input values, is near-time. How much power does real-time feed-forward add?
Test it on teleportation. The circuit below prepares on qubit 0 and performs Alice's half of the protocol. Instead of measuring and applying classically controlled and , add the quantum-controlled corrections.
So feed-forward adds no computational power in what can be computed. It can, however, save a great deal of depth, and depth is what decoherence charges for.
With mid-circuit measurement the same state needs only constant quantum depth on a line, given ancillas between the data qubits: put the data qubits in , measure neighboring parities into the ancillas, then flip each data qubit according to the prefix parity of the outcomes, a cheap classical computation done in real time.
OPENQASM 3.0;
include "stdgates.inc";
const uint N = 4;
qubit[N] d; // data qubits
qubit[N - 1] a; // ancilla a[i] sits between d[i] and d[i+1]
bit[N - 1] s;
bit parity = 0;
reset d;
reset a;
for uint i in [0:N - 1] { h d[i]; }
for uint i in [0:N - 2] { cx d[i], a[i]; cx d[i + 1], a[i]; }
s = measure a; // s[i] = x_i XOR x_(i+1)
// real-time feed-forward: flip d[j] if the prefix parity is odd
for uint j in [1:N - 1] {
parity = parity ^ s[j - 1];
if (parity == 1) { x d[j]; }
}One layer of , two layers of CNOTs, one round of measurement and one layer of : constant quantum depth, versus at least without feed-forward. Ranges in OpenQASM 3 include their endpoint.
Timing, stretch and calibrations
Gates take time, and idle qubits decohere. OpenQASM 3 makes time explicit: duration literals (ns, us, and dt for the backend's sample period), delay[t] on qubits, box to group operations into a block with a fixed or bounded duration that the compiler may not reorder across, and stretch, a duration variable the compiler solves for so that the timing constraints hold.
OPENQASM 3.0;
include "stdgates.inc";
// Fill a fixed 2 us idle window on physical qubit $0 with an XY4 echo.
// The compiler solves for the stretch s so that the box lasts exactly 2 us.
stretch s;
box[2us] {
delay[s] $0;
x $0;
delay[2 * s] $0;
y $0;
delay[2 * s] $0;
x $0;
delay[2 * s] $0;
y $0;
delay[s] $0;
}Here . Symmetric spacing makes the echo refocus slow dephasing, and the same program adapts automatically to whatever the gate duration is.
Finally, defcal attaches a pulse-level implementation to a gate on specific physical qubits, written in a calibration grammar such as OpenPulse. Calibrations are data, not code: the same program runs on different devices by swapping their defcal tables. The next lesson goes below the gate to see where these pulses come from.
OPENQASM 3.0;
defcalgrammar "openpulse";
cal {
extern port d0; // drive line of qubit $0
frame q0_drive = newframe(d0, 5.0e9, 0.0); // carrier frequency, phase
}
defcal x $0 {
waveform w = drag(0.2 + 0.0im, 160dt, 40dt, 0.5); // amp, length, sigma, beta
play(q0_drive, w);
}
defcal rz(angle[20] theta) $0 {
shift_phase(q0_drive, -theta); // virtual Z: a frame update
}Illustrative OpenPulse. Waveform templates such as drag, the frame frequencies and the phase-sign convention are supplied by each backend.
Key takeaways
- IRs (OpenQASM, Quil, QIR, MLIR dialects) decouple front ends from back ends; OpenQASM 3 adds classical control, timing and calibrations.
- Only
Uandgphaseare built in; modifiers build the rest, andctrl @makes global phases observable, so every gate's phase must be fixed. - Deferred measurement: feed-forward adds no computational power, but can cut depth from to , as for GHZ states on a line.
delay,box,stretchanddefcalmake timing and pulse calibrations part of the program.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
What does inv @ pow(2) @ s q; apply?
Show the answer
C.
, and . Modifiers apply from the gate outward; here both orders agree.
The deferred-measurement principle says that mid-circuit measurement with classical feed-forward…
- …lets quantum computers solve problems outside BQP
- …can be replaced by quantum-controlled gates followed by final measurements, with identical outcome statistics
- …cannot be implemented on hardware
- …always increases circuit depth
Show the answer
B. …can be replaced by quantum-controlled gates followed by final measurements, with identical outcome statistics
Classical control on a measured qubit equals quantum control from the unmeasured qubit, measured later. Computational power is unchanged; depth and qubit reuse are not, since the dynamic version can be far shallower and can reset and reuse qubits.
Why can't a depth-3 nearest-neighbor unitary circuit on a line of 10 qubits prepare the 10-qubit GHZ state from ?
- GHZ states can only be prepared with mid-circuit measurements
- Nearest-neighbor gates cannot entangle qubits that are far apart
- The light cones of qubits 1 and 10 (reaching at most 3 sites) do not overlap, so and would be uncorrelated, unlike in GHZ
- Because 10 is even
Show the answer
C. The light cones of qubits 1 and 10 (reaching at most 3 sites) do not overlap, so and would be uncorrelated, unlike in GHZ
Depth requires , i.e. . At the pulled-back lives on qubits 1–4 and on qubits 7–10: disjoint, so their correlation factorizes to , while GHZ needs .
Further reading
- A. W. Cross et al., "OpenQASM 3: A broader and deeper quantum assembly language", ACM Transactions on Quantum Computing 3(3) (2022).
- A. W. Cross, L. S. Bishop, J. A. Smolin & J. M. Gambetta, "Open quantum assembly language", arXiv:1707.03429 (2017).
- R. S. Smith, M. J. Curtis & W. J. Zeng, "A practical quantum instruction set architecture", arXiv:1608.03355 (2016).
- K. Hietala, R. Rand, S.-H. Hung, X. Wu & M. Hicks, "A verified optimizer for quantum circuits", POPL (2021).
- M. A. Nielsen & I. L. Chuang, Quantum Computation and Quantum Information, §4.4 (deferred measurement) (Cambridge, 2010).