Quantum Computing Workshop

Shor's Algorithm

Points
0 / 5

While Shor's algorithm can break discrete log and RSA, as an example we will focus on RSA, even though in the long run it will require more quantum resources. But a quantum computer does not directly break RSA. So what does it break? Let's find out!

concept

Toy RSA with N = 55

RSA starts by multiplying two primes. For a toy example, choose p = 5 and q = 11, so N = p * q = 55. The totient function counts the numbers below N that are coprime to N. For two primes, phi(N) = (p - 1)(q - 1), so phi(55) = 40.

GCD means greatest common divisor: the biggest whole number that divides two numbers. If gcd(e, phi(N)) = 1, then e works as the public exponent. Then d is the inverse of e modulo phi(N), which means e * d leaves remainder 1 when divided by phi(N).

p = 5, q = 11
N = p * q = 55
phi(N) = (p - 1)(q - 1) = 4 * 10 = 40
choose e = 3 because gcd(3, 40) = 1
find d so e * d = 1 mod 40
d = 27 because 3 * 27 = 81 = 1 mod 40
public key:  (N, e) = (55, 3)
private key: (N, d) = (55, 27)
try it

Build and Play with Toy RSA

Private d
27
N = 55
phi(N) = 40
gcd(e, phi) = 1
valid e samples: 3, 7, 9, 11, 13, 17, 19, 21, 23, 27

Encrypt m or decrypt c to see toy RSA move.

exercise

Quantum Attacker Box

You are a quantum attacker, and you managed to find the primes for N. The public key is (N, e) = (187, 7), the ciphertext is c = 15, and your quantum routine found p = 11 and q = 17. Decrypt c by filling in the modular exponentiation.

Fill it as: c to the power of d mod N decrypts to m.

concept

Modular Exponentiation and Period

Modular exponentiation means raising a base to powers, then keeping only the remainder after division by N. For N = 15 and base a = 2, the values repeat.

f(x) = 2^x mod 15
x:    0, 1, 2, 3, 4, 5, 6, 7, ...
f(x): 1, 2, 4, 8, 1, 2, 4, 8, ...
period r = 4
concept

From Period to Factors

Once the period r is even, compute a^(r/2). Then compare that number one step below and one step above against N using gcd. In the N = 15 example, the period is r = 4, so a^(r/2) = 2^2 = 4. The gcd calls reveal the factors.

a^(r/2) = 2^(4/2) = 2^2 = 4
gcd(4 - 1, 15) = gcd(3, 15) = 3
gcd(4 + 1, 15) = gcd(5, 15) = 5
factors found: 3 and 5
exercise

Factor 55 from a Period

Now do the same move for N = 55 and base a = 2. Fill in the period, the value of a^(r/2), and the two factors.

concept

Qubit States Times Gate Matrices

A one-qubit state is a two-entry vector. The top entry is the coefficient of |0>, and the bottom entry is the coefficient of |1>. A gate is a 2 by 2 matrix. Applying the gate means multiplying the matrix by the state vector.

|psi> = alpha|0> + beta|1> = [alpha, beta]^T

[a b] [alpha] = [a alpha + b beta]
[c d] [ beta]   [c alpha + d beta]

X|0> = |1>
Z(alpha|0> + beta|1>) = alpha|0> - beta|1>
H|0> = (|0> + |1>) / sqrt(2)
R_theta(alpha|0> + beta|1>) = alpha|0> + e^(i theta) beta|1>
try it

Gate Multiplication Sandbox

Normalized state
|alpha|^2 + |beta|^2 = 1

Pick alpha and beta for alpha|0> + beta|1>, choose whether each coefficient is real or imaginary, then apply a gate. This lab shows the multiplication step directly.

Beta magnitude
0.707

HMixes |0> and |1>; it turns basis states into equal superpositions.

Matrix
H = (1 / sqrt(2)) [[1,  1],
                    [1, -1]]

H ~= [[0.707,  0.707],
      [0.707, -0.707]]
State before and after
|0>|1>
beforeafter
Computation
1/sqrt(2)1/sqrt(2)1/sqrt(2)-1/sqrt(2)
x
0.7070.707
=
10
The column vector is the transposed state vector: top is |0>, bottom is |1>.
input  = [0.707, 0.707]^T
output = [(0.707)(0.707) + (0.707)(0.707),
          (0.707)(0.707) + (-0.707)(0.707)]^T
       = [1, 0]^T
state  = 1|0> + 0|1>
concept

Phase Is Where Qubits Start Feeling Like Waves

The R_theta gate changes the phase between |0> and |1>. It leaves the |0> part alone, but rotates the |1> part by an angle theta. That relative phase is one of the special things about qubits: amplitudes are not just amounts, they also carry wave-like direction.

A useful analogy is sound. When the peaks of a bass line and a drum hit line up, the sound can feel stronger. When waves are out of phase, they can partially cancel and feel weaker. Quantum algorithms use this same kind of constructive and destructive behavior, but with probability amplitudes.

R_theta(alpha|0> + beta|1>)
  = alpha|0> + e^(i theta) beta|1>

theta changes the relative phase between the two parts.
Later, good phases line up; bad phases cancel.
exercise

Apply H to |+i>

Before the gate, |+i> means:

|+i> = (1 / sqrt(2))|0> + (i / sqrt(2))|1>

H = (1 / sqrt(2)) [[1, 1], [1, -1]]

Find H|+i>.
concept

H, CNOT, Bell States, and Entanglement

H gate

H makes a qubit branch into an equal superposition. On the first qubit, it turns |0x> into a blend of |0x> and |1x>.

CNOT gate

CNOT has a control qubit and a target qubit. If the control is 1, it flips the target. If the control is 0, it does nothing.

Entanglement

A Bell state is entangled: the two-qubit state cannot be split into one independent state for qubit A and one for qubit B.

Start with |00>
Apply H to the first qubit:
(|00> + |10>) / sqrt(2)
Apply CNOT with first qubit as control:
(|00> + |11>) / sqrt(2)
This is a Bell state.
try it

Fixed Circuit: H then CNOT

Choose the starting two-qubit basis state. The circuit always applies H to the first qubit, then CNOT with the first qubit as control and the second qubit as target.

|00>
H on q0
(|00> + |10>) / sqrt(2)
CNOT
(|00> + |11>) / sqrt(2)
Output: Bell state Phi+
q0: -- H -- control --
              |
q1: -------- target  --

Input |00> -> (|00> + |11>) / sqrt(2)
concept

Four Qubits Can Represent 0 Through 15

One classical bit has 2 possibilities. Four classical bits have 2^4 = 16 possible strings, from 0000 to 1111. A four-qubit register uses the same labels, and applying H to each qubit creates an equal superposition over all 16 labels at once.

H on each qubit:
|0000> -> (1/4)(|0000> + |0001> + ... + |1111>)

Decimal labels:
|0> + |1> + |2> + |3> + |4> + |5> + |6> + |7> + |8> + |9> + |10> + |11> + |12> + |13> + |14> + |15>

Binary labels:
|0000> + |0001> + |0010> + |0011> + |0100> + |0101> + |0110> + |0111> + |1000> + |1001> + |1010> + |1011> + |1100> + |1101> + |1110> + |1111>
concept

First Register, Second Register, and Modular Exponentiation

In the Shor toy example, the first register stores x. The second register stores the function value 2^x mod 15. Once the function is computed, the registers are entangled: knowing the second register narrows down which x values could be in the first register.

first register:  x
second register: f(x) = 2^x mod 15

x =  0 (0000): 2^0 mod 15 = 1 (0001)
x =  1 (0001): 2^1 mod 15 = 2 (0010)
x =  2 (0010): 2^2 mod 15 = 4 (0100)
x =  3 (0011): 2^3 mod 15 = 8 (1000)
x =  4 (0100): 2^4 mod 15 = 1 (0001)
x =  5 (0101): 2^5 mod 15 = 2 (0010)
x =  6 (0110): 2^6 mod 15 = 4 (0100)
x =  7 (0111): 2^7 mod 15 = 8 (1000)
x =  8 (1000): 2^8 mod 15 = 1 (0001)
x =  9 (1001): 2^9 mod 15 = 2 (0010)
x = 10 (1010): 2^10 mod 15 = 4 (0100)
x = 11 (1011): 2^11 mod 15 = 8 (1000)
x = 12 (1100): 2^12 mod 15 = 1 (0001)
x = 13 (1101): 2^13 mod 15 = 2 (0010)
x = 14 (1110): 2^14 mod 15 = 4 (0100)
x = 15 (1111): 2^15 mod 15 = 8 (1000)
concept

Total State Before Measuring

The total state pairs each first-register value with the matching second-register function value. First write it as the function, then replace the function with the actual values.

(1/4)(
  |0> |2^0 mod 15>
  |1> |2^1 mod 15>
  |2> |2^2 mod 15>
  |3> |2^3 mod 15>
  |4> |2^4 mod 15>
  |5> |2^5 mod 15>
  |6> |2^6 mod 15>
  |7> |2^7 mod 15>
  |8> |2^8 mod 15>
  |9> |2^9 mod 15>
  |10> |2^10 mod 15>
  |11> |2^11 mod 15>
  |12> |2^12 mod 15>
  |13> |2^13 mod 15>
  |14> |2^14 mod 15>
  |15> |2^15 mod 15>
)

which equals

(1/4)(
  |0> |1>
  |1> |2>
  |2> |4>
  |3> |8>
  |4> |1>
  |5> |2>
  |6> |4>
  |7> |8>
  |8> |1>
  |9> |2>
  |10> |4>
  |11> |8>
  |12> |1>
  |13> |2>
  |14> |4>
  |15> |8>
)
exercise

Group by Second Register String

Group the first-register x values by the second-register output string. Use decimal x values, separated however you like.

concept

Measuring the Second Register Reveals the Comb

If you measure the second register, you only see one of four values: 0001, 0010, 0100, or 1000. Once that happens, the first register collapses to the matching x values. Those surviving x values are spaced by 4, which is the period.

measure second register = |0001>
first register left alive: |0> + |4> + |8> + |12>

measure second register = |0010>
first register left alive: |1> + |5> + |9> + |13>

measure second register = |0100>
first register left alive: |2> + |6> + |10> + |14>

measure second register = |1000>
first register left alive: |3> + |7> + |11> + |15>

Each row is a comb with spacing 4.
concept

Four Numbers Times the DFT Matrix

The discrete Fourier transform takes a signal and rewrites it in terms of frequencies. For four numbers, it is just a matrix multiplication. Repeated patterns become visible as strong frequency outputs.

signal = [s0, s1, s2, s3]^T

DFT_4(signal) = (1/2) *
[[1,  1,  1,  1],
 [1,  i, -1, -i],
 [1, -1,  1, -1],
 [1, -i, -1,  i]]
* [s0, s1, s2, s3]^T
concept

QFT Is the Same Fourier Matrix as a Quantum Operator

The quantum Fourier transform is a quantum operator: a unitary matrix, just like the gates above. It is the DFT matrix acting on amplitudes. The QFT changes a state from a signal-like view into a frequency-like view, so periodic structure shows up as peaks.

Generic QFT formula:
QFT_N |x> = (1 / sqrt(N)) sum_{y=0}^{N-1} e^(2 pi i x y / N) |y>

For the 4 x 4 case:
QFT_4 |x> = (1 / 2) sum_{y=0}^{3} e^((i pi / 2) x y) |y>

Same idea: signal pattern in, frequency peaks out.
exercise

Ultimate Phase Check

Use the comb |0> + |4> + |8> + |12> inside a 16-point Fourier transform. The phase for each x is e^(2 pi i x y / 16). For a good y, the phases compound. For a bad y, they cancel.

phase rule: e^(2 pi i x y / 16)
comb x values: 0, 4, 8, 12

Write the four phases for y = 4.
Then write the four phases for y = 5.

Back to workshops