ADR 0006 — The circuit carries its declared classical width¶
- Status: Accepted (2026-10)
- Contracts: C-2 · Gate vocabulary symmetry, C-3 · Measurement counts format, C-10 · OpenQASM 3 profile
- Supersedes: —
- Issue / PRs: #279 (this record, tests and contract text); #311 (the implementation); #322 (QIR width); #323 (Python
clbits_width); follows #251
Context¶
The circuit IR (polypus-circuit) knew one classical register, implicit: as
wide as the measurements need (max(cbit) + 1, num_qubits with
measure_all). The OpenQASM importers flattened every creg (or bit
register) into it and then forgot how wide the program had declared it.
Where that width matters is the key width of the counts (C-3): Qiskit keys a
measured circuit at the classical bits it declares, so creg c[3]; measure q[0]
-> c[1]; gives "010", not "10". #251 made the native backend agree with Aer
for an OpenQASM string: the importer stored the declared total in a
pub(crate) field of ParameterizedCircuit, ignored by == and read through a
hidden getter, and the native backend carried it next to the circuit
(NativeSource) to format the counts.
That left the other paths inconsistent:
Circuit.from_qasm2(src)/from_qasm3(src)dropped the width at binding, so the imported circuit ran atmax(cbit) + 1on both backends — a different key width from the same program run as text (C-3's "Known limit").- Every export wrote the implicit register:
to_qasm2(from_qasm2(src))lost unwritten trailing bits, and a program without measurements lost itscregaltogether.
Decision¶
As implemented by #311:
ConcreteCircuitcarries the declared width in a private field (Option<usize>,Nonefor nothing declared), and is built withConcreteCircuit::new(num_qubits, gates), which declares none. A struct literal no longer compiles; a future field can be added without breaking callers.ParameterizedCircuitkeeps itspub(crate)field;assign_parameterscopies it, andcloneand the builder methods keep it.clbits_width()(both types) is the effective width:max(declared, num_clbits()), ornum_clbits()when nothing was declared.num_clbits()keeps its meaning, the implicit register the measurements need (0 without measurements), so C-3's "no measurements ⇒num_qubits-wide keys" still reads it. The hiddendeclared_clbits()getter stays, on both types.PartialEq(both types) comparesclbits_width(): a declared width no larger than the measurements' is the same as declaring none; a larger one, measured or not, is a different circuit, because it exports differently.- The OpenQASM 2.0 and 3 exports write
clbits_width()(creg c[n];,bit[n] c;), also without measurements. A builder circuit declares none and exports byte for byte as before. - QIR declares
clbits_width()asrequired_num_results(#322; #311 had left it atnum_clbits()) and records only the measured results. It may declare results it never records, as a partial measurement already does: one written atc[1]declares 2 and records 1. QMIO'sopenqasm,qirandqir_bitcodeformats submit the same width. - The native backend reads the declared width from the circuit before any
transpiler runs (
NativeSource, now filled from theNativecircuit too), so a transpiler that rebuilds the circuit withConcreteCircuit::newcannot change the keys. - Python's
Circuit.num_clbitskeeps returningnum_clbits(), the implicit register;Circuit.clbits_widthreturnsclbits_width()(#323), the width Qiskit'sQuantumCircuit.num_clbitsreports for the same program.
What is modelled is the total width — what changes the length of the
counts' keys. Named registers and their partition are not: creg a[2]; creg
b[1]; is still flattened into one creg c[3], as it already was at the level
of counts (C-3, #206).
Consequences¶
- The same program gives the same keys on Aer and on the native backend
whether it is run as text or as
Circuit.from_qasm2(src)/from_qasm3(src), one QPU or several; C-3 loses its "Known limit". to_qasm2(from_qasm2(s))andto_qasm3(from_qasm3(s))keep the declared width; the export stays a fixed point. A full measurement into a register wider thannum_qubitsis written bit by bit, sincemeasure q -> c;needs equal sizes; the importer collapses it back toMeasureAll.- On the author's local, untracked fuzz corpus (2026-10-14: 855
from_qasm2entries, 76 of which import), the export change of #279's first implementation — equivalent to this one for OpenQASM 2.0 — altered 17 exports, all of them by gaining thecregof a program without measurements; no existing width changed. The repository only tracks the seed files of that corpus, so these figures cannot be reproduced from a clone. - CUNQA receives the exported program as Aer does, so an imported circuit that
declares a
cregnow reaches it with thatcregeven without measurements. Checked later on CUNQA 3.0.0 (#253, #304): the keys match Aer's once the seal adds the full read-out and joins CUNQA's space-separated keys. - Breaking for Rust code:
ConcreteCircuitliterals no longer compile (usenew), and==distinguishes a wider declared register. Python is unaffected except for the counts and exports of imported circuits.
Alternatives considered¶
- A public
declared_clbits: usizefield,num_clbits()as the effective width plus a newmeasured_clbits()for the implicit one,NativeSourceretired, QIR'srequired_num_resultsand Python'sCircuit.num_clbitsas the effective width. This was #279's first implementation. It kept literals constructible (declared_clbits: 0), madeCircuit.num_clbitsmatch Qiskit'sQuantumCircuit.num_clbitsfor imported programs, and made QMIO'sqir/qir_bitcodeformats declare the width itsopenqasmformat does. It was set aside because #311, the design above, was merged first: replacing it would have meant a second, incompatible API change to the same types within days, without a release in between. Option<usize>versus a plainusize. With equality and exports by the effective width,NoneandSome(0)behave identically (and the importers reject empty registers, soSome(0)never arises from them); #311 keepsOption, which the private field makes an internal detail.
Open questions (follow-ups)¶
- ~~QIR / QMIO parity.~~ Decided in #322:
required_num_resultsisclbits_width()(Decision 5), so QMIO's three formats declare the same width. The alternative, documenting the formats' difference as intended, would have left one program submitted at two widths depending onprogram_format. - ~~Python
Circuit.num_clbits.~~ Decided in #323: Python gainsCircuit.clbits_width(Decision 7) andnum_clbitskeeps its meaning. Changingnum_clbitsto the effective width would match Qiskit but break callers, and C-3's "no measurements ⇒num_qubits-wide keys" relies on it staying 0 for an unmeasured circuit. - Letting a builder user declare the width (e.g.
Circuit(3, num_clbits=5)) is new public API no issue asks for yet.
Reopening criteria¶
- Named classical registers, or their partition, become observable (counts keyed per register, mid-circuit classical control): the total width is no longer enough and the registers themselves must be modelled.
- One of the open questions above is decided in a way that changes the meaning
of
num_clbits()orclbits_width().