Interactive Notes

Quantum Algorithms:
Deutsch & Shor

From the first quantum algorithm in history to the one that threatens modern cryptography.

Section 01

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
xf(x)
00
10
CONSTANT 1
xf(x)
01
11
BALANCED (ID)
xf(x)
00
11
BALANCED (NOT)
xf(x)
01
10
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:

U_f |xโŸฉ|yโŸฉ = |xโŸฉ|y โŠ• f(x)โŸฉ
  where โŠ• is addition modulo 2 (XOR)

x yxy โŠ• f(x) 0 000 โŠ• f(0) = f(0) 0 101 โŠ• f(0) = fฬ„(0) 1 010 โŠ• f(1) = f(1) 1 111 โŠ• f(1) = fฬ„(1)

Invertible?
Yes! Apply U_f twice: |xโŸฉ|yโŸฉ โ†’ |xโŸฉ|yโŠ•f(x)โŸฉ โ†’ |xโŸฉ|yโŠ•f(x)โŠ•f(x)โŸฉ = |xโŸฉ|yโŸฉ โœ“ (since aโŠ•a = 0)

The Deutsch Circuit

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.

|0โŸฉ
H
U_f
H
M
"0" if f(0)=f(1)
"1" if f(0)โ‰ f(1)
|1โŸฉ
H
U_f
(ancilla, not measured)

Step-by-Step Calculation

Initial state:
|0โŸฉ|1โŸฉ
After HโŠ—H:
|+โŸฉ|โˆ’โŸฉ = 1/2(|0โŸฉ+|1โŸฉ)(|0โŸฉโˆ’|1โŸฉ) = 1/2(|00โŸฉโˆ’|01โŸฉ+|10โŸฉโˆ’|11โŸฉ)

After U_f:
|0,f(0)โŸฉ + |1,f(1)โŸฉ โˆ’ |0,fฬ„(0)โŸฉ โˆ’ |1,fฬ„(1)โŸฉ (unnorm.)
Regroup:
= |0โŸฉ(|f(0)โŸฉโˆ’|fฬ„(0)โŸฉ) + |1โŸฉ(|f(1)โŸฉโˆ’|fฬ„(1)โŸฉ)
Key trick:
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:
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)
  1. Choose a random x โˆˆ โ„ค, x < n.
         If gcd(x,n) > 1 โ†’ x is already a factor of n! (Lucky stop.)
  2. Find the order r: the smallest r such that xr mod n = 1.   (โŸต THE HARD STEP)
  3. If r is odd, or if (xr/2+1) mod n = 0 โ†’ go back to step 1.
  4. 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 โ‘ฃ?

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) =
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 =
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):
  1. Choose N > Qยฒ (for the continued fraction step to work reliably).
  2. Compute the DFT of the length-N sequence.
  3. Find a peak at position m; compute m/N and extract P via continued fractions.
  4. 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

โ‘  superposition โ‘ก oracle U_f โ‘ข QFT + measure logโ‚‚N qubits โ‹ฎ |0โŸฉ |0โŸฉ H H โŠ— ยทยทยท b qubits โ‹ฎ |0โŸฉ |0โŸฉ U f QFT m m x a mod n โ‘  โ‘ก โ‘ข

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)
First register:
1/โˆšM (|aโŸฉ + |a+rโŸฉ + |a+2rโŸฉ + ... + |a+(M-1)rโŸฉ)
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?

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 โ‘ก.

Case 1: P divides N exactly (N = PยทM)

Input state:
1/โˆšM(|aโŸฉ + |a+rโŸฉ + |a+2rโŸฉ + ... + |a+(Mโˆ’1)rโŸฉ) = 1/โˆšM ฮฃd=0Mโˆ’1 |a+drโŸฉ
QFT output:
ฮฃq=0Nโˆ’1 Xq |qโŸฉ   where   Xq = 1/โˆšN ฮฃk=0Nโˆ’1 xk ฯ‰Nโˆ’kq
Compute Xq:
Xq = 1/โˆš(NM) ฮฃd=0Mโˆ’1 ฯ‰N(a+dr)q = 1/โˆš(NM) ฯ‰Naq ฮฃd=0Mโˆ’1 (ฯ‰Nrq)d
Let ฮฑ = ฯ‰Nrq:
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:

|Xq| โ‰ˆ
1/โˆš(NM) |ฮฃd=0Mโˆ’1 ฮฑd| = 1/โˆš(NM) |1 โˆ’ ฮฑM|/|1 โˆ’ ฮฑ|   where ฮฑ = ej2ฯ€rq/N
= ratio of sinc:
โ‰ˆ 1/โˆšN |sin(ฯ€Mrq/N)| / |sin(ฯ€rq/N)|

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:
Section 08

Complexity of Shor's Algorithm

Summary of Resources
Let b = โŒˆlogโ‚‚nโŒ‰ (number of bits to represent n). Then:
ComponentQubitsGates QFT (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)
Then:
xa = C0aโ‚€ ยท C1aโ‚ ยท ... ยท Cbโˆ’1abโˆ’1 (mod n)
Circuit:
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
Shor's Quantum Algorithm
O(bยณ)
Polynomial!   ~2b qubits needed

Example:
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
01
17
24
313
41 โ† period!
57
โ‹ฎโ‹ฎ

The period is r = 4. The sequence 1, 7, 4, 13 repeats every 4 steps.

After applying HโŠ—10 and then Uf, the state is:

1/โˆš1024 ฮฃk=01023 |kโŸฉ|7k mod 15โŸฉ = 1/โˆš1024 (|0โŸฉ|1โŸฉ + |1โŸฉ|7โŸฉ + |2โŸฉ|4โŸฉ + |3โŸฉ|13โŸฉ + |4โŸฉ|1โŸฉ + ...)

Stage โ‘ก โ€” Measure Second Register

Say we measure f(ยท) = 4. The first register collapses to all k where 7k mod 15 = 4, i.e. k = 2, 6, 10, 14, ..., (2 + 4j):

1/โˆš256(|2โŸฉ + |6โŸฉ + |10โŸฉ + |14โŸฉ + ... + |1022โŸฉ)   (M = 1024/4 = 256 terms)

Stage โ‘ข โ€” QFT and Measure

Since P = 4 divides N = 1024 exactly, M = 256 and the peaks are at q = 0, 256, 512, 768.

Peaks at:
m = 0, 256, 512, 768   (each with probability 1/4 = 25%)
Assume m = 768:
m/N = 768/1024 = 3/4 โ†’ โ„“/r = 3/4 โ†’ r = 4 โœ“

Extract Factors

r = 4 (even โœ“):
xr/2 = 7ยฒ = 49:
49 mod 15 = 4   (so (xr/2+1) mod 15 = 5 โ‰  0 โœ“)
gcd(49โˆ’1, 15):
gcd(48, 15) = gcd(15, 48 mod 15) = gcd(15, 3) = 3
gcd(49+1, 15):
gcd(50, 15) = gcd(15, 50 mod 15) = gcd(15, 5) = 5

Result:
15 = 3 ร— 5 โœ“
Interactive: Run Shor's Algorithm
n: x:
Ex 9.1Interpreting the measurement result

For n=15, x=7, r=4, N=1024, suppose we measure m = 256 from the QFT. What fraction m/N simplifies to, and what is r?

m/N = 256/1024 = โ†’ r =