Hello! Welcome back to our exploration of foundational quantum algorithms.
In our last lesson, we completed the classical part of Shor's algorithm, establishing a high-probability reduction from the problem of factoring an integer to the problem of finding the order of a randomly chosen number modulo . This order-finding task, , is where quantum computation provides its exponential advantage.
Today, we will construct the very heart of that quantum advantage. Our learning outcome is to analyze the construction of a reversible quantum circuit for modular exponentiation based on the repeated squaring algorithm. This circuit implements the unitary operator that computes . This operator is the crucial input to the Quantum Phase Estimation (QPE) algorithm, which we will use to find the period .
This lesson bridges the number-theoretic framework we've built with the practical quantum circuitry required to execute it.
1. The Classical Blueprint: Exponentiation by Squaring
Before building a quantum circuit, it's essential to understand the classical algorithm it's based on. The most efficient classical method for modular exponentiation is known as exponentiation by squaring (or binary exponentiation).
The core idea is to use the binary representation of the exponent . If is an -bit integer, we can write it as:
where are the bits of .
Substituting this into the expression for gives:
Since is either 0 or 1, the term is either or . This means we only include the term in the product if the corresponding bit is 1.
This transforms the problem of one large exponentiation into a series of multiplications. The algorithm proceeds as follows:
- Initialize a result to 1.
- Iterate from to :
- If bit is 1, multiply the result by .
The terms can be efficiently pre-computed classically by repeatedly squaring the previous term: .
This decomposition is the key to designing the quantum circuit.
2. From Classical Logic to a Quantum Circuit
The classical algorithm's structure—"if bit is 1, then perform a multiplication"—maps perfectly onto the concept of a controlled quantum operation.
We will design a quantum circuit that takes two registers as input:
- An -qubit exponent register, which holds the state . In the full Shor's algorithm, this register will be in a superposition of all possible exponents.
- An -qubit result register, which will hold the computed value of . This register must be large enough to store , so we need qubits.
The following reading explains how to translate the classical repeated squaring logic into a sequence of quantum gates.
Shor's Algorithm - Intro to Quantum Software Development
This resource from MITRE's STEM program, titled 'Shor's Algorithm', clearly explains the 'binary substitution technique' and how it leads to a quantum circuit built from controlled modular multiplications.
Please read the sections 'The Modular Exponentiation Function' and 'Quantum Modular Exponentiation'. Focus on how the binary expansion of the exponent x transforms the problem into a series of multiplications, and how this naturally leads to using controlled quantum gates.
As the text explains, the quantum circuit will implement the following steps:
- Initialize the result register to the state .
- For each qubit in the exponent register (from to ):
- Apply a controlled modular multiplication operation.
- The control qubit is .
- The target is the result register.
- The operation multiplies the value in the result register by the constant .
Crucially, the values are pre-computed classically. The quantum circuit doesn't compute ; it simply implements the multiplication by this pre-computed constant, conditional on the state of .
3. The Reversible Modular Exponentiation Circuit
Now we can visualize the complete circuit. It consists of the exponent and result registers, along with a sequence of controlled unitary gates, one for each bit of the exponent.

Let's break down the components in this diagram:
- Register 2 (): This is the -qubit exponent register.
- Register A (): This is the -qubit result register, which accumulates the final product.
CMODMULT(a^(2^i) mod N): This represents a controlled modular multiplier. It is a unitary operation that performs the mapping on the target register if the control qubit is , and does nothing if it is .
The entire circuit implements the transformation:
This is exactly the unitary operator required for the period-finding stage of Shor's algorithm.
The Complexity of Modular Multiplication
We have treated the controlled modular multiplier as a black box. However, constructing this gate is the most resource-intensive part of Shor's algorithm. Building efficient quantum arithmetic circuits is a major area of research in quantum computing.
Given your background, you might appreciate the depth of this sub-problem. The following resource is a recent and comprehensive survey of quantum arithmetic circuits. You don't need to read it in its entirety, but browsing the introduction to the modular exponentiation section will give you a sense of the history and the different design strategies.
A Comprehensive Study of Quantum Arithmetic Circuits
This survey paper, 'A Comprehensive Study of Quantum Arithmetic Circuits', provides an excellent overview of the research landscape for constructing arithmetic operations, including modular exponentiation. It highlights the significant challenges and various approaches developed over the years.
Please read the first few paragraphs of Section 8, 'Quantum Modular Exponentiation' (up to the end of the paragraph discussing Beckman et al.). Also, take a look at Table 10. This will give you context on the foundational work and the distinction between different design philosophies (e.g., Clifford+T vs. QFT-based adders).
As you'll see from the paper, the first concrete proposals for these circuits by Vedral et al. and Beckman et al. in 1996 laid the groundwork. They constructed modular exponentiation from simpler blocks like quantum adders. The efficiency of these adders, multipliers, and the overall circuit architecture in terms of gate count, qubit count, and connectivity has been the subject of continuous improvement ever since.
4. Reversibility and Uncomputation
A fundamental requirement of quantum computation is that all operations (except measurement) must be unitary, and therefore reversible.
The modular exponentiation circuit we've constructed, , is indeed unitary. Its inverse, , is given by:
where is the modular multiplicative inverse of modulo . This inverse can be computed efficiently using the Extended Euclidean Algorithm. The circuit for would be constructed similarly, but using controlled modular multiplications by the inverse constants.
You may have noticed the CMODMULT^-1 gates in the circuit diagram. These are often necessary not for reversing the main computation, but for uncomputation of auxiliary (ancilla) qubits used inside each CMODMULT block. A quantum modular multiplier typically requires extra ancilla qubits to store intermediate results (e.g., in the adders). To make the block a clean unitary operation on just the target register and to allow the ancillas to be reused, these intermediate computations must be reversed, returning the ancillas to their initial state. This is a critical principle in reversible circuit design.
Conclusion
In this lesson, we have dissected the construction of the modular exponentiation circuit, the computational core of Shor's algorithm.
Key Takeaways:
- The quantum circuit for modular exponentiation is a direct translation of the classical exponentiation by squaring algorithm.
- It uses the binary representation of the exponent to decompose the calculation into a sequence of controlled modular multiplications.
- The circuit consists of an exponent register controlling an output register, which is initialized to .
- Each qubit controls a unitary gate that multiplies the output register by a pre-computed constant .
- The construction of the underlying modular multipliers is a complex field of research in its own right and represents the main cost of implementing Shor's algorithm.
- The entire circuit is reversible, a fundamental requirement for quantum algorithms.
We have now built the specific unitary operator whose properties encode the period . In our next lesson, we will finally see how to use this operator within the Quantum Phase Estimation framework to extract this period, completing the quantum part of Shor's algorithm.