Shor’s Quantum Algorithm
Introduction
Among the many applications of the quantum computing model, the most significant in the field of cryptography is the work of the American computer scientist Peter W. Shor. Assuming the availability of a suitable quantum computer, Shor presented to the scientific community, with his 1995 paper “Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer”, a quantum algorithm capable of breaking the RSA scheme and the Diffie–Hellman protocol, which form the foundation of modern public-key cryptography and of most secure communication systems in use today. In fact, all current public-key cryptographic standards (e.g., NIST SP 800-56 A/B, FIPS 186) would be vulnerable to this algorithm. In what follows, the discussion focuses on the RSA scheme, whose security relies on the assumption that the mathematical problem of factoring suitably chosen large integers is computationally intractable for current computers. Even when leveraging the most advanced classical computing technologies, it is estimated that factoring a 2048-bit RSA modulus—which would break the scheme—would require trillions of years. Conversely, if a quantum computer with the required memory and computational capabilities were built, the implementation of Shor’s algorithm could complete the same factorization in a matter of hours. Shor’s result is a concrete example of the exponential speed-up offered by the quantum computing model compared to its classical counterpart. The theoretical analysis of the best classical algorithm currently known [1] for factoring an
-bit integer estimates a computational cost of
operations [2] in asymptotic terms, whereas Shor’s quantum algorithm has complexity
. More precisely, the classical algorithm is said to have subexponential complexity in the parameter
, while the quantum algorithm achieves polynomial complexity in
. Similar considerations apply to the Diffie–Hellman protocol with respect to the discrete logarithm problem. The following sections present the technical details of the quantum factoring algorithm, placing it within a broader class of problems. In order to better approach the discussion, readers are encouraged to review the concepts introduced in the article on qubits. For those interested in exploring the topic further, a seminar hosted by the De Cifris association in collaboration with Telsy and the Politecnico di Torino is also recommended.
Quantum Fourier Transform
A fundamental ingredient for the construction of Shor’s algorithm is the Quantum Fourier Transform, the quantum analogue of the discrete Fourier transform. Given
, the Quantum Fourier Transform of the
-qubit
is defined as
![Rendered by QuickLaTeX.com \[QFT(|x\rangle)=\frac{1}{\sqrt{2^n}}\sum_{y=0}^{2^n-1}e^{\frac{2\pi xy}{2^n}}|y\rangle.\]](https://www.telsy.com/wp-content/ql-cache/quicklatex.com-bb354a145912b67357326cfc2074fbe3_l3.png)
This definition can be extended by linearity to all
-qubits. The quantum Fourier transform can be seen as the information
encoded in a superposition of all basis
-qubits. In the following, it is useful to observe that given
,
, with
binary expansion of
, and
, then
![]()
In these terms, the quantum Fourier transform can be rewritten by considering its action on each qubit.

The quantum circuit to construct QFT is built starting from the previous remark and the usage of Hadamard gates
and controlled Phase Shifts
, with respect to the previous article
denotes
for simplicity.

Notice that at the end of the quantum circuit, the qubit order is reversed and so it has to be fixed via SWAP gates.
Quantum Phase Estimation
The problems solved by Shor’s quantum algorithm belong to a broad class known as Quantum Phase Estimation (QPE). The general formulation of QPE is as follows. Given a unitary transform
and an its eigenstate
, find the phase
describing the corresponding eigenvalue.
![]()
(Notice that the eigenvalue must have this form since
is unitary.) Before proceeding with the analysis of the circuit that solves Quantum Phase Estimation, we introduce two key ingredients necessary to understand the subsequent discussion. 1. Consider the gate
acting as
in the case the controlled bit is
and as the the identity otherwise.

In this way, the
gate can be interpreted as a gate acting just on the first qubit since
is fixed. 2. Observe also that
![]()
The quantum circuit describing
QPE is the following.

The
gate in the middle maps
to
. It is built starting from
gates acting on the
register and controlled by the
-th qubit for any
. As an example, for
and the fixed register ![]()

Applying
for
and exploiting the previous remark, the entire state in
is
![Rendered by QuickLaTeX.com \[\frac{1}{\sqrt{2^{n}}}\underbrace{(|0\rangle+|1\rangle)}_{n \text{ volte}}|\psi\rangle\longmapsto \frac{|0\rangle+w_{1}^{2^n\theta}|1\rangle}{\sqrt{2}}\frac{|0\rangle+w_{2}^{2^n\theta}|1\rangle}{\sqrt{2}}\cdots\frac{|0\rangle+w_{n}^{2^n\theta}|1\rangle}{\sqrt{2}}|\psi\rangle.\]](https://www.telsy.com/wp-content/ql-cache/quicklatex.com-5e167ee8aa8149d1914a54fc1400b669_l3.png)
Substituting
with
in the above expression, the Quantum Fourier Transform of
is obtained according to the previous section. Therefore, it remains to apply the inverse QFT to reobtain
. The result of the final measure is exactly
![]()
Observe that the reasoning works well if
is an integer. In the general case, the quantum circuit returns just an estimate of
that can be used to reconstruct the precise value via a continued fraction-based algorithm.
Shor’s Algorithm
Let
be a positive integer and let
be an integer coprime with
, the objective of Shor’s algorithm is to find the period of the function
![]()
through a number of steps polynomial in
. In the following, the main ingredients to interpret the algorithm in the context of QPE are introduced. Let
be an invertible modulo
, the considered unitary transform is
![]()
where
act on the
-qubit space with
![]()
A possible set of eigenstates is given by
![Rendered by QuickLaTeX.com \[|u_{s}\rangle =\frac{1}{\sqrt{r}}\sum_{k=0}^{r-1}e^{-\frac{2\pi i sk}{r}}|a^{k} \mod{N}\rangle\qquad s =0,\dots, r-1\]](https://www.telsy.com/wp-content/ql-cache/quicklatex.com-ef12c509c60a3390d4fb4e878914e445_l3.png)
where
is the multiplicative order of
, since
![]()
Clearly, if one were able to obtain
for some
, this would imply knowing
, but that is precisely the goal of the algorithm. To circumvent this issue, note that
![Rendered by QuickLaTeX.com \[|1\rangle=\frac{1}{\sqrt{r}}\sum_{s=0}^{r-1}|u_{s}\rangle.\]](https://www.telsy.com/wp-content/ql-cache/quicklatex.com-6d2cde969065691b02f2b3d2e4b086d9_l3.png)

Therefore, proceeding with QPE of
using
(superposition of
eigenstates), the measured outcome will be
![]()
with
a random integer between
and
by linearity. Again, if
does not divide
, the outcome of the measure will be just an estimate of the desired value that can be recovered via a continued fraction strategy.
Solving the factoring problem
In the RSA cryptographic scheme, the considered integer is the product of two big prime numbers, so
. It can be proved that with good probability the resulting period
is even. In this case
![]()
Moreover, with good probability
![]()
At this stage, since
![]()
the factorization of
is obtained as
![]()
[ 1 ] General Number Field Sieve (GNFS) [ 2 ]
is a positive constant
Sources:
[1] General Number Field Sieve (GNFS)
This article is part of a series of contributions by the Telsy Cryptography Research Group dedicated to quantum computing and its implications for cryptography. For other articles, please refer to the index of the section.
For other articles related to quantum and cryptography, please refer to the relevant categories in the blog.
The authors
Edoardo Signorini, graduated in Mathematics with a curriculum in cryptography at the University of Trento, in 2020 carried out the Master’s thesis in the field of Post-Quantum Cryptography (PQC) at Telsy. He currently holds the role of cryptographer within the Telsy research group and carries out an industrial research doctorate in Pure and Applied Mathematics at the Politecnico di Torino. His activity focuses on PQC research and on the analysis and development of cryptographic protocols.
Francesco Stocco, a master’s degree in Mathematics at the University of Padua and the Université de Bordeaux attending the course of study “Algebra Geometry And Number Theory” (ALGANT), joined the Telsy research group in Cryptography at end of 2020 focusing in particular on issues related to quantum technologies.