Order of Operations
In a quantum circuit, gates are applied in time order from left to right — the leftmost gate acts first. This seems obvious, but it has a critical consequence: quantum gates generally do not commute, so swapping two gates changes the answer.
Why order matters
Consider the gate (bit-flip) and the gate (Hadamard). Starting from :
- Applying first, then : .
- Applying first, then : .
Both end in an equal superposition, but the relative phase flips sign — they are physically distinguishable states. In matrix terms:
, confirming that the two orderings produce different unitaries.
Reading and writing the order
In mathematical notation, operators are written right to left — means acts first, then . The circuit diagram convention goes the opposite way: gates are drawn left-to-right in the order they fire. The programming API follows the circuit diagram:
c.x(0); // X fires first
c.h(0); // H fires second → implements the unitary HX
Each method call appends one gate to the end of the timeline. Keep this left-to-right reading in mind whenever you transcribe a sequence of gates from a diagram into code.
Try it
Implement the operation " then " (i.e. the unitary ) on a single qubit. The grader checks the full unitary matrix, so the order of your gate calls must be exactly right.
Sign in on the full site to ask questions and join the discussion.