Transpiler
The Transpiler class maps a logical quantum program onto a target chip topology by performing qubit layout, routing, gate translation, optimization, and scheduling.
Construction
python
Transpiler()Creates a new Transpiler instance with default transpilation passes.
Methods
transpile (topology-edges overload)
Maps a quantum program to a chip described by its coupling-map edges.
python
Transpiler.transpile(
prog: QProg,
chip_topology_edges: list[list[int]] = [],
init_mapping: dict[int, int] = {},
optimization_level: int = 2,
basic_gates: list[str] = [],
) -> QProgParameters
| Parameter | Type | Description |
|---|---|---|
| prog | QProg | The quantum program to transpile. |
| chip_topology_edges | list[list[int]] | Coupling map of the target chip. Each element is a two-element list [u, v] indicating a physical connection between qubit u and qubit v. Defaults to an empty list. |
| init_mapping | dict[int, int] | Initial mapping from virtual qubit indices (keys) to physical qubit indices (values). Defaults to an empty dict (automatic mapping). |
| optimization_level | int | Optimization level applied during transpilation. Higher values enable more aggressive optimizations. Defaults to 2. |
| basic_gates | list[str] | Target gate set. The transpiled circuit will only contain gates from this list. Defaults to an empty list (use default gate set). |
Returns
The transpiled QProg that conforms to the specified chip topology and gate set.
transpile (backend overload)
Maps a quantum program to a chip described by a ChipBackend object.
python
Transpiler.transpile(
prog: QProg,
backend: ChipBackend,
init_mapping: dict[int, int] = {},
optimization_level: int = 2,
) -> QProgParameters
| Parameter | Type | Description |
|---|---|---|
| prog | QProg | The quantum program to transpile. |
| backend | ChipBackend | A ChipBackend object that encapsulates the chip topology, supported gates, timing information, and other hardware properties. |
| init_mapping | dict[int, int] | Initial mapping from virtual qubit indices (keys) to physical qubit indices (values). Defaults to an empty dict (automatic mapping). |
| optimization_level | int | Optimization level applied during transpilation. Defaults to 2. |
Returns
The transpiled QProg that conforms to the specified backend.
Examples
Transpile a quantum program onto a linear topology:
python
from pyqpanda3.transpilation import Transpiler, generate_topology
# Generate a 5-qubit linear topology
topology = generate_topology(5, "linear")
# Build the quantum program (prog) using core gates ...
# from pyqpanda3.core import QProg, H, CNOT
# prog = QProg() << H(0) << CNOT(0, 1)
transpiler = Transpiler()
result = transpiler.transpile(
prog,
chip_topology_edges=topology,
optimization_level=2,
basic_gates=["rx", "ry", "rz", "cx"],
)Transpile using a ChipBackend:
python
from pyqpanda3.transpilation import Transpiler, ChipBackend
# backend obtained from QCloudBackend.chip_backend() or constructed manually
transpiler = Transpiler()
result = transpiler.transpile(
prog,
backend=backend,
init_mapping={0: 2, 1: 3},
optimization_level=2,
)See Also
- generate_topology -- Generate chip coupling maps
- decompose -- Gate decomposition without routing