Gate Identities and Circuit Equivalences
The algebraic rewrite rules that let compilers transform one circuit into an equivalent, cheaper one.
Why identities matter
Two circuits are equivalent if they implement the same unitary, possibly up to a global phase. Compilers use a library of small provable identities as rewrite rules, repeatedly substituting a cheaper subcircuit for an equal one until the circuit stops shrinking. Every optimization pass rests on these identities being exactly true.
Fundamental single-qubit identities
- H·X·H = Z and H·Z·H = X — the Hadamard swaps the X and Z bases
- H·Y·H = -Y — Hadamard flips the sign of Y
- S·S = Z and T·T = S — the phase-gate ladder
- X·Y·Z = i·I — the product of all three Paulis is a phase
Two-qubit identities
- Two CNOTs on the same wires cancel: CNOT·CNOT = I
- Conjugating a CNOT by Hadamards on both qubits swaps control and target
- SWAP = three CNOTs alternating direction
- CNOT with Hadamards on the target line becomes controlled-Z (CZ)
Commutation rules
A control commutes with any Z-diagonal gate on the control line; a target commutes with any X-basis gate on the target line. Two gates that act on disjoint qubits always commute. These rules let a compiler slide gates past one another to expose cancellations.
import numpy as np
H=np.array([[1,1],[1,-1]])/np.sqrt(2)
X=np.array([[0,1],[1,0]]); Z=np.array([[1,0],[0,-1]])
print(np.allclose(H@X@H, Z)) # HXH = Z
Up to global phase
Many identities hold only up to a global phase, which is harmless in isolation but matters under control. A rewrite engine must therefore track phase where a subcircuit may later be controlled. See global phase and transpilation.