Quantum Algorithms Database

Shor's Algorithm

Factors large numbers exponentially faster than any known classical algorithm, threatening RSA encryption.

Last reviewed September 30, 2026. Check the linked sources for anything that may have changed since.

Year

1994 (published in full in 1997)

Inventor(s)

Peter Shor

Speedup Type

Exponential Speedup

Difficulty

★★★★☆

Overview

Shor's algorithm is the most famous quantum algorithm, and the main reason governments and companies are migrating to new encryption. It factors a large integer into its prime factors in a number of steps that grows only polynomially with the number of digits, while the best known classical method, the general number field sieve, takes sub-exponential time. The same idea also computes discrete logarithms, which breaks the elliptic-curve and Diffie-Hellman systems used across the internet.

Nothing on the market today can run Shor's algorithm on a number large enough to matter. The importance of the algorithm is that it shows a real, practical problem with a clear quantum speedup, and that it makes the security of today's public-key cryptography depend on how fast quantum hardware improves.

The problem

Finding the prime factors of a large composite number — believed to be intractable for classical computers at scale.

History

Peter Shor, then at Bell Labs, presented the algorithm at the IEEE Symposium on Foundations of Computer Science in 1994, and a longer version appeared in the SIAM Journal on Computing in 1997. Shor has said he was inspired by Daniel Simon's earlier algorithm, which also finds a hidden period using quantum interference. Within months the result changed quantum computing from a theoretical curiosity into a subject of national-security interest.

The first experimental demonstration came in 2001, when an IBM team used a seven-qubit nuclear magnetic resonance machine to factor 15 into 3 and 5. In 2012 photonic experiments factored 21. Critics pointed out that such tiny demonstrations used shortcuts that required knowing the answer in advance, so they say little about real capability. In 2019 Craig Gidney and Martin Ekerå estimated that RSA-2048 could be broken in about eight hours with roughly 20 million noisy qubits, and in 2025 Gidney reduced the estimate to under one million noisy qubits running for less than a week. In 2023 Oded Regev proposed a related multidimensional algorithm that reduces the gate count at the cost of more qubits.

Intuition

Shor's algorithm turns factoring into a different problem: finding the period of a function. Choose a random number a that shares no factors with N, and consider the sequence a, a squared, a cubed, and so on, each taken modulo N. This sequence eventually repeats, and the smallest r with a to the power r equal to 1 modulo N is called the order. If r is even and a to the r over 2 is not congruent to minus 1, then the greatest common divisor of N with a to the r over 2 plus or minus 1 reveals a nontrivial factor. The reduction is purely classical number theory.

Finding r is the hard part for a classical computer, and it is exactly what a quantum computer does well. The quantum part prepares a superposition of many exponents x, computes a to the x modulo N into a second register, and then applies the quantum Fourier transform to the first register. Different values of x that give the same function value interfere, and the Fourier transform makes only frequencies that are multiples of 1 over r survive. Measuring the register yields a number close to a multiple of 2 to the t divided by r, from which the continued fractions method recovers r.

No single step is a quantum speedup of a classical computation in the naive sense of trying all answers at once. The power comes from interference: amplitudes for wrong periods cancel, and amplitudes for the right period reinforce. That is why the algorithm needs the exact structure of modular exponentiation and cannot be applied to arbitrary problems.

How it works

Transforms factoring into a period-finding problem, solved efficiently using the Quantum Fourier Transform, then recovers factors via classical post-processing.

  1. Pick a random integer a between 2 and N minus 1. If the greatest common divisor of a and N is not 1, you have already found a factor and can stop.
  2. Prepare two quantum registers: a counting register of about 2n qubits, where n is the bit length of N, and a work register of n qubits in the state 1. Apply Hadamard gates to the counting register to create an equal superposition of all exponents.
  3. Apply a controlled modular exponentiation that maps each exponent x to a to the x modulo N in the work register. This is the most expensive part of the circuit.
  4. Measure the work register, or simply ignore it. The counting register is left in a superposition of exponents spaced r apart.
  5. Apply the inverse quantum Fourier transform to the counting register and measure it to obtain a number y.
  6. Use the continued fractions algorithm on y divided by 2 to the t to find a candidate period r.
  7. Check that r is even and that a to the r over 2 is not minus 1 modulo N. If so, compute the greatest common divisors of N with a to the r over 2 plus 1 and minus 1 to obtain factors. If not, repeat with a new a. A random choice of a works with probability of at least one half.

Worked example

Take N = 15 and a = 7. The powers of 7 modulo 15 are 7, 4, 13, 1, and then the pattern repeats, so the order is r = 4. Because r is even, compute 7 to the power 2, which is 49 and equals 4 modulo 15. Then the greatest common divisor of 4 minus 1 and 15 is 3, and the greatest common divisor of 4 plus 1 and 15 is 5. The factors of 15 are 3 and 5.

On a quantum computer, the order r = 4 is found by sampling. With an 8-qubit counting register, the Fourier transform peaks at multiples of 256 divided by 4, namely 0, 64, 128, and 192. Measuring any nonzero peak, such as 64, gives 64 over 256, which is one quarter, and the denominator of that fraction is 4. For realistic numbers the same logic is applied to registers with thousands of qubits.

Complexity and resources

Gate countRoughly O(n cubed) with schoolbook multiplication, where n is the number of bits of N; faster multiplication brings it near n squared times a logarithmic factor
Logical qubitsAbout 2n plus a few for Beauregard's construction; about 3n for simpler versions
Best classicalGeneral number field sieve, running in sub-exponential time in n
Success probabilityAt least one half per random choice of a, so a few repetitions suffice
Resource estimate for RSA-2048Under one million noisy physical qubits and under a week (Gidney 2025, with stated assumptions on error rates and cycle time)

Hardware needs

A fault-tolerant quantum computer with thousands of logical qubits and billions of gates, because the circuit is deep and any uncorrected error ruins the interference pattern.

Fast, reliable quantum error correction with low enough physical error rates, around 0.1% or better, and real-time decoding.

Many repetitions of the circuit, since each run gives only a candidate period that has to be checked.

Real-world impact

Directly motivates post-quantum cryptography; would break RSA and similar encryption if run on a large enough fault-tolerant quantum computer.

Applications

Breaking RSA, Diffie-Hellman, and elliptic-curve cryptography, which protect web traffic, digital signatures, and software updates.

Computing discrete logarithms in groups, which is the same hidden-period structure.

Motivating the migration to post-quantum cryptography and driving the harvest-now-decrypt-later concern.

Serving as the standard benchmark for fault-tolerant quantum computing resource estimates.

Limitations

  • It only offers an advantage for problems with a hidden periodic structure, and it does not speed up factoring-unrelated tasks.
  • Small demonstrations such as factoring 15 or 21 usually exploit precompiled knowledge of the answer, so they are not evidence of scalable capability.
  • The circuit needs millions of physical qubits with current error rates, far beyond what exists today.
  • Symmetric encryption such as AES and hash functions are not broken by it.

Common misconceptions

  • It does not try all factors in parallel. The speedup comes from interference that isolates the period of a function.
  • It does not threaten all encryption. Symmetric ciphers and hash-based signatures are largely unaffected.
  • Having factored 15 does not mean a quantum computer is close to breaking RSA. The gap is many orders of magnitude.

How it compares

Grover's algorithm gives only a quadratic speedup for unstructured search, while Shor's gives a super-polynomial speedup for a structured problem.

Simon's algorithm is the direct ancestor: it finds a hidden XOR period, while Shor finds a hidden arithmetic period.

Compared with lattice-based post-quantum schemes, factoring has no known efficient quantum algorithm for lattice problems, which is why they are the replacement.

Why it matters

Shor's algorithm is the clearest example of a quantum computer solving a problem that matters to the real world. It is the reason the US National Institute of Standards and Technology published post-quantum standards in 2024, including ML-KEM for key exchange and ML-DSA and SLH-DSA for signatures, and why many governments set deadlines for migrating.

It also shapes the timeline for the whole field. Resource estimates for RSA-2048 are a common yardstick for hardware roadmaps, and every drop in the estimated qubit count moves the date at which a large quantum computer could become a security risk closer.

What Shor actually proved in the original paper

Peter Shor's 1994 result is often summarised as 'quantum computers break encryption'. The paper itself is more precise. Its title is 'Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer', and the arXiv record (quant-ph/9508027) lists the journal DOI 10.1137/S0097539795293172. The abstract states that efficient randomized algorithms are given for these two problems on a hypothetical quantum computer, and that the number of steps is polynomial in the input size, for example the number of digits of the integer to be factored.

Two words in that description matter. 'Randomized' means the algorithm can fail on a given run and has to be repeated, which is why the classical post-processing step has a retry loop. 'Hypothetical' is the 1990s wording for a machine nobody had built. The paper is a proof about what such a machine could do, not a description of something that existed.

The page does not state a precise big-O bound in its summary, so this site does not quote one from the abstract. The commonly taught cost is roughly cubic in the number of bits of the number being factored, but if you need an exact figure for a particular implementation, read the paper or a recent resource-estimation paper rather than relying on a textbook shorthand.

The two halves: a classical reduction and a quantum period finder

Shor's algorithm is a hybrid. The hard part of factoring a number N is turned, by classical number theory, into a different problem: finding the period r of the function f(x) = a^x mod N for a randomly chosen a that shares no factor with N. If r turns out to be even and a^(r/2) is not congruent to -1 mod N, then taking the greatest common divisor of a^(r/2) - 1 and N gives a non-trivial factor. If those conditions fail, you pick a new a and try again.

Only the period-finding step is quantum. The machine prepares a superposition over many values of x, computes f(x) into a second register, and then applies the quantum Fourier transform to the first register. Measuring it gives a value that, with good probability, is close to a multiple of 2^n / r, from which a classical continued-fraction calculation recovers r.

This split explains why the algorithm is not 'magic factoring'. The quantum computer does one specific job, extracting a hidden period through interference, and everything else runs on an ordinary computer. It also explains why the same idea generalises: Kitaev's paper 'Quantum measurements and the Abelian Stabilizer Problem' describes a polynomial-time quantum algorithm for the Abelian stabilizer problem, which includes factoring and the discrete logarithm, using a procedure for measuring an eigenvalue of a unitary operator.

  • Classical step 1: pick a, check gcd(a, N). A lucky common factor ends the run immediately.
  • Quantum step: find the period r of a^x mod N using superposition, modular exponentiation and the quantum Fourier transform.
  • Classical step 2: turn the measured value into r with continued fractions, then compute gcds to extract a factor.
  • Failure cases (odd r, trivial square root) are handled by repeating with a different a.

Why the speedup is real, and why it is narrow

The best known classical factoring methods are sub-exponential but super-polynomial. Shor's algorithm is polynomial. That gap is why the algorithm matters for cryptography: RSA and finite-field Diffie-Hellman rely on factoring or discrete logarithms being hard, and elliptic-curve schemes rely on a related discrete-logarithm problem. A large, error-corrected quantum computer running Shor-type algorithms would break these schemes.

The speedup is narrow because it depends on hidden structure. Factoring has a periodic structure that interference can expose. Most problems do not, which is why Shor's result did not turn into a general rule that quantum computers are exponentially faster. Symmetric ciphers such as AES are not attacked by Shor's algorithm, and hash functions are not either. The relevant quantum attack on those is Grover-style search, which gives a quadratic rather than exponential gain.

How many qubits does breaking RSA-2048 need?

This is the question with the most misleading answers online. The number depends entirely on assumptions about error rates, error-correcting codes and runtime, so published estimates have moved a lot. In May 2025 Craig Gidney of Google Quantum AI published a paper (arXiv 2505.15917) estimating that RSA-2048 could be factored with fewer than a million noisy qubits in under a week, a large reduction from his own 2019 estimate. The paper's headline is a statement about estimated resources on an assumed hardware model, not about a machine that exists.

Today's largest devices have hundreds to low thousands of physical qubits and, as far as this site's sources show, no machine has run Shor's algorithm on a number of cryptographic size. The small demonstrations that exist factor tiny numbers such as 15 or 21, and several of them rely on circuit simplifications that use knowledge of the answer, so they should not be read as scalable factoring. When you see a headline about 'breaking RSA', check whether it is a resource estimate, a toy demonstration, or a claim that a real 2048-bit key was factored. The last of these has not happened.

  • Resource estimate (paper): fewer than 1 million noisy qubits, under a week, for RSA-2048, under stated hardware assumptions.
  • Demonstrations (experiment): very small numbers, often with simplified circuits.
  • Cryptographically relevant factoring (real key): not demonstrated as of the date this page was checked.

What this means for security planning

Because encrypted data can be recorded now and decrypted later, the practical response has been to migrate to post-quantum cryptography, meaning classical algorithms believed to resist quantum attack, rather than to wait for a working machine. This is a standards and engineering problem and does not require a quantum computer at all. If you are building something that must stay secret for many years, the migration timeline matters more than any single qubit-count headline.

For learners, the best way to understand Shor is to separate three things that are often blurred together: the algorithm (a proven mathematical result), the resource estimate (an engineering projection that keeps changing), and the hardware (still far from the required scale). Each has a different level of certainty.

Frequently asked questions

Can Shor's algorithm break RSA today?

No. Breaking a 2048-bit RSA key is estimated to need hundreds of thousands to millions of high-quality physical qubits, and today's largest machines have hundreds to a few thousand noisy ones.

What is the quantum part of Shor's algorithm?

Finding the period of the function a to the x modulo N. The rest, including choosing a and computing greatest common divisors, runs on a classical computer.

Why does a quantum computer find the period faster?

Superposition lets the circuit evaluate the function for many exponents at once, and the quantum Fourier transform uses interference to make the period visible in a single measurement.

Does Shor's algorithm affect Bitcoin?

Bitcoin signatures use elliptic-curve cryptography, which Shor's algorithm could break with a large enough quantum computer. The network would need to migrate to quantum-resistant signatures before that happens.

What is post-quantum cryptography?

It is a family of classical encryption and signature schemes designed to resist both classical and quantum attacks. NIST published its first standards in August 2024.

Related algorithms

References and further reading

Sources opened and checked on December 1, 2026. Resource estimates and claims change, so check the originals.

  1. Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer (arXiv (P. W. Shor)) Opened. Source for the title, DOI and the polynomial-time statement.
  2. Quantum measurements and the Abelian Stabilizer Problem (arXiv (A. Yu. Kitaev)) Opened. Source for the generalisation to the Abelian stabilizer problem.
  3. How to factor 2048 bit RSA integers with less than a million noisy qubits (arXiv (C. Gidney, 2025)) Opened earlier in this research. Source for the under-a-million-qubits, under-a-week estimate.

→ Read the full deep-dive in our Learning Center

← Back to Algorithms Database