beginner · Programming · Single-Qubit Gates I: X, Y, Z
Gates as Matrices
A qubit state is a column vector of complex amplitudes, and every gate is a matrix that transforms
that vector. Understanding this connection lets you predict the output of any gate analytically —
before you run a single simulation.
State vectors as column vectors
A single-qubit state ∣ψ⟩=α∣0⟩+β∣1⟩ is written as a column vector
∣ψ⟩=(αβ),
where α,β∈C and ∣α∣2+∣β∣2=1. The basis states themselves
are just the standard basis vectors:
∣0⟩=(10),∣1⟩=(01).
Gates as matrices
A single-qubit gate U acts on ∣ψ⟩ by ordinary matrix–vector multiplication:
Because gates must preserve the norm (so that probabilities still sum to 1), every valid quantum gate
matrix is unitary: UU†=I.
As a worked example, the Pauli-Z gate has matrix
Z=(100−1).
Applying Z to the equal superposition
∣+⟩=21(11) gives
Z∣+⟩=(100−1)(2121)=(21−21)=∣−⟩.
The top amplitude is unchanged; the bottom amplitude flips sign. That sign flip has no effect on
measurement probabilities by itself — ∣−21∣2=21 just as before — but
it matters as soon as the state is fed into further gates that can turn phase differences into
amplitude differences.
Applying gates in sequence
When multiple gates are applied one after another, their combined effect is the product of their
matrices, read right to left. Applying H first, then Z, gives the operator ZH (rightmost
matrix acts first):
ZH=(100−1)⋅21(111−1)=21(1−111).
Applied to ∣0⟩ this yields 21(−11)=∣−⟩, matching the
step-by-step result above.
Try it
Start from ∣0⟩, apply H to reach ∣+⟩, then apply Z. The simulator computes the
resulting state vector — compare it with the hand calculation above.
Run your code to see the quantum state.
After running, you should see amplitudes ≈+0.707 on ∣0⟩ and ≈−0.707 on
∣1⟩, which is exactly ∣−⟩.
Sign in on the full site to ask questions and join the discussion.