|q⟩ Bad Qubits

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|\psi\rangle = \alpha|0\rangle + \beta|1\rangle is written as a column vector

ψ=(αβ),|\psi\rangle = \begin{pmatrix} \alpha \\ \beta \end{pmatrix},

where α,βC\alpha, \beta \in \mathbb{C} and α2+β2=1|\alpha|^2 + |\beta|^2 = 1. The basis states themselves are just the standard basis vectors:

0=(10),1=(01).|0\rangle = \begin{pmatrix} 1 \\ 0 \end{pmatrix}, \qquad |1\rangle = \begin{pmatrix} 0 \\ 1 \end{pmatrix}.

Gates as matrices

A single-qubit gate UU acts on ψ|\psi\rangle by ordinary matrix–vector multiplication:

Uψ=(U00U01U10U11)(αβ)=(U00α+U01βU10α+U11β).U|\psi\rangle = \begin{pmatrix} U_{00} & U_{01} \\ U_{10} & U_{11} \end{pmatrix} \begin{pmatrix} \alpha \\ \beta \end{pmatrix} = \begin{pmatrix} U_{00}\alpha + U_{01}\beta \\ U_{10}\alpha + U_{11}\beta \end{pmatrix}.

Because gates must preserve the norm (so that probabilities still sum to 1), every valid quantum gate matrix is unitary: UU=IU U^\dagger = I.

As a worked example, the Pauli-Z gate has matrix

Z=(1001).Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}.

Applying ZZ to the equal superposition +=12(11)|+\rangle = \tfrac{1}{\sqrt{2}}\binom{1}{1} gives

Z+=(1001)(1212)=(1212)=.Z|+\rangle = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix} \begin{pmatrix} \tfrac{1}{\sqrt{2}} \\ \tfrac{1}{\sqrt{2}} \end{pmatrix} = \begin{pmatrix} \tfrac{1}{\sqrt{2}} \\ -\tfrac{1}{\sqrt{2}} \end{pmatrix} = |{-}\rangle.

The top amplitude is unchanged; the bottom amplitude flips sign. That sign flip has no effect on measurement probabilities by itself — 122=12|{-}\tfrac{1}{\sqrt{2}}|^2 = \tfrac{1}{2} 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 HH first, then ZZ, gives the operator ZHZ H (rightmost matrix acts first):

ZH=(1001)12(1111)=12(1111).ZH = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix} \cdot \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix} = \frac{1}{\sqrt{2}}\begin{pmatrix} 1 & 1 \\ -1 & 1 \end{pmatrix}.

Applied to 0|0\rangle this yields 12(11)=\tfrac{1}{\sqrt{2}}\binom{1}{-1} = |{-}\rangle, matching the step-by-step result above.

Try it

Start from 0|0\rangle, apply HH to reach +|+\rangle, then apply ZZ. 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\approx +0.707 on 0|0\rangle and 0.707\approx -0.707 on 1|1\rangle, which is exactly |{-}\rangle.

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