Skip to content

动态线路 -- qif, qwhile

pyqpanda3 中动态线路控制流的 API 参考。动态线路支持线路中间测量和经典前馈,在量子程序中实现条件执行和循环构造。

概述

pyqpanda3 提供流式构建器 API 用于构建动态量子程序。两个入口函数是 qif()qwhile(),它们产生构建器对象,通过 << 运算符追加到 QProg

构建器链总结:

入口下一个方法用途
qif(cbits).then(prog)开始 if 分支
QIfThen.qendif()无 else 结束
QIfThen.qelse(prog)添加 else 分支并结束
QIfThen.qelseif(cbits)添加另一个条件分支
QElseif.then(prog)绑定 elseif 体
QElseifThen.qendif()无更多分支结束
QElseifThen.qelse(prog)添加 else 分支并结束
QElseifThen.qelseif(cbits)链接另一个 elseif
qwhile(cbits).loop(prog)绑定循环体

qif

创建条件分支构建器,检查所有指定经典比特是否为 1。

签名

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

参数

参数类型说明
cbitslist[int]其值被评估为条件的经典比特地址。当所有指定 cbits 为 1 时条件为真。

返回

一个 QIf 构建器对象。对其调用 .then() 来指定 if 分支体。


QIf

qif() 返回的构建器。使用 .then() 绑定分支子程序。

签名

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

参数

参数类型说明
branch_qprogQProg条件为真时执行的量子程序。

返回

用于链接更多分支或完成构建的 QIfThen 构建器。


QIfThen

QIf.then() 返回的构建器。支持添加 else 分支、elseif 分支或完成构建。

方法

QIfThen.qelse

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

添加 else 分支并返回完成的 QProg

参数类型说明
branch_qprogQProg没有先前条件匹配时执行的量子程序。

QIfThen.qelseif

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

在 if 分支后添加额外的条件分支。

参数类型说明
cbitslist[int]新条件的经典比特地址。

QIfThen.qendif

python
QIfThen.qendif() -> QProg

无 else 分支结束 if 构造,返回完成的 QProg


QElseif

qif 链中额外条件分支的构建器。

签名

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

参数

参数类型说明
branch_qprogQProg此 elseif 条件为真时执行的量子程序。

返回

用于链接更多分支或完成构建的 QElseifThen 构建器。


QElseifThen

QElseif.then() 返回的构建器。支持添加更多 else、elseif 或结束构造。

方法

QElseifThen.qelse

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

添加 else 分支并返回完成的 QProg

参数类型说明
branch_qprogQProg没有先前条件匹配时执行的量子程序。

QElseifThen.qelseif

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

链接另一个 elseif 条件。

参数类型说明
cbitslist[int]新条件的经典比特地址。

QElseifThen.qendif

python
QElseifThen.qendif() -> QProg

无 else 分支结束构造,返回完成的 QProg


qwhile

创建循环构建器,在所有指定经典比特为 1 时重复执行循环体。

签名

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

参数

参数类型说明
cbitslist[int]每次迭代前评估的经典比特地址。当所有指定 cbits 为 1 时循环继续。

返回

一个 QWhile 构建器对象。对其调用 .loop() 来绑定循环体。


QWhile

qwhile() 返回的构建器。使用 .loop() 绑定循环体。

签名

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

参数

参数类型说明
loop_bodyQProg每次迭代执行的量子程序。

返回

包含 while 循环构造的完成的 QProg


示例

带线路中间测量的简单 if 分支

测量量子比特 0 到经典比特 0,然后条件性地对量子比特 1 应用 X 门。

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

if-else 分支

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)

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

带线路中间测量的 while 循环

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)

另见

Released under the MIT License.