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
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.
Number of times to call
body. Must be a non-negative integer known at build time.A zero-argument callable that emits one iteration of gates.
for_each
data at build time, calling body(index, value) for each element. Use it to apply parametric gates driven by classical data without runtime overhead.
A sequence of values to iterate over. The sequence is consumed entirely at build time.
A callable that receives
(index, value) and emits gates for that element.if_then
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.
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.A zero-argument callable that emits gates for the true branch.
A zero-argument callable that emits gates for the false branch. If omitted, the false branch is empty.
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.