Alex Gough > Quantum-Entanglement-0.32 > shor


Annotate this POD

View/Report Bugs


  shor - A short demonstration of Quantum::Entanglement


 ./ [number to factor (>14)]



The important maths

Given a number n which we are trying to factor, and some other number which we have guessed, x, we can say that:


There will also be some other number, r such that

 x**r % n == 1

or, more specifically,

 x**(kr) % n ==1

in other words, the function

 F(a) = x**a % n

is periodic with period r.

Now, starting from

 x**r = 1 % n



and, if r is an even number,


or in nice short words, the term on the left is an integer multiple of n. So long as x**(r/2) != +-1, at least one of the two brackets on the left must share a factor with n.


The steps of the algorithm ^

1. Remove early trivial cases


2. Pick an integer

Chose a number q so that n**2 <= q <= 2n**2, this is done on a classical computer. (This is the size we will use for our quantum register.)

3. Select at random a number coprime to n

Think of some number less than n so that n and x do not share a common factor (if they do, we already know the answer...).



5. Calculate F, store in a second register

We now calculate F(a) = x**a % n where a represents the superposition of states in our first register, we store the result of this in our second register.

6. Look at register2

We now look at the value of register two and get some value k, this forces register1 into a state which can only collapse into values satisfying the equation

 x**a % n = k


7. Find period of register1


8. Observe register1

We now observe register1, and use the result to calculate a likely value for r. From this we can easily calculate two numbers, one of which will have a factor in common with n, by applying an efficient classical algoirthm for finding the greatest common denominator, we will be able to find a value which could be a factor of n.

Things to remember ^




This code is copyright (c) Alex Gough ( )2001. This is free software, you may use, modify and redistribute it under the same terms as Perl itself.



syntax highlighting: