Skip to content
Surf Wiki
Save to docs
technology/cryptography

From Surf Wiki (app.surf) — the open knowledge base

Shor's algorithm

Quantum algorithm for integer factorization

Shor's algorithm

Summary

Quantum algorithm for integer factorization

How to factor 2048 bit RSA integers in 8 hours using 20 million noisy qubits | first1 = Craig | last1 = Gidney | first2 = Martin | last2 = Ekerå| journal=Quantum | year=2021 | volume=5 | article-number=433 | doi=10.22331/q-2021-04-15-433 | bibcode = 2021Quant...5..433G | s2cid=162183806 }}

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 \log N . It takes quantum gates of order O ! \left((\log N)^{2} (\log \log N) (\log \log \log N) \right) using fast multiplication, or even O ! \left((\log N)^{2} (\log \log N) \right) using the asymptotically fastest multiplication algorithm currently known due to Harvey and van der Hoeven, thus demonstrating that the integer factorization problem is in complexity class BQP. Shor's algorithm is asymptotically faster than the most scalable classical factoring algorithm, the general number field sieve, which works in sub-exponential time: O ! \left(e^{1.9 (\log N)^{1/3} (\log \log N)^{2/3}} \right) .

Feasibility and impact

Diagram presenting the encryption and the decryption of a document using asymmetric cryptography. Some forms of encryption (including asymmetric cryptography) are at risk of being broken by future quantum computers.

Assuming 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

  • The RSA scheme
  • The finite-field Diffie–Hellman key exchange
  • The elliptic-curve Diffie–Hellman key exchange{{cite conference | editor1-last = Takagi | editor1-first = Tsuyoshi | editor2-last = Peyrin | editor2-first = Thomas

RSA can be broken if factoring large integers is computationally feasible. As far as is known, this is not possible using classical (non-quantum) computers; no classical algorithm is known that can factor integers in polynomial time. However, Shor's algorithm shows that factoring integers can be done with a polynomial complexity circuit on an ideal quantum computer. Thus, it might be feasible to defeat RSA by constructing a large enough quantum computer. This was 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 (PQC).

Physical implementation

As of 2026, the high error rates of quantum computers and limited number of physical qubits available for quantum error correction, laboratory demonstrations of Shor's algorithm obtain correct results in only in a fraction of attempts, and have only succeeded with small semiprimes.

In 2001, Shor's algorithm was demonstrated by a group at IBM, who factored 15 into 3 \times 5 , using an NMR implementation of a quantum computer with seven qubits. 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. In 2012, the factorization of 15 was performed with solid-state qubits. Later, in 2012, the factorization of 21 was achieved. In 2016, the factorization of 15 was performed again using trapped-ion qubits. However, none of these demonstrations fulfill the requirements of Shor’s algorithm: they compile the circuit using prior knowledge of the solution, and some have even oversimplified the algorithm in a way that makes it equivalent to coin flipping.

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 whether N is a prime power. For prime powers, efficient classical factorization algorithms exist, 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 \leq a 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) \ne 1 ), the algorithm is finished, and the other nontrivial factor is N/\gcd(a, N) . If a nontrivial factor was not identified, then this 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 : a^r \equiv 1 \bmod N, 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 a^r - 1 , written N \mid a^r - 1 . This can be factored using difference of squares: N \mid (a^{r/2} - 1)(a^{r/2} + 1). Since we have factored the expression in this way, the algorithm doesn't work for odd r (because a^{r/2} must be an integer), meaning that the algorithm would have to restart with a new a . Hereafter we can therefore assume that r is even. It cannot be the case that N \mid a^{r/2} - 1 , since this would imply a^{r/2} \equiv 1 \bmod N, which would contradictorily imply that r/2 would be the order of a , which was already r . At this point, it may or may not be the case that N \mid a^{r/2} + 1 . If N does not divide a^{r/2} + 1 , then this means that we are able to find a nontrivial factor of N . We compute d = \gcd(N, a^{r/2} - 1). If d = 1 , then N \mid a^{r/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 N/d , and the algorithm is finished. For this step, it is also equivalent to compute \gcd(N, a^{r/2} + 1) ; it will produce a nontrivial factor if \gcd(N, a^{r/2} - 1) is nontrivial, and will not if it's trivial (where N \mid a^{r/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 .

  1. Pick a random number 1 .
  2. Compute K = \gcd(a, N) , the greatest common divisor of a and N .
  3. If K \neq 1 , then K is a nontrivial factor of N , with the other factor being N/K, and we are done.
  4. Otherwise, use the quantum subroutine to find the order r of a.
  5. If r is odd, then go back to step 1.
  6. Compute g = \gcd(N, a^{r/2} + 1). If g is nontrivial, the other factor is N/g, and we're done. Otherwise, go back to step 1.

It has been shown that this will be likely to succeed after a few runs. 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.

Quantum order-finding subroutine

The goal of the quantum subroutine of Shor's algorithm is, given coprime integers N and 1, to find the order r of a modulo N, the smallest positive integer r such that a^r \equiv 1 \pmod N. 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\le 2^n , i.e., n = \left \lceil {\log_2N} \right \rceil . The size of the first register determines how accurate of an approximation the circuit produces. It can be shown that using 2n 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 \otimes to denote the tensor product.

The algorithm consists of two main steps:

  1. Use quantum phase estimation with unitary matrix U representing the operation of multiplying by a (modulo N), and input state |0\rangle^{\otimes 2 n}\otimes|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 \frac{j}{r} 2^{2n} for random j=0,1,...,r-1.
  2. 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 Alexei Kitaev.

Quantum phase estimation

Quantum subroutine in Shor's algorithm

In general the quantum phase estimation algorithm, for any unitary U and eigenstate |\psi\rangle such that U|\psi\rangle=e^{2\pi i\theta} |\psi\rangle, sends input states |0\rangle|\psi\rangle to output states close to |\phi\rangle|\psi\rangle, where \phi is a superposition of integers close to 2^{2n} \theta. In other words, it sends each eigenstate |\psi_j\rangle of U to a state containing information close to the associated eigenvalue. For the purposes of quantum order-finding, we employ this strategy using the unitary defined by the action U|k\rangle = \begin{cases} |ak \pmod N\rangle & 0 \le k |k\rangle & N \le k \end{cases}The action of U on states |k\rangle with N \leq k is not crucial to the functioning of the algorithm, but needs to be included to ensure that 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 U^{2^j} . This can be accomplished via modular exponentiation, which is the slowest part of the algorithm.

The gate thus defined satisfies U^r = I, which immediately implies that its eigenvalues are the r-th roots of unity \omega_r^k = e^{2\pi ik/r}. Furthermore, each eigenvalue \omega_r^j has an eigenvector of the form |\psi_j\rangle=r^{-1/2}\sum_{k=0}^{r-1}\omega_r^{-kj}|a^k\rangle , and these eigenvectors are such that\begin{align} \frac{1}{\sqrt{r}} \sum_{j = 0}^{r - 1} |\psi_j\rangle &= \frac{1}{r} \sum_{j = 0}^{r - 1} \sum_{k = 0}^{r - 1} \omega_r^{jk}|a^k\rangle \ &= |1\rangle + \frac{1}{r} \sum_{k = 1}^{r - 1} \left(\sum_{j = 0}^{r - 1} \omega_r^{jk} \right) |a^k\rangle =|1\rangle, \end{align} where the last identity follows from the geometric series formula, which implies \sum_{j = 0}^{r - 1} \omega_r^{jk} = 0.

Using quantum phase estimation on an input state |0\rangle^{\otimes 2 n}|\psi_j\rangle would then return the integer 2^{2n} j/r with high probability. More precisely, the quantum phase estimation circuit sends |0\rangle^{\otimes 2 n}|\psi_j\rangle to |\phi_j\rangle|\psi_j\rangle such that the resulting probability distribution p_k \equiv|\langle k|\phi_j\rangle|^2 is peaked around k=2^{2n} j/r, with p_{2^{2n}j/r} \ge 4/\pi^2 \approx 0.4053. This probability can be made arbitrarily close to 1 using extra qubits.

Applying the above reasoning to the input |0\rangle^{\otimes 2 n}|1\rangle, quantum phase estimation thus results in the evolution |0\rangle^{\otimes 2 n}|1\rangle = \frac{1}{\sqrt{r}} \sum_{j = 0}^{r - 1} |0\rangle^{\otimes 2 n} |\psi_j\rangle \to \frac{1}{\sqrt{r}} \sum_{j = 0}^{r - 1} |\phi_j\rangle|\psi_j\rangle. Measuring the first register, we now have a balanced probability 1/r to find each |\phi_j\rangle, each one giving an integer approximation to 2^{2 n} j/r, which can be divided by 2^{2n} to get a decimal approximation for j/r.

Continued-fraction algorithm to retrieve the period

Then, we apply the continued-fraction algorithm to find integers b and c, where b/c gives the best fraction approximation for the approximation measured from the circuit, for b, c and coprime b and c. The number of qubits in the first register, 2n, which determines the accuracy of the approximation, guarantees that \frac{b}{c} = \frac{j}{r}, given the best approximation from the superposition of |\phi_j\rangle was measured (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{b_1}{c_1}, \frac{b_2}{c_2}, \ldots, \frac{b_s}{c_s}, where 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: \operatorname{lcm}(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.

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 qubits is sufficient to guarantee that the optimal bitstring measured from phase estimation (meaning the |k\rangle where k / 2^{2n} is the most accurate approximation of the phase from phase estimation) will allow the actual value of r to be recovered.

Each |\phi_j\rangle before measurement in Shor's algorithm represents a superposition of integers approximating 2^{2 n} j/r. Let |k\rangle represent the most optimal integer in |\phi_j\rangle. The following theorem guarantees that the continued fractions algorithm will recover j/r from k/2^{2 {n}}: \left\vert \frac{j}{r} - \phi\right\vert \leq \frac{1}{2 r^2} then the continued fractions algorithm run on \phi will recover both \frac{j}{\gcd(j,; r)} and \frac{r}{\gcd(j,; r)}. As k is the optimal bitstring from phase estimation, k/2^{2 {n}} is accurate to j/r by 2n bits. Thus,\left\vert\frac{j}{r} - \frac{k}{2^{2n}}\right\vert \leq \frac{1}{2^{2 {n} + 1}} \leq \frac{1}{2N^2} \leq \frac{1}{2r^2}which 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. Reversible circuits typically use on the order of n^3 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

Given a group G with order p and generator g \in G , suppose we know that x = g^{r} \in G , for some r \in \mathbb{Z}p , and we wish to compute r , which is the discrete logarithm: r = {\log{g}}(x) . Consider the abelian group \mathbb{Z}{p} \times \mathbb{Z}{p} , where each factor corresponds to modular addition of values. Now, consider the function

: f \colon \mathbb{Z}{p} \times \mathbb{Z}{p} \to G ;;; f(a,b) = g^{a} x^{- b} .

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. To see this, consider the group of integers under addition, and for a given a\in\mathbb{Z} such that: a^{r}=1, the function : f \colon \mathbb{Z}\to \mathbb{Z} ;;; f(x) = a^{x},; 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.

References

References

  1. (1994). "Proceedings 35th Annual Symposium on Foundations of Computer Science".
  2. Shor, Peter W.. (October 1997). "Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer". SIAM Journal on Computing.
  3. See also [[pseudo-polynomial time]].
  4. (August 1996). "Efficient networks for quantum factoring". Physical Review A.
  5. (March 2021). "Integer multiplication in time O (n log n)". Annals of Mathematics.
  6. "Number Field Sieve". wolfram.com.
  7. (December 2001). "Experimental realization of Shor's quantum factoring algorithm using nuclear magnetic resonance". Nature.
  8. (19 December 2007). "Demonstration of a Compiled Version of Shor's Quantum Factoring Algorithm Using Photonic Qubits". Physical Review Letters.
  9. (19 December 2007). "Experimental Demonstration of a Compiled Version of Shor's Algorithm with Quantum Entanglement". Physical Review Letters.
  10. (2012). "Computing prime factors with a Josephson phase qubit quantum processor". Nature Physics.
  11. (12 October 2012). "Experimental realization of Shor's quantum factoring algorithm using qubit recycling". Nature Photonics.
  12. (4 March 2016). "Realization of a scalable Shor algorithm". Science.
  13. (July 2013). "Oversimplifying quantum factoring". Nature.
  14. (1998). "Detecting perfect powers in essentially linear time". Mathematics of Computation.
  15. For example, computing the first \log_2(N) roots of N, e.g., with the [[Nth_root#Computing_principal_roots. Newton method]] and checking each integer result for primality ([[AKS primality test]]).
  16. (June 2021). "On completely factoring any integer efficiently in a single run of an order-finding algorithm". Quantum Information Processing.
  17. (1995). "Quantum measurements and the Abelian Stabilizer Problem".
  18. (May 2024). "On the Success Probability of Quantum Order Finding". ACM Transactions on Quantum Computing.
  19. (2012). "Constant-Optimized Quantum Circuits for Modular Multiplication and Exponentiation". Quantum Information and Computation.
  20. (2013). "Faster Quantum Number Factoring via Circuit Synthesis". Phys. Rev. A.
  21. (9 December 2010). "Quantum Computation and Quantum Information". Cambridge University Press.
  22. (2017). "Post-Quantum Cryptography".
Wikipedia Source

This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.

Want to explore this topic further?

Ask Mako anything about Shor's algorithm — get instant answers, deeper analysis, and related topics.

Research with Mako

Free with your Surf account

Content sourced from Wikipedia, available under CC BY-SA 4.0.

This content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.

Report