Skip to content

分解(decompose)

将量子程序、线路或酉矩阵分解为目标基本门集。这对于将高级或复合门重写为硬件本机操作非常有用。

重载

根据输入类型提供三个重载版本。

decompose -- QProg

将量子程序分解为目标门集。

python
decompose(
    prog: QProg,
    basic_gates: list[str],
) -> QProg

参数

参数类型描述
progQProg要分解的量子程序。
basic_gateslist[str]目标门集。输出程序将仅包含此列表中的门。

返回值

一个新的 QProg,其门属于指定的基本门集。

decompose -- QCircuit

将量子线路分解为目标门集。

python
decompose(
    circuit: QCircuit,
    basic_gates: list[str] = [],
) -> QCircuit

参数

参数类型描述
circuitQCircuit要分解的量子线路。
basic_gateslist[str]目标门集。默认为空列表(使用默认分解基集)。

返回值

一个新的 QCircuit,其门属于指定的基本门集。

decompose -- 矩阵

将酉矩阵分解为由指定基本门组成的量子线路。

python
decompose(
    matrix: numpy.ndarray,
    qubits: list[int] = [],
    basic_gates: list[str] = [],
) -> QCircuit

参数

参数类型描述
matrixnumpy.ndarray要分解为量子线路的复数值酉矩阵。
qubitslist[int]分解线路的量子比特索引。默认为空列表(自动分配)。
basic_gateslist[str]分解的目标门集。默认为空列表(使用默认分解基集)。

返回值

使用指定基本门实现给定酉矩阵的 QCircuit

示例

将 QProg 分解为 CX 和单比特旋转门:

python
from pyqpanda3.transpilation import decompose
from pyqpanda3.core import QProg, H, CNOT

prog = QProg() << H(0) << CNOT(0, 1)
decomposed = decompose(prog, ["rx", "ry", "rz", "cx"])

分解 QCircuit:

python
from pyqpanda3.transpilation import decompose
from pyqpanda3.core import QCircuit, H, CNOT

circ = QCircuit() << H(0) << CNOT(0, 1)
decomposed = decompose(circ, basic_gates=["u3", "cx"])

分解 2 量子比特酉矩阵:

python
import numpy as np
from pyqpanda3.transpilation import decompose

# 定义一个 2 量子比特酉矩阵
matrix = np.array([[1, 0, 0, 0],
                   [0, 1, 0, 0],
                   [0, 0, 0, 1],
                   [0, 0, 1, 0]], dtype=complex)

circuit = decompose(matrix, qubits=[0, 1], basic_gates=["rx", "ry", "rz", "cx"])

另见

  • Transpiler -- 包含分解、路由和优化的完整转译流水线

Released under the MIT License.