ADR 0004 — OpenQASM 3 profile with Qiskit phase conventions, and angle expressions¶
- Status: Accepted (2026-10)
- Contracts: C-2 · Gate vocabulary symmetry, C-10 · OpenQASM 3 profile with Qiskit phase conventions
- Supersedes: —
- Issue / PRs: #236; #241 (angle expressions), #282 (OpenQASM 3)
Context¶
Public benchmark suites (QED-C, MQT Bench, SupermarQ, qml-benchmarks) ship parameterised circuits Polypus could not load:
GateParamwasFixed(f64) | Param(usize), so an angle could not be an expression of free parameters. Suite ansätze need them: QED-C MaxCut writesrzz(-gamma)andrx(2*beta), Qiskit'szz_feature_mapwritesp((-pi + x0)*(-pi + x1)*2).- OpenQASM 2.0 cannot carry unbound parameters (Qiskit raises
QASM2ExportError). OpenQASM 3 can, withinput float[64] name;.
OpenQASM 3 and Qiskit, its main producer, disagree on the matrices of U,
u2 and u3 by global phases, and the specification is not consistent with
itself on CX (Evidence).
Decision¶
C-10 specifies the profile and C-2 the expressions; this section records why.
- A straight-line profile.
from_qasm3/to_qasm3read and write the part of OpenQASM 3 that carries a parameterised, terminal-measurement circuit (ADR 0001):input floatparameters,stdgates.inc,gatedeclarations, angle expressions,barrier, measurements. Everything else (control flow,reset, classical computation,def, modifiers,gphase, timing, pulse) is aParseerror naming the construct and its line. The importer is our own streaming parser, with the hardening and fuzzing of the OpenQASM 2.0 one. - Qiskit phase conventions.
U,u2andu3use Qiskit's matrices (Polypus'su,u2,u3), off the specification by exactly e^{−iθ/2} and e^{+i(φ+λ)/2}. The API, README and C-10 always call it "OpenQASM 3 profile with Qiskit phase conventions", never "a subset of OpenQASM 3". Amplitudes differ by a global factor only; probabilities, counts and expectation values do not. This holds only while modifiers andgphaseare rejected, because under them a global phase becomes observable. - Spellings. Import
U→u,CX→cx,phase→p,cphase→cp; exportu→U; nothing else.CXfollowsstandard_library.rst("an alias for cx") and Qiskit, not thestdgates.incbody. - Declared gates are never built-ins by name. A declared
rzzstays a declared gate. Definitions are printed for the target dialect (ln↔log,^↔**) and renamed deterministically on collision; a body the target cannot express (arcsinin OpenQASM 2.0, abarrierin OpenQASM 3) fails withGateNotExpressible. OpenQASM 2.0 declarations keep their byte-identical re-emission. - The whole vocabulary exports. The twelve instructions
stdgates.inclacks (rzz,rxx,sxdg,csx,cu1,cu3,u0,rccx,rc3x,c3x,c3sqrtx,c4x) are written as calls of definitions with the same matrices, parsed from one canonical text by the importer itself. - Expressions live in an arena owned by the circuit.
GateParam::Exprholds anExprIdinto a flat postfix arena, soGateParamstaysCopyand 16 bytes. Gate-body and circuit expressions are distinct types (Formal,Inputreferences). Construction is validated; evaluation is one loop and never panics or recurses. AnExprIdcarries a content checksum, so an id from another circuit is rejected (UnknownExpression) without a global counter. (Since #354 the id is a 64-bit checksum, resolved through a per-circuit map and trusted only after the nodes compare equal; identical expressions are stored once.) - Numbers. Exactly the written operations, in source order, in binary64;
no reassociation or folding. Only the final value must be finite. A division
between two integer expressions (
1/2) is rejected: it is 0 in OpenQASM 3 and 0.5 in Qiskit and Python, so either reading silently changes some programs (spec/v3.0.0's ownpow(1/2)is one). Export writes the shortest decimal that reads back as the same value. - Parameter identity. Inputs are the parameters in declaration order;
their names are stored (
theta_<i>by default), part of equality, and exposed asCircuit.param_names. Qiskit's name mangling is not reversed. - The export is a fixed point and stays importable.
to_qasm3(from_qasm3(to_qasm3(c))) == to_qasm3(c)byte for byte. Because a canonical export can be larger than the importer reads back (a broadcast measurement, ac4xbecoming a 98-statement definition), the exporter spends the importer's budgets as it writes and fails withExportLimitrather than write textfrom_qasm3would reject. - Backends never panic on an inexpressible circuit. The backends that
submit OpenQASM 2.0 (Aer, CUNQA, QMIO, the subprocess bridge, and the Rust
backend template) use
try_to_qasm2and reportUnsupportedCircuit(polypus.NativeCircuitError); the native backend runs the circuit.
Evidence¶
The specification repository was read at spec/v3.0.0
(51c36946c687c8b17000962f6ce735ca1d1c9b3b), spec/v3.1.0
(c717508162a0eac892fa32134716fe77a284e835) and main
(7fbf9e9eb3692a1288c014d6efd43523701886c6), with Qiskit 2.5.2,
qiskit-qasm3-import 0.6.0, openqasm3 1.0.1 and antlr4-python3-runtime 4.13.2.
stdgates.inc is identical in v3.1.0 and main; v3.0.0 differs in its
header and in pow(1/2). The profile uses v3.1.0's.
Each stdgates.inc gate was evaluated with the specification's own semantics
(U from gates.rst, gphase, ctrl @, inv @, principal-branch pow @)
and compared, as a full matrix at six random draws, with Qiskit's gate of the
same name, by 0004-phase-table.py
(python 0004-phase-table.py <spec>/examples/stdgates.inc). The result
is the same at all three tags:
| Result | Gates |
|---|---|
| Exactly equal (residual ≤ 8e-16) | p x y z h s sdg t tdg sx rx ry rz cx cy cz cp crx cry crz ch swap ccx cswap cu phase cphase id u1 |
| Global phase e^{iθ/2} (spec = factor × Qiskit) | U |
| Global phase e^{−i(φ+λ)/2} | u2 u3 |
| Not a global phase | CX |
Qiskit behaviours, each pinned by a test in tests/python/test_qasm3_interop.py:
qasm3.dumpswritesQuantumCircuit.uas bareU, andu2/u3by name, without phase compensation.- It drops the circuit's
global_phaseand the global phase of every definition it writes (sxdgbecomess; h; s, e^{iπ/4}·SXdg). The tests compare against Qiskit's own reading of the same text, and against the original circuit up to the phasedumpsdrops, computed from Qiskit's definitions. - For QAOA,
efficient_su2,real_amplitudes,zz_feature_mapand one circuit per vocabulary gate, its output holds nogphase, no modifier and no division of two integers, so every text imports. u3 = p(φ)·ry(θ)·p(λ)exactly, so a specification-normative export withoutgphaseremains possible.
Binding cost (#241): bare-parameter binding stayed within 5 % of the base
commit (benchmarks/bench_native_vs_qiskit.py, median 0.95×), after a
1.7× regression from a 4-byte-aligned ExprId was fixed with
#[repr(align(8))]. Tests pin GateParam at 16 bytes and CircuitError at
48 bytes.
Upstream inconsistencies, recorded and not resolved:
stdgates.incdefinesCXasctrl @ U(π, 0, π), which under the specification'sUis controlled-(iX), a relative phase away from CNOT;standard_library.rstcallsCX"a convenience alias for cx" andgates.rstbuilds it as CNOT. No upstream issue found (2026-09-28).standard_library.rstomits the half angle in therx,ry,rz,crx,crymappings, gives a non-diagonalrz, and mapssdg's |1⟩ to i|1⟩.- The
gates.rstfootnote exampleU_old(0, ϕ, θ) … gphase(-0/2)is garbled. spec/v3.0.0writespow(1/2), which ispow(0)under its own integer division;v3.1.0writespow(0.5).- Qiskit's
UGatedocstring claims the specification's matrix; numerically it is Qiskit's.
Consequences¶
- Circuits from Qiskit and the benchmark suites load with the semantics Qiskit gives them, and Polypus's output reads back in Qiskit as the same circuit.
- A specification-normative reader sees
U,u2,u3off by the declared global factors. Statevector comparisons across tools must apply them. - The OpenQASM 2.0 path writes the same bytes for every circuit without OpenQASM 3 declarations; binding cost stays within 5 %.
ConcreteCircuit::to_qasm2still panics wheretry_to_qasm2errs; new callers should use the fallible form.- From Python, expressions came only from OpenQASM 3 at first; #239 added
polypus.ParamExpr, built with operators onpolypus.Param. - The OpenQASM 3 exporter identifies definitions by address, so value-equal definitions from separately imported programs are written twice; the output is still a fixed point.
- Running an imported circuit on Aer is limited by Aer's basis, not by the profile (#252).
Alternatives considered¶
- Specification-normative
U/u2/u3. Needs global-phase state in the IR, changes only statevector amplitudes, and diverges from Qiskit's output, the main input. Deferred; mandatory if modifiers orgphaseare admitted. CXas itsstdgates.incbody (controlled-(iX)). Contradicts the library's prose and every Qiskit round trip.- A third-party parser (
oq3_semantics0.7.0, last release 2024-10). Full OpenQASM 3, but pre-1.0, a large dependency for a small profile, and it would bypass the hardened, fuzzed importer. - A bespoke JSON circuit format. No parser, but non-standard, and it still needs expressions in the IR.
- Re-parameterising instead of expressions (
φ = 2β). Changes the landscape's scale and initial parameters, and cannot express data encodings. - Expressions as boxed trees inside
GateParam. LosesCopyand growsGateParam, whose layout binding cost is sensitive to: a layout change alone made binding 1.7× slower (Evidence). - Recognising declared gates by name (a declared
rzzas the native one). Wrong whenever the body differs; collisions are renamed instead.
Reopening criteria¶
- Admitting modifiers,
gphaseor global-phase tracking: switch to specification-normativeU/u2/u3, in a new ADR. - Upstream changes:
stdgates.incaligningCXwith CNOT, or Qiskit changing itsUconvention or its export of phases. - Admitting control flow or
reset: that reopens ADR 0001 first.