13.2 Shor’s algorithm
In the previous section, we implemented the inverse quantum Fourier transform, which is central to Shor’s algorithm. However, we still need an additional element if we want to be able to build and run quantum circuits to factor integers: quantum gates for modular multiplication.
Remember that, in order to factor an integer N using Shor’s algorithm, we first choose at random another integer a with no common factors with N. Then, we need to create the following circuit:

Here, the Ua2j gates implement the operation of multiplying an integer times a2j and taking the remainder of the division by N. These are the gates that we need to implement now before we can run Shor’s algorithm.
How to implement these quantum gates is a problem that has been extensively studied in the literature, and several very efficient options exist (see, for instance, [72], [73], [74], [75], [76]). However, describing them in detail is beyond the scope of...