随机线路生成
随机线路生成和量子基准测试工具的 API 参考。
random_qcircuit
生成具有指定量子比特、深度和门类型的随机量子线路。
签名
python
random_qcircuit(qubits: list[int], depth: int, gate_type: list[str]) -> QCircuit
random_qcircuit(qubits: list[int], depth: int, gate_type: list[str], topology: list[list[int]]) -> QCircuit参数
| 参数 | 类型 | 说明 |
|---|---|---|
| qubits | list[int] | 线路中包含的量子比特索引列表 |
| depth | int | 线路的深度(门层数) |
| gate_type | list[str] | 用于生成的门类型字符串列表,例如 ["H", "CNOT"] |
| topology | list[list[int]] | 可选的芯片耦合边列表,格式同 ChipInfo.get_chip_topology(),例如 [[0, 1], [1, 2]]。每个双比特门只会落在这些边上;边引用 qubits 之外的比特时抛 ValueError。 |
返回
QCircuit — 随机生成的量子线路。
示例
python
from pyqpanda3.core import random_qcircuit
# Generate a random circuit on qubits [0, 1, 2] with depth 5 using H and CNOT gates
circ = random_qcircuit([0, 1, 2], 5, ["H", "CNOT"])
print(f"Circuit depth: {circ.depth()}")
print(f"Gate count: {circ.count_ops()}")示例输出(由于 random_qcircuit 不接受随机种子,每次运行的门计数可能不同):
Circuit depth: 5
Gate count: {'H': 2, 'CNOT': 4}random_qprog
生成具有指定量子比特、深度、门类型和可选拓扑约束的随机量子程序。
签名
python
random_qprog(qubits: list[int], depth: int, gate_type: list[str]) -> QProg
random_qprog(qubits: list[int], depth: int, gate_type: list[str], topology: list[list[int]]) -> QProg参数
| 参数 | 类型 | 说明 |
|---|---|---|
| qubits | list[int] | 程序中包含的量子比特索引列表 |
| depth | int | 程序的深度(门层数) |
| gate_type | list[str] | 用于生成的门类型字符串列表,例如 ["H", "CNOT"] |
| topology | list[list[int]] | 可选的芯片耦合边列表,格式同 ChipInfo.get_chip_topology(),例如 [[0, 1], [1, 2]]。每个双比特门只会落在这些边上;边引用 qubits 之外的比特时抛 ValueError。 |
返回
QProg — 随机生成的量子程序。
示例
python
from pyqpanda3.core import random_qcircuit, random_qprog
topology = [[0, 1], [1, 2]]
circ = random_qcircuit([0, 1, 2], 5, ["H", "CNOT"], topology)
prog = random_qprog([0, 1, 2], 5, ["H", "CNOT"], topology)
print(circ.gate_operations())
print(prog.gate_operations())NOTE
topology 参数自 0.5.1 起提供;本示例未在本机执行(当前环境没有 0.5.1 wheel)。
QV — 量子体积基准测试
生成给定配置的量子体积(QV, Quantum Volume)基准测试线路。
签名
python
QV(num_qubit: int, depth: int, seed: int) -> QCircuit参数
| 参数 | 类型 | 说明 |
|---|---|---|
| num_qubit | int | QV 测试的量子比特数 |
| depth | int | 线路的深度(层数) |
| seed | int | 用于可重复性的随机种子 |
返回
QCircuit — 量子体积基准测试线路。
示例
python
from pyqpanda3.core import QV
# Compute quantum volume for a 4-qubit, depth-4 circuit
qv = QV(num_qubit=4, depth=4, seed=42)
print(f"Quantum Volume: {qv}")运行结果:
Quantum Volume:
┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
q_0: |0>─┤1 ├ ┤0 ├ ┤0 ├ ┤0 ├ ──────────── ──────────── ────────────
│ │ │ ORACLE │ │ │ │ │ ┌──────────┐ ┌──────────┐
q_1: |0>─┤ ├ ┤1 ├ ┤ ORACLE ├ ┤ ├ ┤0 ├ ──────────── ┤1 ├
│ ORACLE │ ├──────────┤ │ │ │ ORACLE │ │ ORACLE │ ┌──────────┐ │ │
q_2: |0>─┤ ├ ┤1 ├ ┤1 ├ ┤ ├ ┤1 ├ ┤1 ├ ┤ ORACLE ├
│ │ │ ORACLE │ └──────────┘ │ │ └──────────┘ │ ORACLE │ │ │
q_3: |0>─┤0 ├ ┤0 ├ ──────────── ┤1 ├ ──────────── ┤0 ├ ┤0 ├
└──────────┘ └──────────┘ └──────────┘ └──────────┘ └──────────┘
c : / ═══════════════════════════════════════════════════════════════════════数学背景
量子体积是一个单一数值指标,用于衡量量子处理器的整体能力。它考虑了量子比特数、门保真度、连接性和软件性能。QV 协议运行宽度为
其中
direct_twirl
对量子程序执行直接旋转(随机编译),将其分解为随机泡利帧序列。
签名
python
direct_twirl(
input_circ: QProg,
twirled_gate: str,
seed: int
) -> QProg参数
| 参数 | 类型 | 说明 |
|---|---|---|
| input_circ | QProg | 要旋转的输入量子程序 |
| twirled_gate | str | 用于旋转的门类型。支持:"CNOT"、"CZ" |
| seed | int | 用于可重复性的随机种子。自 0.5.0 起所有种子值(含小于 8000 的值)都会产生不同的 twirl,相同种子复现相同程序 |
返回
QProg — 旋转后的量子程序。
示例
python
from pyqpanda3.core import H, CNOT, direct_twirl, QProg
# Build a simple program
prog = QProg()
prog << H(0) << CNOT(0, 1)
# Apply direct twirling
twirled = direct_twirl(prog, "CNOT", 42)
print(f"Original gates: {prog.count_ops()}")
print(f"Twirled gates: {twirled.count_ops()}")运行结果:
Original gates: {'CNOT': 1, 'H': 1}
Twirled gates: {'CNOT': 1, 'H': 1, 'Z': 1, 'I': 1, 'Y': 2}数学背景
旋转通过用随机泡利算符共轭将一般量子信道转换为泡利信道。对于信道
这简化了错误表征并支持错误缓解技术。