Skip to main content
b01t provides three control flow combinators for structuring programs. repeat and for_each unroll at build time — they emit gates directly by calling your body function multiple times during the .build() call, so the resulting IR contains only flat gate sequences, not loops. if_then is different: it emits a genuine conditional branch in the IR and is only valid inside @adaptive programs. Import all three from b01t.
repeat, for_each, and if_then are only valid inside a .build() call on a @parametric or @adaptive function. They are not available in exact programs (@coherent, @primitive).

repeat

Calls body exactly count times at build time, unrolling the loop into the IR. Use it to apply the same gate pattern multiple times without writing it out manually.
count
int
required
Number of times to call body. Must be a non-negative integer known at build time.
body
Callable[[], None]
required
A zero-argument callable that emits one iteration of gates.

for_each

Iterates over data at build time, calling body(index, value) for each element. Use it to apply parametric gates driven by classical data without runtime overhead.
data
Sequence[Any]
required
A sequence of values to iterate over. The sequence is consumed entirely at build time.
body
Callable[[int, Any], None]
required
A callable that receives (index, value) and emits gates for that element.

if_then

Emits a conditional branch in the IR. Both then_body and else_body are captured as op sequences at build time and stored in an IfOp. At runtime (when lowered to hardware), the condition is evaluated and the appropriate branch is executed. if_then is only valid in @adaptive programs. Calling it inside a @coherent or @parametric function raises DSLValidationError.
cond
Any
required
The classical condition to branch on. Typically the result of a measure call (a string key like "m_q_0"). The condition is stored as-is in the IR and evaluated by the backend.
then_body
Callable[[], None]
required
A zero-argument callable that emits gates for the true branch.
else_body
Callable[[], None]
default:"None"
A zero-argument callable that emits gates for the false branch. If omitted, the false branch is empty.
With only a then branch:
The IfOp emitted by if_then is not supported by QiskitBackend in the current release. Use if_then to model the semantics in the IR; lowering to hardware requires a custom backend that handles classical feed-forward.