Modular Multiplication
Contents
Modular Multiplication#
As part of Shor’s algorithm, we need to perform a controlled version of
There exists efficent (i.e. polynomial time) circuits which implement modular multiplication on a quantum computer. Given such a circuit, it is then straightforward to generate a controlled-version of the gate (simply add a control-wire to each circuit element and decompose back into primitive gates - see the controlled-gate subsection of the univesality page for a more involved discussion of this.)
That said, building this unitary out of H, P, and CNOT is somewhat painful (even arguing it can be done is somewhat painful although see the end of this page). One option is to find your favorite ECE friend and ask them to build you a reversible classical version of such a circuit out of Toffoli gates (which you can then promote to quantum gates). We will take a different approach here.
Our approach#
The approach we actually recommend taking is to implement a controlled-classical
They are also reasonably straightforward to implement.
For simulator Ic:
In the previous section you’ve generated kron
it against the right number of identities in the correct place.
To get the control-
For simulator II: Your simulator essentially works by taking basis elements and responding with the new basis element. Since python does modular multiplication this should be just as easy to add as any of the other gates.
In your circuit description you should add
FUNC 4 5 xyModN 2 15
where the first number is the first wire for the function, the next number is the number of wires for the function (so it works on lines 4,5,6,7,8), the “xyModN” is the name of the function in python, and everything after that is parameters to that function (in this case,
The ctrl-f should work in the following way
CFUNC 3 4 5 xyModN 2 15
where the first number is the control-bit (like the CNOT), the second number is the first wire for the function, the next number is the number of wires for the function, the “xyModN” is the name of the function in python, and everything after that is parameters to that function (in this case,
Implement these atomic gates into your simulator.
Grading
Verify both the normal and controlled versions of xyModN works by writing (two separate) circuit descriptions that uses them and then checking that it works on those descriptions.
In this section, we are going to discuss how to get (or at least argue you can get) modular multiplication out of primitive gates.
Attempt 1#
We have learned that (see the section in this book on “Classical Gates”) there is a general procedure which compiles a python function which takes
which works in time
Addition (with a classical b)#
The first step on our quest is to figure out how to do addition. Let’s start off with taking
Grading
Write a python function that takes
Hint: Think about what you can do with phase gates.
You only need to get the addition working for
Test your function by running your circuit through your emulator and make sure that you get the correct results.
Grading
Now that you have this working, let’s check two things:
What happens if you add two numbers
and which give a number larger then ?What happens if you run your circuit backwards?
Answer these two questions and add them your document.
Addition (with two quantum inputs)#
Our next step is to do arithmetic with two quantum inputs. In particular,
This isn’t much harder then what you’ve already done. Now instead of your python programming outputing gates you have to “control” those gates based on the top wires.
Grading
Write python code that takes the number of bits in
Again, also comment on what happens if you want to add numbers that are too big and if you run your code backwards.
Multiplication by a classical #
Suppose we want a unitary matrix that takes
To accomplish this, we will use the fact that we can write
Use the following general approach:
Copy
once for each (i.e. times).Multiply the i’th copy of
by . Notice that this largely involves swapping around wires.Add up all the copies.
Copy the answer to a new register.
Figure out how to uncompute everything you’ve done so far by running essentially all of it backwards (this is essential to returning all the wires in a zero state so we can use them again).
Grading
Write a python code which takes a number b and produces a quantum circuit which generates
Multiplication by a quantum #
Grading
Now you want to modify this code to take as input
Warning
This section below here is not yet finished/polished.
Putting the modular back in addition.#
At the end of the day, we were attempting to get
In Practice#
We have shown in principle that one could do this. In practice, often for specific values of