The reason why it is said that "RSA encryption will be broken when quantum computers become practical" is Shor's algorithm. However, when reading papers and explanations, classical computations (ordinary computer calculations) and quantum computations are often mixed, making it difficult to see what tasks are performed by the quantum circuits.
In this series, we will build Shor's algorithm as an actual quantum circuit by tracing back the 1995 paper. In the first installment, we will organize the overall flow of the algorithm and the structure of the parts handled by the quantum circuits.
References:
- Shor's Algorithm (Slides)
- Vedral, Barenco, Ekert, Quantum Networks for Elementary Arithmetic Operations (The method of building quantum circuits follows this paper)
Overall Flow: Quantum Only Handles the Middle Part
Shor's algorithm does not use a quantum computer to calculate everything. It consists of three stages: "Classical Pre-processing," "Quantum Order Finding," and "Classical Post-processing."

Let N be the number we want to factor.
1. Classical Pre-processing
- Randomly select an integer x from 2 to N−1
- Calculate the greatest common divisor gcd(x, N) of x and N. If it is not 1, that is a divisor of N, so we are done
- If gcd(x, N) = 1 (they are coprime), proceed to step 2
2. Quantum Order Finding
- Find the smallest positive integer r such that x^r ≡ 1 (mod N). This r is called the order (period)
3. Classical Post-processing
- If r is even, and x^(r/2) mod N is not −1 (i.e., N−1), calculate gcd(x^(r/2) ± 1, N)
- With high probability, this yields a non-trivial divisor of N
Step 2 takes too long on a regular computer. This is the key point of Shor's algorithm: letting the quantum computer handle this part.
Structure of the Quantum Circuit for Finding the Order
The circuit for finding the order consists of two registers (groups of qubits).

First Register (Control and Readout)
- Role: Receives information about the order r as phase, and finally measures it to extract the result
- Number of qubits: Slightly more than twice the number of bits of N. The larger the number, the higher the precision for reading r
- Initial state: Apply Hadamard gates (H) to all qubits to create a superposition state where 0 and 1 have equal weight
Second Register (Computation)
- Role: Stores the result of x^j mod N
- Number of qubits: Enough to represent N (approximately log₂N)
- Initial state: Set only the least significant qubit to 1 using an X gate, so the entire register is in state |1⟩
Three Operations of the Circuit
1. Initialization
Put the first register into a superposition state and the second register into |1⟩.
2. Controlled Modular Exponentiation
Use each qubit of the first register as control to perform multiplication on the second register. This is an operation that transforms state |j⟩|k⟩ to |j⟩|x^j · k mod N⟩.
This is the largest circuit and the most difficult part to build in Shor's algorithm. The paper by Vedral et al. divides this multiplication into components of addition, modular addition, and modular multiplication, showing methods to build each as a reversible circuit (one that can be traced backward). This series will focus primarily on this part.
3. Inverse Quantum Fourier Transform (Inverse QFT)
Interfere the periodic phase imprinted on the first register by the modular exponentiation to concentrate values likely to be obtained upon measurement to those corresponding to the order r.
Finally, when measuring the first register, a value close to s/r (where s is some integer) is obtained. This is subjected to the classical continued fraction expansion to extract the order r.
Next Time: Building the Quantum Adder
This time, we organized the overall flow and the structure of the registers handled by the quantum circuits. The circuit procedures themselves are three steps: "Create superposition → Apply modular exponentiation → Read out via Fourier transform."
From next time onwards, we will use the Python quantum computing library blueqat SDK to implement the largest component, modular exponentiation, starting from its foundational quantum adder.