Skip to content

feat: add while, for, switch, and if/else classical control flow#22

Merged
splch merged 2 commits intomainfrom
feat/classical-control-flow
Mar 15, 2026
Merged

feat: add while, for, switch, and if/else classical control flow#22
splch merged 2 commits intomainfrom
feat/classical-control-flow

Conversation

@splch
Copy link
Copy Markdown
Owner

@splch splch commented Mar 15, 2026

Add structured classical control flow to the circuit IR and all layers of the stack, closing the gap with Qiskit and Q#.

IR: ControlFlow struct on Operation with While/For/Switch/IfElse types, nested []Operation bodies, ForRange, and SwitchArg. Added WalkOps/MapOps helpers and MaxControlFlowIterations safety cap.

Builder: While(), For(), IfElseBlock(), Switch() methods using sub-builder closure pattern consistent with existing IfBlock().

Simulators: Refactored RunDynamic in both statevector and density matrix to recursive executeOps with executeControlFlow dispatch.

QASM: Full OpenQASM 3.0 round-trip — parser handles while, for, switch, and if/else; emitter produces corresponding syntax. Added SWITCH/CASE/DEFAULT tokens.

Drawing: ASCII, LaTeX, and SVG renderers display control flow as labeled spanning boxes across qubits.

Backends: IonQ, Google, Quantinuum, and Quil emit clear errors on unsupported control flow. IBM/Braket get support via QASM emitter.

Compose/Tensor/Inverse updated to recurse into control flow bodies. Transpile passes safely pass through control flow operations.

splch added 2 commits March 14, 2026 23:36
Add structured classical control flow to the circuit IR and all
layers of the stack, closing the gap with Qiskit and Q#.

IR: ControlFlow struct on Operation with While/For/Switch/IfElse
types, nested []Operation bodies, ForRange, and SwitchArg. Added
WalkOps/MapOps helpers and MaxControlFlowIterations safety cap.

Builder: While(), For(), IfElseBlock(), Switch() methods using
sub-builder closure pattern consistent with existing IfBlock().

Simulators: Refactored RunDynamic in both statevector and density
matrix to recursive executeOps with executeControlFlow dispatch.

QASM: Full OpenQASM 3.0 round-trip — parser handles while, for,
switch, and if/else; emitter produces corresponding syntax. Added
SWITCH/CASE/DEFAULT tokens.

Drawing: ASCII, LaTeX, and SVG renderers display control flow as
labeled spanning boxes across qubits.

Backends: IonQ, Google, Quantinuum, and Quil emit clear errors on
unsupported control flow. IBM/Braket get support via QASM emitter.

Compose/Tensor/Inverse updated to recurse into control flow bodies.
Transpile passes safely pass through control flow operations.
- Check error return from emitOp in emitOps (errcheck/G104)
- Rewrite if-else chains to switch statements (gocritic)
- Fix gofmt alignment in ControlFlowType const and test comment
@splch splch merged commit 1376a8b into main Mar 15, 2026
6 checks passed
@splch splch deleted the feat/classical-control-flow branch March 15, 2026 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant