Shor’s algorithm finds the prime factors of an integer by reducing the problem to period-finding, which a quantum computer solves exponentially faster than any known classical method. This example factors N=15 using base a=7: it finds the order r such that 7^r ≡ 1 (mod 15), then computes the factors using the greatest common divisor.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/qasm-playground/llms.txt
Use this file to discover all available pages before exploring further.
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:
Measured a | Binary fraction | Phase φ | Order r | Factors |
|---|---|---|---|---|
010 | 0.010 = 0.25 | 1/4 | 4 | 3 and 5 |
110 | 0.110 = 0.75 | 3/4 | 4 | 3 and 5 |
| other | — | — | reject | retry |
- 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
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].Superposition
Hadamard gates
h q put all three phase-estimation qubits into equal superposition, creating a uniform sum over all 8 input states.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.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.