Lesson 1 of 3
Qubits and Complex Numbers
What a qubit holds, why its amplitudes have to be complex, and what phase does to a wave.
What you will learn
- the Born rule
- complex amplitudes
- modulus squared
- the conjugate
- polar form
- global phase
- relative phase
- kets and bras
- the inner product
- normalization
Before you start
Python, and enough NumPy to index an array. The first section builds the notebook that every later lesson starts from.
Set up your notebook
0:00–0:10
Create an environment, then open a blank notebook and run the two cells below. They mirror the lecture notebooks: imports first, shared helpers second. Qiskit can wait until the course asks for it.
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install numpy scipy matplotlib jupyterlab
jupyter labimport cmath
from functools import reduce
from math import factorial
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft, fftfreq, ifft
from scipy.linalg import expmdef sine_wave(t, frequency, phase):
"""Same function as the lecture notebook: sin(2πft + p)."""
return np.sin(2 * np.pi * frequency * t + phase)
def dag(M):
"""Conjugate transpose, written M† on the slides."""
return M.conj().T
ket0 = np.array([1, 0], dtype=complex)
ket1 = np.array([0, 1], dtype=complex)
plus = (ket0 + ket1) / np.sqrt(2) # |+⟩
minus = (ket0 - ket1) / np.sqrt(2) # |−⟩
I2 = np.eye(2, dtype=complex)
X = np.array([[0, 1], [1, 0]], dtype=complex)
Z = np.array([[1, 0], [0, -1]], dtype=complex)
H = np.array([[1, 1], [1, -1]], dtype=complex) / np.sqrt(2) # Hadamard
S = np.array([[1, 0], [0, 1j]], dtype=complex)The big picture
0:10–0:35 · Overview, slides 1–50
Slow down on slides 4–7, 18–31 and 36–42, where the ideas the mathematics supports are introduced. Skim the rest: history, hardware photos and tooling.
Read with one question in mind: what does a qubit hold, what can a gate do to it, and what comes out when you measure?
Explain it. Answer each of these in a sentence or two, out loud or on paper, before you open the answers at the foot of the page.
- What does the Born rule say about a state α|0⟩ + β|1⟩?
- n qubits hold 2ⁿ amplitudes. Why isn't that a free exponential speedup?
- What two conditions does every quantum gate satisfy?
- What's the difference between a physical and a logical qubit?
- Why can quantum key distribution detect an eavesdropper?
Complex numbers
0:35–1:05 · Part One, slides 3–10 and 15
The fact to own: the modulus squared needs the conjugate, because squaring a complex number doesn't give you a real one.
Treat polar form as the picture: a length and an angle. Multiplying two complex numbers multiplies the lengths and adds the angles.
c = 3 + 4j # Python writes i as j
print(c.real, c.imag)
print(c * c) # c²: complex, so no good as a probability
print(c * c.conjugate()) # real and never negative
print(abs(c), abs(c) ** 2) # modulus and modulus squared
r, phi = cmath.polar(c) # phi = atan2(imag, real)
print(r, phi, cmath.rect(r, phi)) # and back again (tiny float error)
turn = cmath.exp(1j * np.pi / 3) # multiplying by e^(iθ) rotates by θ...
print(abs(c * turn)) # ...and leaves the length alone
d = 1 - 2j
inv = cmath.rect(1 / abs(d), -cmath.phase(d)) # (1/r) with angle −φ
print(c / d, c * inv) # dividing = multiplying by the inverseExplain it.
- Why does the Born rule use the modulus squared rather than the plain square of the amplitude?
- What does multiplying by e^(iθ) do to a complex number?
- What are the modulus and the argument, geometrically?
- Why is division easiest in polar form?
Phase
1:05–1:25 · Part Two, slides 2–11
A phase shift slides a wave along the time axis without changing its shape. A shift of π flips the sign, so a wave plus its shifted copy is zero everywhere.
That is interference in its classical form, and quantum algorithms run on it.
t = np.linspace(0, 1, 1024, endpoint=False)
for shift in (0, np.pi / 2, np.pi):
total = sine_wave(t, 5, 0) + sine_wave(t, 5, shift)
print(f"shift {shift:.2f} rad -> largest |sum| = {np.abs(total).max():.3f}")
plt.plot(t, sine_wave(t, 5, 0), label="phase 0")
plt.plot(t, sine_wave(t, 5, np.pi), label="phase π")
plt.plot(t, sine_wave(t, 5, 0) + sine_wave(t, 5, np.pi), label="sum")
plt.legend()
plt.show()Explain it.
- What does changing the phase do to a wave?
- Why does adding a π-shifted copy cancel the wave?
- The slides say some phases can be ignored and others can't. Which is which?
Kets, bras and the inner product
1:25–1:50 · Part One, slides 16–20
A ket is a column, its bra is the conjugate transpose, and ⟨a|b⟩ is row times column.
Every valid qubit has ⟨ψ|ψ⟩ = 1: the Born rule written as a length. The Bloch sphere is where those normalized states live.
psi = (1 / np.sqrt(2)) * ket0 + (1j / np.sqrt(2)) * ket1 # α = 1/√2, β = i/√2
print(np.vdot(psi, psi)) # ⟨ψ|ψ⟩ ≈ 1: vdot conjugates its FIRST argument
print(np.dot(psi, psi)) # no conjugate: 1/2 + i²/2 = 0, not a length
print(np.abs(psi) ** 2) # P(0), P(1)
print(np.vdot(ket0, psi)) # ⟨0|ψ⟩ is the amplitude α
ket = psi.reshape(2, 1) # column vector
bra = dag(ket) # row vector
print((bra @ ket).shape, (ket @ bra).shape) # bra·ket is 1×1, ket·bra is 2×2Explain it.
- What's the difference between a ket and a bra?
- What does ⟨ψ|ψ⟩ = 1 mean physically?
- What does ⟨0|ψ⟩ give you?
- A qubit has two complex amplitudes, four real numbers in all. Why does it fit on a sphere?
Close the book
1:50–2:00
Without notes, answer every prompt in this lesson again, then open the answers. Write down the ones you missed and reread those slides before the next lesson.
Try these changes
Make these changes in the files beside this guide. Each one only uses what this lesson has already covered. Tick each one when it works.
Check yourself
Three quick questions on this lesson. Get them all right to finish the lesson and earn a pack.
Loading your answers
Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.
Answers to the prompts
The big picture.
- The Born rule. Measuring α|0⟩ + β|1⟩ gives 0 with probability |α|² and 1 with probability |β|², so the two sum to 1. Afterwards the qubit stays in the state you observed.
- Why 2ⁿ amplitudes aren't a free speedup. Measurement returns only n bits and collapses the state, so you can't read the amplitudes out. Algorithms have to use interference to make the right n-bit answer likely, and because results are probabilistic you rerun and take the most common answer.
- The two gate conditions. A gate maps unit vectors to unit vectors, so probabilities still sum to 1, and it is reversible: as many outputs as inputs, and it can be run backwards. The next lesson shows that one matrix property, unitarity, gives you both.
- Physical and logical qubits. Physical qubits are the noisy ones in the hardware. A logical qubit is an error-corrected qubit encoded across many physical ones. Algorithms are written as if every qubit were logical.
- Detecting an eavesdropper. An unknown qubit can't be copied — the no-cloning theorem — and measuring it disturbs it, so anyone intercepting the key leaves errors the two parties can check for.
Answers: complex numbers, phase, kets
- Modulus squared, not the plain square. α² is complex in general. α times its conjugate is a² + b², always real and never negative.
- Multiplying by e^(iθ). It rotates the number around the origin without changing its length, so the modulus squared is unchanged. That is the seed of “a global phase doesn't matter”.
- Modulus and argument. The length of the arrow from 0 to the point, and the angle it makes with the positive real axis, atan2(Im, Re).
- Division in polar form. The inverse of a number with length r and angle φ has length 1/r and angle −φ: flip the length, negate the angle, then multiply.
- Changing the phase of a wave. It slides the wave along the time axis. Shape, frequency and amplitude stay the same.
- Why a π-shifted copy cancels. sin(x + π) = −sin(x), so the two sum to zero everywhere: destructive interference, the idea behind noise-cancelling headphones.
- Which phases can be ignored. A phase on the whole state leaves every modulus squared unchanged, so no measurement can see it. A phase between amplitudes changes how they add or cancel under later gates, so it changes outcomes. The last lesson's capstone shows this.
- Ket and bra. A ket is a column vector; the bra is its conjugate transpose, a row. The conjugate is what makes ⟨ψ|ψ⟩ real.
- What ⟨ψ|ψ⟩ = 1 means. The measurement probabilities sum to 1.
- What ⟨0|ψ⟩ gives you. The amplitude on |0⟩, which is α. Its modulus squared is the probability of measuring 0.
- Why a qubit fits on a sphere. Normalization removes one degree of freedom and global phase removes another, leaving two angles: a point on the unit sphere.
Where the slides simplify
The slides trade precision for speed in a few places. These are the versions to keep in your head.
- Not “try every answer at once” (Overview, slides 28–30). Measurement returns n bits, so algorithms use interference to make the right answer likely. Speedups are problem-specific: super-polynomial for structured problems like factoring, quadratic for unstructured search, and NP-complete problems aren't believed to become efficient.
- Entanglement doesn't carry messages (Overview, slides 36–37). Each side alone sees random results, so nothing usable travels faster than light. The outcomes are correlated — equal for some entangled states, opposite for others — and Bell-test experiments rule out the idea that the values were fixed when the qubits separated.
- Superconducting qubits, and the real overhead (Overview, slides 32–35). IBM's and Google's qubits are superconducting circuits, not single atoms; trapped ions and neutral atoms are other platforms. And 5–9 physical qubits per logical qubit is the size of the smallest codes; practical schemes such as the surface code need hundreds or more at today's error rates.
- Typo, Part One slide 10. The inverse in polar form keeps the i in the exponent: length 1/r at angle −φ.