ADVANCED · MATHS · 4 × 20 MIN
Maths foundations: vectors, tensor products, measurement, modular arithmetic
Terms in this lesson
- ket
- bra
- inner product
- unitary
- Hermitian
- eigenvector
- tensor product
- product state
- Born rule
- projector
- global phase
- relative phase
- density operator
- gcd
- modular inverse
- unit
- Euler's φ
- order
- continued fraction
- convergent
What you’ll learn
- Compute H|0⟩ and H|1⟩, and show that H is unitary and Hermitian.
- Write CNOT(H ⊗ I)|00⟩ as a column of four numbers and decide whether it is a product state.
- Compute the probability of each outcome when a two-qubit state is measured, and the state left afterwards.
- Compute the order of a mod n and a modular inverse by hand.
- Expand a fraction as a continued fraction and list its convergents.
Four short units with the maths every later lesson in this track uses. M1 to M3 are the language of quantum states and gates. M4 is the number theory inside Shor's algorithm. Each unit ends with exercises, each with a full solution.
Needs: What is a qubit, really? and Superposition without the hype. You should be able to multiply a 2 × 2 matrix by a column of two numbers and to multiply complex numbers, using i2 = −1.
M1. Linear algebra for qubits
States are columns of complex numbers
A qubit's state is a column of two complex numbers, the amplitudes from Superposition without the hype:
|ψ⟩ = αβ = α|0⟩ + β|1⟩
|0⟩ = 10, |1⟩ = 01
|ψ⟩ is read "ket psi": a column of complex numbers. ℂ2 (gloss: the set of all columns of two complex numbers) is where one-qubit states live.
The bra ⟨ψ| is the same state written as a row, with every entry replaced by its complex conjugate (gloss: the conjugate of a + bi is a − bi, written with a star, (a + bi)* = a − bi):
⟨ψ| = (α*, β*)
This operation, transpose (gloss: rows become columns) then conjugate, is the conjugate transpose, written †. So ⟨ψ| = |ψ⟩†.
Inner product and norm
The inner product of two states (gloss: a row times a column, giving one complex number) is
⟨φ|ψ⟩ = φ0* ψ0 + φ1* ψ1
Swapping the order conjugates the result: ⟨ψ|φ⟩ = ⟨φ|ψ⟩*. The norm is ‖ψ‖ = √⟨ψ|ψ⟩ = √(|α|2 + |β|2). A quantum state always has norm 1; this is the rule from the site's superposition lesson that the chances add up to 100%.
Two states are orthogonal if their inner product is 0. A set of states that are each of norm 1 and pairwise orthogonal is orthonormal; if it spans the space (gloss: every state is a combination of them) it is an orthonormal basis. {|0⟩, |1⟩} is the computational basis. A second one is
|+⟩ = (1/√2)(|0⟩ + |1⟩)
|−⟩ = (1/√2)(|0⟩ − |1⟩)
Worked example. Take |ψ⟩ = 0.6|0⟩ + 0.8i|1⟩. Norm: |0.6|2 + |0.8i|2 = 0.36 + 0.64 = 1. Inner product with |+⟩:
⟨+|ψ⟩ = (1/√2)(1 · 0.6 + 1 · 0.8i)
= (0.6 + 0.8i)/√2
|⟨+|ψ⟩|2 = (0.36 + 0.64)/2 = 0.5
(In M3 this number becomes the probability of the outcome "+".) In the other order, ⟨ψ|+⟩ = (0.6 − 0.8i)/√2, the conjugate.
Matrices act on states
A 2 × 2 matrix sends a column to a column, so it is an operator on ℂ2. The gates used throughout the track:
X = 0110
Y = 0−ii0
Z = 100−1
H = (1/√2)111−1
X, Y and Z are the Pauli matrices; H is the Hadamard gate. Applying H to the basis states:
H|0⟩ = (1/√2)11 = |+⟩
H|1⟩ = (1/√2)1−1 = |−⟩
Unitary and Hermitian
A matrix U is unitary if U†U = I (gloss: its conjugate transpose undoes it; I is the identity matrix, which changes nothing). A matrix A is Hermitian if A† = A (gloss: equal to its own conjugate transpose).
H has real entries and is symmetric, so H† = H: H is Hermitian. Then
H†H = H2 = ½111−1111−1
= ½2002
= I,
so H is unitary. The same check shows X, Y and Z are each unitary and Hermitian.
Derivation: a unitary keeps the norm. For any |v⟩, ‖U|v⟩‖2 = (U|v⟩)†(U|v⟩) = ⟨v|U†U|v⟩ = ⟨v|v⟩ = ‖v‖2. So a unitary sends states of norm 1 to states of norm 1; that is why every gate in a quantum circuit is a unitary. The same step with two vectors shows that unitaries keep every inner product: ⟨Uφ|Uψ⟩ = ⟨φ|ψ⟩.
The matrix 1101 is not unitary: it sends |+⟩ to (2, 1)/√2, whose norm is √(5/2) ≈ 1.5811. It cannot be a gate.
Eigenvalues and eigenvectors
A non-zero |v⟩ is an eigenvector of A with eigenvalue λ if A|v⟩ = λ|v⟩ (gloss: A only rescales it, by the number λ).
- Z: eigenvectors |0⟩ (eigenvalue +1) and |1⟩ (−1).
- X: |+⟩ (+1) and |−⟩ (−1). Check: X|−⟩ = (1/√2)(−1, 1) = −|−⟩.
- Y: (1, i)/√2 (+1) and (1, −i)/√2 (−1).
- H: eigenvalues ±1; the +1 eigenvector is (cos π/8, sin π/8).
Derivation: a Hermitian matrix has real eigenvalues. If A† = A and A|v⟩ = λ|v⟩ with ⟨v|v⟩ = 1, then λ = ⟨v|A|v⟩, and its conjugate is λ* = ⟨v|A†|v⟩ = ⟨v|A|v⟩ = λ. So λ is real. This is why measurable quantities are written as Hermitian matrices.
Derivation: a unitary's eigenvalues have size 1. If U|v⟩ = λ|v⟩, then ‖v‖ = ‖Uv‖ = |λ|‖v‖, so |λ| = 1: every eigenvalue is eiθ for some real θ (gloss: eiθ = cos θ + i sin θ, a complex number of size 1). Lesson Q3 (phase estimation) is built on this fact.
Outer products
The outer product |a⟩⟨b| (gloss: a column times a row, giving a matrix) acts as |a⟩⟨b| |v⟩ = ⟨b|v⟩ |a⟩. Examples:
|0⟩⟨1| = 0100
|0⟩⟨0| + |1⟩⟨1| = I
X = |0⟩⟨1| + |1⟩⟨0|
The matrix |0⟩⟨0| is a projector: it keeps the |0⟩ part of a state and deletes the rest. M3 uses projectors for measurement.
Pause and recall optional
How is ⟨ψ|φ⟩ related to ⟨φ|ψ⟩?
Show answer
It is its complex conjugate.
Why must every gate be a unitary matrix?
Show answer
A gate must take every state of norm 1 to a state of norm 1, and the matrices that keep every norm are exactly the unitary ones.
Exercises M1
Easy
M1.1. Verify HZH = X by multiplying the matrices.
Show solution, M1.1
ZH = (1/√2)11−11.
H(ZH) = ½1·1 + 1·(−1)1·1 + 1·11·1 + (−1)(−1)1·1 + (−1)·1 = ½0220 = 0110 = X.
(Similarly HXH = Z.)
Medium
M1.2. Show XZ = −ZX. Then write XZ as a multiple of Y.
Show solution, M1.2
XZ = 0110100−1 = 0−110.
ZX = 100−10110 = 01−10 = −XZ.
And −iY = −i0−ii0 = 0−110, so XZ = −iY.
Medium
M1.3. Check that (1, i)/√2 and (1, −i)/√2 are eigenvectors of Y, with eigenvalues +1 and −1, and that they are orthogonal.
Show solution, M1.3
Y(1, i) = (−i · i, i · 1) = (1, i): eigenvalue +1.
Y(1, −i) = (−i · (−i), i) = (−1, i) = −(1, −i): eigenvalue −1.
Inner product: ½(1* · 1 + i* · (−i)) = ½(1 + (−i)(−i)) = ½(1 − 1) = 0.
M2. Tensor products
Two qubits, four amplitudes
Two qubits have four basis states |00⟩, |01⟩, |10⟩, |11⟩, and a two-qubit state is a column of four amplitudes. The tensor product ⊗ (gloss: the rule that builds the joint column from the two one-qubit columns) is
ab ⊗ cd = acadbcbd
Convention used in this track: in |xy⟩ = |x⟩ ⊗ |y⟩ the left qubit is written first and is the most significant bit, so the entries are in the order 00, 01, 10, 11. Qiskit numbers qubits from 0 and prints qubit 0 as the rightmost bit; in this track the first (left) qubit is the most significant, and code in this track converts Qiskit's output to that order. Examples: |0⟩ ⊗ |1⟩ = |01⟩ = (0, 1, 0, 0); |1⟩ ⊗ |0⟩ = |10⟩ = (0, 0, 1, 0); |1⟩ ⊗ |+⟩ = (0, 0, 1, 1)/√2.
n qubits have 2n basis states: 4 for two, 8 for three, 1,024 for ten, and 250 = 1,125,899,906,842,624 (about 1.13 × 1015) for fifty. A full description of a 50-qubit state is that many complex numbers.
Tensor products of matrices
For matrices, every entry of A multiplies the whole of B:
A ⊗ B = a00Ba01Ba10Ba11B
H ⊗ H = ½11111−11−111−1−11−1−11
A ⊗ B means "do A on the first qubit and B on the second". H ⊗ I applies H to the first qubit and leaves the second alone.
Derivation: (A ⊗ B)(|a⟩ ⊗ |b⟩) = A|a⟩ ⊗ B|b⟩. Entry (i, j) of the joint vector is ai bj, and the entry of A ⊗ B in row (i, j) and column (k, l), written ((i, j), (k, l)), is Aik Bjl. So the (i, j) entry of the product is
Σk,l Aik Bjl ak bl = (Σk Aik ak)(Σl Bjl bl)
= (A|a⟩)i (B|b⟩)j,
which is entry (i, j) of A|a⟩ ⊗ B|b⟩. By the same index bookkeeping, (A ⊗ B)(C ⊗ D) = AC ⊗ BD.
Derivation: (A ⊗ B)† = A† ⊗ B†. Entry ((i, j), (k, l)) of (A ⊗ B)† is the conjugate of entry ((k, l), (i, j)) of A ⊗ B, that is (Aki Blj)* = (A†)ik (B†)jl, which is entry ((i, j), (k, l)) of A† ⊗ B†. Consequence: if U and V are unitary, (U ⊗ V)†(U ⊗ V) = U†U ⊗ V†V = I ⊗ I, so U ⊗ V is unitary. Inner products also factor: ⟨a ⊗ b | c ⊗ d⟩ = ⟨a|c⟩⟨b|d⟩.
CNOT and the Bell state
CNOT flips the second qubit when the first is 1:
CNOT = |0⟩⟨0| ⊗ I + |1⟩⟨1| ⊗ X
= 1000010000010010
Worked example.
(H ⊗ I)|00⟩ = H|0⟩ ⊗ |0⟩
= (1/√2)(1, 0, 1, 0)
CNOT (1/√2)(1, 0, 1, 0) = (1/√2)(1, 0, 0, 1)
= (1/√2)(|00⟩ + |11⟩)
CNOT swapped the last two entries. The result is a Bell state.
Product or entangled?
A two-qubit state is a product state if it equals |u⟩ ⊗ |v⟩ for some one-qubit states; otherwise it is entangled.
Test. (a, b, c, d) is a product state if and only if ad = bc. (Exercise M2.3 asks for the proof.)
- |+⟩|+⟩ = ½(1, 1, 1, 1): ad − bc = ¼ − ¼ = 0. Product, as built.
- (1/√2)(1, 0, 0, 1): ad − bc = ½ − 0 = ½ ≠ 0. Entangled.
The same test in matrix form, which Q1 uses for larger systems: arrange (a, b, c, d) as the 2 × 2 matrix abcd. ad − bc is its determinant.
Gates have the same distinction. CNOT is not of the form A ⊗ B; that is why it can turn the product state (1/√2)(1, 0, 1, 0) into an entangled one.
Pause and recall optional
In this track, which qubit of |xy⟩ is the most significant bit?
Show answer
The left one, x: the entries are in the order 00, 01, 10, 11.
Which number decides whether (a, b, c, d) is a product state?
Show answer
ad − bc: the state is a product exactly when it is 0.
Exercises M2
Easy
M2.1. Compute |−⟩ ⊗ |1⟩ and (H ⊗ H)|00⟩.
Show solution, M2.1
First, |−⟩ ⊗ |1⟩ = (1 · (0, 1), −1 · (0, 1))/√2 = (0, 1, 0, −1)/√2.
(H ⊗ H)|00⟩ = H|0⟩ ⊗ H|0⟩ = |+⟩|+⟩ = ½(1, 1, 1, 1): the first column of H ⊗ H.
Medium
M2.2. Write (1, i, 2, 2i)/√10 as |u⟩ ⊗ |v⟩ with |u⟩, |v⟩ of norm 1.
Show solution, M2.2
ad = 1 · 2i/10 = 2i/10 and bc = i · 2/10 = 2i/10, so it is a product state.
The ratio of rows is (2, 2i) = 2(1, i), so |v⟩ ∝ (1, i) and |u⟩ ∝ (1, 2).
Normalised: |u⟩ = (1, 2)/√5, |v⟩ = (1, i)/√2, and |u⟩ ⊗ |v⟩ = (1, i, 2, 2i)/√10.
Hard
M2.3. Prove that (a, b, c, d) is a product state if and only if ad = bc.
Show solution, M2.3
If (a, b, c, d) = (α, β) ⊗ (γ, δ) = (αγ, αδ, βγ, βδ), then ad = αγβδ = bc.
Conversely suppose ad = bc. If a ≠ 0, take (α, β) = (a, c) and (γ, δ) = (1, b/a): the product is (a, b, c, cb/a) = (a, b, c, d), using d = bc/a.
If a = 0 then bc = 0. If b = 0 the vector is (0, 0, c, d) = (0, 1) ⊗ (c, d). If c = 0 it is (0, b, 0, d) = (b, d) ⊗ (0, 1).
In every case it is a tensor product; dividing each factor by its norm (and the state has norm 1) gives normalised factors.
M3. Probability and measurement
The Born rule
Measuring in the computational basis gives outcome m with probability |⟨m|ψ⟩|2, the squared size of that amplitude. This is the Born rule. In general form (Nielsen and Chuang, §2.2.5, projective measurement; "NC" from here on): a measurement is a set of projectors Pm (gloss: matrices with Pm2 = Pm = Pm†), one per outcome, that add up to I. Projectors that add up to I are automatically orthogonal: Pm Pm′ = 0 for m ≠ m′. Then
p(m) = ⟨ψ|Pm|ψ⟩
after outcome m: |ψ⟩ → Pm|ψ⟩ / √p(m)
The second formula is the post-measurement state: the part of |ψ⟩ consistent with the outcome, renormalised to norm 1.
Worked example, two qubits. |ψ⟩ = ½|00⟩ + ½|01⟩ + (1/√2)|11⟩ = (½, ½, 0, 1/√2).
Measure both qubits: p(00) = ¼, p(01) = ¼, p(10) = 0, p(11) = ½. They sum to 1.
Measure only the first qubit. The projector for "first qubit is 0" is P0 = |0⟩⟨0| ⊗ I, which keeps the |00⟩ and |01⟩ entries:
p(0) = ⟨ψ|P0|ψ⟩ = ¼ + ¼ = ½
P0|ψ⟩ / √(1/2) = (1/√2)(|00⟩ + |01⟩)
= |0⟩|+⟩
For outcome 1, p(1) = ½ and the state left is |11⟩. The second qubit was not measured, but the state it is left in depends on the first qubit's outcome (|+⟩ after 0, |1⟩ after 1), because the joint state was not a product state. Q1 says what the second qubit's state is before any measurement.
Applied to the Bell state (1/√2)(|00⟩ + |11⟩): measuring the first qubit gives 0 with probability ½ and leaves |00⟩, or 1, also with probability ½, and leaves |11⟩. The second qubit then reads the same value with certainty.
Measuring in another basis uses that basis's projectors. In the X basis {|+⟩, |−⟩}: p(+) = |⟨+|ψ⟩|2. For 0.6|0⟩ + 0.8i|1⟩ this is 0.5, from M1.
Global and relative phase
Derivation: a global phase cannot be observed. Multiply a state by eiθ. Every probability becomes ⟨ψ|e−iθ Pm eiθ|ψ⟩ = ⟨ψ|Pm|ψ⟩, unchanged, for every measurement. So eiθ|ψ⟩ and |ψ⟩ are the same physical state.
A relative phase (a phase on one amplitude and not the other) can be observed. |+⟩ and |−⟩ differ only by the sign of the |1⟩ amplitude. In the computational basis both give 0 or 1 with probability ½ each. In the X basis, |+⟩ gives "+" with probability 1 and |−⟩ gives "+" with probability 0. In a circuit, an X-basis measurement is H followed by a computational-basis measurement: H|+⟩ = |0⟩, H|−⟩ = |1⟩.
First look at the density operator
For a state |ψ⟩, the density operator is ρ = |ψ⟩⟨ψ| (gloss: the outer product of the state with itself, a 2 × 2 matrix for one qubit). The Born rule becomes p(m) = tr(Pm ρ) (gloss: tr is the trace, the sum of the diagonal entries).
For |ψ⟩ = 0.6|0⟩ + 0.8i|1⟩:
ρ = 0.60.8i 0.6−0.8i
= 0.36−0.48i0.48i0.64
The diagonal holds the computational-basis probabilities (0.36, 0.64). The off-diagonal entries carry the relative phase: the computational basis never reads them, and here the X basis doesn't either (tr(|+⟩⟨+| ρ) = 0.5, the two imaginary entries cancel), but the basis in exercise M3.2 does. A global phase cancels in ρ, because eiθ from the ket meets e−iθ from the bra, so ρ describes the physical state with no leftover phase. For a state written as one ket, tr ρ = 1 and tr ρ2 = 1. Q1 uses ρ for one qubit of an entangled pair, where it cannot be written as one ket; Q6 uses it for noise.
Pause and recall optional
After outcome m, what state is left?
Show answer
Pm|ψ⟩ divided by √p(m): the part of |ψ⟩ consistent with m, renormalised to norm 1.
Can a global phase eiθ change any measurement probability?
Show answer
No: eiθ from the ket meets e−iθ from the bra and they cancel.
Exercises M3
Easy
M3.1. For (½, ½, 0, 1/√2), compute the probability that the two qubits give the same value, and the probability that the second qubit reads 1.
Show solution, M3.1
Same value: p(00) + p(11) = ¼ + ½ = ¾.
Second qubit 1: p(01) + p(11) = ¼ + ½ = ¾.
Medium
M3.2. |a⟩ = 0.6|0⟩ + 0.8i|1⟩ and |b⟩ = 0.6|0⟩ − 0.8i|1⟩ give the same probabilities in the computational basis and in the X basis. Find a measurement that tells them apart: compute p(+i) for each, where |+i⟩ = (|0⟩ + i|1⟩)/√2.
Show solution, M3.2
Computational basis: both give 0.36 and 0.64.
X basis: |⟨+|a⟩|2 = |0.6 + 0.8i|2/2 = 0.5 and |⟨+|b⟩|2 = |0.6 − 0.8i|2/2 = 0.5.
In the basis |±i⟩ = (|0⟩ ± i|1⟩)/√2: ⟨+i|a⟩ = (0.6 + (−i)(0.8i))/√2 = 1.4/√2, so p(+i) = 1.96/2 = 0.98.
⟨+i|b⟩ = (0.6 + (−i)(−0.8i))/√2 = (0.6 − 0.8)/√2 = −0.2/√2, so p(+i) = 0.04/2 = 0.02.
The relative phase (+i versus −i) shows up only in this basis.
Medium
M3.3. Write ρ for |+⟩ and for |−⟩. Which entries differ?
Show solution, M3.3
ρ+ = ½1111, ρ− = ½1−1−11.
The diagonals (the computational-basis probabilities) agree; the off-diagonal entries have opposite signs. That sign is the relative phase.
M4. Modular arithmetic and continued fractions
a mod n
For integers a and n > 0, a mod n is the remainder r in a = qn + r with 0 ≤ r < n (gloss: q is the quotient). 23 mod 7 = 2, since 23 = 3 · 7 + 2; −7 mod 5 = 3, since −7 = −2 · 5 + 3. We write a ≡ b (mod n) (gloss: "a is congruent to b mod n") when n divides a − b. Sums and products can be reduced mod n at every step: (a · b) mod n = ((a mod n)(b mod n)) mod n. That is how 7100 mod 15 is computed without ever writing 7100.
gcd and Euclid's algorithm
gcd(a, b) is the largest integer dividing both. Euclid's algorithm: divide, keep the remainder, repeat with the last divisor and the remainder; the last non-zero remainder is the gcd. It works because any common divisor of a and b also divides r = a − qb, and the other way round, so gcd(a, b) = gcd(b, r).
252 = 1 · 198 + 54
198 = 3 · 54 + 36
54 = 1 · 36 + 18
36 = 2 · 18 + 0
so gcd(252, 198) = 18
Derivation: it terminates, and fast. The remainders are non-negative integers that strictly decrease, so they reach 0. Better: every two steps the remainder at least halves. Take consecutive remainders ri > ri+1 > ri+2. If ri+1 ≤ ri/2, then ri+2 < ri+1 ≤ ri/2. Otherwise ri+1 > ri/2, so the next quotient is 1 and ri+2 = ri − ri+1 < ri/2. So the number of steps is at most about 2 log2 b: for numbers of ℓ bits, at most about 2ℓ steps. Consecutive Fibonacci numbers are the slowest case for their size: (89, 55) takes 9 divisions, the most for any pair up to 89.
Bézout's identity. Running the steps backwards writes the gcd as ax + by for integers x, y. From the example above: 18 = 54 − 36 = 54 − (198 − 3 · 54) = 4 · 54 − 198 = 4(252 − 198) − 198 = 4 · 252 − 5 · 198. This is the extended Euclidean algorithm.
Modular inverses
An inverse of a mod n is a number x with ax ≡ 1 (mod n), written a−1 mod n.
a has an inverse mod n if and only if gcd(a, n) = 1. If gcd(a, n) = 1, Bézout gives ax + ny = 1, so ax ≡ 1 (mod n). Conversely, if ax = 1 + kn, any common divisor of a and n divides 1.
Worked example: 4−1 mod 21. 21 = 5 · 4 + 1, so 1 = 21 − 5 · 4, and 4 · (−5) ≡ 1 (mod 21). The inverse is −5 mod 21 = 16. Check: 4 · 16 = 64 = 3 · 21 + 1. Likewise 7−1 mod 15 = 13, since 7 · 13 = 91 = 6 · 15 + 1. But 6 and 7 have no inverse mod 21: gcd(6, 21) = 3 and gcd(7, 21) = 7.
Euler's φ
φ(n) (Euler's totient; gloss: how many of 1, …, n are coprime to n, meaning share no factor with it) counts the numbers that have an inverse mod n. For a prime p, φ(p) = p − 1. For distinct primes p and q, the numbers in 1, …, pq sharing a factor with pq are the q multiples of p and the p multiples of q, with pq itself counted twice, so
φ(pq) = pq − q − p + 1
= (p − 1)(q − 1)
φ(15) = 2 · 4 = 8; φ(21) = 2 · 6 = 12. The units mod 21 (gloss: the numbers with an inverse mod 21) are 1, 2, 4, 5, 8, 10, 11, 13, 16, 17, 19, 20.
The order of a mod n
For gcd(a, n) = 1, the order of a mod n is the smallest r ≥ 1 with ar ≡ 1 (mod n). Shor's algorithm (lesson Q4) is a quantum method for finding r; everything else in it is classical.
Worked examples. List the powers until 1 appears:
- 7k mod 15, k = 0, 1, 2, …: 1, 7, 4, 13, 1, 7, … The order is 4.
- 2k mod 21: 1, 2, 4, 8, 16, 11, 1, 2, … The order is 6.
The powers repeat with period r, because ak+r = ak ar ≡ ak.
Derivation: ak ≡ 1 if and only if r divides k. Write k = sr + t with 0 ≤ t < r. Then ak = (ar)s at ≡ at. If ak ≡ 1 then at ≡ 1 with t < r, so t = 0 by the minimality of r.
Derivation: Euler's theorem, aφ(n) ≡ 1 (mod n). Let u1, …, uφ be the units mod n. Multiplying each by a gives units again (a product of units has an inverse), and no two coincide (if a ui ≡ a uj, multiply by a−1). So a u1, …, a uφ is the same list in another order, and
∏i (a ui) ≡ ∏i ui (mod n)
so aφ(n) ∏i ui ≡ ∏i ui (mod n)
so aφ(n) ≡ 1 (mod n),
cancelling the product by multiplying with its inverse. With the previous result, the order of a divides φ(n). Mod 21 the orders are 1, 2, 3 or 6, all dividing 12; no unit mod 21 has order 12.
Continued fractions
Any fraction p/q can be written as
p/q = a0 + 1/(a1 + 1/(a2 + 1/(… + 1/am)))
= [a0; a1, a2, …, am]
The ai are the quotients of Euclid's algorithm on (p, q). Worked example: 171/512.
171 = 0 · 512 + 171
512 = 2 · 171 + 170
171 = 1 · 170 + 1
170 = 170 · 1 + 0
so 171/512 = [0; 2, 1, 170]
Cutting the expansion after aj gives the j-th convergent hj/kj (gloss: the best short approximations to the fraction). They follow the recurrence
hj = aj hj−1 + hj−2
kj = aj kj−1 + kj−2
h−1 = 1,
h−2 = 0,
k−1 = 0,
k−2 = 1
For [0; 2, 1, 170]: 0/1, 1/2, 1/3, 171/512. For 427/512 = [0; 1, 5, 42, 2]: 0/1, 1/1, 5/6, 211/253, 427/512. Consecutive convergents satisfy hj kj−1 − hj−1 kj = (−1)j−1, so every convergent is already in lowest terms. Proof by induction: the recurrence gives hj kj−1 − hj−1 kj = −(hj−1 kj−2 − hj−2 kj−1), and at j = 0 the value is h0k−1 − h−1k0 = h0 · 0 − 1 · 1 = −1, for any a0.
The theorem Shor's algorithm uses. If x is a real number and p/s a fraction with
|x − p/s| < 1/(2s2),
then p/s is a convergent of x. This is Legendre's theorem on continued fractions (Hardy and Wright, An Introduction to the Theory of Numbers, Chapter X). It is stated here without proof; a short program in this lesson's code checks it for every fraction x = p/q between 0 and 1 with q < 120. The factor 2 matters: with the weaker bound 1/s2, 4/9 qualifies for x = 13/30 (|13/30 − 4/9| = 1/90 < 1/81, though not 1/(2 · 81) = 1/162) but is not a convergent of 13/30, whose convergents are 0, 1/2, 3/7, 13/30.
How M4 is used in Shor's algorithm
This section only sets up the arithmetic; lesson Q4 derives the quantum part.
Shor's order-finding step for n = 21 chooses q (here q is Shor's power of 2, not a quotient), "the power of 2 with n2 ≤ q < 2n2" (Shor, arXiv v2, 1996, §5): 441 ≤ 512 < 882, so q = 512. A measurement returns an integer c with, for some d, |c/q − d/r| ≤ 1/(2q) (Shor's inequality (5.13)). Because r < n and q ≥ n2,
1/(2q) ≤ 1/(2n2) < 1/(2r2),
so Legendre's theorem applies: d/r, in lowest terms, is a convergent of c/q. Recovery rule used in this track: expand c/q and take the last convergent whose denominator is less than n. Shor's own wording is "rounding c/q to the nearest fraction having a denominator smaller than n", found "by using a continued fraction expansion of c/q" (§5); whenever Shor's inequality holds, the two give the same fraction.
Proof. (1) Two different fractions h/k and h′/k′ with k, k′ < n differ by at least 1/(kk′) > 1/n2 ≥ 1/q, so at most one fraction with denominator below n lies within 1/(2q) of c/q. When Shor's inequality holds, that fraction is d/r, and it is then also the nearest such fraction, as Shor's rule asks. (2) By Legendre's theorem d/r is a convergent of c/q. Each convergent is strictly closer to c/q than the one before (Hardy and Wright, Chapter X; stated here without proof and checked by this lesson's code). So any later convergent with denominator below n would be a second fraction within 1/(2q) of c/q, which is impossible. So d/r is the last convergent with denominator below n. This lesson's code confirms it for every n from 3 to 39 and every outcome c.
With a = 2, n = 21 (order 6):
- c = 427: 427/512 = [0; 1, 5, 42, 2]; the last convergent with denominator < 21 is 5/6. Candidate r = 6, and 26 = 64 ≡ 1 (mod 21). Found.
- c = 171: last convergent with denominator < 21 is 1/3. The candidate 3 fails: 23 mod 21 = 8. Here d = 2 and r = 6 share a factor, so 2/6 reduced to 1/3. Shor: "if d happens to be relatively prime to r, this will give us r" (§5). Q4 handles this case.
With r = 6 even, gcd(23 − 1, 21) = 7 and gcd(23 + 1, 21) = 3 give the factors of 21. Q4 derives why.
Pause and recall optional
When does a have an inverse mod n?
Show answer
Exactly when gcd(a, n) = 1.
What is the order of 7 mod 15?
Show answer
4: the powers are 1, 7, 4, 13, 1.
Exercises M4
Easy
M4.1. Find 5−1 mod 21 by Euclid's algorithm. Then use gcd to explain why 9 and 14 have no inverse mod 21.
Show solution, M4.1
21 = 4 · 5 + 1, 5 = 5 · 1 + 0, so gcd(5, 21) = 1 and 1 = 21 − 4 · 5.
Then 5 · (−4) ≡ 1, and −4 ≡ 17 (mod 21). Check: 5 · 17 = 85 = 4 · 21 + 1.
For 9: 21 = 2 · 9 + 3, 9 = 3 · 3 + 0, so gcd(9, 21) = 3. For 14: 21 = 1 · 14 + 7, 14 = 2 · 7 + 0, so gcd(14, 21) = 7.
A number with gcd > 1 has no inverse, because 9x − 21k = 1 would make 3 divide 1 (and 14x − 21k = 1 would make 7 divide 1).
Medium
M4.2. Find the order of 5 mod 21 by listing powers. Check that it divides φ(21).
Show solution, M4.2
5k mod 21 for k = 0, 1, 2, …: 1, 5, 25 mod 21 = 4, 20, 100 mod 21 = 16, 80 mod 21 = 17, 85 mod 21 = 1.
The order is 6, and 6 divides φ(21) = 12.
Medium
M4.3. Expand 85/512 as a continued fraction, list its convergents, and give the last one with denominator less than 21. What candidate order does it give for a = 2, n = 21?
Show solution, M4.3
85 = 0 · 512 + 85; 512 = 6 · 85 + 2; 85 = 42 · 2 + 1; 2 = 2 · 1 + 0. So 85/512 = [0; 6, 42, 2]. Recurrence, starting from h−2 = 0, h−1 = 1, k−2 = 1, k−1 = 0:
- a0 = 0: h0 = 0 · 1 + 0 = 0, k0 = 0 · 0 + 1 = 1 → 0/1.
- a1 = 6: h1 = 6 · 0 + 1 = 1, k1 = 6 · 1 + 0 = 6 → 1/6.
- a2 = 42: h2 = 42 · 1 + 0 = 42, k2 = 42 · 6 + 1 = 253 → 42/253.
- a3 = 2: h3 = 2 · 42 + 1 = 85, k3 = 2 · 253 + 6 = 512 → 85/512.
The last convergent with denominator < 21 is 1/6, so the candidate order is r = 6, and 26 = 64 ≡ 1 (mod 21): found. The outcome is a valid one: |85/512 − 1/6| = 2/3,072 = 1/1,536 ≤ 1/(2 · 512) = 1/1,024, so Shor's inequality (5.13) holds with d = 1.
Hard
M4.4. Prove that the order of a mod n divides φ(n). Then list the orders this allows for a unit mod 15, and for each one either find a unit with that order or show that none exists.
Show solution, M4.4
Euler's theorem gives aφ(n) ≡ 1, and ak ≡ 1 holds exactly when the order r divides k (both derived in M4), so r divides φ(n).
For n = 15, φ(15) = 8, so the possible orders are 1, 2, 4, 8. Units mod 15: 1, 2, 4, 7, 8, 11, 13, 14.
Order 1: 1. Order 2: 4 (16 ≡ 1), also 11 and 14. Order 4: 2 (2, 4, 8, 16 ≡ 1), also 7, 8, 13.
Order 8: none; every unit mod 15 has a4 ≡ 1 (the list shows every unit's order is 1, 2 or 4), so 8 is a possible divisor that no unit reaches.
What this lesson does not cover
Abstract vector spaces, infinite dimensions, the spectral theorem in general form, mixed states beyond the one-ket ρ (Q1 and Q6 take them further), group theory beyond "order" (NC Appendix 2 is the reference), and a proof of Legendre's theorem. The real difficulty is not any single calculation here but keeping the conventions straight later: qubit ordering in ⊗, which argument of ⟨·|·⟩ is conjugated, and which recovery rule is used for continued fractions. Lessons Q1 to Q4 use the conventions fixed in this lesson.
Sources
- M1–M3: Nielsen and Chuang, Quantum Computation and Quantum Information, 10th anniversary edition (Cambridge University Press, 2010; DOI 10.1017/CBO9780511976667): §2.1 (linear algebra), §2.1.7 (tensor products), §2.2.3 and §2.2.5 (measurement, projective measurement), §2.2.7 (phase), §2.2.8 (composite systems), §2.4 (density operator).
- M4: Nielsen and Chuang, Appendix 4 (number theory). P. W. Shor, "Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer", arXiv quant-ph/9508027v2 (29 Jan 1996), SIAM J. Comput. 26 (1997), DOI 10.1137/S0097539795293172, §5: the choice of q, inequality (5.13), the continued-fraction recovery and the gcd(d, r) = 1 condition (quoted above). G. H. Hardy and E. M. Wright, An Introduction to the Theory of Numbers, Chapter X (continued fractions; cited by Shor §5).
- Every number in this lesson is computed in this lesson's code (Python 3.13, numpy 2.5.3).