Deutsch's Algorithm โ The First Quantum Algorithm in History
In 1985, David Deutsch published the first quantum algorithm ever devised. It is beautifully simple,
yet it captures the essence of quantum advantage: solving a problem with one
query to a black box where a classical computer would need two.
This algorithm directly inspired Shor's factoring algorithm a decade later.
The Problem
You are given a black-box function f : {0,1} โ {0,1}. You cannot see inside the box โ you can only feed it an input and read the output. Your task: determine whether f(0) = f(1) (constant) or f(0) โ f(1) (balanced), using as few calls to f as possible.
There are exactly four possible functions of this type:
CONSTANT 0
x
f(x)
0
0
1
0
CONSTANT 1
x
f(x)
0
1
1
1
BALANCED (ID)
x
f(x)
0
0
1
1
BALANCED (NOT)
x
f(x)
0
1
1
0
Classical approach: You must call f twice โ once with input 0, once with input 1 โ and compare the results. There is no shortcut: after a single call, you simply don't have enough information to decide.
The Quantum Oracle Uf
The quantum version of the black box acts on two qubits: an input register |xโฉ and an ancilla register |yโฉ. It computes:
The circuit uses a clever trick: if we put the ancilla qubit in the state |โโฉ = (|0โฉโ|1โฉ)/โ2, the oracle "kicks back" the phase of f(x) into the input register โ without ever collapsing it.
Note |f(x)โฉโ|fฬ(x)โฉ = ยฑ(|0โฉโ|1โฉ) = ยฑ|โโฉยทโ2, so the sign is (โ1)f(x).
Therefore state โ (โ1)f(0)|0โฉ + (โ1)f(1)sup>|1โฉ, times |โโฉ.
If f(0)=f(1):
First register โ (โ1)f(0)(|0โฉ+|1โฉ) = |+โฉ โ After H: |0โฉ โ measure "0"
If f(0)โ f(1):
First register โ (โ1)f(0)(|0โฉโ|1โฉ) = |โโฉ โ After H: |1โฉ โ measure "1"
The quantum advantage in one sentence: By putting the input into superposition, U_f evaluates f(0) and f(1) simultaneously. The subsequent Hadamard gate interferes these two evaluations: they add constructively for the correct answer and cancel for the wrong one. This is the blueprint for every quantum algorithm that follows โ including Shor's.
One oracle call vs two: Classically, you need 2 calls. Quantum: exactly 1 call suffices. This is a provable quantum advantage โ not just a speedup in practice, but a fundamental difference in query complexity.
Select a function f and see what happens at each step of the circuit.
Choose f:
Ex 1.1Phase kickback
When the ancilla is in state |โโฉ and we apply Uf, the result is (โ1)f(x)|xโฉ|โโฉ. This is called "phase kickback." What is the phase when f(x) = 1?
Phase factor:
(โ1)f(x). If f(x)=1 then (โ1)1 = โ1.
Ex 1.2How many oracle calls?
Classically, how many evaluations of f are needed in the worst case to determine if f is constant or balanced?
Answer:
Section 02
Factorization = Order Finding
Shor's algorithm (1994) is the most famous quantum algorithm. Its primary application: factor large integers efficiently. This threatens RSA encryption, which relies on the difficulty of factoring numbers that are products of two large primes.
Why factoring is hard classically: To factor n, the naive approach tries all primes less than n โ roughly โn candidates. For a 1024-bit RSA number, that's about 2>512 trials. The best known classical algorithm still runs in sub-exponential but super-polynomial time โ impractical for large n.
The Key Insight: Reduction to Order Finding
Shor's brilliant observation is that factoring reduces to finding the "order" of an element in modular arithmetic. This connection transforms an apparently number-theoretic problem into a signal processing problem.
The Algorithm (4 steps)
Choose a random x โ โค, x < n.
If gcd(x,n) > 1 โ x is already a factor of n! (Lucky stop.)
Find the order r: the smallest r such that xr mod n = 1. (โต THE HARD STEP)
If r is odd, or if (xr/2+1) mod n = 0 โ go back to step 1.
Compute gcd(xr/2โ1, n) and gcd(xr/2+1, n). These are non-trivial factors of n!
Why Does This Work?
Start:
xr mod n = 1 โน xr = qยทn + 1 โน xr โ 1 = qยทn
If r even:
(x(r/2) โ 1)(x(r/2) + 1) = qยทn
Therefore:
n divides the product (x(r/2) -1)(x(r/2) +1).
If neither factor is divisible by all of n, then gcd extracts the factor we want.
Why not trivial?
n cannot have all its factors inside xr/2โ1 alone, because that would mean xr/2 mod n = 1,
contradicting r being the smallest such integer (by definition of order).
Similarly for x(r/2)+1 (the step โข condition eliminates this case).
gcd is cheap! The Euclidean algorithm computes gcd(a,b) in O(logยฒ(n)) time โ polynomial. So steps โ , โข, โฃ are all easy. The entire difficulty concentrates in step โก: finding the order r. That's exactly what Shor's quantum algorithm solves.
Enter n and x to trace through the factoring algorithm. (Works for small numbers.)
n:x:
Ex 2.1Why is step โข needed?
Step โข says: if r is odd, restart. Why can't we use an odd r in step โฃ?
Because x(r/2) needs to be an integer. If r is odd, r/2 is not an integer and x(r/2) mod n is undefined. We need r to be even so that x(r/2) โ โค.
Ex 2.2Verify the factoring of 15
For n=15 and x=7: the order is r=4 (meaning 74 mod 15 = 1). Compute gcd(7ยฒโ1, 15) = gcd(48, 15). What factor do you get?
gcd(48,15) =
48 = 3ร16, 15 = 3ร5. gcd(48,15) = 3. And gcd(50,15) = 5. So factors of 15 are 3 and 5. โ
Section 03
Order Finding = Period Finding
The order problem has a beautiful signal-processing interpretation. Let's define the function:
The Modular Exponentiation Function
f(โ) = xโ mod n โ = 0, 1, 2, ...
This function is periodic with period r:
f(โ) = xโ mod n = x(โ+r) mod n = f(โ+r) for all โ
Because xr mod n = 1, so multiplying by xr changes nothing.
The chain of reductions:
Factoring n
โก Order finding of x mod n (for random x)
Order finding
โก Period finding of f(โ) = xโ mod n
Period finding
โก Finding peaks in the Fourier transform of f
Modular exponentiation f(โ) = xโ mod n โ observe the periodic pattern.
n:x:
Ex 3.1Find the period
Compute f(โ) = 2โ mod 7 for โ = 0,1,2,3,4,5. What is the period r?
r =
20 =1, 21 =2, 22 =4, 23 =1 (mod 7). So r=3 (the sequence 1,2,4 repeats).
Section 04
Period Finding with Fourier Transforms
You already know this idea from signal processing: a periodic signal has a spectrum with sharp lines. We'll use the Discrete Fourier Transform (DFT) to detect the period.
Continuous time analogy
A continuous signal with period T has a Fourier transform with lines at frequencies f0 = 1/T, 2f0, 3f0, ... This is the Fourier series. For a discrete signal of length N with period P (where P < N):
Key DFT Result
If xk = xk+P (periodic with period P), and N = PยทM (P divides N exactly), then the DFT has peaks exactly at:
m = 0, M, 2M, 3M, ..., (Pโ1)M
i.e. m = โ ยท (N/P) for โ โ โค
So if we observe a peak at position m, we know:
m/N โ โ/P โน P โ Nยทโ/m
By simplifying the fraction m/N we can recover P (= the denominator after simplification)!
What if P does not divide N exactly? The peaks appear near โยทN/P but are slightly broadened (like a sinc function). In that case, we find an approximation m/N โ โ/P, and use the continued fraction algorithm to extract the exact denominator P from the rational approximation.
Multiple measurements to handle common factors
When we simplify m/N, we might lose factors. For example, if N=240, P=20, and we see m=12 (corresponding to โ=1), then m/N = 12/240 = 1/20 โ. But if โ=2 we'd see m=24, and 24/240 = 1/10 โ we recover only a factor of P=20, not P itself.
Solution: collect multiple measurements mโ, mโ, ... Each gives a (possibly partial) factor of P. The true period P = lcm(Pโ, Pโ, ...) where Pi is the denominator after simplifying mi/N. After a few measurements we recover P with high probability.
DFT of the periodic sequence f(โ) = xโ mod n. The peaks reveal the period.
n:x:N:
Section 05
Classical Period Finding โ Why It Fails
The algorithm using DFT to find the period looks promising. But let's check its classical complexity.
Classical Period Finding Algorithm
Given a sequence xk with period P < Q (where Q is a known upper bound on P):
Choose N > Qยฒ (for the continued fraction step to work reliably).
Compute the DFT of the length-N sequence.
Find a peak at position m; compute m/N and extract P via continued fractions.
Verify: if xk+P = xk for all k, stop. Otherwise collect more measurements and take the lcm.
Why it's exponential classically
Step โก:
Classical DFT costs O(N logโN). If N is b bits โ N โ 2b โ cost = O(b ยท 2b). Exponential in b!
Step โข:
Even finding the peak requires examining N entries. Again exponential.
Example:
For RSA n = 1024 bits, we need N โ 22048. Storing the sequence alone requires 22048 entries. That's more atoms than in the observable universe!
Period finding with classical FT is completely impractical. The exponential blowup from having to store and process N โ 22b values makes this approach useless for large inputs. This is precisely where the Quantum Fourier Transform saves the day.
The QFT costs only O(bยฒ) gates on b qubits. Instead of computing N = 2b amplitudes explicitly, the quantum circuit implicitly operates on a superposition encoding all 2b values simultaneously. The cost is polynomial, not exponential.
Section 06
Order Finding with QFT โ Shor's Algorithm
Here is the actual quantum circuit. It has three stages, labeled โ โกโข in the notes.
Setup
Given x and n, choose N > nยฒ (so that N has at least logโ(nยฒ) = 2b bits, where b = โlogโnโ).
We need โ logโN โ 2b qubits for the first register (the frequency register).
We need โlogโnโ = b qubits for the second register (to store xโ mod n).
The Circuit
Stage โ โ Create Superposition and Apply Oracle
Start:
|0โฉโlogโN โ |0โฉโb
Apply HโlogโN
on register 1:
1/โN ฮฃk=0Nโ1 |kโฉ|0โฉ
Apply U_f:
1/โN ฮฃk=0Nโ1 |kโฉ|xk mod nโฉ (quantum parallelism: all values computed at once!)
The function f(k) = xk mod n is periodic with period r (the order we want to find). So the second register cycles through the values 1, x, xยฒ, ..., xrโ1, 1, x, ... repeatedly.
Stage โก โ Measure the Second Register
Measure 2nd
register:
Say we get result f(a) = xa mod n (for some random a โ [0, r)).
Collapse:
The first register collapses to all k compatible with f(k) = f(a), i.e. k = a, a+r, a+2r, ..., a+(M-1)r. (M โ N/r terms)
This is a periodic quantum state! The first register is now a uniform superposition of arithmetic progression {a, a+r, a+2r, ...}. We can't read r directly because a is unknown. But its Fourier transform has peaks at multiples of N/r โ that's stage โข.
Stage โข โ Apply QFT and Measure
Apply QFT:
QFT maps the periodic state in the first register to a state concentrated near m = โยทN/r for integer โ.
Measure m:
Read an integer m โ โยทN/r with high probability.
Extract r:
m/N โ โ/r. Use the continued fraction algorithm to find the best rational approximation with denominator โค Q โ n โ this gives the denominator r.
Why N > nยฒ = Qยฒ? The continued fraction algorithm is guaranteed to recover the exact fraction โ/r from a rational approximation p/q if |p/q โ โ/r| < 1/(2rยฒ). Choosing N > rยฒ โค nยฒ ensures the approximation m/N is close enough for this to work.
Ex 6.1Why measure the second register?
After the oracle U_f, we have a superposition of all |kโฉ|f(k)โฉ. Why do we measure the second register before applying the QFT?
Measuring the second register collapses the first register to a purely periodic state โ a uniform superposition of {a, a+r, a+2r, ...}. Without this collapse, the first register is entangled with the second and the QFT would not give clean peaks at multiples of N/r.
Section 07
QFT of the First Register โ The Calculation
Let's work out what the QFT produces when applied to the periodic state from stage โก.
where ฯN = ej2ฯ/N, ฯNrq = ej2ฯrq/N.
If r = N/P = M (i.e. q is a multiple of M), then ฮฑ = ej2ฯq/M = ej2ฯยทinteger = 1.
Result:
|Xq| = 1/โP if q = โยท(N/P) for some integer โ, and |Xq| = 0 otherwise.
โน exactly P peaks of equal height 1/โP, at positions 0, N/P, 2N/P, ..., (Pโ1)ยทN/P
The QFT perfectly separates the P peaks when P | N. Each peak has amplitude 1/โP, so measurement probability 1/P for each โ a uniform distribution over P possible outcomes. Any outcome m = โยทN/P gives m/N = โ/P, from which we recover P.
Case 2: P does not divide N
In general, N/P is not an integer. The peaks are broadened by a sinc-like envelope:
This still has peaks near q = โยทN/P, and measurement gives m โ โยทN/P with high probability. The continued fraction method recovers r from the rational approximation m/N โ โ/r.
QFT output |Xq|ยฒ for periodic f(โ) = xโ mod n. Red lines mark the ideal peak positions โยทN/P.
x:n:N:
Ex 7.1Number of peaks
If the order of x mod n is r = P = 4 and we choose N = 1024 (so that P | N), how many distinct peaks appear in the QFT output? At what positions?
# peaks:
There are P=4 peaks, at q = 0, N/4=256, 2N/4=512, 3N/4=768. Each with amplitude 1/โP = 1/2 and probability 1/P = 25%.
Section 08
Complexity of Shor's Algorithm
Summary of Resources
Let b = โlogโnโ (number of bits to represent n). Then:
ComponentQubitsGatesQFT (register 1)logโN โ 2b (linear in b)(logโN)ยฒ โ 4bยฒ (poly in b)Modular exp Uflogโn = b + ancillaO((logโn)ยณ) = O(bยณ) (poly in b)Totalโ 2b qubitsO(bยณ) gates
Modular Exponentiation โ how it's implemented
Computing f(a) = xa mod n for a superposition of a values requires an efficient quantum circuit. We write a in binary: a = a0 + a1ยท2 + a2ยท4 + ... and precompute:
Precompute:
Ci = x2i mod n for i = 0, 1, ..., bโ1 (classical precomputation)
A sequence of b controlled modular multipliers, each conditioned on one bit ai.
Each controlled modular multiplier costs O(bยฒ) operations (using binary multiplication), so the total U_f circuit costs O(b ยท bยฒ) = O(bยณ). This dominates the QFT cost of O(bยฒ).
Comparison with Classical Algorithms
Classical โ Best Known
General Number Field Sieve: exp(O(b1/3 (log b)2/3)) Sub-exponential but still super-polynomial
RSA-1024 (b=1024 bits):
Classical: ~2(30 to 50) operations (impractical)
Shor's: ~bยณ โ 10โน operations with 2048 qubits (feasible with fault-tolerant hardware)
This is an exponential speedup. Not just a constant or polynomial improvement โ a genuine exponential gap. If cryptographically relevant quantum computers are built, RSA and related systems would be broken. This has driven massive investment in post-quantum cryptography.
Complexity comparison: classical O(2b) vs Shor's O(bยณ). Drag the slider to see how they diverge.
10
Section 09
Full Example: Factoring n = 15
Let's trace through Shor's algorithm step by step for n = 15, x = 7.
Setup
n = 15:
4 bits to represent 15 โ b = 4
Choose N:
N > nยฒ = 225 โ choose N = 1024 = 210 โ logโN = 10 qubits for register 1
Choose x:
x = 7, gcd(7, 15) = 1 โ (not a factor yet)
Register 2:
โlogโ15โ = 4 qubits (to store values 0โ14)
Stage โ โ The Periodic Function
โ
7โ mod 15
0
1
1
7
2
4
3
13
4
1 โ period!
5
7
โฎ
โฎ
The period is r = 4. The sequence 1, 7, 4, 13 repeats every 4 steps.