Skip to content

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:

EntryNext methodPurpose
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 ​

python
qif(cbits: list[int]) -> QIf

Parameters ​

ParameterTypeDescription
cbitslist[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 ​

python
class QIf:
    def then(branch_qprog: QProg) -> QIfThen

Parameters ​

ParameterTypeDescription
branch_qprogQProgThe 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 ​

python
QIfThen.qelse(branch_qprog: QProg) -> QProg

Adds an else-branch and returns the completed QProg.

ParameterTypeDescription
branch_qprogQProgThe quantum program to execute when no prior condition matched.

QIfThen.qelseif ​

python
QIfThen.qelseif(cbits: list[int]) -> QElseif

Adds an additional conditional branch after the if-branch.

ParameterTypeDescription
cbitslist[int]Classical bit addresses for the new condition.

QIfThen.qendif ​

python
QIfThen.qendif() -> QProg

Finalizes the if construct without an else-branch and returns the completed QProg.


QElseif ​

Builder for an additional conditional branch within a qif chain.

Signature ​

python
class QElseif:
    def then(branch_qprog: QProg) -> QElseifThen

Parameters ​

ParameterTypeDescription
branch_qprogQProgThe 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 ​

python
QElseifThen.qelse(branch_qprog: QProg) -> QProg

Adds an else-branch and returns the completed QProg.

ParameterTypeDescription
branch_qprogQProgThe quantum program to execute when no prior condition matched.

QElseifThen.qelseif ​

python
QElseifThen.qelseif(cbits: list[int]) -> QElseif

Chains another elseif condition.

ParameterTypeDescription
cbitslist[int]Classical bit addresses for the new condition.

QElseifThen.qendif ​

python
QElseifThen.qendif() -> QProg

Finalizes 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 ​

python
qwhile(cbits: list[int]) -> QWhile

Parameters ​

ParameterTypeDescription
cbitslist[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 ​

python
class QWhile:
    def loop(loop_body: QProg) -> QProg

Parameters ​

ParameterTypeDescription
loop_bodyQProgThe 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.

python
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()

运行结果:

          ┌─┐           ┌─┐            
q_0:  |0>─┤X├───────── ─┤M├── ──────── 
          ├─┴────────   └╥┼─┐ ───────┐ 
q_1:  |0>─┤1 QIF     ─ ──╫┤X├  QENDIF├ 
          └╥─────────    ║└─┘ ───────┘ 
 c :   / ══╩═════════════╩════════════
           0:All 0b1      0

If-else branch ​

python
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)

运行结果:

          ┌─┐           ┌─┐                          
q_0:  |0>─┤X├───────── ─┤M├── ───────── ─── ──────── 
          ├─┴────────   └╥┼─┐ ┌────────     ───────┐ 
q_1:  |0>─┤1 QIF     ─ ──╫┤X├ ┤1 QELSE  ───  QENDIF├ 
          ┤              ║└─┘ ┤         ┌─┐        ├ 
q_2:  |0>─┤2         ─ ──╫─── ┤2        ┤X├        ├ 
          └╥─────────    ║    └──────── └─┘ ───────┘ 
 c :   / ══╩═════════════╩══════════════════════════
           0:All 0b1      0

Multi-branch with elseif ​

python
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)
)

运行结果:

          ┌─┐  ┌─┐     ┌──────────      ┌──────────  ┌─┐ ┌────────────      ┌────────        ───────┐ 
q_0:  |0>─┤X├ ─┤M├──── ┤0 QIF     ─ ─── ┤0 QELSEIF ─ ┤X├ ┤0 QELSEIF   ─ ─── ┤0 QELSE  ───*──  QENDIF├ 
          ├─┤  └╥┘     ┤            ┌─┐ ┤            └─┘ ┤                  ┤         ┌──┴─┐        ├ 
q_1:  |0>─┤I├ ──╫───── ┤1         ─ ┤X├ ┤1         ─ ─── ┤1           ─ ─── ┤1        ┤CNOT├        ├ 
          ├─┤   ║      ┤            └─┘ ┤                ┤              ┌─┐ ┤         └────┘        ├ 
q_2:  |0>─┤I├ ──╫───── ┤2         ─ ─── ┤2         ─ ─── ┤2           ─ ┤X├ ┤2        ──────        ├ 
          ├─┤   ║  ┌─┐ └╥─────────      └╥─────────      └╥───────────  └─┘ └────────        ───────┘ 
q_3:  |0>─┤I├ ──╫──┤M├ ─║────────── ─── ─║────────── ─── ─║──────────── ─── ───────── ────── ──────── 
          └─┘   ║  └╥┘  ║                ║                ║                                           
 c :   / ═══════╩═══╩═══╩════════════════╩════════════════╩══════════════════════════════════════════
                 0   3  0:All 0b1        3:All 0b1        0,3:All 0b1

While loop with mid-circuit measurement ​

python
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)

运行结果:

          ┌─┐  ┌─┐ 
q_0:  |0>─┤X├ ─┤M├ 
          └─┘  └╥┘ 
q_1:  |0>──── ──╫─ 
                ║  
 c :   / ═══════╩═
                 0

See Also ​

Released under the MIT License.