Part 3: Shor’s Algorithm (classically)

References


There are various ways to factor classically, but they all require exponential time. The best current algorithm is the general number field sieve. Quantum computers, though, can factor in polynomial time using an algorithm called Shor’s algorithm.

Shor’s algorithm contains many steps, all of which run quickly on a classical computer except for the period finding step, which runs slowly and can only be efficiently evaluated by a quantum computer. In this section you will write a python script that implements Shor’s algorithm classically. The period running step will obviously run slowly. Later on you will replace the period finding piece with your quantum computing simulator (it will still run slowly because you're using a simulator, but you can imagine your code is a real quantum computer and then in your imagination it will run quickly).


Here are the steps of Shor’s algorithm:

Goal: Factor a number N

Do you see why this works? If not, we should talk about it.

Write a python script that implements this algorithm with a separate function for period finding. Your algorithm should check for failure modes and simply start over with a new random $x$ in the case of a failure. Use your code to factor some numbers!

Grading

Show that your factoring code works by factoring a lot of numbers. Show that you can factor a number up to 10 bits.

Paste into your document the $x$ and $r$ you find by factoring $N=30$ Put a list of ten N, x, r where N is less than 5 bits and x and r are not trivial. You will use these later on for testing.

Congrats! You now have an algorithm that factors. Shor's algorithm will involve replacing period finding with a quantum computer