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.
decompose(
prog: QProg,
basic_gates: list[str],
) -> QProgParameters
| Parameter | Type | Description |
|---|---|---|
| prog | QProg | The quantum program to decompose. |
| basic_gates | list[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.
decompose(
circuit: QCircuit,
basic_gates: list[str] = [],
) -> QCircuitParameters
| Parameter | Type | Description |
|---|---|---|
| circuit | QCircuit | The quantum circuit to decompose. |
| basic_gates | list[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.
decompose(
matrix: numpy.ndarray,
qubits: list[int] = [],
basic_gates: list[str] = [],
) -> QCircuitParameters
| Parameter | Type | Description |
|---|---|---|
| matrix | numpy.ndarray | A complex-valued unitary matrix to decompose into a quantum circuit. |
| qubits | list[int] | Qubit indices for the decomposed circuit. Defaults to an empty list (auto-assign). |
| basic_gates | list[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:
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"])Decompose a QCircuit:
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"])Decompose a 2-qubit unitary matrix:
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", "cx"])See Also
- Transpiler -- Full transpilation pipeline including decomposition, routing, and optimization