线路 -- Qubit, CBit, Operation, QGate, QCircuit, QProg
pyqpanda3 中量子线路构建的 API 参考。这些类构成了描述量子计算的核心构建模块:量子比特引用、操作、门、线路和程序。
Qubit
表示由数字地址标识的量子比特(qubit)。Qubit 对象是构建门和线路时使用的轻量级句柄。
签名
Qubit(qubit: int)参数
| 参数 | 类型 | 说明 |
|---|---|---|
| qubit | int | 量子比特的整数地址(索引)。 |
方法
| 方法 | 返回类型 | 说明 |
|---|---|---|
get_qubit_addr() | int | 返回此量子比特的地址(索引)。 |
特殊方法
__int__()-- 将量子比特转换为其整数地址。__repr__()-- 返回字符串表示,例如Qubit(0)。
示例
from pyqpanda3.core import Qubit
q0 = Qubit(0)
q1 = Qubit(1)
print(q0) # Qubit(0)
print(int(q0)) # 0
print(q0.get_qubit_addr()) # 0运行结果:
Qubit(0)
0
0另见
CBit
表示用于存储测量结果的经典比特。与 Qubit 类似,它是由数字地址标识的轻量级句柄。
签名
CBit(addr: int)参数为位置参数(positional-only)。
参数
| 参数 | 类型 | 说明 |
|---|---|---|
| addr | int | 经典比特的整数地址(索引)。 |
方法
| 方法 | 返回类型 | 说明 |
|---|---|---|
get_cbit_addr() | int | 返回此经典比特的地址(索引)。 |
特殊方法
__int__()-- 将经典比特转换为其整数地址。__repr__()-- 返回字符串表示,例如CBit(0)。
示例
from pyqpanda3.core import CBit
c0 = CBit(0)
c1 = CBit(1)
print(c0) # CBit(0)
print(int(c0)) # 0
print(c0.get_cbit_addr()) # 0运行结果:
CBit(0)
0
0另见
Operation
量子程序中所有操作的基类。QGate 继承自 Operation。Operation 携带关于其类型、目标量子比特、控制量子比特和关联的经典比特(用于测量操作)的元数据。
签名
Operation()属性
| 属性 | 类型 | 说明 |
|---|---|---|
m_operation_type | OpType | 此操作的类型(Gate、Measure 等)。 |
m_target_qubits | list[int] | 目标量子比特索引。 |
m_control_qubits | list[int] | 控制量子比特索引。 |
m_measure_cbits | list[int] | 与测量关联的经典比特索引。 |
方法
| 方法 | 返回类型 | 说明 |
|---|---|---|
is_controlled() | bool | 如果此操作至少有一个控制量子比特则返回 True。 |
示例
from pyqpanda3.core import H, CNOT, RX
# H is a single-qubit gate (Operation subclass)
h_gate = H(0)
print(h_gate.m_operation_type) # OpType.Gate
print(h_gate.m_target_qubits) # [0]
print(h_gate.is_controlled()) # False
# CNOT is a two-qubit gate
cnot = CNOT(0, 1)
print(cnot.m_control_qubits) # []
print(cnot.m_target_qubits) # [0, 1]
print(cnot.is_controlled()) # False运行结果:
OpType.Gate
[0]
False
[]
[0, 1]
False另见
QGate
表示量子门操作。QGate 继承自 Operation,并添加了门特有的功能,如矩阵表示、门类型、参数、dagger 和幂运算以及控制量子比特管理。
签名
QGate 实例通过模块级门工厂函数(如 H、X、CNOT、RX 等)创建,而非直接调用 QGate 构造函数。
# Typical construction via factory functions
gate = H(0) # single-qubit gate
gate = CNOT(0, 1) # two-qubit gate
gate = RX(0, 3.14) # parameterized gate方法
qubits_num
qubits_num() -> int返回此门作用的总量子比特数(包括目标和控制量子比特)。
qubits
qubits() -> list[int]返回此门使用的所有量子比特索引(包括目标和控制量子比特)。
control_qubits
control_qubits() -> list[int]返回此门的控制量子比特索引。
target_qubits
target_qubits() -> list[int]返回目标量子比特索引。
dagger
dagger() -> QGate返回一个新的 QGate,是该门的厄米共轭(adjoint)。原始门不会被修改。
is_dagger
is_dagger() -> bool如果此门处于 dagger(伴随)形式则返回 True。
power
power(k: float) -> QGate返回一个新门,表示此门的 k 次幂。
| 参数 | 类型 | 说明 |
|---|---|---|
| k | float | 指数。 |
clear_control
clear_control() -> None移除此门的所有控制量子比特。
control
control(qubit: int) -> QGate
control(qubits: list[int]) -> QGate向此门添加一个或多个控制量子比特。返回一个新的 QGate,原始门不会被修改。
| 参数 | 类型 | 说明 |
|---|---|---|
| qubit | int | 要添加的单个控制量子比特索引。 |
| qubits | list[int] | 要添加的控制量子比特索引列表。 |
matrix
matrix(expanded: bool = False) -> numpy.ndarray返回此门的矩阵表示。当 expanded 为 True 时,矩阵扩展到包含控制量子比特的完整量子比特空间。
| 参数 | 类型 | 说明 |
|---|---|---|
| expanded | bool | 是否返回扩展矩阵(默认 False)。 |
gate_type
gate_type() -> GateType返回此门的 GateType 枚举值。
remap
remap(mapping: dict[int, int]) -> QGate
remap(mapping: list[int]) -> QGate返回一个新门,其量子比特索引根据提供的映射重新映射。
| 参数 | 类型 | 说明 |
|---|---|---|
| mapping | dict[int, int] 或 list[int] | 将旧量子比特索引映射到新索引的字典,或位置 i 给出量子比特 i 的新索引的列表。 |
parameters
parameters() -> list[float]返回此门的参数列表(非参数化门为空列表)。
set_parameters
set_parameters(params: list[float]) -> None设置此门的参数。
| 参数 | 类型 | 说明 |
|---|---|---|
| params | list[float] | 新的参数值。 |
name
name() -> str返回此门的名称。
示例
from pyqpanda3.core import H, X, RX, CNOT
# Basic gate properties
h = H(0)
print(h.gate_type()) # GateType.H
print(h.qubits()) # [0]
print(h.qubits_num()) # 1
print(h.name()) # "H"
# Parameterized gate
rx = RX(0, 1.57)
print(rx.parameters()) # [1.57]
rx.set_parameters([3.14])
print(rx.parameters()) # [3.14]
# Dagger (adjoint)
h_dag = h.dagger()
print(h_dag.is_dagger()) # True
# Power
rx_squared = rx.power(2)
# Control qubits
cnot = CNOT(0, 1)
print(cnot.control_qubits()) # [] (control is encoded in the gate type)
print(cnot.target_qubits()) # [0, 1]
# Add control to a single-qubit gate
controlled_h = H(0).control(2)
print(controlled_h.control_qubits()) # [2]
# Add multiple controls
controlled_x = X(0).control([2, 3])
print(controlled_x.control_qubits()) # [2, 3]
# Clear controls
controlled_x.clear_control()
print(controlled_x.control_qubits()) # []
# Matrix representation
mat = H(0).matrix()
print(mat.shape) # (2, 2)运行结果:
GateType.H
[0]
1
H
[1.57]
[3.14]
True
[]
[0, 1]
[2]
[2, 3]
[]
(2, 2)另见
QCircuit
表示量子线路:一个有序的量子门操作序列。线路可以使用 << 运算符组合,支持 dagger、控制、深度分析和矩阵计算等操作。
签名
QCircuit() # empty circuit
QCircuit(qubits_num: int) # circuit with a specified register size
QCircuit(circuit: QCircuit) # copy constructor参数
| 参数 | 类型 | 说明 |
|---|---|---|
| qubits_num | int | 要在线路寄存器中分配的量子比特数。 |
| circuit | QCircuit | 要拷贝的现有线路。 |
方法
size
size() -> int返回线路中的操作数量。
append
append(gate: QGate) -> None
append(circuit: QCircuit) -> None向此线路追加一个门或另一个线路。
| 参数 | 类型 | 说明 |
|---|---|---|
| gate | QGate | 要追加的量子门。 |
| circuit | QCircuit | 要追加的线路。 |
clear
clear() -> None移除此线路中的所有操作。
matrix
matrix() -> numpy.ndarray返回整个线路的幺正矩阵表示。
qubits
qubits() -> list[int]返回此线路中使用的量子比特索引列表。
target_qubits
target_qubits() -> list[int]返回此线路中使用的目标量子比特索引。
control_qubits
control_qubits() -> list[int]返回此线路中使用的控制量子比特索引。
dagger
dagger() -> QCircuit返回一个新的 QCircuit,其中每个门被替换为其厄米共轭。原始线路不会被修改。
control
control(qubit: int) -> QCircuit
control(qubits: list[int]) -> QCircuit向整个线路添加一个或多个控制量子比特。返回一个新的 QCircuit,原始线路不会被修改。
| 参数 | 类型 | 说明 |
|---|---|---|
| qubit | int | 要添加的单个控制量子比特索引。 |
| qubits | list[int] | 要添加的控制量子比特索引列表。 |
clear_control
clear_control() -> None移除此线路的所有控制量子比特。
set_name
set_name(name: str) -> None为线路设置自定义名称。
| 参数 | 类型 | 说明 |
|---|---|---|
| name | str | 要分配的名称。 |
name
name() -> str返回线路的名称。
operations
operations() -> list返回线路中的所有操作列表(可能包含 QGate 和嵌套的 QCircuit 对象)。
gate_operations
gate_operations(only_q2: bool = False) -> list[QGate]返回所有门级操作,展平嵌套线路。当 only_q2 为 True 时,仅返回双量子比特门。
| 参数 | 类型 | 说明 |
|---|---|---|
| only_q2 | bool | 如果为 True,仅返回双量子比特门(默认 False)。 |
num_2q_gate
num_2q_gate() -> int返回线路中双量子比特门的数量。
depth
depth(only_q2: bool = False) -> int返回线路深度(顺序门层数)。当 only_q2 为 True 时,仅考虑双量子比特门。
| 参数 | 类型 | 说明 |
|---|---|---|
| only_q2 | bool | 如果为 True,仅使用双量子比特门计算深度(默认 False)。 |
count_ops
count_ops(only_q2: bool = False) -> dict[str, int]返回将门名称映射到其出现次数的字典。
| 参数 | 类型 | 说明 |
|---|---|---|
| only_q2 | bool | 如果为 True,仅统计双量子比特门(默认 False)。 |
get_register_size
get_register_size() -> int返回为此线路分配的量子比特寄存器大小。
expand
expand() -> QCircuit返回一个新线路,其中所有嵌套子线路被展平为门序列。
originir
originir(precision: int = 8) -> str返回线路的 OriginIR 字符串表示。
| 参数 | 类型 | 说明 |
|---|---|---|
| precision | int | 参数值的浮点精度(默认 8)。 |
remap
remap(mapping: list[int]) -> QCircuit
remap(mapping: dict[int, int]) -> QCircuit返回一个新线路,其量子比特索引根据提供的映射重新映射。
| 参数 | 类型 | 说明 |
|---|---|---|
| mapping | list[int] 或 dict[int, int] | 将旧量子比特索引映射到新索引的列表或字典。 |
运算符
<<(左移)
追加一个门或线路。返回线路本身,支持链式调用。
c = QCircuit()
c << H(0)
c << CNOT(0, 1) # append gates to QCircuit特殊方法
__str__()-- 返回基于文本的线路图。__lshift__()-- 实现<<运算符用于追加门和线路。
示例
基本线路构建
from pyqpanda3.core import QCircuit, H, X, CNOT, T
# 创建空线路
circ = QCircuit()
# 使用 << 追加门
circ << H(0) << CNOT(0, 1)
print(circ.size()) # 2
print(circ.qubits()) # [0, 1]运行结果:
2
[0, 1]带寄存器大小的线路
from pyqpanda3.core import QCircuit, H, CNOT, X
circ = QCircuit(3)
circ << H(0) << CNOT(0, 1) << X(2)
print(circ.get_register_size()) # 3
print(circ.size()) # 3运行结果:
3
3组合线路
from pyqpanda3.core import QCircuit, H, CNOT, T, S
# Sub-circuit A
sub_a = QCircuit()
sub_a << H(0) << CNOT(0, 1)
# Sub-circuit B
sub_b = QCircuit()
sub_b << T(0) << S(1)
# Combine using <<
full = QCircuit()
full << sub_a << sub_b
print(full.size()) # 2 (two nested sub-circuits)运行结果:
2分析:深度和门计数
from pyqpanda3.core import QCircuit, H, CNOT, T
circ = QCircuit()
circ << H(0) << H(1) << CNOT(0, 1) << T(0) << T(1)
print(circ.depth()) # 3 (H layer, CNOT layer, T layer)
print(circ.depth(only_q2=True)) # 1 (only the CNOT layer)
print(circ.num_2q_gate()) # 1
print(circ.count_ops()) # {'CNOT': 1, 'H': 2, 'T': 2}运行结果:
3
1
1
{'CNOT': 1, 'H': 2, 'T': 2}Dagger 和控制
from pyqpanda3.core import QCircuit, H, CNOT, RX
circ = QCircuit()
circ << H(0) << CNOT(0, 1) << RX(1, 1.57)
# 整个线路的 dagger
circ_dag = circ.dagger()
# 向整个线路添加控制量子比特
ctrl_circ = QCircuit()
ctrl_circ << H(0) << CNOT(0, 1)
ctrl_circ = ctrl_circ.control([2])
print(ctrl_circ.control_qubits()) # [2]运行结果:
[2]矩阵表示
from pyqpanda3.core import QCircuit, H, X
circ = QCircuit()
circ << H(0) << X(1)
mat = circ.matrix()
print(mat.shape) # (4, 4)运行结果:
(4, 4)OriginIR 输出
from pyqpanda3.core import QCircuit, H, CNOT, RX
circ = QCircuit()
circ << H(0) << CNOT(0, 1) << RX(1, 1.57)
print(circ.originir())运行结果:
QINIT 2
CREG 1
H q[0]
CNOT q[0],q[1]
RX q[1],(1.57000000)重映射量子比特
from pyqpanda3.core import QCircuit, H, CNOT
circ = QCircuit()
circ << H(0) << CNOT(0, 1)
# 重映射:量子比特 0 -> 2,量子比特 1 -> 3
remapped = circ.remap([2, 3])
print(remapped.qubits()) # [2, 3]运行结果:
[2, 3]另见
QProg
表示量子程序:一个完整的量子计算,可以包括门、线路、测量和动态控制流(经典条件分支和循环)。QProg 是提交程序到模拟器或量子硬件时使用的顶级容器。
签名
QProg() # empty program
QProg(qubits_num: int) # program with a specified number of qubits
QProg(prog: QProg) # copy constructor
QProg(originir_src: str, is_file: bool = False) # from OriginIR source or file
QProg(node: QCircuit) # from a QCircuit参数
| 参数 | 类型 | 说明 |
|---|---|---|
| qubits_num | int | 要分配的量子比特数。 |
| prog | QProg | 要拷贝的现有程序。 |
| originir_src | str | OriginIR 源码字符串或文件路径。 |
| is_file | bool | 如果为 True,originir_src 被视为文件路径(默认 False)。 |
| node | QCircuit | 用于初始化程序的线路。 |
方法
qubits_num
qubits_num() -> int返回程序中使用的量子比特数。
cbits_num
cbits_num() -> int返回程序中使用的经典比特数。
qubits
qubits() -> list[int]返回程序中使用的量子比特索引。
cbits
cbits() -> list[int]返回程序中使用的经典比特索引。
operations
operations() -> list返回程序中的所有操作(门、测量、子程序等)。
gate_operations
gate_operations(only_q2: bool = False) -> list[QGate]返回所有门级操作,展平嵌套结构。当 only_q2 为 True 时,仅返回双量子比特门。
| 参数 | 类型 | 说明 |
|---|---|---|
| only_q2 | bool | 如果为 True,仅返回双量子比特门(默认 False)。 |
name
name() -> str返回程序的名称。
get_measure_nodes
get_measure_nodes() -> list返回程序中的所有测量节点。
flatten
flatten() -> QProg返回一个新程序,其中所有嵌套子程序被展平为扁平序列。
to_circuit
to_circuit() -> QCircuit将程序转换为 QCircuit(不包括测量操作)。
count_ops
count_ops(only_q2: bool = False) -> dict[str, int]返回将操作名称映射到其计数的字典。
| 参数 | 类型 | 说明 |
|---|---|---|
| only_q2 | bool | 如果为 True,仅统计双量子比特门(默认 False)。 |
depth
depth(only_q2: bool = False) -> int返回程序深度(顺序门层数)。
| 参数 | 类型 | 说明 |
|---|---|---|
| only_q2 | bool | 如果为 True,仅使用双量子比特门计算深度(默认 False)。 |
clear
clear() -> None移除程序中的所有操作。
append
append(gate: QGate) -> None
append(circuit: QCircuit) -> None
append(prog: QProg) -> None
append(measure: MeasureNode) -> None
append(karus: Karus) -> None
append(karus_list: list[Karus]) -> None向程序追加一个操作。接受门、线路、程序、测量节点和 Kraus 噪声算子。
| 参数 | 类型 | 说明 |
|---|---|---|
| gate | QGate | 要追加的量子门。 |
| circuit | QCircuit | 要追加的线路。 |
| prog | QProg | 要追加的程序。 |
| measure | MeasureNode | 要追加的测量节点。 |
| karus | Karus | 要追加的单个 Kraus 噪声算子(如 depolarizing_error(0.01).error_karus([0]))。 |
| karus_list | list[Karus] | 要追加的一组 Kraus 噪声算子。 |
remap
remap(remap_qubits: list[int], remap_cbits: list[int]) -> QProg
remap(remap_qubits: dict[int, int], remap_cbits: dict[int, int] = {}) -> QProg返回一个新程序,其量子比特和经典比特索引被重新映射。
| 参数 | 类型 | 说明 |
|---|---|---|
| remap_qubits | list[int] 或 dict[int, int] | 量子比特索引映射。 |
| remap_cbits | list[int] 或 dict[int, int] | 经典比特索引映射。默认为空映射,(0.5.0 起)保持经典比特索引不变而非丢弃。 |
originir
originir(precision: int = 8) -> str返回程序的 OriginIR 字符串表示。
| 参数 | 类型 | 说明 |
|---|---|---|
| precision | int | 参数值的浮点精度(默认 8)。 |
originbis
Deprecated: 请改用
Utils.convert_qprog_to_originbis。
originbis() -> bytes获取程序的 OriginBIS 二进制序列化。
from_originbis
Deprecated: 请改用
Utils.convert_originbis_to_qprog。
from_originbis(data: bytes) -> None从 OriginBIS 二进制数据反序列化程序。
| 参数 | 类型 | 说明 |
|---|---|---|
| data | bytes | OriginBIS 二进制数据。 |
to_instruction
to_instruction(backend: ChipBackend, offset: int = 1, is_scheduling: bool = False, compensate_pulse: bool = True) -> str将程序转换为与指定芯片后端兼容的 JSON 指令字符串。
| 参数 | 类型 | 说明 |
|---|---|---|
| backend | ChipBackend | 芯片后端配置对象(来自 pyqpanda3.transpilation)。 |
| offset | int | 量子比特索引偏移(默认 1)。 |
| is_scheduling | bool | 是否包含调度信息(默认 False)。 |
| compensate_pulse | bool | 是否应用来自后端的 CZ 虚拟 Z 相位补偿(默认 True)。 |
指令导出行为
- 空程序会导出为
[]。 offset会加到导出的每个量子比特标签上;默认offset=1时,逻辑量子比特0会写成物理标签1。- 当
is_scheduling=True时,时间戳会根据后端的single_gate_timing、double_gate_timing以及门模式规则生成。 compensate_pulse=True会把后端的 CZ 补偿信息折叠进指令流;如果你想保留原始相位,请显式传入compensate_pulse=False。
instruction_to_qprog
instruction_to_qprog(
instruction_json: str,
qubit_offset: int = -1,
insert_barrier: bool = True,
) -> QProg | list[QProg]把指令 JSON 流还原成 QProg(或多个程序的列表)。返回类型自适应输入:
- 单条流
[{...}]或包一层的[[{...}]]返回一个QProg。 - 多条流
[[{...}], [{...}]]返回list[QProg],每条流一个程序。
| 参数 | 类型 | 说明 |
|---|---|---|
| instruction_json | str | 由 QProg.to_instruction() 生成的 JSON 指令流,或兼容的指令数组。 |
| qubit_offset | int | JSON 使用的量子比特标签偏移,默认 -1。读取标准 QProg.to_instruction()(其 offset 默认为 1)输出时,请显式传入 qubit_offset=1。0.4.0 的旧关键字 offset 已在 0.4.1 移除(传入会抛 TypeError),仅 qubit_offset 生效。 |
| insert_barrier | bool | 是否在同一时间层之间插入 BARRIER 操作,默认 True。 |
指令往返示例
import json
from pyqpanda3.core import instruction_to_qprog
single_stream = [
{"RPhi": [1, 90.0, 0.0, 0]},
{"CZ": [1, 2, 40]},
{"Measure": [[2, 1], 120]},
]
prog = instruction_to_qprog(json.dumps(single_stream), qubit_offset=1)
programs = instruction_to_qprog(
json.dumps([single_stream, single_stream]),
qubit_offset=1,
insert_barrier=False,
)
print(type(prog).__name__)
print(type(programs).__name__, len(programs))预期输出:
QProg
list 2运算符
<<(左移)
追加一个门、线路、程序或测量节点。返回程序本身,支持链式调用。
prog << gate # append a QGate
prog << circuit # append a QCircuit
prog << other_prog # append a QProg
prog << measure_node # append a MeasureNode
prog << karus # append a single Karus noise operator
prog << karus_list # append a list of Karus noise operators特殊方法
__str__()-- 返回程序的基于文本的线路图。
示例
带测量的基本程序
from pyqpanda3.core import QProg, H, CNOT, measure
prog = QProg()
prog << H(0) << CNOT(0, 1) << measure(0, 0) << measure(1, 1)
print(prog.qubits_num()) # 2
print(prog.cbits_num()) # 2
print(prog.qubits()) # [0, 1]
print(prog.cbits()) # [0, 1]运行结果:
2
2
[0, 1]
[0, 1]从 OriginIR 创建程序
from pyqpanda3.core import QProg
# 从 OriginIR 字符串
ir_source = """
QINIT 2
CREG 2
H q[0]
CNOT q[0],q[1]
MEASURE q[0],c[0]
MEASURE q[1],c[1]
"""
prog = QProg(ir_source)
print(prog.originir())运行结果:
QINIT 2
CREG 2
H q[0]
CNOT q[0],q[1]
MEASURE q[0],c[0]
MEASURE q[1],c[1]从 QCircuit 创建程序
from pyqpanda3.core import QProg, QCircuit, H, CNOT, measure
circ = QCircuit()
circ << H(0) << CNOT(0, 1)
prog = QProg(circ)
prog << measure(0, 0) << measure(1, 1)分析:深度和门计数
from pyqpanda3.core import QProg, H, CNOT, RX, T
prog = QProg()
prog << H(0) << H(1) << CNOT(0, 1) << RX(1, 0.5) << T(0)
print(prog.depth()) # 3
print(prog.depth(only_q2=True)) # 1
print(prog.count_ops()) # {'RX': 1, 'CNOT': 1, 'H': 2, 'T': 1}运行结果:
3
1
{'RX': 1, 'CNOT': 1, 'H': 2, 'T': 1}展平并转换为线路
from pyqpanda3.core import QProg, QCircuit, H, CNOT
# 嵌套结构
sub = QCircuit()
sub << H(0) << CNOT(0, 1)
prog = QProg()
prog << sub << H(2)
# 展平为线性门序列
flat_prog = prog.flatten()
# 转换为线路(不包括测量)
circ = prog.to_circuit()
print(circ.qubits()) # [0, 1, 2]运行结果:
[0, 1, 2]重映射量子比特和经典比特
from pyqpanda3.core import QProg, H, CNOT, measure
prog = QProg()
prog << H(0) << CNOT(0, 1) << measure(0, 0) << measure(1, 1)
# 使用列表重映射:量子比特 0->3,1->4;经典比特 0->5,1->6
remapped = prog.remap([3, 4], [5, 6])
print(remapped.qubits()) # [3, 4]
print(remapped.cbits()) # [5, 6]
# 使用字典重映射
remapped2 = prog.remap({0: 3, 1: 4}, {0: 5, 1: 6})运行结果:
[3, 4]
[5, 6]导出为后端指令
from pyqpanda3.core import QProg, H, CNOT, measure
from pyqpanda3.transpilation import ChipBackend, Transpiler
prog = QProg()
prog << H(0) << CNOT(0, 1) << measure(0, 0) << measure(1, 1)
# 创建并配置芯片后端
cb = ChipBackend()
cb.basic_gates = ['RPHI', 'CZ']
# 编译为本机门
transpiler = Transpiler()
transpiled = transpiler.transpile(prog, cb)
# 导出为 JSON 指令字符串
json_instr = transpiled.to_instruction(cb, offset=1, is_scheduling=False)
print(json_instr)获取测量节点
from pyqpanda3.core import QProg, H, measure
prog = QProg()
prog << H(0) << measure(0, 0)
measure_nodes = prog.get_measure_nodes()
print(len(measure_nodes)) # 1运行结果:
1另见
- QCircuit -- 量子线路(仅包含门的容器)。
- QGate -- 量子门类。
- MeasureNode -- 测量节点类。
- 动态线路 --
QProg的条件和循环构造。 - OriginIR -- OriginIR 中间表示。
- 模拟器 -- 在模拟器上运行程序。
draw_qprog
将量子程序或线路绘制为文本图或 LaTeX 源码。此函数同时接受 QProg 和 QCircuit 对象。
签名
draw_qprog(qprog: QProg, p: PIC_TYPE = PIC_TYPE.TEXT, expend_map: dict[str, int] = {'all': 1}, param_show: bool = False, with_logo: bool = False, line_length: int = 100, output_file: str = '', encode: str = 'utf-8') -> str
draw_qprog(circuit: QCircuit, p: PIC_TYPE = PIC_TYPE.TEXT, ...) -> str参数
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| qprog | QProg | — | 要绘制的量子程序 |
| circuit | QCircuit | — | 要绘制的量子线路 |
| p | PIC_TYPE | PIC_TYPE.TEXT | 输出格式:PIC_TYPE.TEXT 为 ASCII 文本,PIC_TYPE.LATEX 为 LaTeX |
| expend_map | dict[str, int] | {'all': 1} | 要展开的量子比特索引映射(使用 {'all': 1} 展开全部) |
| param_show | bool | False | 是否在输出中显示门参数值 |
| with_logo | bool | False | 是否在输出中包含 pyqpanda3 标志 |
| line_length | int | 100 | 文本输出每行最大字符数 |
| output_file | str | '' | 保存输出的文件路径(空字符串表示输出到控制台) |
| encode | str | 'utf-8' | 输出文件的字符编码 |
返回值
str — 渲染后的线路图字符串。
示例
from pyqpanda3.core import draw_qprog, QProg, H, CNOT, PIC_TYPE
prog = QProg()
prog << H(0) << CNOT(0, 1)
# 以文本形式绘制(默认)
text_output = draw_qprog(prog)
print(text_output)
# 以 LaTeX 形式绘制
latex_output = draw_qprog(prog, PIC_TYPE.LATEX)
# 绘制时显示参数值
text_with_params = draw_qprog(prog, param_show=True)
# 保存到文件
draw_qprog(prog, output_file="circuit.txt")运行结果:
┌─┐
q_0: |0>─┤H├ ───*──
└─┘ ┌──┴─┐
q_1: |0>──── ┤CNOT├
└────┘
c : / ═══════════