You are reading without signing in. Sign in or continue as guest to take quizzes and climb the ranks.
Circuit Identities & Gate Cancellation
The handful of rewrite rules an optimiser uses, and a bloated circuit you can watch shrink from level 0 to level 3.
- 01Recognise self-inverse pairs: H·H, X·X and CNOT·CNOT.
- 02Use H·Z·H = X and H·X·H = Z to swap bit flips and phase flips.
- 03Merge rotations about one axis and predict what an optimiser leaves of a bloated circuit.
An optimiser does not understand your algorithm. It knows a list of local rules, short patterns of gates that equal something shorter, and applies them until nothing more fits. Learn the rules and you can predict its output, or write the short circuit yourself.
| Identity | Why it holds | Saving |
|---|---|---|
| H · H = I | H is its own inverse | 2 gates → 0 |
| X · X = I | a flip done twice is no flip (same for Y and Z) | 2 gates → 0 |
| CNOT · CNOT = I | the target is XOR-ed with the control twice | 2 two-qubit gates → 0 |
| H · Z · H = X | H swaps the X and Z axes of the Bloch sphere | 3 gates → 1 |
| H · X · H = Z | the same swap, the other way round | 3 gates → 1 |
| RZ(α) · RZ(β) = RZ(α+β) | two turns about one axis add up | 2 gates → 1 |
01When a pair does not cancel
Two CNOTs cancel only if they have the same control and target and nothing in between blocks them: an H on the target between them, for example, does not commute with the CNOT and keeps both alive. That is why optimisers check commutation. Level 1 cancels gates that are already neighbours; levels 2 and 3 also slide gates past each other to bring pairs together, and level 3 re-synthesises whole two-qubit blocks.
Shrink a bloated circuit
Eleven gates that do very little. Compile at level 0, then level 1 and level 3 (or press Compare levels). Level 0 keeps every gate and translates each one, so you get two two-qubit gates and a long run of rz and sx. Level 1 already cancels the pairs, turns H·Z·H into one x and merges the two rz into rz(0.8). Level 3 goes at least as far; an rz right before a measurement cannot change the counts, so it may shorten or drop it.
OPENQASM 3.0;
include "stdgates.inc";
qubit[2] q;
bit[2] c;
h q[0];
h q[0];
x q[1];
x q[1];
cx q[0], q[1];
cx q[0], q[1];
h q[1];
z q[1];
h q[1];
rz(0.3) q[0];
rz(0.5) q[0];
c = measure q;
2 questions · one try each counts
What does H·X·H equal?
Sign in to answer and climb the ranks; you will come back to this lesson.
One qubit has rz(pi/4), rz(pi/4), rz(pi/2) in a row. What can the optimiser replace them with?
Sign in to answer and climb the ranks; you will come back to this lesson.