Dynamic Circuit -- qif, qwhile
API reference for dynamic circuit control flow in pyqpanda3. Dynamic circuits allow mid-circuit measurement and classical feed-forward, enabling conditional execution and loop constructs within a quantum program.
Overview
pyqpanda3 provides a fluent builder API for constructing dynamic quantum programs. The two entry-point functions are qif() and qwhile(), which produce builder objects that are appended to a QProg via the << operator.
Builder chain summary:
| Entry | Next method | Purpose |
|---|---|---|
qif(cbits) | .then(prog) | Start an if-branch |
QIfThen | .qendif() | End with no else |
QIfThen | .qelse(prog) | Add an else-branch and end |
QIfThen | .qelseif(cbits) | Add another condition branch |
QElseif | .then(prog) | Bind the elseif body |
QElseifThen | .qendif() | End with no further branches |
QElseifThen | .qelse(prog) | Add an else-branch and end |
QElseifThen | .qelseif(cbits) | Chain another elseif |
qwhile(cbits) | .loop(prog) | Bind the loop body |
qif
Creates a conditional branch builder that checks whether all specified classical bits are 1.
Signature
qif(cbits: list[int]) -> QIfParameters
| Parameter | Type | Description |
|---|---|---|
| cbits | list[int] | Classical bit addresses whose values are evaluated as the condition. The condition is true when all specified cbits are 1. |
Returns
A QIf builder object. Call .then() on it to specify the if-branch body.
QIf
Builder returned by qif(). Use .then() to bind a branch sub-program.
Signature
class QIf:
def then(branch_qprog: QProg) -> QIfThenParameters
| Parameter | Type | Description |
|---|---|---|
| branch_qprog | QProg | The quantum program to execute when the condition is true. |
Returns
A QIfThen builder for chaining further branches or finalizing.
QIfThen
Builder returned by QIf.then(). Supports adding else-branches, elseif-branches, or finalizing.
Methods
QIfThen.qelse
QIfThen.qelse(branch_qprog: QProg) -> QProgAdds an else-branch and returns the completed QProg.
| Parameter | Type | Description |
|---|---|---|
| branch_qprog | QProg | The quantum program to execute when no prior condition matched. |
QIfThen.qelseif
QIfThen.qelseif(cbits: list[int]) -> QElseifAdds an additional conditional branch after the if-branch.
| Parameter | Type | Description |
|---|---|---|
| cbits | list[int] | Classical bit addresses for the new condition. |
QIfThen.qendif
QIfThen.qendif() -> QProgFinalizes the if construct without an else-branch and returns the completed QProg.
QElseif
Builder for an additional conditional branch within a qif chain.
Signature
class QElseif:
def then(branch_qprog: QProg) -> QElseifThenParameters
| Parameter | Type | Description |
|---|---|---|
| branch_qprog | QProg | The quantum program to execute when this elseif condition is true. |
Returns
A QElseifThen builder for chaining further branches or finalizing.
QElseifThen
Builder returned by QElseif.then(). Supports adding further else, elseif, or ending the construct.
Methods
QElseifThen.qelse
QElseifThen.qelse(branch_qprog: QProg) -> QProgAdds an else-branch and returns the completed QProg.
| Parameter | Type | Description |
|---|---|---|
| branch_qprog | QProg | The quantum program to execute when no prior condition matched. |
QElseifThen.qelseif
QElseifThen.qelseif(cbits: list[int]) -> QElseifChains another elseif condition.
| Parameter | Type | Description |
|---|---|---|
| cbits | list[int] | Classical bit addresses for the new condition. |
QElseifThen.qendif
QElseifThen.qendif() -> QProgFinalizes the construct without an else-branch and returns the completed QProg.
qwhile
Creates a loop builder that repeatedly executes a body while all specified classical bits are 1.
Signature
qwhile(cbits: list[int]) -> QWhileParameters
| Parameter | Type | Description |
|---|---|---|
| cbits | list[int] | Classical bit addresses evaluated before each iteration. The loop continues while all specified cbits are 1. |
Returns
A QWhile builder object. Call .loop() on it to bind the loop body.
QWhile
Builder returned by qwhile(). Use .loop() to bind the loop body.
Signature
class QWhile:
def loop(loop_body: QProg) -> QProgParameters
| Parameter | Type | Description |
|---|---|---|
| loop_body | QProg | The quantum program to execute on each iteration. |
Returns
The completed QProg containing the while loop construct.
Examples
Simple if-branch with mid-circuit measurement
Measure qubit 0 into classical bit 0, then conditionally apply an X gate to qubit 1.
from pyqpanda3.core import QProg, X, measure, qif
prog = QProg()
prog << X(0) << measure(0, 0)
if_branch = QProg()
if_branch << X(1)
prog << qif([0]).then(if_branch).qendif()If-else branch
from pyqpanda3.core import QProg, X, measure, qif
prog = QProg()
prog << X(0) << measure(0, 0)
if_branch = QProg()
if_branch << X(1)
else_branch = QProg()
else_branch << X(2)
prog << qif([0]).then(if_branch).qelse(else_branch)Multi-branch with elseif
from pyqpanda3.core import QProg, X, I, CNOT, measure, qif
prog = QProg()
prog << X(0) << I(1) << I(2) << I(3) << measure(0, 0) << measure(3, 3)
branch1 = QProg()
branch1 << X(1)
branch2 = QProg()
branch2 << X(0)
branch3 = QProg()
branch3 << X(2)
else_branch = QProg()
else_branch << CNOT(0, 1)
(prog << qif([0]).then(branch1)
.qelseif([3]).then(branch2)
.qelseif([0, 3]).then(branch3)
.qelse(else_branch)
)While loop with mid-circuit measurement
from pyqpanda3.core import QProg, X, measure, qwhile
prog = QProg()
prog << X(0) << measure(0, 0)
loop_body = QProg()
loop_body << X(1) << X(0) << measure(0, 0)
prog << qwhile([0]).loop(loop_body)