Shor’s Algorithm Lab

Factor a small number the way Shor’s algorithm does. Pick N and a guess a, see the sequence a^x mod N repeat, look at the exact measurement distribution of the quantum step, watch continued fractions recover the period, count which guesses work, and break a toy RSA key.

Reviewed December 2026. New to this? Start on the Factor tab with N = 15, a = 7.

Step 1: is a already lucky?

gcd(7, 15) = 1

Step 2: the powers of a, mod N

7^0=17^1=77^2=47^3=137^4=17^5=77^6=47^7=137^8=17^9=7

The sequence repeats with period r = 4: a^r ≡ 1 (mod N). Finding r is the only step a quantum computer helps with.

Step 3: turn r into factors

a^(r/2) = 4. gcd(4 − 1, 15) = 3 and gcd(4 + 1, 15) = 5. So 15 = 3 × 5.

The algebra

a^r ≡ 1 (mod N)
(a^(r/2) − 1)(a^(r/2) + 1) ≡ 0 (mod N)

If N divides neither bracket alone, the two brackets share the factors of N between them, and gcd finds them. This part is ordinary arithmetic. Try a few values of a: some work, some hit the odd-period or −1 traps.

A five-minute path

  1. Factor tab, N = 15, a = 7: see the sequence 1, 7, 4, 13, 1 … and the period 4. The factors 3 and 5 appear.
  2. Change a to 14. The run fails because a^(r/2) ≡ −1. Try “Pick a working a” and “Pick a failing a”.
  3. Period finding tab: look at the four equal peaks. Press Measure once a few times and read the convergents.
  4. Switch to N = 21. The peaks no longer sit on whole numbers, so they spread. Compare the measured y values.
  5. Which a works? tab: count the failures for N = 77, then click any a to inspect it.
  6. Toy RSA tab: choose primes, encrypt a message, and decrypt it as an attacker who has factored N.

What the lab does and does not do

  • It computes the exact output distribution of the quantum period-finding step, with an ideal noiseless machine.
  • It does not build the modular-exponentiation circuit gate by gate. That circuit is much larger than any browser simulator handles.
  • The post-processing is the standard one: continued fractions, then testing d, 2d, up to 8d.
  • It does not simulate noise or error correction.

Reference tables

Computed by the same code as the lab.

Powers a^x mod 15 for x = 0 … 8. Each row repeats with its own period.
ax=0x=1x=2x=3x=4x=5x=6x=7x=8r
21248124814
7174131741314
1111111111111112
1411411411411412
Every a from 2 to 14 for N = 15. “Lucky” means gcd(a, 15) is already a factor.
aPeriod rOutcomeFactors
24works3 × 5
3nonelucky gcd3 × 5
42works3 × 5
5nonelucky gcd5 × 3
6nonelucky gcd3 × 5
74works3 × 5
84works3 × 5
9nonelucky gcd3 × 5
10nonelucky gcd5 × 3
112works5 × 3
12nonelucky gcd3 × 5
134works3 × 5
142a^(r/2) ≡ −1none
Counting every a from 2 to N−1. “Works” means a coprime to N whose period is even and with a^(r/2) not ≡ −1.
NLucky gcdOdd perioda^(r/2) ≡ −1WorksShare of coprime a
15601685.7%
21823654.5%
3312451052.6%
3510231878.3%
771614153050.8%
9118895476.1%
1432214159075.6%

Eight guides

Predict, then check

N = 21, a = 4. What is the period, and do you get factors?

r = 3. The period r = 3 is odd, so a^(r/2) does not make sense. Try another a.

N = 15, a = 14. Predict the outcome before you open the Factor tab.

a^(r/2) = 14 ≡ −1 (mod 15), so both gcds are trivial. Try another a. It always fails, because 14 ≡ −1.

For N = 15, a = 7 and 8 counting qubits, what is the chance one shot recovers the period?

75.0%. The outcome y = 0 (25%) is useless, and the other three outcomes all work.

For N = 21, how many of the coprime values of a give factors?

6 of 11 (54.5%).

N = 21, a = 2: one shot returns y = 171 with Q = 512. What period does the post-processing find?

r = 6. The fraction 171/512 is close to 1/3, so the first denominator is 3, and 2³ ≠ 1, but 2⁶ ≡ 1 (mod 21). The chance one shot gives the period is 83.1%.

Related on QuantumAtlas

Frequently asked questions

What does Shor’s algorithm do?

It factors an integer N into primes by finding the period of the sequence a^x mod N. Only that period-finding step needs a quantum computer. The rest is ordinary arithmetic.

Does this lab run on a quantum computer?

No. It simulates the quantum step exactly on a classical computer, which is possible only for small N (here up to 143).

Is the period-finding distribution exact or random?

Exact. The probability of each outcome is computed from the formula and checked against a direct Fourier-transform calculation. Only the “Measure once” and “Run 100 shots” buttons use a seeded random generator.

Why does the lab only offer odd numbers that are not prime powers?

Even numbers and prime powers can be factored classically before Shor’s algorithm starts, so they are not interesting cases.

Why do some choices of a fail?

If the period r is odd, or a^(r/2) ≡ −1 (mod N), both gcds are trivial. The Which a works? tab counts them.

How many counting qubits are used?

By default t = ⌈log₂ N²⌉, the textbook choice, capped at 11 in the browser. The slider adds extra qubits.

Can this lab break real encryption?

No. Real keys are thousands of bits long. The Real scale tab shows the published resource estimates for that size.

Are the RSA resource numbers certain?

No. They are published estimates under stated assumptions about error rates and hardware speed, and they have changed a lot over time.

Has Shor’s algorithm been run on real hardware?

Small demonstrations such as factoring 15 have been reported. They do not show the scaling that matters. Check the original papers for what was implemented.

Where can I build the circuit gate by gate?

The Circuit Builder includes the quantum Fourier transform. A full modular-exponentiation circuit is too large for its 5-qubit limit.

Sources and further reading

References were written from memory of the original works. Check the originals for exact citations.