Shor's algorithm explained

Shor's algorithm is a quantum algorithm for finding the prime factors of an integer. It was developed in 1994 by the American mathematician Peter Shor.[1] [2] It is one of the few known quantum algorithms with compelling potential applications and strong evidence of superpolynomial speedup compared to best known classical (non-quantum) algorithms. On the other hand, factoring numbers of practical significance requires far more qubits than available in the near future.[3] Another concern is that noise in quantum circuits may undermine results, requiring additional qubits for quantum error correction.

Shor proposed multiple similar algorithms for solving the factoring problem, the discrete logarithm problem, and the period-finding problem. "Shor's algorithm" usually refers to the factoring algorithm, but may refer to any of the three algorithms. The discrete logarithm algorithm and the factoring algorithm are instances of the period-finding algorithm, and all three are instances of the hidden subgroup problem.

On a quantum computer, to factor an integer

N

, Shor's algorithm runs in polynomial time, meaning the time taken is polynomial in

logN

, where

N

is the size of the integer given as input.[4] Specifically, it takes quantum gates of order

O\left((logN)2(loglogN)(logloglogN)\right)

using fast multiplication,[5] or even

O\left((logN)2(loglogN)\right)

utilizing the asymptotically fastest multiplication algorithm currently known due to Harvey and Van Der Hoven,[6] thus demonstrating that the integer factorization problem can be efficiently solved on a quantum computer and is consequently in the complexity class BQP. This is significantly faster than the most efficient known classical factoring algorithm, the general number field sieve, which works in sub-exponential time:

O

1.9(logN)1/3(loglogN)2/3
\left(e

\right)

.[7]

Feasibility and impact

If a quantum computer with a sufficient number of qubits could operate without succumbing to quantum noise and other quantum-decoherence phenomena, then Shor's algorithm could be used to break public-key cryptography schemes, such as

RSA is based on the assumption that factoring large integers is computationally intractable. As far as is known, this assumption is valid for classical (non-quantum) computers; no classical algorithm is known that can factor integers in polynomial time. However, Shor's algorithm shows that factoring integers is efficient on an ideal quantum computer, so it may be feasible to defeat RSA by constructing a large quantum computer. It was also a powerful motivator for the design and construction of quantum computers, and for the study of new quantum-computer algorithms. It has also facilitated research on new cryptosystems that are secure from quantum computers, collectively called post-quantum cryptography.

Physical implementation

Given the high error rates of contemporary quantum computers and too few qubits to use quantum error correction, laboratory demonstrations obtain correct results only in a fraction of attempts.

In 2001, Shor's algorithm was demonstrated by a group at IBM, who factored

15

into

3 x 5

, using an NMR implementation of a quantum computer with seven qubits.[9] After IBM's implementation, two independent groups implemented Shor's algorithm using photonic qubits, emphasizing that multi-qubit entanglement was observed when running the Shor's algorithm circuits.[10] [11] In 2012, the factorization of

15

was performed with solid-state qubits.[12] Later, in 2012, the factorization of

21

was achieved.[13] In 2016, the factorization of

15

was performed again using trapped-ion qubits with a recycling technique.[14] In 2019, an attempt was made to factor the number

35

using Shor's algorithm on an IBM Q System One, but the algorithm failed because of accumulating errors.[15] However, all these demonstrations have compiled the algorithm by making use of prior knowledge of the answer, and some have even oversimplified the algorithm in a way that makes it equivalent to coin flipping.[16] Furthermore, attempts using quantum computers with other algorithms have been made.[17] However, these algorithms are similar to classical brute-force checking of factors, so unlike Shor's algorithm, they are not expected to ever perform better than classical factoring algorithms.[18]

Theoretical analyses of Shor's algorithm assume a quantum computer free of noise and errors. However, near-term practical implementations will have to deal with such undesired phenomena (when more qubits are available, Quantum error correction can help). In 2023, Jin-Yi Cai showed that in the presence of noise, Shor's algorithm fails asymptotically almost surely for large semiprimes that are products of two primes in .[19] These primes

p

have the property that

p-1

has a prime factor larger than

p2/3

, and have a positive density in the set of all primes. Hence error-correction will be needed to be able to factor all numbers with Shor's algorithm.

Algorithm

The problem that we are trying to solve is: given an odd composite number

N

, find its integer factors.

To achieve this, Shor's algorithm consists of two parts:

  1. A classical reduction of the factoring problem to the problem of order-finding. This reduction is similar to that used for other factoring algorithms, such as the quadratic sieve.
  2. A quantum algorithm to solve the order-finding problem.

Classical reduction

A complete factoring algorithm is possible if we're able to efficiently factor arbitrary

N

into just two integers

p

and

q

greater than 1, since if either

p

or

q

are not prime then the factoring algorithm can in turn be run on those until only primes remain.

A basic observation is that, using Euclid's algorithm, we can always compute the GCD between two integers efficiently. In particular, this means we can check efficiently whether

N

is even, in which case 2 is trivially a factor. Let us thus assume that

N

is odd for the remainder of this discussion. Afterwards, we can use efficient classical algorithms to check if

N

is a prime power.[20] For prime powers, efficient classical factorization algorithms exist,[21] hence the rest of the quantum algorithm may assume that

N

is not a prime power.

If those easy cases do not produce a nontrivial factor of

N

, the algorithm proceeds to handle the remaining case. We pick a random integer

2\leqa<N

. A possible nontrivial divisor of

N

can be found by computing

\gcd(a,N)

, which can be done classically and efficiently using the Euclidean algorithm. If this produces a nontrivial factor (meaning

\gcd(a,N)\ne1

), the algorithm is finished, and the other nontrivial factor is \frac . If a nontrivial factor was not identified, then that means that

N

and the choice of

a

are coprime, so

a

is contained in the multiplicative group of integers modulo

N

, having a multiplicative inverse modulo

N

. Thus,

a

has a multiplicative order

r

modulo

N

, meaning

ar\equiv1\bmodN

and

r

is the smallest positive integer satisfying this congruence.

The quantum subroutine finds

r

. It can be seen from the congruence that

N

divides

ar-1

, written

N\midar-1

. This can be factored using difference of squares: N \mid (a^ - 1)(a^ + 1) Since we have factored the expression in this way, the algorithm doesn't work for odd

r

(because

ar/2

must be an integer), meaning the algorithm would have to restart with a new

a

. Hereafter we can therefore assume

r

is even. It cannot be the case that

N\midar/2-1

, since this would imply

ar/2\equiv1\bmodN

, which would contradictorily imply that \frac would be the order of

a

, which was already

r

. At this point, it may or may not be the case that

N\midar/2+1

. If it is not true that

N\midar/2+1

, then that means we are able to find a nontrivial factor of

N

. We compute d = \gcd(N, a^ - 1) If

d=1

, then that means

N\midar/2+1

was true, and a nontrivial factor of

N

cannot be achieved from

a

, and the algorithm must restart with a new

a

. Otherwise, we have found a nontrivial factor of

N

, with the other being \frac , and the algorithm is finished. For this step, it is also equivalent to compute

\gcd(N,ar/2+1)

; it will produce a nontrivial factor if

\gcd(N,ar/2-1)

is nontrivial, and will not if it's trivial (where

N\midar/2+1

).

The algorithm restated shortly follows: let

N

be odd, and not a prime power. We want to output two nontrivial factors of

N

.It has been shown that this will be likely to succeed after a few runs.[2] In practice, a single call to the quantum order-finding subroutine is enough to completely factor

N

with very high probability of success if one uses a more advanced reduction.[22]

Quantum order-finding subroutine

The goal of the quantum subroutine of Shor's algorithm is, given coprime integers

N

and

1<a<N

, to find the order

r

of

a

modulo

N

, which is the smallest positive integer such that

ar\equiv1\pmodN

. To achieve this, Shor's algorithm uses a quantum circuit involving two registers. The second register uses

n

qubits, where

n

is the smallest integer such that

N\le2n

, i.e.,

n=\left\lceil{log2N}\right\rceil

. The size of the first register determines how accurate of an approximation the circuit produces. It can be shown that using

2n+1

qubits gives sufficient accuracy to find

r

. The exact quantum circuit depends on the parameters

a

and

N

, which define the problem. The following description of the algorithm uses bra–ket notation to denote quantum states, and

to denote the tensor product, rather than logical AND.

The algorithm consists of two main steps:

  1. Use quantum phase estimation with unitary

U

representing the operation of multiplying by

a

(modulo

N

), and input state

|0\rangle|1\rangle

(where the second register is

|1\rangle

made from

n

qubits). The eigenvalues of this

U

encode information about the period, and

|1\rangle

can be seen to be writable as a sum of its eigenvectors. Thanks to these properties, the quantum phase estimation stage gives as output a random integer of the form
j
r

22n

for random

j=0,1,...,r-1

.
  1. Use the continued fractions algorithm to extract the period

r

from the measurement outcomes obtained in the previous stage. This is a procedure to post-process (with a classical computer) the measurement data obtained from measuring the output quantum states, and retrieve the period.

The connection with quantum phase estimation was not discussed in the original formulation of Shor's algorithm, but was later proposed by Kitaev.[23]

Quantum phase estimation

In general the quantum phase estimation algorithm, for any unitary

U

and eigenstate

|\psi\rangle

such that

U|\psi\rangle=e2\pi|\psi\rangle

, sends input states

|0\rangle|\psi\rangle

to output states close to

|\phi\rangle|\psi\rangle

, where

\phi

is an integer close to

22n\theta

. In other words, it sends each eigenstate

|\psij\rangle

of

U

to a state close to the associated eigenvalue. For the purposes of quantum order-finding, we employ this strategy using the unitary defined by the actionU|k\rangle = \begin |ak \pmod N\rangle & 0 \le k < N, \\ |k\rangle & N \le k < 2^n.\endThe action of

U

on states

|k\rangle

with

N\leqk<2n

is not crucial to the functioning of the algorithm, but needs to be included to ensure the overall transformation is a well-defined quantum gate. Implementing the circuit for quantum phase estimation with

U

requires being able to efficiently implement the gates
2j
U

. This can be accomplished via modular exponentiation, which is the slowest part of the algorithm.The gate thus defined satisfies

Ur=I

, which immediately implies that its eigenvalues are the

r

-th roots of unity
k=e
\omega
r

2\pi

. Furthermore, each eigenvalue
j
\omega
r
has an eigenvector of the form |\psi_j\rangle=r^\sum_^\omega_r^|a^k\rangle , and these eigenvectors are such that\begin\frac \sum_^ |\psi_j\rangle &= \frac \sum_^ \sum_^ \omega_r^|a^k\rangle \\&= |1\rangle + \frac \sum_^ \left(\sum_^ \omega_r^ \right) |a^k\rangle =|1\rangle,\end

where the last identity follows from the geometric series formula, which implies \sum_^ \omega_r^ = 0.

Using quantum phase estimation on an input state

|0\rangle|\psij\rangle

would then return the integer

22nj/r

with high probability. More precisely, the quantum phase estimation circuit sends

|0\rangle|\psij\rangle

to

|\phij\rangle|\psij\rangle

such that the resulting probability distribution

pk\equiv|\langle

2
k|\phi
j\rangle|

is peaked around k=2^ j/r , with p_\ge \frac \approx 0.4053 . This probability can be made arbitrarily close to 1 using extra qubits.

Applying the above reasoning to the input

|0\rangle|1\rangle

, quantum phase estimation thus results in the evolution|0\rangle^|1\rangle = \frac \sum_^ |0\rangle^ |\psi_j\rangle \to \frac \sum_^ |\phi_j\rangle|\psi_j\rangle. Measuring the first register, we now have a balanced probability

1/r

to find each

|\phij\rangle

, each one giving an integer approximation to

22j/r

, which can be divided by

22n

to get a decimal approximation for

j/r

.

Continued fraction algorithm to retrieve the period

Then, we apply the continued fractions algorithm to find integers b and c, where \frac gives the best fraction approximation for the approximation measured from the circuit, for b, c < N and coprime b and c. The number of qubits in the first register,

2n+1

, which determines the accuracy of the approximation, guarantees that\frac = \fracgiven the best approximation from the superposition of |\phi_j\rangle was measured[2] (which can be made arbitrarily likely by using extra bits and truncating the output). However, while b and c are coprime, it may be the case that j and r are not coprime. Because of that, b and c may have lost some factors that were in j and r. This can be remedied by rerunning the quantum order-finding subroutine an arbitrary number of times, to produce a list of fraction approximations\frac_ \; \frac_ \ldots \vphantom_ \; \fracwhere s is the number of times the subroutine was run. Each c_k will have different factors taken out of it because the circuit will (likely) have measured multiple different possible values of j. To recover the actual r value, we can take the least common multiple of each c_k:\mathrm(c_1, c_2, \ldots, c_s)The least common multiple will be the order r of the original integer a with high probability. In practice, a single run of the quantum order-finding subroutine is in general enough if more advanced post-processing is used.[24]

Choosing the size of the first register

Phase estimation requires choosing the size of the first register to determine the accuracy of the algorithm, and for the quantum subroutine of Shor's algorithm,

2n+1

qubits is sufficient to guarantee that the optimal bitstring measured from phase estimation (meaning the

|k\rangle

where k / 2^ is the most accurate approximation of the phase from phase estimation) will allow the actual value of

r

to be recovered.

Each

|\phij\rangle

before measurement in Shor's algorithm represents a superposition of integers approximating

22j/r

. Let

|k\rangle

represent the most optimal integer in

|\phij\rangle

. The following theorem guarantees that the continued fractions algorithm will recover

j/r

from

k/22+1}

: As

k

is the optimal bitstring from phase estimation,

k/22+1}

is accurate to

j/r

by

2n+1

bits. Thus,\left\vert\frac - \frac\right\vert \leq \frac \leq \frac \leq \fracwhich implies that the continued fractions algorithm will recover

j

and

r

(or with their greatest common divisor taken out).

The bottleneck

The runtime bottleneck of Shor's algorithm is quantum modular exponentiation, which is by far slower than the quantum Fourier transform and classical pre-/post-processing. There are several approaches to constructing and optimizing circuits for modular exponentiation. The simplest and (currently) most practical approach is to mimic conventional arithmetic circuits with reversible gates, starting with ripple-carry adders. Knowing the base and the modulus of exponentiation facilitates further optimizations.[25] [26] Reversible circuits typically use on the order of

n3

gates for

n

qubits. Alternative techniques asymptotically improve gate counts by using quantum Fourier transforms, but are not competitive with fewer than 600 qubits owing to high constants.

Period finding and discrete logarithms

Shor's algorithms for the discrete log and the order finding problems are instances of an algorithm solving the period finding problem.. All three are instances of the hidden subgroup problem.

Shor's algorithm for discrete logarithms

G

with order

p

and generator

g\inG

, suppose we know that

x=gr\inG

, for some

r\inZp

, and we wish to compute

r

, which is the discrete logarithm:

r={logg

}(x) . Consider the abelian group

Zp x Zp

, where each factor corresponds to modular addition of values. Now, consider the function

f\colonZp x Zp\toG;f(a,b)=gax-.

This gives us an abelian hidden subgroup problem, where

f

corresponds to a group homomorphism. The kernel corresponds to the multiples of

(r,1)

. So, if we can find the kernel, we can find

r

. A quantum algorithm for solving this problem exists. This algorithm is, like the factor-finding algorithm, due to Peter Shor and both are implemented by creating a superposition through using Hadamard gates, followed by implementing

f

as a quantum transform, followed finally by a quantum Fourier transform. Due to this, the quantum algorithm for computing the discrete logarithm is also occasionally referred to as "Shor's Algorithm."

The order-finding problem can also be viewed as a hidden subgroup problem.[27] To see this, consider the group of integers under addition, and for a given

a\inZ

such that:

ar=1

, the function

f\colonZ\toZ;f(x)=ax,f(x+r)=f(x).

For any finite abelian group

G

, a quantum algorithm exists for solving the hidden subgroup for

G

in polynomial time.

See also

Further reading

External links

Notes and References

  1. Book: 10.1109/sfcs.1994.365700 . Algorithms for quantum computation: Discrete logarithms and factoring . Proceedings 35th Annual Symposium on Foundations of Computer Science . 1994 . Shor . P.W. . 124–134 . 978-0-8186-6580-6 .
  2. Shor . Peter W. . October 1997 . Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer . SIAM Journal on Computing . 26 . 5 . 1484–1509 . quant-ph/9508027 . 10.1137/S0097539795293172 . 2337707 .
  3. 1905.09749. How to factor 2048 bit RSA integers in 8 hours using 20 million noisy qubits . Craig . Gidney . Martin . Ekerå. Quantum . 2021 . 5 . 433 . 10.22331/q-2021-04-15-433 . 2021Quant...5..433G . 162183806 .
  4. See also pseudo-polynomial time.
  5. Beckman . David . Chari . Amalavoyal N. . Devabhaktuni . Srikrishna . Preskill . John . Efficient networks for quantum factoring . Physical Review A . August 1996 . 54 . 2 . 1034–1063 . 10.1103/physreva.54.1034 . 9913575 . quant-ph/9602016 . 1996PhRvA..54.1034B .
  6. Harvey . David . van der Hoeven . Joris . Integer multiplication in time O (n log n) . Annals of Mathematics . March 2021 . 193 . 2 . 10.4007/annals.2021.193.2.4 .
  7. Web site: Number Field Sieve. wolfram.com. 23 October 2015.
  8. Roetteler . Martin . Naehrig . Michael . Svore . Krysta M. . Krysta Svore . Lauter . Kristin E. . Kristin Lauter . Takagi . Tsuyoshi . Peyrin . Thomas . 1706.06752 . Quantum resource estimates for computing elliptic curve discrete logarithms . 10.1007/978-3-319-70697-9_9 . 241–270 . Springer . Lecture Notes in Computer Science . Advances in Cryptology – ASIACRYPT 2017 – 23rd International Conference on the Theory and Applications of Cryptology and Information Security, Hong Kong, China, December 3–7, 2017, Proceedings, Part II . 10625 . 2017. 978-3-319-70696-2 .
  9. Vandersypen . Lieven M. K. . Steffen . Matthias . Breyta . Gregory . Yannoni . Costantino S. . Sherwood . Mark H. . Chuang . Isaac L. . Experimental realization of Shor's quantum factoring algorithm using nuclear magnetic resonance . Nature . December 2001 . 414 . 6866 . 883–887 . 10.1038/414883a . 11780055 . quant-ph/0112176 . 2001Natur.414..883V .
  10. Lu . Chao-Yang . Browne . Daniel E. . Yang . Tao . Pan . Jian-Wei . Demonstration of a Compiled Version of Shor's Quantum Factoring Algorithm Using Photonic Qubits . Physical Review Letters . 19 December 2007 . 99 . 25 . 250504 . 10.1103/PhysRevLett.99.250504 . 18233508 . 0705.1684 . 2007PhRvL..99y0504L .
  11. Lanyon . B. P. . Weinhold . T. J. . Langford . N. K. . Barbieri . M. . James . D. F. V. . Gilchrist . A. . White . A. G. . Experimental Demonstration of a Compiled Version of Shor's Algorithm with Quantum Entanglement . Physical Review Letters . 19 December 2007 . 99 . 25 . 250505 . 10.1103/PhysRevLett.99.250505 . 18233509 . 0705.1398 . 2007PhRvL..99y0505L .
  12. Lucero. Erik. Barends. Rami. Chen. Yu. Kelly. Julian. Mariantoni. Matteo. Megrant. Anthony. O'Malley. Peter. Sank. Daniel. Vainsencher. Amit. Wenner. James. White. Ted. Yin. Yi. Cleland. Andrew N.. Martinis. John M.. Computing prime factors with a Josephson phase qubit quantum processor. Nature Physics. 8. 10. 719. 2012. 10.1038/nphys2385. 2012NatPh...8..719L. 1202.5707. 44055700.
  13. Martín-López. Enrique. Martín-López. Enrique. Laing. Anthony. Lawson. Thomas. Alvarez. Roberto. Zhou. Xiao-Qi. O'Brien. Jeremy L.. Experimental realization of Shor's quantum factoring algorithm using qubit recycling. Nature Photonics. 6. 11. 773–776. 12 October 2012. 10.1038/nphoton.2012.259. 1111.4147. 2012NaPho...6..773M. 46546101.
  14. Monz. Thomas . Nigg. Daniel. Martinez. Esteban A.. Brandl. Matthias F.. Schindler. Philipp. Rines. Richard. Wang. Shannon X.. Chuang. Isaac L.. Blatt. Rainer. Realization of a scalable Shor algorithm. Science. 351. 6277. 1068–1070. 4 March 2016. 10.1126/science.aad9480. 26941315 . 1507.08852. 2016Sci...351.1068M. 17426142.
  15. Amico . Mirko . Saleem . Zain H. . Kumph . Muir . Experimental study of Shor's factoring algorithm using the IBM Q Experience . Physical Review A . 8 July 2019 . 100 . 1 . 012305 . 10.1103/PhysRevA.100.012305 . 1903.00768 . 2019PhRvA.100a2305A . 92987546 .
  16. Smolin . John A. . Smith . Graeme . Vargo . Alexander . Oversimplifying quantum factoring . Nature . July 2013 . 499 . 7457 . 163–165 . 10.1038/nature12290 . 23846653 . 1301.7007 . 2013Natur.499..163S .
  17. Karamlou . Amir H. . Simon . William A. . Katabarwa . Amara . Scholten . Travis L. . Peropadre . Borja . Cao . Yudong . Analyzing the performance of variational quantum factoring on a superconducting quantum processor . npj Quantum Information . 28 October 2021 . 7 . 1 . 156 . 10.1038/s41534-021-00478-z . 2012.07825 . 2021npjQI...7..156K .
  18. Web site: 2019-12-28. Quantum computing motte-and-baileys. 2021-11-15. Shtetl-Optimized. en-US.
  19. 2306.10072 . Cai . Jin-Yi . 2024 . Shor's algorithm does not factor large integers in the presence of noise . Science China Information Sciences . 67 . 7 . 10.1007/s11432-023-3961-3 .
  20. Bernstein . Daniel . Detecting perfect powers in essentially linear time . Mathematics of Computation . 1998 . 67 . 223 . 1253–1283 . 10.1090/S0025-5718-98-00952-1 .
  21. e.g., computing the first

    log2(N)

    roots of

    N

    , e.g., with the Newton method and checking each integer result for primality (AKS primality test).
  22. Ekerå . Martin . On completely factoring any integer efficiently in a single run of an order-finding algorithm . Quantum Information Processing . June 2021 . 20 . 6 . 205 . 10.1007/s11128-021-03069-1 . 2007.10044 . 2021QuIP...20..205E . free .
  23. quant-ph/9511026 . Kitaev . A. Yu . 1995 . Quantum measurements and the Abelian Stabilizer Problem .
  24. Ekerå . Martin . On the Success Probability of Quantum Order Finding . ACM Transactions on Quantum Computing . May 2024 . 5 . 2 . 1–40 . 10.1145/3655026 . free . 2201.07791 .
  25. Igor L. . Markov . Mehdi . Saeedi . Constant-Optimized Quantum Circuits for Modular Multiplication and Exponentiation . Quantum Information and Computation . 12 . 5–6 . 361–394 . 2012 . 10.26421/QIC12.5-6-1 . 1202.6614 . 2012arXiv1202.6614M . 16595181 .
  26. Igor L. . Markov . Mehdi . Saeedi . Faster Quantum Number Factoring via Circuit Synthesis . Phys. Rev. A . 87 . 1. 012310 . 2013 . 1301.3210 . 2013PhRvA..87a2310M . 10.1103/PhysRevA.87.012310 . 2246117 .
  27. Book: Nielsen . Michael A. . Quantum Computation and Quantum Information . https://web.archive.org/web/20190711070716/http://mmrc.amss.cas.cn/tlb/201702/W020170224608149940643.pdf . 2019-07-11 . live . Chuang . Isaac L. . 9 December 2010 . Cambridge University Press . 978-1-107-00217-3 . 7th . 24 April 2022.
  28. Book: 10.1007/978-3-319-59879-6_18 . Post-quantum RSA . Post-Quantum Cryptography . Lecture Notes in Computer Science . 2017 . Bernstein . Daniel J. . Heninger . Nadia . Lou . Paul . Valenta . Luke . 10346 . 311–329 . 978-3-319-59878-9 .