Circuit overview
The circuit uses 3 phase-estimation qubits (q) and 4 ancilla qubits (a) initialized to |1⟩ (via x a[3]). Applying Hadamard to q creates superposition, then the controlled modular exponentiation encodes the period into the phase, and the inverse QFT extracts it.
Shor's Algorithm (N=15, a=7)
Reading the results
After running, the ancilla registera is measured. The meaningful outcomes and their interpretation:
From the order r=4, compute the factors using GCD:
- gcd(7^(4/2) − 1, 15) = gcd(48, 15) = 3
- gcd(7^(4/2) + 1, 15) = gcd(50, 15) = 5
Shor’s algorithm is probabilistic. If you measure a state other than
010 or 110, the run is discarded and repeated. The useful outcomes occur with roughly 50% probability.How the circuit works
1
Initialize
Three phase-estimation qubits
q start in |0⟩. Four ancilla qubits a start in |0001⟩ (the number 1 mod 15), set by x a[3].2
Superposition
Hadamard gates
h q put all three phase-estimation qubits into equal superposition, creating a uniform sum over all 8 input states.3
Controlled modular exponentiation
modexp(q, a) applies controlled-U^(2^i) operations. Each controlled step multiplies the ancilla register by 7^(2^i) mod 15 when the corresponding control qubit is |1⟩. This encodes the periodic structure of 7^x mod 15 into entangled phase.4
Inverse QFT
inv_qft(q) extracts the period from the phase register. It applies a 3-qubit inverse quantum Fourier transform using Hadamard gates and controlled-phase rotations.5
Measure and interpret
Measure
a. Convert the 3-bit result to a binary fraction to get the phase φ, then use continued fractions to recover r. Finally, compute gcd(a^(r/2)±1, N) to obtain the prime factors.