World Library  
Flag as Inappropriate
Email this Article

Shor's algorithm


Shor's algorithm

Shor's algorithm, named after mathematician Peter Shor, is a quantum algorithm (an algorithm that runs on a quantum computer) for integer factorization formulated in 1994. Informally it solves the following problem: Given an integer N, find its prime factors.

On a quantum computer, to factor an integer N, Shor's algorithm runs in polynomial time (the time taken is polynomial in log N, which is the size of the input).[1] Specifically it takes time O((log N)3), demonstrating that the integer factorization problem can be efficiently solved on a quantum computer and is thus in the complexity class BQP. This is substantially faster than the most efficient known classical factoring algorithm, the general number field sieve, which works in sub-exponential time — about O(e1.9 (log N)1/3 (log log N)2/3).[2] The efficiency of Shor's algorithm is due to the efficiency of the quantum Fourier transform, and modular exponentiation by repeated squarings.

The factorization also needs huge numbers of quantum gates. It increases with N as (log N)3.[3] Thus factoring of a 4096-bit number requires 4,947,802,324,992 quantum gates.[4]

If a quantum computer with a sufficient number of qubits could operate without succumbing to noise and other quantum decoherence phenomena, Shor's algorithm could be used to break public-key cryptography schemes such as the widely used RSA scheme. RSA is based on the assumption that factoring large numbers is computationally intractable. So far as is known, this assumption is valid for classical (non-quantum) computers; no classical algorithm is known that can factor in polynomial time. However, Shor's algorithm shows that factoring 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.

In 2001, Shor's algorithm was demonstrated by a group at IBM, who factored 15 into 3 × 5, using an NMR implementation of a quantum computer with 7 qubits.[5] However, some doubts have been raised as to whether IBM's experiment was a true demonstration of quantum computation, since no entanglement was observed.[6] Since IBM's implementation, several other groups have implemented Shor's algorithm using photonic qubits, emphasizing that entanglement was observed.[7][8] In 2012, the factorization of 15 was repeated.[9] Also in 2012, the factorization of 21 was achieved, setting the record for the largest number factored with a quantum computer.[10] In April 2012, the factorization of 143 was achieved, although this used adiabatic quantum computation rather than Shor's algorithm. [1]


  • Procedure 1
    • Classical part 1.1
    • Quantum part: Period-finding subroutine 1.2
  • Explanation of the algorithm 2
    • Obtaining factors from period 2.1
    • Finding the period 2.2
    • The bottleneck 2.3
  • Discrete logarithms 3
  • In popular culture 4
  • References 5
  • Further reading 6


The problem we are trying to solve is: given an odd composite number N, find an integer d, strictly between 1 and N, that divides N. We are interested in odd values of N because any even value of N trivially has the number 2 as a prime factor. We can use a primality testing algorithm to make sure that N is indeed composite.

Moreover, for the algorithm to work, we need N not to be the power of a prime. This can be tested by taking square, cubic, ..., k-roots of N, for k \le \log_{3}(N), and checking that none of these is an integer. (This actually excludes that N = M^{k} for some integer M and k > 1.)

Since N is not a power of a prime, it is the product of two coprime numbers greater than 1. As a consequence of the Chinese remainder theorem, the number 1 has at least four distinct square roots modulo N, two of them being 1 and -1. The aim of the algorithm is to find a square root b of one, other than 1 and -1; such a b will lead to a factorization of N, as in other factoring algorithms like the quadratic sieve.

In turn, finding such a b is reduced to finding an element a of even period with a certain additional property (as explained below, it is required that the condition of Step 6 of the classical part does not hold). The quantum algorithm is used for finding the period of randomly chosen elements a, as order-finding is a hard problem on a classical computer.

Shor's algorithm consists of two parts:

  1. A reduction, which can be done on a classical computer, of the factoring problem to the problem of order-finding.
  2. A quantum algorithm to solve the order-finding problem.

Classical part

  1. Pick a random number a < N.
  2. Compute gcd(a, N). This may be done using the Euclidean algorithm.
  3. If gcd(a, N) ≠ 1, then there is a nontrivial factor of N, so we are done.
  4. Otherwise, use the period-finding subroutine (below) to find r, the period of the following function:
    f(x) = a^x \bmod N,
    i.e. the order r of a in (\mathbb{Z}_N)^\times, which is the smallest positive integer r for which f(x+r) = f(x), or f(x+r) = a^{x+r} \bmod N = a^x \bmod N.
  5. If r is odd, go back to step 1.
  6. If a r /2 ≡ −1 (mod N), go back to step 1.
  7. gcd(ar/2 ± 1, N) is a nontrivial factor of N. We are done.

For example: N = 15, a = 7, r = 4, gcd(7^2 \pm 1, 15) = gcd(49 \pm 1, 15), where gcd(48, 15) = 3, and gcd(50, 15) = 5.

Quantum part: Period-finding subroutine

Quantum subroutine in Shor's algorithm.

The quantum circuits used for this algorithm are custom designed for each choice of N and each choice of the random a used in f(x) = ax mod N. Given N, find Q = 2q such that N^2 \le Q < 2N^2, which implies Q/r > N. The input and output qubit registers need to hold superpositions of values from 0 to Q − 1, and so have q qubits each. Using what might appear to be twice as many qubits as necessary guarantees that there are at least N different x which produce the same f(x), even as the period r approaches N/2.

Proceed as follows:

  1. Initialize the registers to
    Q^{-1/2} \sum_{x=0}^{Q-1} \left|x\right\rangle \left|0\right\rangle
    where x runs from 0 to Q − 1. This initial state is a superposition of Q states.
  2. Construct f(x) as a quantum function and apply it to the above state, to obtain
    Q^{-1/2} \sum_x \left|x, f(x)\right\rangle.
    This is still a superposition of Q states.
  3. Apply the quantum Fourier transform to the input register. This transform (operating on a superposition of power-of-two Q = 2q states) uses a Qth root of unity such as \omega = e^{2 \pi i /Q} to distribute the amplitude of any given \left|x\right\rangle state equally among all Q of the \left|y\right\rangle states, and to do so in a different way for each different x.
    • Let y be one of the r possible integers modulo N such that yr/Q is an integer; then
    U_{QFT} \left|x\right\rangle = Q^{-1/2} \sum_y \omega^{x y} \left|y\right\rangle.

    This leads to the final state

    Q^{-1} \sum_x \sum_y \omega^{x y} \left|y, f(x)\right\rangle.

    Now we reorder this sum as

    Q^{-1} \sum_{z} \sum_y \left|y, z\right\rangle \sum_{x:\, f(x)=z} \omega^{x y}.

    This is a superposition of many more than Q states, but many fewer than Q2 states, since there are fewer than Q distinct values of z = f(z). Let

    • \omega = e^{2 \pi i /Q} be a Qth root of unity,
    • r be the period of f,
    • x0 be the smallest of the x which have f(x) = z (we have x0 < r), and
    • b indexes these x, running from 0 to \lfloor(Q-x_0-1)/r\rfloor so that x_0 + rb < Q.

    Then \omega^{ry} is a unit vector in the complex plane (\omega is a root of unity and r and y are integers), and the coefficient of Q^{-1}\left|y, z\right\rangle in the final state is

    \sum_{x:\, f(x)=z} \omega^{x y} = \sum_{b} \omega^{(x_0 + r b) y} = \omega^{x_0y} \sum_{b} \omega^{r b y}.
    Each term in this sum represents a different path to the same result, and quantum interference occurs—constructive when the unit vectors \omega^{ryb} point in nearly the same direction in the complex plane, which requires that \omega^{ry} point along the positive real axis.
  4. Perform a measurement. We obtain some outcome y in the input register and z in the output register. Since f is periodic, the probability of measuring some pair y and z is given by
    \left| Q^{-1} \sum_{x:\, f(x)=z} \omega^{x y} \right|^2 = Q^{-2} \left| \sum_{b} \omega^{(x_0 + r b) y} \right|^2 = Q^{-2} \left| \sum_{b} \omega^{ b r y} \right|^2.
    Analysis now shows that this probability is higher the closer the unit vector \omega^{ry} is to the positive real axis, or the closer yr/Q is to an integer. Unless r is a power of 2, it won't be a factor of Q.
  5. Since yr/Q is close to some integer c, the known value y/Q is close to the unknown value c/r. Performing [classical] continued fraction expansion on y/Q allows us to find approximations d/s of it which satisfy two conditions:
    • A: s
    • B: |y/Q - d/s| < 1/2Q.
    Given these conditions (and assuming d/s is irreducible), s is very likely to be the appropriate period r, or at least a factor of it.
  6. Check [classically] if f(x) = f(x + s) \Leftrightarrow a^r \equiv 1 \pmod{N} If so, we are done.
  7. Otherwise, [classically] obtain more candidates for r by using multiples of s, or by using other s with d/s near y/Q. If any candidate works, we are done.
  8. Otherwise, try again starting from step 1 of this subroutine.

Explanation of the algorithm

The algorithm is composed of two parts. The first part of the algorithm turns the factoring problem into the problem of finding the period of a function, and may be implemented classically. The second part finds the period using the quantum Fourier transform, and is responsible for the quantum speedup.

Obtaining factors from period

The integers less than N and coprime with N form a finite Abelian group G under multiplication modulo N. The size is given by Euler's totient function \phi(N). By the end of step 3, we have an integer a in this group. Since the group is finite, a must have a finite order r, the smallest positive integer such that

a^r \equiv 1\ \mbox{mod}\ N.\,

Therefore, N divides (also written | ) a r − 1 . Suppose we are able to obtain r, and it is even. (If r is odd, see step 5.) Now b \equiv a^{r/2} \pmod{N} is a square root of 1 modulo N, different from 1. This is because r is the order of a modulo N, so a^{r/2} \not\equiv 1 \pmod{N}, else the order of a in this group would be r/2. If a^{r/2} \equiv -1 \pmod{N}, by step 6 we have to restart the algorithm with a different random number a.

Eventually, we must hit an a, of order r in G, such that b \equiv a^{r/2} \not\equiv 1, -1 \pmod{N}. This is because such a b is a square root of 1 modulo N, other than 1 and -1, whose existence is guaranteed by the Chinese remainder theorem, since N is not a prime power.

We claim that d = \operatorname{gcd}(b-1, N) is a proper factor of N, that is, d \ne 1, N. In fact if d = N, then N divides b - 1, so that b \equiv 1 \pmod{N}, against the construction of b. If on the other hand d = \operatorname{gcd}(b-1, N) = 1, then by Bézout's identity there are integers u, v such that

(b-1) u + N v = 1.

Multiplying both sides by b+1 we obtain

(b^{2}-1) u + N(b+1) v = b+1.

Since N divides b^{2} - 1 \equiv a^{r} - 1 \pmod{N}, we obtain that N divides b+1, so that b \equiv -1 \pmod{N}, again contradicting the construction of b.

Thus d is the required proper factor of N.

Finding the period

Shor's period-finding algorithm relies heavily on the ability of a quantum computer to be in many states simultaneously. Physicists call this behavior a "superposition" of states. To compute the period of a function f, we evaluate the function at all points simultaneously.

Quantum physics does not allow us to access all this information directly, though. A measurement will yield only one of all possible values, destroying all others. If not for the no cloning theorem, we could first measure f(x) without measuring x, and then make a few copies of the resulting state (which is a superposition of states all having the same f(x)). Measuring x on these states would provide different x values which give the same f(x), leading to the period. Because we cannot make exact copies of a quantum state, this method does not work. Therefore we have to carefully transform the superposition to another state that will return the correct answer with high probability. This is achieved by the quantum Fourier transform.

Shor thus had to solve three "implementation" problems. All of them had to be implemented "fast", which means that they can be implemented with a number of quantum gates that is polynomial in \log N.

  1. Create a superposition of states. This can be done by applying Hadamard gates to all qubits in the input register. Another approach would be to use the quantum Fourier transform (see below).
  2. Implement the function f as a quantum transform. To achieve this, Shor used repeated squaring for his modular exponentiation transformation. It is important to note that this step is more difficult to implement than the quantum Fourier transform, in that it requires ancillary qubits and substantially more gates to accomplish.
  3. Perform a quantum Fourier transform. By using controlled rotation gates and Hadamard gates, Shor designed a circuit for the quantum Fourier transform (with Q = 2q) that uses just q(q-1)/2 = O((\log Q)^2) gates.[11]

After all these transformations a measurement will yield an approximation to the period r. For simplicity assume that there is a y such that yr/Q is an integer. Then the probability to measure y is 1. To see that we notice that then

e^{-2 \pi i b yr/Q} = 1\,

for all integers b. Therefore the sum whose square gives us the probability to measure y will be Q/r since b takes roughly Q/r values and thus the probability is 1/r^2. There are r y such that yr/Q is an integer and also r possibilities for f(x_0), so the probabilities sum to 1.

Note: another way to explain Shor's algorithm is by noting that it is just the quantum phase estimation algorithm in disguise.

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.[12][13] 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 less than 600 qubits due to high constants.

Discrete logarithms

Given prime p with generator g where 1 < g , suppose we know that x = g^r \pmod{p}, for some r, and we wish to compute r, which is the discrete logarithm: r = \log_g x \pmod{p}. Consider the Abelian group \left( \mathbb{Z}_{p} \right)^\times \times \left(\mathbb{Z}_p\right)^\times where each factor corresponds to modular multiplication of nonzero values, assuming p is prime. Now, consider the function

f(a,b) = g^a x^{-b} \pmod{p}.

This gives us an Abelian hidden subgroup problem, as f corresponds to a group homomorphism. The kernel corresponds to modular multiples of (r,1). So, if we can find the kernel, we can find r.

In popular culture

On the television show Stargate Universe, the lead scientist, Dr. Nicholas Rush, hoped to use Shor's algorithm to crack Destiny's master code. He taught a quantum cryptography class at the University of California, Berkeley, in which Shor's algorithm was studied.

Shor's algorithm was also a correct answer to a question in a Physics Bowl competition in the episode "The Bat Jar Conjecture" of the TV series The Big Bang Theory.


  1. ^ See also Pseudo-polynomial time.
  2. ^ MathWorld: Number Field Sieve
  3. ^ - Efficient Networks for Quantum Factoring
  4. ^ - Bruce Schneier: Quantum Computing: Hype vs. Reality
  5. ^ Vandersypen, Lieven M. K.; Steffen, Matthias; Breyta, Gregory; Yannoni, Costantino S.; Sherwood, Mark H. &  
  6. ^ Braunstein, S. L.; Caves, C. M.; Jozsa, R.; Linden, N.; Popescu, S.; Schack, R. (1999), "Separability of Very Noisy Mixed States and Implications for NMR Quantum Computing", Phys. Rev. Lett 83 (5): 1054–1057,  
  7. ^ Lu, Chao-Yang; Browne, Daniel E.; Yang, Tao & Pan, Jian-Wei (2007), "Demonstration of a Compiled Version of Shor's Quantum Factoring Algorithm Using Photonic Qubits",  
  8. ^ Lanyon, B. P.; Weinhold, T. J.; Langford, N. K.; Barbieri, M.; James, D. F. V.; Gilchrist, A. & White, A. G. (2007), "Experimental Demonstration of a Compiled Version of Shor's Algorithm with Quantum Entanglement", Physical Review Letters 99 (25): 250505,  
  9. ^ - Computing prime factors with a Josephson phase qubit quantum processor
  10. ^ Martín-López, Enrique; Enrique Martín-López, Anthony Laing, Thomas Lawson, Roberto Alvarez, Xiao-Qi Zhou & Jeremy L. O'Brien (12 October 2012). "Experimental realization of Shor's quantum factoring algorithm using qubit recycling". Nature Photonics.  
  11. ^ Shor 1999, p. 14.
  12. ^ Markov, Igor L.; Saeedi, Mehdi (2012). "Constant-Optimized Quantum Circuits for Modular Multiplication and Exponentiation". Quantum Information and Computation 12 (5–6): 361–394.  
  13. ^ Markov, Igor L.; Saeedi, Mehdi (2013). "Faster Quantum Number Factoring via Circuit Synthesis". Phys. Rev. A 87: 012310.  

Further reading

  • Nielsen, Michael A. & Chuang, Isaac L. (2000), Quantum Computation and Quantum Information, Cambridge University Press .
  • Phillip Kaye, Raymond Laflamme, Michele Mosca, An introduction to quantum computing, Oxford University Press, 2007, ISBN 0-19-857049-X
  • "Explanation for the man in the street" by Scott Aaronson, "approved" by Peter Shor. (Shor wrote "Great article, Scott! That’s the best job of explaining quantum computing to the man on the street that I’ve seen."). An alternate metaphor for the QFT was presented in one of the comments. Scott Aaronson suggests the following 12 references as further reading (out of "the 10105000 quantum algorithm tutorials that are already on the web."):
  • Shor, Peter W. (1997), "Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer", SIAM J. Comput. 26 (5): 1484–1509,  . Revised version of the original paper by Peter Shor ("28 pages, LaTeX. This is an expanded version of a paper that appeared in the Proceedings of the 35th Annual Symposium on Foundations of Computer Science, Santa Fe, NM, Nov. 20--22, 1994. Minor revisions made January, 1996").
  • Quantum Computing and Shor's Algorithm, Matthew Hayward, 2005-02-17,, LaTeX2HTML version of the original 2750 line LaTeX document, also available as a 61 page PDF or postscript document.
  • Quantum Computation and Shor's Factoring Algorithm, Ronald de Wolf, CWI and University of Amsterdam, January 12, 1999, 9 page postscript document.
  • Shor's Factoring Algorithm, Notes from Lecture 9 of Berkeley CS 294-2, dated 4 Oct 2004, 7 page postscript document.
  • Chapter 6 Quantum Computation, 91 page postscript document, Caltech, Preskill, PH229.
  • Quantum computation: a tutorial by Samuel L. Braunstein.
  • The Quantum States of Shor's Algorithm, by Neal Young, Last modified: Tue May 21 11:47:38 1996.
  • III. Breaking RSA Encryption with a Quantum Computer: Shor's Factoring Algorithm, Lecture notes on Quantum computation, Cornell University, Physics 481-681, CS 483; Spring, 2006 by N. David Mermin. Last revised 2006-03-28, 30 page PDF document.
  • arXiv quant-ph/0303175 Shor's Algorithm for Factoring Large Integers. C. Lavor, L.R.U. Manssur, R. Portugal. Submitted on 29 Mar 2003. This work is a tutorial on Shor's factoring algorithm by means of a worked out example. Some basic concepts of Quantum Mechanics and quantum circuits are reviewed. It is intended for non-specialists which have basic knowledge on undergraduate Linear Algebra. 25 pages, 14 figures, introductory review.
  • arXiv quant-ph/0010034 Shor's Quantum Factoring Algorithm, Samuel J. Lomonaco, Jr, Submitted October 9, 2000, This paper is a written version of a one hour lecture given on Peter Shor's quantum factoring algorithm. 22 pages.
  • Chapter 20 Quantum Computation, from Computational Complexity: A Modern Approach, Draft of a book: Dated January 2007, Comments welcome!, Sanjeev Arora and Boaz Barak, Princeton University.
  • A Step Toward Quantum Computing: Entangling 10 Billion Particles, from "Discover Magazine", Dated January 19, 2011.
  • Quantum Computing ChallengesJosef Gruska - also in Mathematics unlimited: 2001 and beyond, Editors Björn Engquist, Wilfried Schmid, Springer, 2001, ISBN 978-3-540-66913-5
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.