← Back to section · Quantum Computing

Circuit Identities and Simplification: How Compilers Rewrite Quantum Circuits

A systematic tour of the equivalence rules for quantum circuits — self-inverse cancellation, Hadamard conjugation, commutation and exchange, rotation merging and phase folding, Pauli propagation and the CNOT bridge identity — and why circuit equivalence checking is hard and compilers rely on local rewriting.

Published 22 November 2026 · 7 min read · PQ Frontier

Circuit Identities and Simplification: How Compilers Rewrite Quantum Circuits
AI-generated site artwork
Video explainer · Quantum Gates & Circuits

One and the same unitary can be realized by countless circuits, some long and error-prone, some compact and clean. The compiler’s job is to find the cheap one among a crowd of equivalent circuits. It has no magic; it relies on applying circuit identities — local equivalence-replacement rules — thousands of times until a fixed point is reached. This article goes through those rules systematically: they are both the compiler’s toolbox and the basic skill set for reading circuits and estimating costs by hand.

The most basic identities: self-inverse gates and conjugation

The first class of rules comes from the algebra of the gates themselves. HH, XX, YY, ZZ, CNOT, and SWAP are all self-inverse: applying the same gate twice in a row equals doing nothing, so adjacent identical gates cancel outright. The phase gates form an additive chain instead: T⋅T=ST \cdot T = S, S⋅S=ZS \cdot S = Z, Z⋅Z=IZ \cdot Z = I, so consecutive phase gates can be merged and promoted. The second class is conjugation: an XX sandwiched by HH becomes ZZ (HXH=ZHXH = Z), a sandwiched ZZ becomes XX (HZH=XHZH = X), and a sandwiched YY yields −Y-Y; an XX sandwiched by SS becomes YY (SXS†=YSXS^\dagger = Y). Conjugation rules let a gate “pass through” another gate and emerge transformed on the other side — a stretch of HH–XX–HH can be replaced wholesale by a single ZZ, which is the most common trick in simplifying circuits by hand, and also the theoretical basis for writing measurement-basis changes into circuits (measuring XX equals applying HH and then measuring ZZ). The third class is single-qubit gate fusion: any two adjacent single-qubit gates multiply into a single-qubit unitary, implementable in one UU gate, so compilers compress fragmented single-qubit sequences to at most one gate per qubit per layer.

Commutation relations: which gates may change places

Simplification presupposes rearrangement, and rearrangement rests on commutation. Gates on disjoint qubit sets commute trivially. More useful is structured commutation: all diagonal gates commute with each other — RzR_z, SS, TT, CZ, and controlled-phase gates can be reordered arbitrarily and same-axis rotations merged (Rz(α)Rz(β)=Rz(α+β)R_z(\alpha) R_z(\beta) = R_z(\alpha + \beta)). A CNOT partially commutes with certain Pauli gates: ZZ on the control side and XX on the target side both commute with CNOT and pass through freely; conversely, when XX on the control side or ZZ on the target side passes through a CNOT, it spreads:

CNOT (X⊗I) CNOT=X⊗X,CNOT (I⊗Z) CNOT=Z⊗Z.\mathrm{CNOT}\,(X \otimes I)\,\mathrm{CNOT} = X \otimes X, \qquad \mathrm{CNOT}\,(I \otimes Z)\,\mathrm{CNOT} = Z \otimes Z.

This set of Pauli propagation rules is the engine of the stabilizer formalism: it lets compilers (and error-correction decoders) track where a Pauli operator flows through a circuit without actually executing it. Combining the propagation rules with phase-gate merging yields phase folding: zz-axis rotations and phase gates scattered through a circuit are pushed together along commuting paths and merged into a few rotations, directly cutting the most expensive TT count in fault-tolerant synthesis. Template optimization goes one step further: Nam and co-workers compiled common small circuit fragments and their equivalent reduced forms into a template library, and the compiler pattern-matches and substitutes them across the circuit, achieving order-of-magnitude gate-count reductions on nn-qubit arithmetic circuits.

The bridge identity across qubits

Some identities exist specifically to serve connectivity-limited hardware. Suppose qubit bb lies between aa and cc on the coupling graph; then the sequence

CNOTb→c  CNOTa→b  CNOTb→c  CNOTa→b\mathrm{CNOT}_{b\to c}\; \mathrm{CNOT}_{a\to b}\; \mathrm{CNOT}_{b\to c}\; \mathrm{CNOT}_{a\to b}

as a whole implements exactly a remote CNOTa→c_{a\to c}, while the middle qubit bb is restored unchanged at the end — as can be verified by hand on any basis state ∣a,b,c⟩|a, b, c\rangle: cc is XORed twice and ends up flipped exactly aa times, while bb, XORed with aa twice, returns to itself. This bridge identity uses 4 neighboring CNOTs to replace the 7-plus CNOTs of “SWAP over, apply the gate, SWAP back,” and is a standard item in the router’s toolbox. Cousins include building a remote CZ by bridging and decomposing SWAPs into existing CNOT sequences. Together they show that topology-friendly members hide inside a circuit’s equivalence class, and whether they are found depends on how rich the compiler’s stock of identities is.

The limits of simplification: cancellation is not always profit

Identities are true, but applying them is not always profitable. First, simplification targets can fight each other: cancelling two gates saves gate count, yet if it serializes a structure that could have run in parallel, depth increases instead; fusing single-qubit gates reduces fragmentation within a layer but may block a joint rearrangement with two-qubit gates later. Second, some “equivalences” hold only in isolation: two gates differing by a global phase are interchangeable, but once that subcircuit is controlled as a whole, the global phase becomes a relative phase and the substitution fails on the spot — before processing a subcircuit, one must ask in what context it will sit. Third, approximation changes the ledger: in fault-tolerant synthesis, rotation gates can only be implemented approximately, “equal” must be relaxed to “within tolerance ε\varepsilon,” and the exact cancellations offered by identities have to be accounted together with the approximation error budget; greedily cancelling while pushing the error to the edge of the budget is a common way to crash. Fourth, measurement is a boundary for simplification: gates before and after a measurement cannot be cancelled across it at will, and in dynamic circuits with classical control the rewrite rules must respect causal order. Mature compilers therefore turn rewriting into a search guided by a cost model — each rule is annotated with its expected effect on gate count, depth, and TT count, and heuristics decide the order of application, rather than unconditionally “cancelling on sight.”

Why equivalence checking is hard, and what compilers actually do

Given how important equivalence transformations are, why not simply verify that two circuits are fully equivalent? Because in general this becomes exponentially harder with the number of qubits: the unitary a circuit implements has 2n×2n2^n \times 2^n matrix entries, and the cost of exact comparison explodes with nn; deciding whether two circuits implement the same unitary is a famously hard problem. Compilers in practice therefore never verify globally, and instead combine three kinds of methods: local rewriting (applying identities and templates, each step obviously meaning-preserving), numerical checks on small windows (computing and comparing matrices directly for subcircuits on a few qubits, at controlled cost), and structured representations such as decision diagrams (compressing unitaries with data structures like QMDD in verification tools, effective for circuits with regular structure). The optimization target itself also switches with context: NISQ compilation watches two-qubit gate count and depth, fault-tolerant compilation watches TT count and TT depth, and compiling for a specific chip must further account for crosstalk and calibration data. The same identity is worth different amounts under different targets — merging TT gates in the NISQ era merely saves a gate, while in the fault-tolerant era it saves a round of magic-state distillation. When reading a compilation result, first ask what it was optimizing, then judge how well it optimized.

Summary

Circuit identities are the grammar of quantum compilation: self-inverse cancellation cleans up redundancy, conjugation and commutation let gates flow and regroup through the circuit, Pauli propagation and phase folding consolidate scattered phases into a single ledger, and the bridge identity makes circuits accommodate hardware topology. The hardness of equivalence checking draws the boundary of the method — compilers approach the optimum by applying local rules repeatedly, not by global equivalence decisions. Once this grammar is understood, any “optimized circuit” can be audited item by item for exactly where it got cheaper.

References

  1. Nam, Y. et al., Automated optimization of large quantum circuits with continuous parameters, npj Quantum Information 4, 23 (2018). https://arxiv.org/abs/1710.07345
  2. Nielsen, M. A. & Chuang, I. L., Quantum Computation and Quantum Information, Cambridge University Press, 2010. https://www.cambridge.org/9780521635035
  3. Amy, M., Maslov, D., Mosca, M. & Roetteler, M., A meet-in-the-middle algorithm for fast synthesis of depth-optimal quantum circuits, IEEE Trans. CAD 32, 818 (2013). https://arxiv.org/abs/1206.0758

Tags: Circuit OptimizationCompilersCircuit IdentitiesGate Simplification