Skip to content

decompose ​

Decomposes a quantum program, circuit, or unitary matrix into a target set of basic gates. This is useful for rewriting high-level or composite gates into hardware-native operations.

Overloads ​

Three overloads are provided depending on the input type.

decompose -- QProg ​

Decomposes a quantum program into a target gate set.

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

Parameters ​

ParameterTypeDescription
progQProgThe quantum program to decompose.
basic_gateslist[str]Target gate set. The output program will contain only gates from this list.

Returns ​

A new QProg whose gates belong to the specified basic gate set.

decompose -- QCircuit ​

Decomposes a quantum circuit into a target gate set.

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

Parameters ​

ParameterTypeDescription
circuitQCircuitThe quantum circuit to decompose.
basic_gateslist[str]Target gate set. Defaults to an empty list (use the default decomposition basis).

Returns ​

A new QCircuit whose gates belong to the specified basic gate set.

decompose -- Matrix ​

Decomposes a unitary matrix into a quantum circuit composed of the specified basic gates.

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

Parameters ​

ParameterTypeDescription
matrixnumpy.ndarray[numpy.complex128[m, n]]A complex-valued unitary matrix to decompose into a quantum circuit.
qubitslist[int]Qubit indices for the decomposed circuit. Defaults to an empty list (auto-assign).
basic_gateslist[str]Target gate set for the decomposition. Defaults to an empty list (use the default decomposition basis).

Returns ​

A QCircuit implementing the given unitary matrix using the specified basic gates.

Examples ​

Decompose a QProg into CX and single-qubit rotations:

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", "CNOT"])
print(decomposed)

Output:

          ┌──────────────┐ ┌──────────────┐        
q_0:  |0>─┤RY(1.57079633)├ ┤RX(3.14159265)├ ───*── 
          └──────────────┘ └──────────────┘ ┌──┴─┐ 
q_1:  |0>───────────────── ──────────────── ┤CNOT├ 
                                            └────┘ 
 c :   / ═════════════════════════════════════════

Decompose a 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", "CNOT"])
print(decomposed)

Output:

          ┌──────────────────────────────────────┐        
q_0:  |0>─┤U3(1.57079633, 0.00000000, 3.14159265)├ ───*── 
          └──────────────────────────────────────┘ ┌──┴─┐ 
q_1:  |0>───────────────────────────────────────── ┤CNOT├ 
                                                   └────┘ 
 c :   / ════════════════════════════════════════════════

Decompose a 2-qubit unitary matrix:

python
import numpy as np
from pyqpanda3.transpilation import decompose

# Define a 2-qubit unitary matrix
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", "CNOT"])
print(type(circuit).__name__)

Output:

QCircuit

See Also ​

  • Transpiler -- Full transpilation pipeline including decomposition, routing, and optimization

Released under the MIT License.