QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
HomeLearnCatalogRanksPlaygroundDocs
04 · Circuit Identities & Gate Cancellation0%

You are reading without signing in. Sign in or continue as guest to take quizzes and climb the ranks.

Learn/QC 203Hardware and Compilation·Lesson 4 of 6

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.

Intermediate12 min read5 sections · 2 questions+45
By the end you will be able to
  1. 01Recognise self-inverse pairs: H·H, X·X and CNOT·CNOT.
  2. 02Use H·Z·H = X and H·X·H = Z to swap bit flips and phase flips.
  3. 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.

IdentityWhy it holdsSaving
H · H = IH is its own inverse2 gates → 0
X · X = Ia flip done twice is no flip (same for Y and Z)2 gates → 0
CNOT · CNOT = Ithe target is XOR-ed with the control twice2 two-qubit gates → 0
H · Z · H = XH swaps the X and Z axes of the Bloch sphere3 gates → 1
H · X · H = Zthe same swap, the other way round3 gates → 1
RZ(α) · RZ(β) = RZ(α+β)two turns about one axis add up2 gates → 1
HZH=X,HXH=Z,Rz​(α)Rz​(β)=Rz​(α+β)
H·Z·H = X is also why a phase-flip error looks like a bit-flip error when you measure in the X basis.

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.

Hands-on

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
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;
Sign in to run (nothing is saved)
Open in the IDE Open in the Playground
Knowledge check

2 questions · one try each counts

Question 1

What does H·X·H equal?

Sign in to answer and climb the ranks; you will come back to this lesson.

Question 2

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.

Key takeaway

Optimisers apply a few local identities: self-inverse pairs vanish, H turns X into Z and back, and rotations about one axis merge. Commuting gates decide which pairs can meet. Write circuits without such pairs and every level has less to fix.

Finished reading?

Knowledge check: 2 questions waiting above. Questions are optional. Mark it complete, or press Next: moving on marks it complete too.

Up next
Lesson 5 · 10 min
Topology and SWAP cost
Coupling maps, the heavy-hex lattice, and how to lay out a circuit so the router adds nothing.
Further reading
  • Docs: optimisation levels 0–3
More in Hardware and Compilation
  • From an abstract qubit to a device
  • Classical Compiler vs. Quantum Transpiler
  • Transpilation and basis gates
Transpilation and basis gatesTopology and SWAP cost
In this lesson0%
  1. Introduction
  2. When a pair does not cancel
  3. Try it: Shrink a bloated circuit
  4. Knowledge check
  5. Key takeaway
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in