|q⟩ Bad Qubits

beginner · Programming · Building & Reading Quantum Circuits

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 XX gate (bit-flip) and the HH gate (Hadamard). Starting from 0|0\rangle:

Both end in an equal superposition, but the relative phase flips sign — they are physically distinguishable states. In matrix terms:

HX=12(1111)(0110)=12(1111),XH=12(0110)(1111)=12(1111).HX = \frac{1}{\sqrt{2}}\begin{pmatrix}1&1\\1&-1\end{pmatrix}\begin{pmatrix}0&1\\1&0\end{pmatrix} = \frac{1}{\sqrt{2}}\begin{pmatrix}1&1\\-1&1\end{pmatrix}, \qquad XH = \frac{1}{\sqrt{2}}\begin{pmatrix}0&1\\1&0\end{pmatrix}\begin{pmatrix}1&1\\1&-1\end{pmatrix} = \frac{1}{\sqrt{2}}\begin{pmatrix}1&-1\\1&1\end{pmatrix}.

HXXHHX \neq XH, confirming that the two orderings produce different unitaries.

Reading and writing the order

In mathematical notation, operators are written right to leftHXHX means XX acts first, then HH. 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 "XX then HH" (i.e. the unitary HXHX) on a single qubit. The grader checks the full unitary matrix, so the order of your gate calls must be exactly right.

Run your code to see the quantum state.

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