Overview
Algorithms are written as circuits of convenient gates on qubits that can all interact. A device offers a small native gate set (for example arbitrary single-qubit rotations plus one fixed entangling gate), a connectivity graph saying which pairs can interact, and error rates that differ from gate to gate. A quantum compiler bridges the two. Its main passes are:
- Decomposition (synthesis). Rewrite multi-qubit gates into native ones. For two qubits the Cartan decomposition shows that three CNOTs and single-qubit gates suffice (Vidal and Dawson; Vatan and Williams); for qubits generic unitaries need exponentially many CNOTs (Shende, Markov and Bullock).
- Mapping and routing. Place logical qubits on physical ones and insert SWAPs so that every two-qubit gate acts on neighbours (Siraichi et al.; Li, Ding and Xie). Optimal versions of this step are NP-hard in natural formulations (Botea, Kishimoto and Marinescu).
- Optimization. Cancel and merge gates by local rewrite rules and templates (Maslov et al.; Nam et al.).
- Discrete synthesis. For fault-tolerant targets, approximate continuous rotations by words in a finite gate set such as Clifford+T; this is covered in Clifford+T synthesis.
Intuition
Compilation resembles classical compilation with an unusual cost model. Every extra gate adds error, so a compiler that saves a few CNOTs can matter more than one that saves time. Connectivity plays the role of register allocation: an instruction can only combine operands held in adjacent "registers", and moving data (a SWAP) costs three entangling gates.
Two facts frame what is possible. First, two-qubit gates are well understood: after removing single-qubit gates on each side, a two-qubit gate is described by just three angles, which is why any of them costs at most three CNOTs. Second, large unitaries are hopeless to synthesise exactly: a parameter count shows that almost all -qubit unitaries need about CNOTs, so practical compilers never synthesise arbitrary unitaries beyond a few qubits; they preserve the structure of the algorithm and optimise locally.
Prerequisites
The circuit model, the Z–Y–Z decomposition, the universality of CNOT and single-qubit gates, and the notion of a connectivity graph. The lesson gates and unitaries covers the gate identities used here.
Mathematical Definitions
Let be the device's connectivity graph and let be a circuit on logical qubits , , whose two-qubit gates act on pairs and are partially ordered by the circuit's dependency DAG. A layout is an injective map . A SWAP on an edge changes the layout by exchanging the logical qubits held at and .
A mapping and routing solution is an initial layout together with a sequence of gates, consisting of the gates of in an order compatible with the DAG interleaved with SWAPs, such that each two-qubit gate on is scheduled at a time when the current layout satisfies . The resulting circuit implements followed by the permutation of qubits given by the final layout, which is tracked classically rather than undone. The objective is to minimise the number of SWAPs (each typically three CNOTs, by the SWAP identity), the depth, or an estimated success probability built from per-edge error rates.
The problem was posed for cloud superconducting processors as qubit allocation by Siraichi et al.. Widely used heuristics include the bidirectional search SABRE of Li, Ding and Xie, the A*-based layer-by-layer method of Zulehner, Paler and Wille, and the architecture-agnostic routing of Cowtan et al..
- Two-qubit gates only between adjacent physical qubits; single-qubit gates anywhere; data moved only by SWAP gates (other movement primitives change the cost model).
A rewrite rule is a pair of circuits on the same qubits with (up to phase) and . A peephole optimizer repeatedly finds an occurrence of some in the circuit, where an occurrence may be spread out in time provided the gates between its parts commute past them, and replaces it with . Typical rules: cancel (for example two adjacent CNOTs or Hadamards on the same qubits); merge , moving through CNOT controls; and resynthesise a maximal two-qubit block with the three-CNOT construction when that is cheaper.
A template is a circuit implementing the identity. Any occurrence of (with more than half of 's gates) can be replaced by , which is shorter (Maslov et al.). Finding all maximal matches of a pattern in a circuit modulo commutation is itself an algorithmic problem; Iten et al. give an exact algorithm that is polynomial in the circuit size for a fixed pattern size. Rule-based optimizers with continuous rotation angles, phase-polynomial resynthesis and floating rotations are described by Nam et al..
Local rewriting always terminates when each step strictly lowers an integer cost, but it finds only local optima: the final circuit depends on the order in which rules are applied.
- Circuits are sequences of gates from a fixed library; equivalence means equality of the implemented unitaries, possibly up to a global phase.
Theorems
Two-qubit gates
For every there are , real numbers and a phase such that
The triple is not unique. Permuting its entries (conjugation by exchanges and , and by exchanges and ), changing the sign of two entries (conjugation by flips and ), or shifting one entry by () is absorbed into the local factors and the phase. Restricting the triple to a fundamental region for such moves gives the Weyl chamber of two-qubit gates. Any property invariant under single-qubit gates, such as entangling power or the minimal CNOT count, depends only on the triple. For example CZ, and hence CNOT, has triple and SWAP has modulo these moves (see the proof).
The decomposition is the two-qubit case of the Cartan decomposition of used in control theory by Khaneja and Glaser; it was obtained independently, with its use for entangling power, by Kraus and Cirac, and the magic-basis form used in the proof is that of Vatan and Williams.
- acting on two qubits; are the Pauli matrices and abbreviates .
Proof · Proof of the two-qubit Cartan decomposition
Magic basis. Let be the unitary whose columns are , with the usual Bell states. We use one fact, proved by Vatan and Williams (see also Kraus and Cirac): maps onto . The second fact is a direct check: each Bell state is a joint eigenvector of , , , with eigenvalue vectors (over )
and the phases in do not change eigenvectors. Together with these are four mutually orthogonal vectors, so every diagonal matrix in the magic basis is a real linear combination of when its entries are real, and every diagonal unitary is for real .
Step 1. Remove a phase: with . Put and . Then is unitary and symmetric.
Step 2. Write with real. Symmetry of makes symmetric, and . Unitarity gives , so . Two commuting real symmetric matrices are diagonalised by one real orthogonal matrix ; replacing a column of by its negative if necessary, . So with diagonal, and is unitary because is.
Step 3. Choose a diagonal with . Then ; if , negate one diagonal entry of , so . Define . It is unitary, and
Hence , so : is real orthogonal, with , i.e. .
Step 4. , so . By the first fact the outer factors are and with . The middle factor is diagonal and unitary in the magic basis, so it equals . Collecting into gives the theorem.
Examples. In the magic basis SWAP is , which is (check: the exponent gives on the three states with and on ). For CZ, , as one checks on the four computational basis states; CNOT differs from CZ by Hadamards on the target.
For every there is a circuit with single-qubit gates (CNOT directions fixed by the construction). By the Cartan decomposition it suffices to implement the core for arbitrary , and one three-CNOT circuit with single-qubit rotations whose angles are explicit functions of does this.
The result was obtained independently by Vidal and Dawson, Vatan and Williams and Shende, Markov and Bullock, the last also for other gate libraries. Vatan and Williams show that two CNOTs suffice when is a real orthogonal matrix of determinant (); a real of determinant , such as SWAP, may need three. That three are necessary for almost every follows from the parameter-counting bound with .
- Gate library of CNOT and arbitrary single-qubit unitaries, no ancillas.
On a basis state the three gates give , then , then . Both sides are linear and agree on a basis, so they are equal; identities of this kind are collected in chapter 4 of Nielsen and Chuang. The middle CNOT may be reversed with Hadamards, , when the hardware supports only one direction. By the parameter-counting bound three is also the generic minimum for two-qubit gates.
A related bridge identity implements a CNOT between the ends of a three-qubit line without moving data: in time order maps to , which is .
- has control and target ; exact equality, no phase.
Many qubits
Let . The set of that can be implemented, up to a global phase, by a circuit with fewer than CNOT gates and any number of single-qubit gates has Haar measure zero.
Values: , , . For two qubits the bound is tight: three CNOTs suffice for every two-qubit gate (see the topic page). For general the quantum Shannon decomposition of Shende, Bullock and Markov uses CNOTs, a constant factor above . The bound and the counting argument are due to Shende, Markov and Bullock.
- Circuits consist of CNOT gates between any ordered pairs of the qubits and arbitrary single-qubit unitaries; no ancillas and no measurements.
- "Almost every" refers to the Haar measure on ; equality is up to a global phase.
Proof · Proof of the CNOT lower bound for generic unitaries
Normal form. Fix a circuit with CNOTs. Merge adjacent single-qubit gates on the same qubit. A single-qubit gate on qubit commutes with every CNOT not acting on , so it can be moved backwards in time until it meets the most recent earlier CNOT acting on , or the start of the circuit. The circuit then has the form: one layer of single-qubit gates, followed, for , by the CNOT with control and target and then single-qubit gates on and on .
Removing redundant parameters. Process . By the Z–Y–Z decomposition (theorem), , and since on the control commutes with CNOT, the factor , applied first, can be moved before and merged into the previous gate on qubit (a or with , or a factor of ), which remains an arbitrary single-qubit gate. Likewise (the X–Y–X decomposition, obtained from Z–Y–Z by conjugating with a Hadamard, which maps to and to ), and on the target commutes with CNOT, so moves before . Global phases are collected in front. After the pass, each and depends on two real parameters, on (each ), and there is one global phase.
Counting. For a fixed sequence of CNOT positions the implemented unitaries, modulo global phase, form the image of a smooth map from a manifold of dimension into one of dimension . If , every point of the domain is critical, so by Sard's theorem the image has measure zero. There are finitely many CNOT position sequences for each , and a finite union of null sets is null. The inequality holds exactly when , i.e. for all . Since the projection maps Haar-null sets to null sets and back (the measures are locally products with the phase circle), the set of such has Haar measure zero.
Values. : , so . : , so . : , so .
The matching upper bound comes from the quantum Shannon decomposition, which recursively splits an -qubit unitary into multiplexed -qubit pieces and uses CNOTs (Shende, Bullock and Markov).
Discrete gate sets
Let satisfy the assumptions. There is a constant such that for every and every there is a sequence with
where the implied constant depends on but not on or . The sequence is produced by a classical algorithm whose running time is also polylogarithmic in .
The recursive construction analysed by Dawson and Nielsen gives and running time with ; see the exponent equation. Refined versions reach for every , and a 2023 preprint of Kuperberg, which summarises that earlier state, lowers the exponent to (as of 2026-11 this improvement is known from the preprint). A volume-counting argument shows that no finite gate set can do better than .
The theorem is due to R. Solovay (an unpublished 1995 manuscript) and, independently, to Kitaev; textbook proofs are in Kitaev, Shen and Vyalyi and in Appendix 3 of Nielsen and Chuang. Its practical meaning is that the choice of a universal gate set changes circuit sizes by at most a polylogarithmic factor: a circuit of gates from one set, each approximated to , becomes a circuit of gates from another with total error at most .
- The gate set is finite and closed under inverses ().
- The group generated by is dense in ; is fixed while .
- Accuracy is measured in operator norm.
For every angle and , the probabilistic algorithm of Ross and Selinger
- with access to a factoring oracle, returns an ancilla-free Clifford+T circuit with of the smallest T-count among all such circuits;
- without the oracle, under a mild number-theoretic hypothesis, runs in expected time polynomial in and returns a circuit of T-count , where is the T-count of the second-best solution;
- in the typical case produces T-count
The method reduces approximation to two problems over the ring : a two-dimensional grid problem, enumerating ring elements close to in a thin region of the unit disc (and whose Galois conjugate lies in the unit disc), and a norm equation to complete to a unitary with entries in the ring; the result is then turned into a circuit by exact synthesis. An earlier algorithm of Selinger achieves T-count with for -rotations and proves a worst-case lower bound of the same form, , for certain -rotations.
- Target , accuracy in operator norm, single-qubit Clifford+T circuits with no ancillas or measurements.
- Optimality relies on a factoring oracle; the classical runtime bound relies on a stated number-theoretic hypothesis.
Solovay–Kitaev applies to any finite universal gate set and gives words of length polylogarithmic in (Dawson and Nielsen); number-theoretic methods for Clifford+T reach the optimal for single-qubit rotations (Ross and Selinger). The surrounding theory (exact synthesis, phase polynomials, T-count optimisation such as Amy, Maslov and Mosca) is on the Clifford+T synthesis page.
Worked Examples
The nonlocal core of CZ
In the magic basis, , , are diagonal, and the Cartan decomposition reads CZ as . Check on : gives , gives , and the phase restores overall, as CZ requires; on the factors are , and , with product . So CZ has interaction triple , equivalent to under the moves listed in the theorem. It needs exactly one CNOT, while SWAP, with triple , uses three.
Why initial layout matters
Hardware: a line . Logical circuit: then (a fan-out from ).
- Layout , , : the first gate is on an edge, the second is not. One SWAP on fixes it: total CNOTs. Alternatively the bridge identity implements with four CNOTs and no change of layout: also 5.
- Layout : both gates are on edges, total 2 CNOTs.
With two-qubit error per CNOT and errors adding, the first layout has error about and the second about . Choosing the layout is the "mapping" half of the routing problem; a triangle of interactions on the same line needs at least one SWAP or bridge for every layout, because a triangle is not a subgraph of a path.
A peephole saving T gates
On a control qubit, (with up to phase on the control) becomes , because on the control commutes with CNOT. A peephole rule thus turns two T gates into one Clifford gate, lowering the T-count from 2 to 0. The same commutation is the engine behind the phase-polynomial view of CNOT+T circuits.
CNOT budgets
The parameter-counting bound gives = 3, 14, 61, 252 for . A generic five-qubit unitary therefore needs at least 252 CNOTs; at an error rate of per CNOT that alone contributes about to the error. This is why compilers do not resynthesise large blocks from their matrices.
Algorithms and Implementations
- Routing heuristics. SABRE (Li, Ding and Xie) chooses SWAPs greedily against a look-ahead cost on the front layer of the dependency graph and refines the initial layout by routing the circuit forwards and backwards. Zulehner, Paler and Wille use A* search layer by layer, and Cowtan et al. describe a device-independent router.
- Rule-based optimization. Gate cancellation, rotation merging and phase-polynomial resynthesis (Nam et al.); template matching with exact pattern matching (Iten et al.).
- Block resynthesis. Collect maximal runs of gates on two qubits and resynthesise each with the three-CNOT construction when that is cheaper.
- Graphical rewriting. The ZX-calculus gives a complete set of rewrite rules that underlies several optimizers.
Computational Complexity
Exact synthesis of an arbitrary -qubit unitary is exponential in in output size, by the lower bound above. For routing, Botea, Kishimoto and Marinescu prove NP-completeness of a makespan-optimization formulation of circuit compilation onto restricted hardware, and exact solvers (search, SAT and integer programming) scale exponentially in the worst case, so production compilers use heuristics such as those above. Peephole optimization with a fixed rule set runs in polynomial time per pass but carries no global optimality guarantee.
Limitations and Common Pitfalls
- Wrong cost model. CNOT count is the right metric on noisy devices, T-count and T-depth on fault-tolerant ones (resource estimation); optimising one can worsen the other.
- Final permutation. Routing leaves qubits permuted. Forgetting to relabel measurement results gives wrong answers that still look plausible.
- Equivalence up to phase. Rewrites valid up to a global phase are not valid inside controlled operations, where the phase becomes relative.
- Uniform error assumptions. Counting SWAPs treats all edges alike; real devices have per-edge error rates and crosstalk, which hardware-aware algorithms and noise-aware compilers take into account.
- Verification. Optimizers are complex programs; checking that output and input circuits are equivalent is itself a task of quantum software engineering.
Related Research
The architecture side, including how connectivity shapes error-correcting codes and lattice-surgery layouts, is on quantum architecture; compilation for fault-tolerant machines continues in Clifford+T synthesis and lattice surgery. Pulse-level compilation, which replaces fixed native gates by optimised control waveforms, connects to quantum control.