# Transpilation **Transpilation** converts your abstract [[qiskit-quantum-circuits|quantum circuit]] into a concrete circuit that runs on a specific [[qiskit-backends|backend]]. Each backend has constraints: a gate set (only certain gates are native), qubit connectivity (which pairs can interact directly), and calibration data. Transpilation rewrites your circuit to respect these constraints while preserving the computation. Think of transpilation like compiling source code to machine code: the abstract algorithm is the same, but the target-specific details differ. ## Key Transpilation Tasks 1. **Gate decomposition**: replace unsupported gates with native gates 2. **Layout**: assign your logical qubits to physical qubits respecting connectivity 3. **Routing**: insert SWAP gates to move qubits when two-qubit gates need non-adjacent qubits 4. **Optimization**: reduce gate count and depth to minimize error accumulation ```python from qiskit import transpile, QuantumCircuit from qiskit_aer import AerSimulator qc = QuantumCircuit(3, 3) qc.h([0, 1, 2]) qc.cx(0, 1) qc.cx(1, 2) qc.measure([0, 1, 2], [0, 1, 2]) backend = AerSimulator() transpiled = transpile(qc, backend, optimization_level=2) print(transpiled.draw()) ``` ## Optimization Levels - **Level 0**: minimal optimization; only converts to native gates - **Level 1**: light optimization; remove redundant gates - **Level 2**: medium optimization; more aggressive rewriting (default) - **Level 3**: aggressive optimization; may be slow but produces smaller circuits Higher levels produce shallower circuits at the cost of transpilation time. For variational algorithms, level 2 is usually a good balance. ## Transpilation for Hardware When targeting real hardware, transpilation accounts for measured error rates and gate times. Transpilers try to minimize two-qubit gates (the primary error source) and circuit depth (time qubits spend in superposition, where they decohere).