User:Fawly/sandbox/shor

Procedure
The problem that we are trying to solve is, given a composite number $$ N $$, to find a non-trivial divisor of $$ N $$ (a divisor strictly between $$ 1 $$ and $$ N $$). Before attempting to find such a divisor, if there's any doubt whether $$ N $$ is composite or prime, one can use relatively quick primality-testing algorithms to verify that $$ N $$ 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 $$ b $$ of $$ 1 $$ modulo $$ N $$ that is different from $$ 1 $$ and $$ - 1 $$, because then
 * $$ b^2 - 1 = (b+1)(b-1) = mN $$

for a non-zero integer $$ m $$ that gives us two distinct non-trivial divisors $$ \gcd(N, b+1) $$ and $$ \gcd(N, b-1) $$ of $$ N $$. 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 $$ N $$ to be odd (otherwise $$ 2 $$ 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 $$ b $$ of $$ 1 $$ modulo $$ N $$.

In turn, finding such a $$ b $$ is reduced to finding an element $$ a $$ 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 $$ a $$, as this is a difficult problem on a classical computer.

Classical part
1. Pick a random number $ 1 < a < N $.

2. Compute $ K=\gcd(a,N) $, the greatest common divisor of $ a $ and $ N $. This may be done using the Euclidean algorithm.

3. If $ K \neq 1 $, then $K$ is a nontrivial factor of $ N $, so we are done.

4. Otherwise, use the quantum period-finding subroutine (below) to find $ r $, which denotes the period of the following function:
 * $ f(x) = a^{x} \bmod N. $

Equivalently, $ r $ is the smallest positive integer that satisfies $a^{r} \equiv 1 \bmod N $.

5. If $ r $ is odd, then go back to step 1.

6. If $ a^{r / 2} = - 1 \bmod N $, then go back to step 1.

7. Otherwise, both $ \gcd(a^{r / 2} + 1,N) $ or $ \gcd(a^{r / 2} - 1,N) $ are nontrivial factors of $ N $, so we are done.

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

Quantum part: period-finding subroutine
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) = a^{x} \bmod N $$. Given $$ N $$, find $$ Q = 2^{q} $$ such that $$ N^{2} \leq Q < 2 N^{2} $$, which implies that $$ \dfrac{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 values of $$ x $$ that produce the same $$ f(x) $$, even as the period $$ r $$ approaches $$ \dfrac{N}{2} $$.

Proceed as follows:

1. Initialize the registers to



\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). $ where $\otimes$ denotes the tensor product.

This initial state is a superposition of $ Q $ states, and is easily obtained by generating $ q $ independent qubits, each a superposition of $ 0 $ and $ 1 $ states. We can accomplish this by initializing the qubits to the zero-position, and then applying the Hadamard gate in parallel to each of the $ q $ qubits, where $ 2^{q} = Q $.
 * Construct $ f(x) $ as a quantum function and apply it to the above state, to obtain



U_f | x, 0^q \rangle = | x,f(x) \rangle $



U_f \frac{1}{\sqrt{Q}} \sum_{x = 0}^{Q - 1} | x, 0^q \rangle = \frac{1}{\sqrt{Q}} \sum_{x = 0}^{Q - 1} | x,f(x) \rangle $

This is still a superposition of $ Q $ states. However, the $ q + n $ qubits, i.e, the $ q $ input qubits and $ n $ ($ > {\log_{2}}(N) $) 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 $ r $ we are looking for is now stored in the phase of the input qubits $ x $ as a result of "phase kickback", where using qubits as control inputs to unitary gates alters the relative phase of the control qubits.
 * Apply the inverse quantum Fourier transform to the input register. This transform (operating on a superposition of $ Q = 2^{q} $ states) uses a $ Q $-th root of unity such as $ \omega = e^{\frac{2 \pi i}{Q}} $ to distribute the amplitude of any given $ | x \rangle $ state equally among all $ Q $ of the $ | y \rangle $ states, and to do so in a different way for each different $ x $. We thus obtain



{U_{\operatorname{QFT}}}(| x \rangle) = \frac{1}{\sqrt{Q}} \sum_{y = 0}^{Q - 1} \omega^{x y} | y \rangle. $

This leads to the final state



\frac{1}{Q} \sum_{x = 0}^{Q - 1} \sum_{y = 0}^{Q - 1} \omega^{x y} | y,f(x) \rangle. $

Now, we reorder this sum as



\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. $

This is a superposition of many more than $ Q $ states, but many fewer than $ Q^{2} $ states, as there are fewer than $ Q $ distinct values of $ z = f(x) $. Let


 * $ \omega = e^{\frac{2 \pi i}{Q}} $ be a $ Q $-th root of unity,
 * $ r $ be the period of $ f $,
 * $ x_{0} $ be the smallest of the $ x \in \{ 0,\ldots,Q - 1 \} $ for which $ f(x) = z $ (we have $ x_{0} < r $), and
 * write $ m - 1 = \left\lfloor \frac{Q - x_{0} - 1}{r} \right\rfloor $
 * $ b $ indexes these $ x $, running from $ 0 $ to $ m - 1 $, so that $ x_{0} + r b < Q $.

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



\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}. $

Each term in this sum represents a different path to the same result, and quantum interference occurs — constructive when the unit vectors $ \omega^{r y b} $ point in nearly the same direction in the complex plane, which requires that $ \omega^{r y} $ point along the positive real axis.
 * Perform a measurement. We obtain some outcome $ y $ in the input register and some outcome $ z $ in the output register. As $ f $ is periodic, the probability of measuring some state $ | y,z \rangle $ is given by



\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} $



= \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})} $

Analysis now shows that this probability is higher the closer the unit vector $ \omega^{r y} $ is to the positive real axis, or the closer $ \dfrac{y r}{Q} $ is to an integer. Unless $ r $ is a power of $ 2 $, it will not be a factor of $ Q $. 1. $ s < N $.
 * Since $ \frac{y r}{Q} $ is close to some integer $ c $, the known value $ \dfrac{y}{Q} $ is close to the unknown value $ \dfrac{c}{r} $. Performing [classical] continued fraction expansion on $ \dfrac{y}{Q} $ allows us to find approximations $ \dfrac{d}{s} $ of it that satisfy two conditions:

2. $ \left

3. \dfrac{y}{Q} - \dfrac{d}{s} \right

4. < \dfrac{1}{2 Q} $.

Given these multiple conditions (and assuming $ \dfrac{d}{s} $ is irreducible), $ s $ is very likely to be the appropriate period $ r $, or at least a factor of it.
 * Check (classically) if $ f(x) = f(x + s) \Leftrightarrow a^{s} \equiv 1 \bmod N $. If so, then we are done.
 * Otherwise, (classically) obtain more candidates for $ r $ by using multiples of $ s $, or by using other $ s $ with $ \dfrac{d}{s} $ near $ \dfrac{y}{Q} $. If any candidate works, then we are done.
 * Otherwise, try again starting from step 1 of this subroutine.
 * undefined

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
Shor's algorithm hinges on finding a non-trivial square root of $$ 1 $$ modulo $$ N $$; That is, a solution to
 * $$ b^2\equiv 1\bmod N $$

where $$ b\not\equiv \plusmn1\bmod N $$.

If such $$ b $$ exists, we claim that $$ d = \gcd(b - 1,N) $$ is a proper factor of $$ N $$, i.e., $$ d \neq 1,N $$. In fact, if $$ d = N $$, then $$ N $$ divides $$ b - 1 $$, so that $$ b \equiv 1 \bmod N $$, which goes against the construction of $$ b $$. If, on the other hand, $$ d = \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. $$

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

Therefore, $$ d $$ is the required proper factor of $$ N $$. Similarly, it can be proven that $$ \gcd(b + 1,N) $$ is also a proper factor of $$ N $$.

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

Therefore, for Shor's algorithm to work, we need $$ N $$ to be odd (otherwise $$ 2 $$ 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 $$ \sqrt[k]{N} $$ for $$ 2 \leq k \leq {\log_{3}}(N) $$, and if $$ N $$ is not a power of any integer, it is not a power of any odd prime. Here, the upper bound for the integer $$ k $$ that we need to check is determined by $$ \sqrt[k]{N}\geq3 $$, since for $$ N $$ to be odd, $$ \sqrt[k]{N} $$ cannot be $$ 2 $$. This check, however, cannot rule out that $$ N $$ may be an odd prime itself, which can only be ruled out by primality-testing algorithms.

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

Obtaining factors from period
The integers less than $$ N $$ and coprime with $$ N $$ form the multiplicative group of integers modulo $ N $, which is a finite abelian group $$ G $$. The size of this group is given by $$ \varphi(N) $$. By the end of step 3, we have an integer $$ a $$ in this group. As the group is finite, $$ a $$ must have a finite order $$ r $$, which is the smallest positive integer such that



a^{r} \equiv 1 \bmod N. $$

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

Therefore, $$ N $$ divides $$ a^{r} - 1 $$ (also written $$ N \mid a^{r} - 1 $$). Suppose that we are able to obtain $$ r $$ and that it is even. (If $$ r $$ is odd, then by step 5, we have to restart the algorithm with a different random number $$a$$) Now $$ b \equiv a^{r / 2} \bmod N $$ is a square root of $$ 1 $$ modulo $$ N $$ that is different from $$ 1 $$. This is because $$ r $$ is the order of $$ a $$ modulo $$ N $$, so $$ a^{r / 2} \not\equiv 1 \bmod N $$, or else the order of $$ a $$ in this group would be $$ \dfrac{r}{2} $$. If $$ a^{r / 2} \equiv - 1 \bmod N $$, then 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 \pm 1 \bmod 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, as the odd number $$ N $$ 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 $$ f $$, 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 $$ 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 = 2^{q} $$) that uses just $$ \dfrac{q (q - 1)}{2} = O \! \left((\log Q)^{2} \right) $$ gates.

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



e^{- \frac{2 \pi i b y r}{Q}} = 1 $$

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

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 $$N$$. While the computational basis $$|1\rangle$$ is not an eigenvector of this unitary, it is a uniform superposition of its $$r$$ 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.

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.

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, as $$ 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.