Path: blob/main/notebooks/ch-algorithms/quantum-fourier-transform.ipynb
3855 views
Quantum Fourier Transform
In this tutorial, we introduce the quantum fourier transform (QFT), derive the circuit, and implement it using Qiskit. We show how to run QFT on a simulator and a five qubit device.
1. Introduction
The Fourier transform occurs in many different versions throughout classical computing, in areas ranging from signal processing to data compression to complexity theory. The quantum Fourier transform (QFT) is the quantum implementation of the discrete Fourier transform over the amplitudes of a wavefunction. It is part of many quantum algorithms, most notably Shor's factoring algorithm and quantum phase estimation.
The discrete Fourier transform acts on a vector and maps it to the vector according to the formula
where .
Similarly, the quantum Fourier transform acts on a quantum state and maps it to the quantum state according to the formula
with defined as above. Note that only the amplitudes of the state were affected by this transformation.
This can also be expressed as the map:
Or the unitary matrix:
2. Intuition
The quantum Fourier transform (QFT) transforms between two bases, the computational (Z) basis, and the Fourier basis. The H-gate is the single-qubit QFT, and it transforms between the Z-basis states and to the X-basis states and . In the same way, all multi-qubit states in the computational basis have corresponding states in the Fourier basis. The QFT is simply the function that transforms between these bases.
(We often note states in the Fourier basis using the tilde (~)).
2.1 Counting in the Fourier basis:
In the computational basis, we store numbers in binary using the states and :

Note the frequency with which the different qubits change; the leftmost qubit flips with every increment in the number, the next with every 2 increments, the third with every 4 increments, and so on. In the Fourier basis, we store numbers using different rotations around the Z-axis:

The number we want to store dictates the angle at which each qubit is rotated around the Z-axis. In the state , all qubits are in the state . As seen in the example above, to encode the state on 4 qubits, we rotated the leftmost qubit by full turns ( radians). The next qubit is turned double this ( radians, or full turns), this angle is then doubled for the qubit after, and so on.
Again, note the frequency with which each qubit changes. The leftmost qubit (qubit 0) in this case has the lowest frequency, and the rightmost the highest.
3. Example 1: 1-qubit QFT
Consider how the QFT operator as defined above acts on a single qubit state . In this case, , , and . Then,
and
such that the final result is the state
This operation is exactly the result of applying the Hadamard operator () on the qubit:
If we apply the operator to the state , we obtain the new state:
Notice how the Hadamard gate performs the discrete Fourier transform for on the amplitudes of the state.
So what does the quantum Fourier transform look like for larger ? Let's derive a transformation for , acting on the state where is the most significant bit. This maths is here for those that find it useful, if you struggle with it then don’t worry; as long as you understand the intuition in section 2 then you can continue straight to the next section.
This is a mathematical description of the animation we saw in the intuition section:

5. The Circuit that Implements the QFT
The circuit that implements QFT makes use of two gates. The first one is a single-qubit Hadamard gate, , that you already know. From the discussion in Example 1 above, you have already seen that the action of on the single-qubit state is
The second is a two-qubit controlled rotation given in block-diagonal form as
where
The action of on a two-qubit state where the first qubit is the control and the second is the target is given by
and
Given these two gates, a circuit that implements an n-qubit QFT is shown below.

The circuit operates as follows. We start with an n-qubit input state .
- After the first Hadamard gate on qubit 1, the state is transformed from the input state to
Noting that
we can write the above state as
which is exactly the QFT of the input state as derived above with the caveat that the order of the qubits is reversed in the output state.
6. Example 2: 3-qubit QFT
The steps to creating the circuit for would be:
- Apply a Hadamard gate to
The example above demonstrates a very useful form of the QFT for . Note that only the last qubit depends on the values of all the other input qubits and each further bit depends less and less on the input qubits. This becomes important in physical implementations of the QFT, where nearest-neighbor couplings are easier to achieve than distant couplings between qubits.
Additionally, as the QFT circuit becomes large, an increasing amount of time is spent doing increasingly slight rotations. It turns out that we can ignore rotations below a certain threshold and still get decent results, this is known as the approximate QFT. This is also important in physical implementations, as reducing the number of operations can greatly reduce decoherence and potential gate errors.
8. Qiskit Implementation
In Qiskit, the implementation of the gate used in the discussion above is a controlled phase rotation gate. This gate is defined in OpenQASM as
Hence, the mapping from the gate in the discussion above into the gate is found from the equation
8.1 Example on 3 Qubits
It is useful to work out the relevant code for the 3-qubit case before generalizing to the -qubit case. First, we must define our quantum circuit:
Note: Remember that Qiskit's least significant bit has the lowest index (0), thus the circuit will be mirrored through the horizontal in relation to the image in section 5. First, we apply a H-gate to qubit 2 :
Next, we want to turn this an extra quarter turn if qubit 1 is in the state :
And another eighth turn if the least significant qubit (0) is :
With that qubit taken care of, we can now ignore it and repeat the process, using the same logic for qubits 0 and 1:
Finally we must swap the qubits 0 and 2 to complete the QFT:
8.2 General QFT Function
We will now create a general circuit for the QFT in Qiskit. Creating large general circuits like this is really where Qiskit shines.
It is easier to build a circuit that implements the QFT with the qubits upside down, then swap them afterwards; we will start off by creating the function that rotates our qubits correctly. Let’s start as we did with the 3 qubit example, by correctly rotating the most significant qubit (the qubit with the highest index):
Let’s see how this looks:
We can use the widget below to see how this circuit scales with the number of qubits in our circuit:
Great! This is the first part of our QFT. Now we have correctly rotated the most significant qubit, we need to correctly rotate the second most significant qubit. Then we must deal with the third most significant, and so on. But why write more code? When we get to the end of our qft_rotations() function, we can use the same code to repeat the process on the next n-1 qubits:
That was easy! Process in which a function calls itself directly or indirectly is called recursion. It can greatly simplify code. We can again see how this scales using the widget below:
Finally, we need to add the swaps at the end of the QFT function to match the definition of the QFT. We will combine this into the final function qft():
This is the generalised circuit for the quantum Fourier transform. We can again see how this scales using the widget below:
We now want to demonstrate this circuit works correctly. To do this we must first encode a number in the computational basis. We can see the number 5 in binary is 101:
(The 0b just reminds us this is a binary number). Let's encode this into our qubits:
And let's check the qubit's states using the aer simulator:
Finally, let's use our QFT function and view the final state of our qubits:
We can see out QFT function has worked correctly. Compared the state , Qubit 0 has been rotated by of a full turn, qubit 1 by full turns (equivalent to of a full turn), and qubit 2 by full turns (equivalent to of a full turn).
If we tried running the circuit at the end of section 8.2 on a real device, the results would be completely random, since all qubits are in equal superposition of and . If we want to demonstrate and investigate the QFT working on real hardware, we can instead create the state seen at the end of section 8.2, run the QFT in reverse, and verify the output is the state as expected.
Firstly, let’s use Qiskit to easily reverse our QFT operation:
Now let's put our qubits in the state :
And we can see this does indeed result in the Fourier state :
Finally, let's apply our inverse QFT:
We (hopefully) see that the highest probability outcome is .
The above implementation of QFT was tested by preparing the Fourier state for which . Try to find the state such that .
Find the state such that .
Try to write the QFT function without recursion. Use Qiskit's unitary simulator to verify your results.
M. Nielsen and I. Chuang, Quantum Computation and Quantum Information, Cambridge Series on Information and the Natural Sciences (Cambridge University Press, Cambridge, 2000).