Shor's algorithm is a
quantum algorithm for finding the
prime factor
A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways ...
s of an integer. It was developed in 1994 by the American mathematician
Peter Shor.
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.
[
] 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
, Shor's algorithm runs in
polynomial time, meaning the time taken is polynomial in
. It takes
quantum gates
In quantum computing and specifically the quantum circuit model of computation, a quantum logic gate (or simply quantum gate) is a basic quantum circuit operating on a small number of qubits. Quantum logic gates are the building blocks of quantu ...
of order
using fast multiplication,
or even
utilizing the asymptotically fastest multiplication algorithm currently known due to Harvey and
van der Hoeven,
thus demonstrating that the
integer factorization
In mathematics, integer factorization is the decomposition of a positive integer into a product of integers. Every positive integer greater than 1 is either the product of two or more integer factors greater than 1, in which case it is a comp ...
problem can be efficiently solved on a quantum computer and is consequently in the
complexity class
In computational complexity theory, a complexity class is a set (mathematics), set of computational problems "of related resource-based computational complexity, complexity". The two most commonly analyzed resources are time complexity, time and s ...
BQP. This is significantly faster than the most efficient known classical factoring algorithm, the
general number field sieve, which works in
sub-exponential time:
.
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
Public-key cryptography, or asymmetric cryptography, is the field of cryptographic systems that use pairs of related keys. Each key pair consists of a public key and a corresponding private key. Key pairs are generated with cryptographic alg ...
schemes, such as
* The
RSA scheme
* The finite-field
Diffie–Hellman key exchange
* The elliptic-curve Diffie–Hellman key exchange
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 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
Post-quantum cryptography (PQC), sometimes referred to as quantum-proof, quantum-safe, or quantum-resistant, is the development of cryptographic algorithms (usually public-key algorithms) that are currently thought to be secure against a crypt ...
.
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
International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American Multinational corporation, multinational technology company headquartered in Armonk, New York, and present in over 175 countries. It is ...
, who factored
into
, 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
was performed with solid-state qubits. Later, in 2012, the factorization of
was achieved. In 2016, the factorization of
was performed again using trapped-ion qubits with a recycling technique. In 2019, an attempt was made to factor the number
using Shor's algorithm on an IBM
Q System One, but the algorithm failed because of accumulating errors. 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. Furthermore, attempts using quantum computers with other algorithms have been made. 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.
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 .
These primes
have the property that
has a prime factor larger than
, 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
A composite number is a positive integer that can be formed by multiplying two smaller positive integers. Accordingly it is a positive integer that has at least one divisor other than 1 and itself. Every positive integer is composite, prime numb ...
, find its
integer factors''.
To achieve this, Shor's algorithm consists of two parts:
# 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.
# 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
into just two integers
and
greater than 1, since if either
or
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
is even, in which case 2 is trivially a factor. Let us thus assume that
is odd for the remainder of this discussion. Afterwards, we can use efficient classical algorithms to check whether
is a
prime power. For prime powers, efficient classical factorization algorithms exist, hence the rest of the quantum algorithm may assume that
is not a prime power.
If those easy cases do not produce a nontrivial factor of
, the algorithm proceeds to handle the remaining case. We pick a random integer
. A possible nontrivial divisor of
can be found by computing
, which can be done classically and efficiently using the
Euclidean algorithm
In mathematics, the Euclidean algorithm,Some widely used textbooks, such as I. N. Herstein's ''Topics in Algebra'' and Serge Lang's ''Algebra'', use the term "Euclidean algorithm" to refer to Euclidean division or Euclid's algorithm, is a ...
. If this produces a nontrivial factor (meaning
), the algorithm is finished, and the other nontrivial factor is
. If a nontrivial factor was not identified, then this means that
and the choice of
are
coprime
In number theory, two integers and are coprime, relatively prime or mutually prime if the only positive integer that is a divisor of both of them is 1. Consequently, any prime number that divides does not divide , and vice versa. This is equiv ...
, so
is contained in the
multiplicative group of integers modulo , having a
multiplicative inverse
In mathematics, a multiplicative inverse or reciprocal for a number ''x'', denoted by 1/''x'' or ''x''−1, is a number which when Multiplication, multiplied by ''x'' yields the multiplicative identity, 1. The multiplicative inverse of a ra ...
modulo
. Thus,
has a
multiplicative order
In number theory, given a positive integer ''n'' and an integer ''a'' coprime to ''n'', the multiplicative order of ''a'' modulo ''n'' is the smallest positive integer ''k'' such that a^k\ \equiv\ 1 \pmod n.
In other words, the multiplicative orde ...
modulo
, meaning
:
and
is the smallest positive integer satisfying this congruence.
The quantum subroutine finds
. It can be seen from the congruence that
divides , written
. This can be factored using
difference of squares:
Since we have factored the expression in this way, the algorithm doesn't work for odd
(because
must be an integer), meaning that the algorithm would have to restart with a new
. Hereafter we can therefore assume that
is even. It cannot be the case that
, since this would imply
, which would contradictorily imply that
would be the order of
, which was already
. At this point, it may or may not be the case that
. If
does not divide
, then this means that we are able to find a nontrivial factor of
. We compute
If
, then
was true, and a nontrivial factor of
cannot be achieved from
, and the algorithm must restart with a new
. Otherwise, we have found a nontrivial factor of
, with the other being
, and the algorithm is finished. For this step, it is also equivalent to compute
; it will produce a nontrivial factor if
is nontrivial, and will not if it's trivial (where
).
The algorithm restated shortly follows: let
be odd, and not a prime power. We want to output two nontrivial factors of
.
# Pick a random number
.
# Compute
, the
greatest common divisor
In mathematics, the greatest common divisor (GCD), also known as greatest common factor (GCF), of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For two integers , , the greatest co ...
of
and
.
# If
, then
is a
nontrivial factor of
, with the other factor being
, and we are done.
# Otherwise, use the quantum subroutine to find the order
of
.
# If
is odd, then go back to step 1.
# Compute
. If
is nontrivial, the other factor is
, 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
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
In number theory, two integers and are coprime, relatively prime or mutually prime if the only positive integer that is a divisor of both of them is 1. Consequently, any prime number that divides does not divide , and vice versa. This is equiva ...
and