Dynamic Circuits
A dynamic circuit interleaves quantum gates with mid-circuit measurement and classical control flow — branching on measured bits while the qubits stay coherent.
Classical feedforward
import qalgora
from qalgora import h, x, mz
@qalgora.kernel
def adaptive():
q = qalgora.qvector(2)
h(q[0])
b = mz(q[0]) # mid-circuit measurement
if b: # real-time conditional gate
x(q[1])
for _ in range(2): # bounded loops are allowed
h(q[1])
mz(q[1])
How
When a kernel is compiled for a dynamic-circuit backend, if b: is capturedif b: is captured at
compile time as a hardware real-time conditional driven by the measured bit — it becomes part
of the circuit, not a host-side Python branch evaluated after the job returns. (The reference
simulator reproduces the same behavior by re-executing the kernel per shot.)
What you can do
- Conditionals — apply gates based on measured bits (
if). - Bounded loops — repeat blocks a fixed number of times.
- Qubit reuse — measure, reset, and reuse a qubit later in the circuit.
Powers
Dynamic circuits underpin teleportation, real-time
error correction, and adaptive algorithms. See also
Mid-Circuit Measurement.
动态线路
动态线路将量子门与线路中测量和经典控制流交织在一起——在量子比特保持相干的同时,根据测量比特进行分支。
经典前馈
import qalgora
from qalgora import h, x, mz
@qalgora.kernel
def adaptive():
q = qalgora.qvector(2)
h(q[0])
b = mz(q[0]) # mid-circuit measurement
if b: # real-time conditional gate
x(q[1])
for _ in range(2): # bounded loops are allowed
h(q[1])
mz(q[1])
if b 如何被捕获
当内核被编译到动态线路后端时,if b: 会在编译期被捕获为由测量比特驱动的硬件实时条件——
它是线路的一部分,而不是作业返回后在主机侧执行的 Python 分支。(参考实现的模拟器通过每次采样
重新执行内核来复现相同行为。)
可以实现的功能
- 条件 — 根据测量比特应用门操作(
if)。 - 有界循环 — 将代码块重复固定次数。
- 量子比特复用 — 对量子比特进行测量、重置,并在线路后续阶段复用。