'''Shor's algorithm''' is a [[quantum computer|quantum]] [[algorithm]] for [[Integer factorization|factoring]] a number ''N'' in [[Big O notation |O]]((log ''N'')3) time and O(log ''N'') space, named after [[Peter Shor]].
The algorithm is significant because it implies that [[public key cryptography]] might be easily broken, given a sufficiently large quantum computer. [[Wikipedia:RSA| RSA]], for example, uses a public key ''N'' which is the product of two large prime numbers. One way to crack RSA encryption is by factoring ''N'', but with classical algorithms, factoring becomes increasingly time-consuming as ''N'' grows large; more specifically, no classical algorithm is known that can factor in time O((log ''N'')''k'') for any ''k''. By contrast, Shor's algorithm can crack RSA in polynomial time. It has also been extended to attack many other public key cryptosystems.
Like all quantum computer algorithms, Shor's algorithm is probabilistic: it gives the correct answer with high probability, and the probability of failure can be decreased by repeating the algorithm.
Shor's algorithm was demonstrated in 2001 by a group at IBM, which factored 15 into 3 and 5, using a quantum computer with 7 [[qubit|qubits]].
=== Procedure ===
The problem we are trying to solve is that, given an integer ''N'', we try to find another integer ''p'' between ''1'' and ''N'' that divides ''N''.
Shor's algorithm consists of two parts:
# A reduction of the factoring problem to the problem of [[Order (group theory)|order-finding]], which can be done on a classical computer.
# A quantum algorithm to solve the order-finding problem.
==== Classical part ====
- Pick a pseudo-random number ''a'' < ''N''
- Compute [[greatest common divisor|gcd]](''a'', ''N''). This may be done using the [[Euclidean algorithm]].
- If gcd(''a'', ''N'') ≠ 1, then there is a nontrivial factor of ''N'', so we are done.
- Otherwise, use the period-finding subroutine (below) to find ''r'', the [[periodic function|period]] of the following function: :, i.e. the smallest integer ''r'' for which .
- If ''r'' is odd, go back to step 1.
- If ''a'' ''r''/2 ≡ -1 (mod ''N''), go back to step 1.
- The factors of ''N'' are gcd(a''r''/2 ± 1, ''N''). We are done.
- Start with a pair of input and output [[qubit]] registers with log2''N'' qubits each, and initialize them to : where ''x'' runs from 0 to ''N'' - 1.
- Construct ''f''(''x'') as a quantum function and apply it to the above state, to obtain :
- Apply the [[quantum Fourier transform]] on the input register. The quantum Fourier transform on ''N'' points is defined by: : This leaves us in the following state: :
- Perform a measurement. We obtain some outcome ''y'' in the input register and in the output register. Since ''f'' is periodic, the probability to measure some ''y'' is given by : Analysis now shows that this probability is higher, the closer ''yr/N'' is to an integer.
- Turn ''y/N'' into an [[irreducible fraction]], and extract the denominator ''r''′, which is a candidate for ''r''.
- Check if ''f''(''x'') = ''f''(''x'' + ''r''′). If so, we are done.
- Otherwise, obtain more candidates for ''r'' by using values near ''y'', or multiples of ''r''′. If any candidate works, we are done.
- Otherwise, go back to step 1 of the subroutine.
- Create a superposition of states. This can be done by applying [[Hadamard transform|Hadamard]] gates to all qubits in the input register. Another approach would be to use the quantum Fourier transform (see below).
- Implement the function ''f'' as a quantum transform. To achieve this, Shor used [[Exponentiating by squaring|repeated squaring]] for his modular exponentiation transformation.
- Perform a quantum Fourier transform. By using controlled NOT gates and single qubit rotation gates Shor designed a circuit for the quantum Fourier transform that uses just gates.