Shor's algorithm

From HandWiki
Short description: Quantum algorithm for integer factorization

Shor's algorithm is a quantum computer algorithm for finding the prime factors of an integer. It was developed in 1994 by the American mathematician Peter Shor.[1]

On a quantum computer, to factor an integer [math]\displaystyle{ N }[/math], Shor's algorithm runs in polylogarithmic time, meaning the time taken is polynomial in [math]\displaystyle{ \log N }[/math], the size of the integer given as input.[2] Specifically, it takes quantum gates of order [math]\displaystyle{ O \! \left((\log N)^{2} (\log \log N) (\log \log \log N) \right) }[/math] using fast multiplication,[3] or even [math]\displaystyle{ O \! \left((\log N)^{2} (\log \log N) \right) }[/math] utilizing the asymptotically fastest multiplication algorithm currently known due to Harvey and Van Der Hoven,[4] 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 almost exponentially faster than the most efficient known classical factoring algorithm, the general number field sieve, which works in sub-exponential time: [math]\displaystyle{ O \! \left(e^{1.9 (\log N)^{1/3} (\log \log N)^{2/3}} \right) }[/math].[5] The efficiency of Shor's algorithm is due to the efficiency of the quantum Fourier transform, and modular exponentiation by repeated squarings.[6]

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

  • The RSA scheme
  • The Finite Field Diffie-Hellman key exchange
  • The Elliptic Curve Diffie-Hellman key exchange[7]

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.

In 2001, Shor's algorithm was demonstrated by a group at IBM, who factored [math]\displaystyle{ 15 }[/math] into [math]\displaystyle{ 3 \times 5 }[/math], using an NMR implementation of a quantum computer with [math]\displaystyle{ 7 }[/math] qubits.[8] 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.[9][10] In 2012, the factorization of [math]\displaystyle{ 15 }[/math] was performed with solid-state qubits.[11] Later, in 2012, the factorization of [math]\displaystyle{ 21 }[/math] was achieved.[12] In 2019 an attempt was made to factor the number [math]\displaystyle{ 35 }[/math] using Shor's algorithm on an IBM Q System One, but the algorithm failed because of accumulating errors.[13] Though larger numbers have been factored by quantum computers using other algorithms,[14] 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.[15]


The problem that we are trying to solve is, given a composite number [math]\displaystyle{ N }[/math], to find a non-trivial divisor of [math]\displaystyle{ N }[/math] (a divisor strictly between [math]\displaystyle{ 1 }[/math] and [math]\displaystyle{ N }[/math]). Before attempting to find such a divisor, if there's any doubt whether [math]\displaystyle{ N }[/math] is composite or prime, one can use relatively quick primality-testing algorithms to verify that [math]\displaystyle{ N }[/math] is indeed composite, although this is not a part of Shor's algorithm.

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.

The aim of the algorithm is to find a non-trivial square root [math]\displaystyle{ b }[/math] of [math]\displaystyle{ 1 }[/math] modulo [math]\displaystyle{ N }[/math] that is different from [math]\displaystyle{ 1 }[/math] and [math]\displaystyle{ - 1 }[/math], because then

[math]\displaystyle{ b^2 - 1 = (b+1)(b-1) = mN }[/math]

for a non-zero integer [math]\displaystyle{ m }[/math] that gives us two distinct non-trivial divisors [math]\displaystyle{ \gcd(N, b+1) }[/math] and [math]\displaystyle{ \gcd(N, b-1) }[/math] of [math]\displaystyle{ N }[/math]. This idea is similar to other factoring algorithms, such as the quadratic sieve, and a more detailed explanation can be found in the Explanation section below. Before starting the algorithm, it is imperative to check [math]\displaystyle{ N }[/math] to be odd (otherwise [math]\displaystyle{ 2 }[/math] is a divisor) and not to be any power of an integer (otherwise that integer is a divisor), so as to guarantee the existence of a non-trivial square root [math]\displaystyle{ b }[/math] of [math]\displaystyle{ 1 }[/math] modulo [math]\displaystyle{ N }[/math].

In turn, finding such a [math]\displaystyle{ b }[/math] is reduced to finding an element [math]\displaystyle{ a }[/math] as a parameter in an integer function, such that the function has an 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 [math]\displaystyle{ a }[/math], as this is a difficult problem on a classical computer.

Classical part

  1. Pick a random number [math]\displaystyle{ 1 \lt a \lt N }[/math].
  2. Compute [math]\displaystyle{ K=\gcd(a,N) }[/math], the greatest common divisor of [math]\displaystyle{ a }[/math] and [math]\displaystyle{ N }[/math]. This may be done using the Euclidean algorithm.
  3. If [math]\displaystyle{ K \neq 1 }[/math], then [math]\displaystyle{ K }[/math] is a nontrivial factor of [math]\displaystyle{ N }[/math], so we are done.
  4. Otherwise, use the quantum period-finding subroutine (below) to find [math]\displaystyle{ r }[/math], which denotes the period of the following function:
    [math]\displaystyle{ f(x) = a^{x} (\bmod N). }[/math]
    Equivalently, [math]\displaystyle{ r }[/math] is the smallest positive integer that satisfies [math]\displaystyle{ a^{r} \equiv 1 (\bmod N }[/math]).
  5. If [math]\displaystyle{ r }[/math] is odd, then go back to step 1.
  6. If [math]\displaystyle{ a^{r / 2} = - 1 \bmod N }[/math], then go back to step 1.
  7. Otherwise, both [math]\displaystyle{ \gcd(a^{r / 2} + 1,N) }[/math] or [math]\displaystyle{ \gcd(a^{r / 2} - 1,N) }[/math] are nontrivial factors of [math]\displaystyle{ N }[/math], so we are done.

For example: Given [math]\displaystyle{ N = 15 }[/math], [math]\displaystyle{ a = 7 }[/math], and [math]\displaystyle{ r = 4 }[/math], i.e.,[math]\displaystyle{ \bmod(1,15)=\bmod(7^4,15)=\bmod(7^8,15), }[/math] we have [math]\displaystyle{ \gcd(7^{2} \pm 1,15) = \gcd(49 \pm 1,15) }[/math], where [math]\displaystyle{ \gcd(48,15) = 3 }[/math] and [math]\displaystyle{ \gcd(50, 15) = 5 }[/math]. For [math]\displaystyle{ N }[/math] that is a product of two distinct primes, [math]\displaystyle{ p }[/math] and [math]\displaystyle{ q }[/math], [math]\displaystyle{ \varphi(N)=(p-1)(q-1) }[/math], which for [math]\displaystyle{ N = 15 }[/math] is [math]\displaystyle{ 8 }[/math], and [math]\displaystyle{ r }[/math] divides [math]\displaystyle{ 8 }[/math].

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 [math]\displaystyle{ N }[/math] and each choice of the random [math]\displaystyle{ a }[/math] which have the relationship [math]\displaystyle{ f(x) = a^{x} \bmod N }[/math]. Given value [math]\displaystyle{ N }[/math], a value [math]\displaystyle{ Q = 2^{q} }[/math] is chosen such that [math]\displaystyle{ N^{2} \leq Q \lt 2 N^{2} }[/math]. Such a value of [math]\displaystyle{ Q }[/math] implies that [math]\displaystyle{ \dfrac{Q}{r} \gt N }[/math]. The input and output qubit registers will store superpositions of values from [math]\displaystyle{ 0 }[/math] to [math]\displaystyle{ Q - 1 }[/math]. Therefore, these registers have [math]\displaystyle{ q }[/math] qubits each. Using what might appear to be twice as many qubits as necessary guarantees that there are at least [math]\displaystyle{ N }[/math] different values of [math]\displaystyle{ x }[/math] that produce the same [math]\displaystyle{ f(x) }[/math], even as the period [math]\displaystyle{ r }[/math] approaches [math]\displaystyle{ \dfrac{N}{2} }[/math]. The following uses bra-ket notation to denote quantum states.

Proceed as follows:

  1. Initialize the registers to
    [math]\displaystyle{ \frac{1}{\sqrt{Q}} \sum_{x = 0}^{Q - 1} | x \rangle = \left(\frac{1}{\sqrt{2}} \sum_{x_{1} = 0}^{1} | x_{1} \rangle \right) \otimes \cdots \otimes \left(\frac{1}{\sqrt{2}} \sum_{x_{q} = 0}^{1} | x_{q} \rangle \right). }[/math]

    where [math]\displaystyle{ \otimes }[/math] denotes the tensor product.

    This initial state is a superposition of [math]\displaystyle{ Q }[/math] states, and is obtained by generating [math]\displaystyle{ q }[/math] independent qubits, each an equal superposition of [math]\displaystyle{ 0 }[/math] and [math]\displaystyle{ 1 }[/math] states. We can accomplish this by initializing the qubits to the zero-position, and then applying a Hadamard gate to each of the [math]\displaystyle{ q }[/math] qubits, where [math]\displaystyle{ 2^{q} = Q }[/math]. This process is called the Hadamard transform.
  2. Construct [math]\displaystyle{ f(x) }[/math] as a quantum function and apply it to the above state, to obtain
    [math]\displaystyle{ U_f | x, 0^n \rangle = | x,f(x) \rangle }[/math]
    [math]\displaystyle{ U_f \frac{1}{\sqrt{Q}} \sum_{x = 0}^{Q - 1} | x, 0^n \rangle = \frac{1}{\sqrt{Q}} \sum_{x = 0}^{Q - 1} | x,f(x) \rangle }[/math]
    Note: [math]\displaystyle{ f(x) = a^{x} \bmod N }[/math]. This is still a superposition of [math]\displaystyle{ Q }[/math] states. However, the [math]\displaystyle{ q + n }[/math] qubits, i.e, the [math]\displaystyle{ q }[/math] input qubits and [math]\displaystyle{ n }[/math] ([math]\displaystyle{ \gt {\log_{2}}(N) }[/math]) output qubits, are now entangled or not separable, as the state cannot be written as a tensor product of states of individual qubits. Importantly, the value containing the [math]\displaystyle{ r }[/math] we are looking for is now stored in the phase of the input qubits [math]\displaystyle{ x }[/math] as a result of "phase kickback", where using qubits as control inputs to unitary gates alters the relative phase of the control qubits.[16]
  3. Apply the inverse quantum Fourier transform to the input register. This transform (operating on a superposition of [math]\displaystyle{ Q = 2^{q} }[/math] states) uses a [math]\displaystyle{ Q }[/math]-th root of unity such as [math]\displaystyle{ \omega = e^{\frac{2 \pi i}{Q}} }[/math] to distribute the amplitude of any given [math]\displaystyle{ | x \rangle }[/math] state equally among all [math]\displaystyle{ Q }[/math] of the [math]\displaystyle{ | y \rangle }[/math] states, and to do so in a different way for each different [math]\displaystyle{ x }[/math]. We thus obtain
    [math]\displaystyle{ {U_{\operatorname{QFT}}}(| x \rangle) = \frac{1}{\sqrt{Q}} \sum_{y = 0}^{Q - 1} \omega^{x y} | y \rangle. }[/math]
    This leads to the final state
    [math]\displaystyle{ \frac{1}{Q} \sum_{x = 0}^{Q - 1} \sum_{y = 0}^{Q - 1} \omega^{x y} | y,f(x) \rangle. }[/math]
    Now, we reorder this sum as
    [math]\displaystyle{ \frac{1}{Q} \sum_{z = 0}^{N - 1} \sum_{y = 0}^{Q - 1} \left[ \sum_{x \in \{ 0,\ldots,Q - 1 \}; ~ f(x) = z} \omega^{x y} \right] | y,z \rangle. }[/math]
    This is a superposition of many more than [math]\displaystyle{ Q }[/math] states, but many fewer than [math]\displaystyle{ Q^{2} }[/math] states, as there are fewer than [math]\displaystyle{ Q }[/math] distinct values of [math]\displaystyle{ z = f(x) }[/math]. Let
    • [math]\displaystyle{ \omega = e^{\frac{2 \pi i}{Q}} }[/math] be a [math]\displaystyle{ Q }[/math]-th root of unity,
    • [math]\displaystyle{ r }[/math] be the period of [math]\displaystyle{ f }[/math],
    • [math]\displaystyle{ x_{0} }[/math] be the smallest of the [math]\displaystyle{ x \in \{ 0,\ldots,Q - 1 \} }[/math] for which [math]\displaystyle{ f(x) = z }[/math] (we have [math]\displaystyle{ x_{0} \lt r }[/math]), and
    • write [math]\displaystyle{ m - 1 = \left\lfloor \frac{Q - x_{0} - 1}{r} \right\rfloor }[/math]
    • [math]\displaystyle{ b }[/math] indexes these [math]\displaystyle{ x }[/math], running from [math]\displaystyle{ 0 }[/math] to [math]\displaystyle{ m - 1 }[/math], so that [math]\displaystyle{ x_{0} + r b \lt Q }[/math].
    Then [math]\displaystyle{ \omega^{r y} }[/math] is a unit vector in the complex plane ([math]\displaystyle{ \omega }[/math] is a root of unity, and [math]\displaystyle{ r }[/math] and [math]\displaystyle{ y }[/math] are integers), and the coefficient of [math]\displaystyle{ \dfrac{1}{Q} \left| y,z \right\rangle }[/math] in the final state is
    [math]\displaystyle{ \sum_{x \in \{ 0,\ldots,Q - 1 \}; ~ f(x) = z} \omega^{x y} = \sum_{b = 0}^{m - 1} \omega^{(x_{0} + r b) y} = \omega^{x_{0} y} \sum_{b = 0}^{m - 1} \omega^{r b y}. }[/math]
    Each term in this sum represents a different path to the same result, and quantum interference occurs — constructive when the unit vectors [math]\displaystyle{ \omega^{r y b} }[/math] point in nearly the same direction in the complex plane, which requires that [math]\displaystyle{ \omega^{r y} }[/math] point along the positive real axis.
  4. Perform a measurement. We obtain some outcome [math]\displaystyle{ y }[/math] in the input register and some outcome [math]\displaystyle{ z }[/math] in the output register. As [math]\displaystyle{ f }[/math] is periodic, the probability of measuring some state [math]\displaystyle{ | y,z \rangle }[/math] is given by
    [math]\displaystyle{ \mathrm{Pr}(| y, z \rangle)= \left| \frac{1}{Q} \sum_{x \in \{ 0,\ldots,Q - 1 \}; ~ f(x) = z} \omega^{x y} \right|^{2} = \frac{1}{Q^{2}} \left| \sum_{b = 0}^{m - 1} \omega^{(x_{0} + r b) y} \right|^{2} = \frac{1}{Q^{2}} |\omega^{x_{0} y}|^{2}\left| \sum_{b = 0}^{m - 1} \omega^{b r y} \right|^{2} }[/math]
    [math]\displaystyle{ = \frac{1}{Q^{2}} \left| \sum_{b = 0}^{m - 1} \omega^{b r y} \right|^{2} = \frac{1}{Q^{2}} \left|\frac{\omega^{m r y} - 1}{\omega^{r y} - 1} \right|^{2} = \frac{1}{Q^{2}} \frac{\sin^2(\frac{\pi m r y}{Q})}{\sin^2(\frac{\pi r y}{Q})} }[/math]
    Analysis now shows that this probability is higher the closer the unit vector [math]\displaystyle{ \omega^{r y} }[/math] is to the positive real axis, or the closer [math]\displaystyle{ \dfrac{y r}{Q} }[/math] is to an integer. Unless [math]\displaystyle{ r }[/math] is a power of [math]\displaystyle{ 2 }[/math], it will not be a factor of [math]\displaystyle{ Q }[/math].
  5. Since [math]\displaystyle{ \frac{y r}{Q} }[/math] is close to some integer [math]\displaystyle{ c }[/math], the known value [math]\displaystyle{ \dfrac{y}{Q} }[/math] is close to the unknown value [math]\displaystyle{ \dfrac{c}{r} }[/math]. Performing [classical] continued fraction expansion on [math]\displaystyle{ \dfrac{y}{Q} }[/math] allows us to find approximations [math]\displaystyle{ \dfrac{d}{s} }[/math] of it that satisfy two conditions:
    1. [math]\displaystyle{ s \lt N }[/math].
    2. [math]\displaystyle{ \left| \dfrac{y}{Q} - \dfrac{d}{s} \right| \lt \dfrac{1}{2 Q} }[/math].
    Given these multiple conditions (and assuming [math]\displaystyle{ \dfrac{d}{s} }[/math] is irreducible), [math]\displaystyle{ s }[/math] is very likely to be the appropriate period [math]\displaystyle{ r }[/math], or at least a factor of it.
  6. Check (classically) if [math]\displaystyle{ f(x) = f(x + s) \Leftrightarrow a^{s} \equiv 1 \bmod N }[/math]. If so, then we are done.
  7. Otherwise, (classically) obtain more candidates for [math]\displaystyle{ r }[/math] by using multiples of [math]\displaystyle{ s }[/math], or by using other [math]\displaystyle{ s }[/math] with [math]\displaystyle{ \dfrac{d}{s} }[/math] near [math]\displaystyle{ \dfrac{y}{Q} }[/math]. If any candidate works, then 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.

Non-trivial square root of [1 modulo N]

Shor's algorithm hinges on finding a non-trivial square root of [math]\displaystyle{ 1 }[/math] modulo [math]\displaystyle{ N }[/math]; That is, a solution to

[math]\displaystyle{ b^2\equiv 1\bmod N }[/math]

where [math]\displaystyle{ b\not\equiv \plusmn1\bmod N }[/math].

If such [math]\displaystyle{ b }[/math] exists, we claim that [math]\displaystyle{ d = \gcd(b - 1,N) }[/math] is a proper factor of [math]\displaystyle{ N }[/math], i.e., [math]\displaystyle{ d \neq 1,N }[/math]. In fact, if

[math]\displaystyle{ d = N }[/math], then [math]\displaystyle{ N }[/math] divides [math]\displaystyle{ b - 1 }[/math], so that [math]\displaystyle{ b \equiv 1 \bmod N }[/math], which goes against the construction of [math]\displaystyle{ b }[/math]. If, on the other hand, [math]\displaystyle{ d = \gcd(b - 1,N) = 1 }[/math], then by Bézout's identity, there are integers [math]\displaystyle{ u,v }[/math] such that

[math]\displaystyle{ (b - 1) u + N v = 1. }[/math]

Multiplying both sides by [math]\displaystyle{ b + 1 }[/math], we obtain

[math]\displaystyle{ (b^{2} - 1) u + N (b + 1) v = b + 1. }[/math]

As [math]\displaystyle{ N }[/math] divides [math]\displaystyle{ b^{2} - 1 }[/math], we find that [math]\displaystyle{ N }[/math] divides [math]\displaystyle{ b + 1 }[/math], so that [math]\displaystyle{ b \equiv - 1 \bmod N }[/math], again contradicting the construction of [math]\displaystyle{ b }[/math].

Therefore, [math]\displaystyle{ d }[/math] is the required proper factor of [math]\displaystyle{ N }[/math]. Similarly, it can be proven that [math]\displaystyle{ \gcd(b + 1,N) }[/math] is also a proper factor of [math]\displaystyle{ N }[/math].

For such a non-trivial square root of [math]\displaystyle{ 1 }[/math] modulo [math]\displaystyle{ N }[/math] to exist, notice that [math]\displaystyle{ -1\equiv 1\bmod 2 }[/math], and for any power of an odd prime [math]\displaystyle{ N=p^{n} }[/math], there is no non-trivial square root of [math]\displaystyle{ 1 }[/math] modulo [math]\displaystyle{ N }[/math]: For any [math]\displaystyle{ (b+1)(b-1)=m p^{n}, }[/math] either [math]\displaystyle{ b-1 }[/math] or [math]\displaystyle{ b+1 }[/math] has to be a multiple of [math]\displaystyle{ N=p^{n} }[/math].

Therefore, for Shor's algorithm to work, we need [math]\displaystyle{ N }[/math] to be odd (otherwise [math]\displaystyle{ 2 }[/math] is a divisor) and not to be any power of an odd prime (otherwise that prime is a divisor). We can check that there are no integer roots [math]\displaystyle{ \sqrt[k]{N} }[/math] for [math]\displaystyle{ 2 \leq k \leq {\log_{3}}(N) }[/math], and if [math]\displaystyle{ N }[/math] is not a power of any integer, it is not a power of any odd prime. Here, the upper bound for the integer [math]\displaystyle{ k }[/math] that we need to check is determined by [math]\displaystyle{ \sqrt[k]{N}\geq3 }[/math], since for [math]\displaystyle{ N }[/math] to be odd, [math]\displaystyle{ \sqrt[k]{N} }[/math] cannot be [math]\displaystyle{ 2 }[/math]. This check, however, cannot rule out that [math]\displaystyle{ N }[/math] may be an odd prime itself, which can only be ruled out by primality-testing algorithms.

Given that [math]\displaystyle{ N }[/math] is odd and not any power of an odd prime, based on the fundamental theorem of arithmetic, we may assume that [math]\displaystyle{ N }[/math] is the product of two coprime integers greater than [math]\displaystyle{ 2 }[/math] ([math]\displaystyle{ N=n_1n_2 }[/math] and [math]\displaystyle{ n_1,n_2\gt 2,\, \gcd(n_1,n_2)=1 }[/math]). From the four combinations of choosing plus sign and minus sign in the integer equations [math]\displaystyle{ x=m_1n_1\plusmn1=m_2n_2\plusmn1 }[/math], based on the Chinese remainder theorem and [math]\displaystyle{ n_1,n_2\gt 2 }[/math], there are at least four distinct square roots of [math]\displaystyle{ 1 }[/math] modulo [math]\displaystyle{ N }[/math], and therefore at least two distinct non-trivial square roots exist. In fact, they are the solutions to [math]\displaystyle{ b'=m_1n_1+1=m_2n_2-1 }[/math] and [math]\displaystyle{ b''=m_1n_1-1=m_2n_2+1 }[/math].

Obtaining factors from period

The integers less than [math]\displaystyle{ N }[/math] and coprime with [math]\displaystyle{ N }[/math] form the multiplicative group of integers modulo [math]\displaystyle{ N }[/math], which is a finite abelian group [math]\displaystyle{ G }[/math]. The size of this group is given by [math]\displaystyle{ \varphi(N) }[/math]. By the end of step 3, we have an integer [math]\displaystyle{ a }[/math] in this group. As the group is finite, [math]\displaystyle{ a }[/math] must have a finite order [math]\displaystyle{ r }[/math], which is the smallest positive integer such that

[math]\displaystyle{ a^{r} \equiv 1 \bmod N. }[/math]

This is the order [math]\displaystyle{ r }[/math] of the finite cyclic subgroupa⟩ of the group [math]\displaystyle{ (\mathbb{Z} {N})^{\times} }[/math], which is the smallest positive integer [math]\displaystyle{ r }[/math] for which [math]\displaystyle{ a^{x + r} \bmod N \equiv a^{x} \bmod N }[/math]. Since [math]\displaystyle{ a }[/math] and [math]\displaystyle{ N }[/math] are coprime, by Euler's totient theorem, [math]\displaystyle{ r }[/math] must exist, and divides [math]\displaystyle{ \varphi(N) }[/math], where [math]\displaystyle{ \varphi }[/math] denotes Euler's totient function.

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

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

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 [math]\displaystyle{ f }[/math], we evaluate the function at all points simultaneously.

Quantum physics does not allow us to access all this information directly, however. A measurement will yield only one of all possible values, destroying all others. If not for the no-cloning theorem, we could first measure [math]\displaystyle{ f(x) }[/math] without measuring [math]\displaystyle{ x }[/math], and then make a few copies of the resulting state (which is a superposition of states all having the same [math]\displaystyle{ f(x) }[/math]). Measuring [math]\displaystyle{ x }[/math] on these states would provide different [math]\displaystyle{ x }[/math] values which give the same [math]\displaystyle{ f(x) }[/math], 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 [math]\displaystyle{ \log N }[/math].

  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 [math]\displaystyle{ f }[/math] 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 [math]\displaystyle{ Q = 2^{q} }[/math]) that uses just [math]\displaystyle{ \dfrac{q (q - 1)}{2} = O \! \left((\log Q)^{2} \right) }[/math] gates.[17]

After all these transformations, a measurement will yield an approximation to the period [math]\displaystyle{ r }[/math]. For simplicity assume that there is a [math]\displaystyle{ y }[/math] such that [math]\displaystyle{ \dfrac{y r}{Q} }[/math] is an integer. Then the probability to measure [math]\displaystyle{ y }[/math] is [math]\displaystyle{ 1 }[/math]. To see this, we notice that then

[math]\displaystyle{ e^{- \frac{2 \pi i b y r}{Q}} = 1 }[/math]

for all integers [math]\displaystyle{ b }[/math]. Therefore, the sum whose square gives us the probability to measure [math]\displaystyle{ y }[/math] will be [math]\displaystyle{ \dfrac{Q}{r} }[/math], as [math]\displaystyle{ b }[/math] takes roughly [math]\displaystyle{ \dfrac{Q}{r} }[/math] values and thus the probability is [math]\displaystyle{ \dfrac{1}{r^{2}} }[/math]. There are [math]\displaystyle{ r }[/math] possible values of [math]\displaystyle{ y }[/math] such that [math]\displaystyle{ \dfrac{y r}{Q} }[/math] is an integer, and also [math]\displaystyle{ r }[/math] possibilities for [math]\displaystyle{ f(x_{0}) }[/math], so the probabilities sum to [math]\displaystyle{ 1 }[/math].

The period-finding routine can be considered a variation of the more general quantum phase estimation algorithm to determine the eigenvalue of a unitary corresponding to an eigenvector. In the case of the period-finding routine used in Shor's Algorithm, the unitary in question is modular multiplication by the chosen base mod [math]\displaystyle{ N }[/math]. While the computational basis [math]\displaystyle{ |1\rangle }[/math] is not an eigenvector of this unitary, it is a uniform superposition of its [math]\displaystyle{ r }[/math] eigenvectors and thus the measurement will give the eigenvalue's phase for one of the eigenvectors. Since not all such phases can be used to extract the period, the retries of the subroutine may be necessary.[18]

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.[19][20] Reversible circuits typically use on the order of [math]\displaystyle{ n^3 }[/math] gates for [math]\displaystyle{ n }[/math] 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.

Discrete logarithms

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

[math]\displaystyle{ f \colon \mathbb{Z}_{p} \times \mathbb{Z}_{p} \to G \;;\; f(a,b) = g^{a} x^{- b} . }[/math]

This gives us an abelian hidden subgroup problem, as [math]\displaystyle{ f }[/math] corresponds to a group homomorphism. The kernel corresponds to the multiples of [math]\displaystyle{ (r,1) }[/math]. So, if we can find the kernel, we can find [math]\displaystyle{ r }[/math]. 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 [math]\displaystyle{ f }[/math] as a quantum transform, followed finally by a quantum Fourier transform.[18] 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.[18] To see this, consider the group of integers under addition, and for a given [math]\displaystyle{ a\in\mathbb{Z} }[/math] such that: [math]\displaystyle{ a^{r}=1 }[/math], the function

[math]\displaystyle{ f \colon \mathbb{Z}\to \mathbb{Z} \;;\; f(x) = a^{x},\; f(x+r) = f(x) . }[/math]

For any finite abelian group G, a quantum algorithm exists for solving the hidden subgroup for G in polynomial time.[18]

See also


  1. Shor, P.W. (1994). "Algorithms for quantum computation: discrete logarithms and factoring". Proceedings 35th Annual Symposium on Foundations of Computer Science (IEEE Comput. Soc. Press): 124–134. doi:10.1109/sfcs.1994.365700. ISBN 0818665807. 
  2. See also pseudo-polynomial time.
  3. Beckman, David; Chari, Amalavoyal N.; Devabhaktuni, Srikrishna; Preskill, John (1996). "Efficient Networks for Quantum Factoring". Physical Review A 54 (2): 1034–1063. doi:10.1103/PhysRevA.54.1034. PMID 9913575. Bibcode1996PhRvA..54.1034B. 
  4. Harvey, David; van Der Hoeven, Joris (2020). "Integer multiplication in time O(n log n)". Annals of Mathematics. doi:10.4007/annals.2021.193.2.4. 
  5. "Number Field Sieve". 
  6. Gidney, Craig. "Shor's Quantum Factoring Algorithm". 
  7. "Quantum resource estimates for computing elliptic curve discrete logarithms". 2017. arXiv:1706.06752 [quant-ph].
  8. Vandersypen, Lieven M. K.; Steffen, Matthias; Breyta, Gregory; Yannoni, Costantino S.; Sherwood, Mark H.; Chuang, Isaac L. (2001), "Experimental realization of Shor's quantum factoring algorithm using nuclear magnetic resonance", Nature 414 (6866): 883–887, doi:10.1038/414883a, PMID 11780055, Bibcode2001Natur.414..883V, 
  9. 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", Physical Review Letters 99 (25): 250504, doi:10.1103/PhysRevLett.99.250504, PMID 18233508, Bibcode2007PhRvL..99y0504L, 
  10. 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, doi:10.1103/PhysRevLett.99.250505, PMID 18233509, Bibcode2007PhRvL..99y0505L, 
  11. Lucero, Erik; Barends, Rami; Chen, Yu; Kelly, Julian; Mariantoni, Matteo; Megrant, Anthony; O'Malley, Peter; Sank, Daniel et al. (2012). "Computing prime factors with a Josephson phase qubit quantum processor". Nature Physics 8 (10): 719. doi:10.1038/nphys2385. Bibcode2012NatPh...8..719L. 
  12. Martín-López, Enrique; Martín-López, Enrique; Laing, Anthony; Lawson, Thomas; Alvarez, Roberto; Zhou, Xiao-Qi; O'Brien, Jeremy L. (12 October 2012). "Experimental realization of Shor's quantum factoring algorithm using qubit recycling". Nature Photonics 6 (11): 773–776. doi:10.1038/nphoton.2012.259. Bibcode2012NaPho...6..773M. 
  13. Amico, Mirko; Saleem, Zain H.; Kumph, Muir (2019-07-08). "An Experimental Study of Shor's Factoring Algorithm on IBM Q". Physical Review A 100 (1): 012305. doi:10.1103/PhysRevA.100.012305. ISSN 2469-9926. 
  14. Karamlou, Amir H.; Simon, William A.; Katabarwa, Amara; Scholten, Travis L.; Peropadre, Borja; Cao, Yudong (2021-10-28). "Analyzing the performance of variational quantum factoring on a superconducting quantum processor" (in en). NPJ Quantum Information 7 (1): 156. doi:10.1038/s41534-021-00478-z. ISSN 2056-6387. Bibcode2021npjQI...7..156K. 
  15. "Quantum computing motte-and-baileys" (in en-US). 2019-12-28. 
  16. Qiskit authors. "Qiskit Textbook: Quantum Phase Estimation". IBM. 
  17. Shor 1999, p. 14
  18. 18.0 18.1 18.2 18.3 Nielsen, Michael A.; Chuang, Isaac L. (9 December 2010). Quantum Computation and Quantum Information (7th ed.). Cambridge University Press. ISBN 978-1-107-00217-3. Retrieved 24 April 2022. 
  19. Markov, Igor L.; Saeedi, Mehdi (2012). "Constant-Optimized Quantum Circuits for Modular Multiplication and Exponentiation". Quantum Information and Computation 12 (5–6): 361–394. doi:10.26421/QIC12.5-6-1. Bibcode2012arXiv1202.6614M. 
  20. Markov, Igor L.; Saeedi, Mehdi (2013). "Faster Quantum Number Factoring via Circuit Synthesis". Phys. Rev. A 87 (1): 012310. doi:10.1103/PhysRevA.87.012310. Bibcode2013PhRvA..87a2310M. 
  21. Bernstein, Daniel J.; Heninger, Nadia; Lou, Paul; Valenta, Luke (2017). "Post-quantum RSA". International Workshop on Post-Quantum Cryptography. Lecture Notes in Computer Science 10346: 311–329. doi:10.1007/978-3-319-59879-6_18. ISBN 978-3-319-59878-9. 

Further reading

External links