Shor's algorithm hinges on a result from number theory: the function
is a periodic function when
is an
integer coprime to
. In the context of Shor's algorithm
will be
the number we wish to factor. When two numbers are coprime it means
that their greatest common divisor is 1.
Calculating this function for an exponential number of
's would
take exponential time on a classical computer. Shor's algorithm
utilizes quantum parallelism to perform the exponential number of
operations in one step.
Since
is a periodic function, it has some period
. We know that
(since
), and
therefore
, and
, and so on.
Given this information and through the following algebraic
manipulation:
We can see that the product
is an
integer multiple of
, the number to be factored. So long as
is not
, then at least one of
,
must have a nontrivial factor in common with
.
So by computing
, and
, we
will obtain a factor of
, where
is the greatest common
denominator function.
Shor's algorithm tries to find
, the period of
,
where
is the number to be factored by Shor's algorithm, and
is
an integer coprime to
. To do this Shor's algorithm creates a
quantum memory register with two parts. In the first part the
algorithm places a superposition of the integers which are to be
's
in the
function. We will choose our
's to be the
integers 0 through
, where
is the power of two such that
. Then the algorithm calculates
, where
is the superposition of the states, and places the
result in the second part of the quantum memory register.
Remember, the number
is represented by a
bit
string. We must calculate
an exponential number of
times, with respect to the length of the encoded input to the
algorithm (although it is a polynomial number of times with respect to
the value of
).
Next the algorithm measures the state of the second register, the one
that contains the superposition of all possible outcomes for
. Measuring this register has the effect of collapsing the
state into some observed value, say
. It also has the side effect
of projecting the first part of the quantum register into a state
consistent with the value measured in the second part. So: measurement
of the second part results in exactly one value, and causes the other
partition to collapse into a superposition of the base states
consistent with the value observed in the second part.
After this measurement the second part of the register contains the
value
, and the first part of the register contains a superposition
of the base states whose evaluation in
produce
. We
know
is a periodic function, therefore the first part
of the register will contain the values
,
,
,
where
is the lowest integer such that
.
The next step is to perform a discrete Fourier transform on the
contents of first part of the register. The application of the
discrete Fourier transformation has the effect of peaking the
probability amplitudes of the first part of the register at integer
multiples of the quantity
.
Measuring the first part of the quantum register will yield an integer
multiple of the inverse of the period with high probability. Once this
number is retrieved from the quantum memory register, a classical
computer can do analysis of this number, make a guess as to the actual
value of
, and from that compute the possible factors of
. This
post processing is covered in more detail below. (Shor)