|q⟩ Bad Qubits

advanced · Programming · Advanced Circuit Construction & Transpilation

Circuit Identities and Simplification

Once a circuit has been decomposed into a native gate set it is rarely optimal. The same unitary can be written in many ways, and a transpiler's optimization stage rewrites the gate list into a cheaper equivalent without changing the unitary it implements. The rewrites are driven by a catalogue of circuit identities — small, provable equivalences applied repeatedly until no more fire.

Three families of identity

Cancellation. A gate adjacent to its own inverse vanishes. Since XX, ZZ, HH, and CNOT are self-inverse,

XX=I,HH=I,CNOTCNOT=I,X X = I, \qquad H H = I, \qquad \mathrm{CNOT}\,\mathrm{CNOT} = I,

and TT=IT T^\dagger = I, SS=IS S^\dagger = I. Spotting and deleting such pairs is the single most productive optimization.

Merging. Rotations about the same axis add their angles:

Rz(α)Rz(β)=Rz(α+β),P(α)P(β)=P(α+β).R_z(\alpha) R_z(\beta) = R_z(\alpha + \beta), \qquad P(\alpha) P(\beta) = P(\alpha + \beta).

A run of zz-rotations collapses to one. If the total is a multiple of 2π2\pi it disappears entirely.

Conjugation / basis change. Gates can be transformed into one another by surrounding gates:

HZH=X,HXH=Z,SXS=Y.H Z H = X, \qquad H X H = Z, \qquad S X S^\dagger = Y.

These let the optimizer slide an awkward gate past a Hadamard into a friendlier form.

Commutation enables the others

Identities only fire when the gates involved are adjacent. Most rewriting power therefore comes from commutation rules that let the optimizer reorder gates to bring an inverse pair together. Two gates commute when they act on disjoint qubits, and many that share qubits commute too — for example, two CNOTs that share a control commute, as do two that share a target. The optimizer repeatedly commutes gates and then cancels or merges, a loop that is the core of peephole circuit optimization.

Correctness is unitary equivalence

Two circuits are interchangeable precisely when they implement the same unitary (up to global phase). That is the contract every simplification must honour, and it is exactly what the grader enforces by comparing full matrices — a smaller gate count is worthless if the operation changed.

Try it

Realize SWAP(0,1)\mathrm{SWAP}(0,1) using only CNOTs — no swap(). The grader compares the complete two-qubit unitary, so an incorrect rewrite will fail even if it is shorter.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.