Cryptography

Constructing the Quantum Fourier Circuit

Published on September 11, 2026

Constructing the Quantum Fourier Circuit

From the previous post, we have seen the theory on how the Quantum Fourier Transform is used to crack the RSA. In order to utilize the QFT, we need to implement a circuit for it. In other words, we need to program it.

In this blog post, we will start with the basic definition of the Quantum Fourier Transform and implement a circuit for a 2 qubit system. We will then show the generalization to multiple qubits and implement a circuit for a 4-qubit system which we will use later to implement Shor's algorithm.

Definition

The Quantum Fourier Transform is defined on its action on the computational basis as

UFTx=12ny=02n1e2πixy/2ny\mathbf{U}_\mathrm{FT} |x\rangle = \displaystyle \frac{1}{\sqrt{2^n}}\sum_{y=0}^{2^n-1}e^{2\pi i xy/2^n}|y\rangle

2-Qubit Example

To see how this looks like for a 2-qubit system, let's write out the terms:

UFTx1x0=122y=0221e2πixy/22y=12(eπix0/20+eπix1/21+eπix2/22+eπix3/23)=12(00+eπix1/201+eπix2/210+eπix3/211)\begin{array}{rl} \mathbf{U}_\mathrm{FT} |x_1x_0\rangle &= \displaystyle \frac{1}{\sqrt{2^2}}\sum_{y=0}^{2^2-1}e^{2\pi i xy/2^2}|y\rangle\\[10px] &= \displaystyle \frac{1}{2}\left( e^{\pi i x\cdot 0/2}|0\rangle + e^{\pi i x\cdot 1/2}|1\rangle + e^{\pi i x\cdot 2/2}|2\rangle + e^{\pi i x\cdot 3/2}|3\rangle\right )\\[10px] &= \displaystyle \frac{1}{2}\left( |00\rangle + e^{\pi i x\cdot 1/2}|01\rangle + e^{\pi i x\cdot 2/2}|10\rangle + e^{\pi i x\cdot 3/2}|11\rangle\right )\\[10px] \end{array}

In the last line, we used the binary representation of the integer yy.

The multiplication, like x3x\cdot 3 is done in the following manner:

(x121+x020)(121+120)=4x1+2x1+2x0+x0(x_1\cdot 2^1 + x_0\cdot 2^0)(1\cdot 2^1 + 1\cdot 2^0) = 4x_1 + 2x_1 + 2x_0 + x_0

For example,

eπix3/2=eπi(4x1+2x1+2x0+x0)/2=exp[πi(2x1+x1+x0+x02)]=exp[πi(x1+x0+x02)]\begin{array}{rl} \displaystyle e^{\pi i x\cdot 3/2} &= \displaystyle e^{\pi i \left( 4x_1 + 2x_1 + 2x_0 + x_0 \right)/2}\\[10px] &= \displaystyle \exp\left[\pi i \left( 2x_1 + x_1 + x_0 + \frac{x_0}{2} \right)\right]\\[10px] &= \displaystyle \exp\left[\pi i \left( x_1 + x_0 + \frac{x_0}{2} \right)\right]\\[10px] \end{array}

The last line above results because for any integer mm

exp(2πimx)=cos(2πmx)+isin(2πmx)=1\begin{array}{rl} \exp(2\pi i mx) &= \cos(2\pi mx) + i\sin(2\pi mx)\\ &= 1 \end{array}

Going back to the Fourier Transform and expanding the multiplications, we get

UFTx1x0=12(00+exp[πi(x1+x02)]01+exp[πi(x0)10+exp[πi(x1+x02+x0)11)\begin{array}{rl} \mathbf{U}_\mathrm{FT} |x_1x_0\rangle &=\displaystyle \frac{1}{2}\left(|00\rangle + \underbrace{\exp[\pi i(x_1 + \frac{x_0}{2})]}|01\rangle +\exp[\pi i(x_0)|10\rangle + \exp[\pi i(x_1+ \frac{x_0}{2} + x_0)|11\rangle\right) \end{array}

We can group the 2nd and 4th terms and factor out the term in the underbrace:

UFTx1x0=12[00+exp[πi(x0)10+exp[πi(x1+x02)]01+exp[πi(x1+x02+x0)11]=12[(0+exp[πi(x0)]1)0+(0+exp[πi(x0)]1)exp[πi(x1+x02)]1]\begin{array}{rl} \mathbf{U}_\mathrm{FT} |x_1x_0\rangle &= \displaystyle \frac{1}{2}\left[|00\rangle + \exp[\pi i(x_0)|10\rangle + \exp[\pi i(x_1 + \frac{x_0}{2})]|01\rangle + \exp[\pi i(x_1+ \frac{x_0}{2} + x_0)|11\rangle\right]\\[10px] &= \displaystyle \frac{1}{2}\left[\underbrace{\left(|0\rangle + \exp[\pi i(x_0)]|1\rangle\right)}\otimes|0\rangle + \underbrace{\left(|0\rangle + \exp[\pi i(x_0)]|1\rangle \right)}\otimes\exp[\pi i(x_1 + \frac{x_0}{2})]|1\rangle\right] \end{array}

Factor out the term in the underbrace to get

UFTx=12[(0+exp[πi(x0)]1)0+(0+exp[πi(x0)]1)exp[πi(x1+x02)]1]=12[0+exp[πi(x0)]1)12(0+exp[πi(x1+x02)]1]\begin{array}{rl} \mathbf{U}_\mathrm{FT} |x\rangle &= \displaystyle \frac{1}{2}\left[\underbrace{\left(|0\rangle + \exp[\pi i(x_0)]|1\rangle\right)}|0\rangle + \underbrace{\left(|0\rangle + \exp[\pi i(x_0)]|1\rangle \right)}\exp[\pi i(x_1 + \frac{x_0}{2})]|1\rangle\right]\\[10px] &= \displaystyle \frac{1}{\sqrt{2}}\left[|0\rangle + \exp[\pi i(x_0)]|1\rangle\right)\otimes \frac{1}{\sqrt{2}}\left(|0\rangle + \exp[\pi i(x_1 + \frac{x_0}{2})]|1\rangle\right] \end{array}

Some Gates We Need For the Circuit

The Hadamard Gate

The first factor from the left is just the Hadamard gate acting on the first qubit since

12(0+exp[πi(x0)]1)=0+(1)x012=Hx0\begin{array}{rl} \displaystyle \frac{1}{\sqrt{2}}\left(|0\rangle + \exp[\pi i(x_0)]|1\rangle\right) &= \displaystyle \frac{|0\rangle + (-1)^{x_0} |1\rangle}{\sqrt{2}}\\[10px] &= \mathbf{H}|x_0\rangle \end{array}

The Controlled-Phase Rotation

Let's define the following operator acting on two qubits:

Vijxn1xjxi0=eπixixj/2ijxn1xjxi0\begin{array}{rl} \mathbf{V}_{ij}|x_{n-1}\cdots x_j\cdots x_i\cdots 0\rangle &= e^{\pi i x_ix_j/2^{|i-j|}}|x_{n-1}\cdots x_j\cdots x_i\cdots 0\rangle \end{array}

This is called the Controlled-Phase rotation operator. Notice that if either xix_i or xjx_j is 0, the operator becomes the identity. Only when both are 1 will the operator multiply a phase eπi/2ije^{\pi i/2^{|i-j|}}.

Using these 2 operators, we can now write the gates for the Fourier Transformation.

Constructing the circuit for a 2-qubit System

Consider the following sequence of operations

H1V01H0x0x1=H1V01x012(0+exp(πix1)1)=H1x012(0+exp(πi(x1+x02)1)=12(0+exp(πix0)112(0+exp(πi(x1+x02)1)\begin{array}{rl} \mathbf{H}_1\mathbf{V}_{01}\mathbf{H}_0 |x_0x_1\rangle &= \displaystyle \mathbf{H}_1\mathbf{V}_{01}|x_0\rangle\frac{1}{\sqrt{2}}(|0\rangle + \exp(\pi ix_1)|1\rangle)\\ &= \displaystyle \mathbf{H}_1 |x_0\rangle\frac{1}{\sqrt{2}}(|0\rangle + \exp(\pi i(x_1 + \frac{x_0}{2})|1\rangle)\\ &= \displaystyle \frac{1}{\sqrt{2}}(|0\rangle + \exp(\pi ix_0)|1\rangle\frac{1}{\sqrt{2}}\otimes(|0\rangle + \exp(\pi i(x_1 + \frac{x_0}{2})|1\rangle)\\ \end{array}

If you compare this with the UFTx1x0\mathbf{U}_\mathrm{FT}\vert x_1x_0\rangle, they are similar except for the order of the bits. Therefore, if you measure

H1V01H0x0x1\mathbf{H}_1\mathbf{V}_{01}\mathbf{H}_0 \vert x_0x_1\rangle

and get a value yy, you just need to reverse the bits of the binary representation of yy to get the corresponding state in UFTx1x0\mathbf{U}_\mathrm{FT}\vert x_1x_0\rangle.

Visualizing the Circuit

This is the QISKit code to visualize the circuit

# 2-qubit circuit from qiskit import Aer import math from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute from qiskit.tools.visualization import circuit_drawer, plot_histogram from qiskit import BasicAer, execute input_register = QuantumRegister(2,name="qin") c = ClassicalRegister(4) qc = QuantumCircuit(input_register) qc.h(input_register[0]) qc.cu1(math.pi/float(2),input_register[0],input_register[1]) qc.h(input_register[1]) qc.draw(output='mpl')

And here is the result

General Fourier Transform Circuit

The Fourier Transform of an n-qubit system is generalized by its action on the computational basis

12n(0+e2πi0.x01)(0+e2πi0.x1x01)\displaystyle \frac{1}{\sqrt{2^n}} (|0\rangle + e^{2\pi i0.x_0}|1\rangle)\otimes(|0\rangle + e^{2\pi i0.x_1x_0}|1\rangle)\otimes\cdots (0+e2πi0.xmx2x01)\displaystyle \otimes(|0\rangle + e^{2\pi i 0.x_m\ldots x_2x_0}|1\rangle)

where

0.xm1xm2x1x0=xm121+xm222++x02m0.x_{m-1}x_{m-2}\ldots x_1x_0 = \displaystyle \frac{x_{m-1}}{2^1}+\frac{x_{m-2}}{2^2}+\cdots + \frac{x0}{2^m}

Constructing the circuit for a 4-qubit System

Using this formula, the Fourier Transform of a 4-qubit system is

UFTx3x2x1x0=(0+e2πi0.x01)(0+e2πi0.x1x01)(0+e2πi0.x2x1x01)(0+e2πi0.x3x2x1x01)\begin{array}{rl} \mathbf{U}_\mathrm{FT} |x_3x_2x_1x_0\rangle &= (|0\rangle + e^{2\pi i0.x_0}|1\rangle)\otimes(|0\rangle + e^{2\pi i0.x_1x_0}|1\rangle)\otimes\\ & (|0\rangle + e^{2\pi i0.x_2x_1x_0}|1\rangle)\otimes(|0\rangle + e^{2\pi i0.x_3x_2x_1x_0}|1\rangle) \end{array}

Consider the following series of transformations:

H3V23H2V13V12H1V03V02V01H0x3x2x1x0\mathbf{H}_3\mathbf{V}_{23}\mathbf{H}_2\mathbf{V}_{13}\mathbf{V}_{12}\mathbf{H}_1\mathbf{V}_{03}\mathbf{V}_{02}\mathbf{V}_{01}\mathbf{H}_0|x_3x_2x_1x_0\rangle

This will give you

(0+e2πi0.x31)(0+e2πi0.x2x31)(|0\rangle + e^{2\pi i0.x3}|1\rangle)\otimes(|0\rangle + e^{2\pi i0.x_2x_3}|1\rangle) (0+e2πi0.x1x2x31)(0+e2πi0.x0x1x2x31)\otimes(|0\rangle + e^{2\pi i0.x_1x_2x_3}|1\rangle)\otimes(|0\rangle + e^{2\pi i0.x_0x_1x_2x_3}|1\rangle)

If you compare this with UTFx3x2x1x0\mathbf{U}_\mathrm{TF}\vert x_3x_2x_1x_0\rangle, they look similar except for the order of the bits. If you define the mapping

x3x0x2x1x1x2x0x3\begin{array}{rl} x_3 &\rightarrow x_0\\ x_2 &\rightarrow x_1\\ x_1 &\rightarrow x_2\\ x_0 &\rightarrow x_3 \end{array}

and apply it to the transformation above, you'll get the UFTx3x2x1x0\mathbf{U}_\mathrm{FT}\vert x_3x_2x_1x_0\rangle.

Therefore, whatever yy you measure using

H3V23H2V13V12H1V03V02V01H0\mathbf{H}_3\mathbf{V}_{23}\mathbf{H}_2\mathbf{V}_{13}\mathbf{V}_{12}\mathbf{H}_1\mathbf{V}_{03}\mathbf{V}_{02}\mathbf{V}_{01}\mathbf{H}_0

just reverse the bits to get the corresponding result using UFTx3x2x1x0\mathbf{U}_\mathrm{FT}\vert x_3x_2x_1x_0\rangle.

Implementing the 4-Qubit Fourier Transform

The following code in QISKit implements the circuit

H3V23H2V13V12H1V03V02V01H0x3x2x1x0\mathbf{H}_3\mathbf{V}_{23}\mathbf{H}_2\mathbf{V}_{13}\mathbf{V}_{12}\mathbf{H}_1\mathbf{V}_{03}\mathbf{V}_{02}\mathbf{V}_{01}\mathbf{H}_0|x_3x_2x_1x_0\rangle
# Fourier Transform from qiskit import Aer from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute from qiskit.tools.visualization import circuit_drawer, plot_histogram from qiskit import BasicAer, execute import math input_register = QuantumRegister(4,name="qin") c = ClassicalRegister(4) qc = QuantumCircuit(input_register) qc.h(input_register[0]) qc.cu1(math.pi/float(2),input_register[0],input_register[1]) qc.cu1(math.pi/float(2**2),input_register[0],input_register[2]) qc.cu1(math.pi/float(2**3),input_register[0],input_register[3]) qc.barrier() qc.h(input_register[1]) qc.cu1(math.pi/float(2),input_register[1],input_register[2]) qc.cu1(math.pi/float(2**2),input_register[1],input_register[3]) qc.barrier() qc.h(input_register[2]) qc.cu1(math.pi/float(2),input_register[2],input_register[3]) qc.h(input_register[3]) qc.barrier() qc.draw(output='mpl')

Below is the circuit diagram

Now that we have a circuit for a 4-qubit Fourier Transform, let's proceed to the next blog post and use it to crack the RSA!

Image Credit: "fourier" by fotodiagramas is marked with CC BY-NC 2.0. To view the terms, visit https://creativecommons.org/licenses/by-nc/2.0/?ref=openverse