Shor's Algorithm Simulator
Run Shor's factoring algorithm on a simulated 12- or 15-qubit quantum computer: a real circuit with a real quantum Fourier transform finds the period and the factors, and the known failure cases show up too.
Choose the number to factor
The counting register after the quantum Fourier transform
Each bar is an outcome y of the counting register; y / 2t is close to a multiple of 1/r, which is how the period is recovered.
Show the Larzscript program that just ran
Run a calculation to see the exact program.
This is the real program the page sent to the Larzscript interpreter (compiled to WebAssembly) in your browser. The library it imports is the same code the automated tests check.
The idea: factoring by finding a period
To factor N, pick a base a coprime to N and consider f(x) = ax mod N. This function repeats: there is a smallest r (the period or order) with ar ≡ 1 (mod N). If r is even and ar/2 ≢ −1, then
Finding r is the hard part classically — and exactly what a quantum computer does well. Peter Shor's 1994 algorithm prepares a superposition of all x in a counting register, computes ax mod N into a second register, and applies the quantum Fourier transform. Interference concentrates the probability on multiples of 2t/r, so measuring the counting register reveals r.
Worked example, computed above
For N = 15 and a = 7 the simulated 12-qubit run finds period 4 and the factors 3 x 5, matching the brute-force period 4. The counting register has 8 qubits, so its 256 outcomes are spaced 1/256 apart, and the four tall peaks at multiples of 64/256 = 1/4 reveal r = 4.
N = 21 needs 15 qubits (32,768 amplitudes). With a = 2 it finds the period 6 and the factors 7 x 3; with a = 4 the period 3 is odd, so no factors come out and you must choose another base. Trying a = 14 for N = 15 also gives no factors (aⁱ ≡ −1 mod N), a textbook failure case.
What is verified
The library's test suite runs this circuit for six bases of N = 15 and checks every period (a = 2, 7, 8, 13 → 4; a = 4, 11 → 2), the factors 3 × 5, the known failure a = 14, the lucky case where a shares a factor with N, and that oversized N is refused. The quantum Fourier transform itself is checked against a direct discrete Fourier transform to 12 digits.
Limits: the simulator is limited to N = 15 and 21 by the exponential cost of the state vector — which is exactly why real quantum computers are interesting. The modular multiplications are applied as exact basis-state permutations rather than compiled into elementary gates.
Frequently asked questions
What does Shor's algorithm do?
It finds the prime factors of a large integer in polynomial time on a quantum computer, where the best known classical algorithms need super-polynomial time. It does this by using a quantum Fourier transform to find the period of the function ax mod N; from the period, the factors follow by simple arithmetic.
Does this break encryption?
No. Factoring numbers big enough to threaten RSA needs thousands of high-quality logical qubits, and today's machines are nowhere near that. This page factors 15 and 21 on a simulated 12- and 15-qubit register — a demonstration that the mathematics works, not an attack.
How do the factors come out of the period?
Once you know the period r of ax mod N, and r is even, then gcd(ar/2 ± 1, N) usually gives factors. For N = 15 and a = 7 the period is 4, 7² = 49 ≡ 4 (mod 15), and gcd(4 − 1, 15) = 3 and gcd(4 + 1, 15) = 5 — so 15 = 3 x 5.
Why does it sometimes fail?
For some bases the period is odd, or ar/2 ≡ −1 (mod N), and the arithmetic gives only trivial factors. Then you simply pick another a. On this page N = 15 with a = 14 (which is −1 mod 15) fails, and so do N = 21 with a = 4 (odd period 3) and a = 5. That is genuine behaviour of Shor's algorithm, not a bug.
Is the quantum part real?
Yes. The page builds the actual circuit on a 2n-amplitude state vector: Hadamards on the counting register, controlled modular multiplications as exact permutations of the basis states, then an inverse quantum Fourier transform. The period is read from the peaks of the resulting probability distribution. What is simulated (rather than performed on quantum hardware) is only the physics of the qubits.
Every number on this page is computed by the open-source larzscript-cosmos library (qalgo, quantum, physmath), written in Larzscript, and checked against independent references in its test suite. How it is verified →