Skip to content
Current section
The Quantum Computing Enthusiast’s Hub
Saved
All sections

All sections

Saved
Skip Learn navigation
Encyclopedia
Topic

Shor factoring algorithm

A polynomial-time quantum algorithm that reduces integer factoring to order finding and classical number theory.

Also called Shor's algorithm, quantum factoring

Edition released Coverage: Overview · Technical
On this page

Definition and significance

Shor's factoring algorithm finds nontrivial factors through the multiplicative order of a chosen integer modulo N. The quantum subroutine estimates this order; classical greatest-common-divisor calculations convert suitable orders into factors. The input size is the bit length of N, not N itself.

The algorithm establishes an asymptotic quantum advantage over the best known general classical factoring methods. It is not a proof that factoring is outside classical polynomial time, and small compiled demonstrations are not evidence that cryptographically relevant integers can already be factored.

Mechanism and mathematical treatment

Choose a with 1<a<N and compute gcd(a,N). A nontrivial gcd already gives a factor. Otherwise find the least r>0 with a^r=1 modulo N. If r is even and a^(r/2) is not -1 modulo N, then gcd(a^(r/2)-1,N) and gcd(a^(r/2)+1,N) can reveal factors. Failed choices are retried.

The quantum circuit uses coherent modular exponentiation and Fourier or phase-estimation machinery to obtain information about r. A measured phase approximates s/r for some integer s. Continued fractions can recover a candidate denominator when precision and coprimality conditions cooperate; the candidate must be verified classically.

Fully worked reference example

Take N=15 and a=2. The powers modulo 15 are 2,4,8,1, so r=4. Then a^(r/2)=4. The gcds are gcd(3,15)=3 and gcd(5,15)=5. The factors multiply back to 15.

For a=14, the order is 2, but a^(r/2)=14=-1 modulo 15. The gcds become gcd(13,15)=1 and gcd(15,15)=15, yielding no nontrivial factor. This is a legitimate failure branch, not an error in the quantum subroutine.

For phase sample s/r=2/4=1/2, continued fractions alone returns denominator 2 rather than 4. Checking 2^2 modulo 15 gives 4, not 1, so additional samples or candidate processing are needed. A complete explanation includes this classical verification.

Limitations and common misconceptions

Modular arithmetic, logical error correction and non-Clifford synthesis dominate realistic resource estimates. Factoring 15 with constants chosen using knowledge of its factors is a teaching circuit, not a scalable benchmark of unrestricted Shor factoring. Security relevance depends on algorithm, key size, architecture and total resource assumptions.

Scientific relationships

Order-finding variants estimate phases of modular multiplication.

The oracle-period problem preceded and influenced later order-finding work; this is historical sequence, not an assertion that one algorithm contains the other.

The periodic arithmetic supplies the order-finding instance.

Classical rational recovery proposes candidate orders from measured phases.

Historical source context

Shor presented a conference version in 1994. The expanded arXiv record began in August 1995, was revised in January 1996 and was published in a 1997 SIAM journal issue. These stages describe one developing result.

Factoring thirty-five through a non-power-of-two order

Choose N=35 and a=2. Their greatest common divisor is one. The successive powers modulo 35 are 2,4,8,16,32,29,23,11,22,9,18,1. Therefore the order is r=12. Since r is even, compute 2^6 modulo 35=29. The two greatest common divisors are gcd(28,35)=7 and gcd(30,35)=5, yielding the complete factorization 35=5 times 7.

The choice r=12 is instructive because it is not a power of two. A binary phase register cannot represent every fraction s/12 exactly. Some spectral samples, such as s=3, reduce to 1/4 and are exactly representable, but their reduced denominator four is only a divisor of the order. Checking 2^4 modulo 35 gives 16, so four must be rejected as an order candidate. A later sample can supply missing information.

For s=5 and a phase-register size Q=4096, the ideal phase 5/12 is closest to m/Q=1707/4096. Their difference is 1/12288, well below 1/(2r^2)=1/288. The continued-fraction convergents of 1707/4096 include 5/12, whose denominator passes the modular test. This is a concrete rational-recovery branch with sufficient precision, not a claim that every phase measurement returns that numerator.

Recovering an order from several denominators

Suppose two successful rational reconstructions yield reduced fractions 1/4 and 1/3. Their denominators are four and three, and their least common multiple is twelve. Testing 2^12 modulo 35 gives one, so twelve is an exponent compatible with the order. To establish that it is the least positive exponent, divide out prime factors whenever the modular test continues to pass: test exponent six and exponent four. They give 29 and 16, neither one, so twelve is minimal.

This small calculation illustrates useful classical postprocessing beyond treating one measured denominator as final. In a general algorithm, a reconstructed denominator can be a divisor of the true order because the sampled numerator shares a common factor with it. Repeated samples can reveal complementary factors. Candidate verification is efficient modular arithmetic and should be retained even when the quantum distribution is ideal.

An exponent that is a multiple of the true order also passes a^r=1. Therefore the modular equality alone proves divisibility by the true order, not minimality. Depending on the factoring reduction, a suitable multiple may still yield factors, but its parity and half-power must be checked directly. Reporting every passing exponent as the exact multiplicative order would be mathematically inaccurate.

Failure branches are part of the reduction

For the same N=35, choose a=6. The powers are 6,1, so r=2 and a^(r/2)=6. The gcds gcd(5,35)=5 and gcd(7,35)=7 succeed. Choose a=34 instead. Its order is two, but a^(r/2)=34=-1 modulo 35, giving gcd(33,35)=1 and gcd(35,35)=35. That branch fails despite perfect order information. A fresh choice of a is required.

If a shares a nontrivial factor with N, the initial classical gcd already solves part of the problem and no order-finding call is needed. For example, a=10 gives gcd(10,35)=5. A complete factoring routine also handles even integers and perfect powers through classical preprocessing. The celebrated quantum subroutine is embedded in this larger classical reduction, not used indiscriminately for every input.

The modular exponentiation circuit must be defined reversibly on the full computational space. Multiplication by a modulo N is a permutation of residues when gcd(a,N)=1. If a register has extra basis states outside 0 through N-1, the implementation must specify a reversible extension, often leaving those states unchanged. State preparation and arithmetic must keep the intended computation in the valid subspace, but correctness cannot rely on an undefined action elsewhere.

For N=35 the input uses six bits, while a convenient phase register can use twelve bits. These small widths are illustrative only. Scalable cost depends on the bit length of N, the modular multiplication architecture, ancillas, non-Clifford synthesis and logical error requirements. Trial division is a trivial classical alternative for thirty-five; this instance explains the mechanism and the classical verification steps rather than demonstrating an advantage.

Sources