Quantum Compilation Pipeline
A quantum compiler transforms high-level quantum circuits into hardware-executable instructions:
- Frontend parsing: accept quantum program (OpenQASM, QIR)
- Optimization: reduce circuit depth and gate count
- Transpilation: map to native gate set
- Layout/routing: map logical qubits to physical qubits
- Scheduling: assign timing and parallelism
- Backend code generation: produce pulse-level instructions
Gate Synthesis
Gate synthesis decomposes arbitrary unitaries into native gate sets:
For a single qubit, any unitary can be decomposed as:
For two qubits, the KAK decomposition (Cartan decomposition) writes any as:
with at most 3 CNOT gates.
Circuit Optimization
Circuit optimization reduces gate count and depth:
- Gate cancellation: remove adjacent inverse gates
- Gate merging: combine multiple gates into one
- Template matching: replace subcircuits with equivalent shorter ones
- Algebraic simplification: use gate identities
For example, and .
Qubit Routing
Qubit routing maps logical qubits to physical qubits with limited connectivity:
- Initial placement: assign logical to physical qubits
- SWAP insertion: add SWAP gates to enable non-adjacent interactions
The routing problem is NP-hard. Heuristics include:
- SABRE: look-ahead SWAP routing
- Graph-based: use hardware connectivity graph
- Noise-aware: consider error rates when routing
Transpilation Levels
Qiskit provides transpilation levels 0-3:
- Level 0: no optimization, default layout
- Level 1: light optimization, trivial layout
- Level 2: medium optimization, layout by noise
- Level 3: heavy optimization, full routing and scheduling
Higher levels produce shorter circuits but take longer to compile.
Resource Estimation
Resource estimation counts the resources needed for a quantum algorithm:
- Qubit count: number of physical qubits
- Gate count: number of gates (by type)
- Circuit depth: depth of the circuit
- T-gate count: number of T gates (costly in fault-tolerant QC)
Resource estimation guides hardware requirements and algorithm design.
Python: Circuit Optimization
import numpy as np
def naive_hadamard_circuit(n):
# n Hadamards -> can be simplified.
return ['H'] * n
def optimized_hadamard_circuit(n):
# n Hadamards = I if n is even, H if n is odd.
return ['H'] if n % 2 else []
def count_gates(circuit):
return len(circuit)
for n in [10, 100, 1000]:
naive = naive_hadamard_circuit(n)
optimized = optimized_hadamard_circuit(n)
print(f"n={n}: naive={count_gates(naive)}, optimized={count_gates(optimized)}")
Transpilation Algorithms
The SABRE algorithm for qubit routing:
- Initial placement: map logical to physical qubits using graph matching
- Forward pass: for each gate, find shortest path and insert SWAPs
- Backward pass: optimize SWAP insertion
- Iterate: repeat until no improvement
The algorithm runs in time where is the gate count and is the qubit count.
Native Gate Sets
Different hardware has different native gate sets:
| Hardware | Single-Qubit | Two-Qubit |
|---|---|---|
| IBM | Rx, Ry, Rz | CX (CNOT) |
| Rx, Ry, Rz | CZ | |
| IonQ | Rx, Ry, Rz | XX |
| Rigetti | Rx, Ry, Rz | CZ |
The transpiler must convert arbitrary gates to these native sets.