{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "remove_cell" ] }, "source": [ "# Quantum Fourier Transform" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "## Contents\n", "1. [Introduction](#introduction)\n", "2. [Intuition](#intuition) \n", " 2.1 [Counting in the Fourier Basis](#counting-fourier) \n", "3. [Example 1: 1-qubit QFT](#example1)\n", "4. [The Quantum Fourier transform](#qfteqn)\n", "5. [The Circuit that Implements the QFT](#circuit)\n", "6. [Example 2: 3-qubit QFT](#example2)\n", "7. [Some Notes About the Form of the QFT Circuit](#formnote)\n", "8. [Qiskit Implementation](#implementation) \n", " 8.1 [Example on 3 Qubits](#threeqft) \n", " 8.2 [General QFT Function](#generalqft) \n", " 8.3 [Running QFT on a Real Quantum Device](#implementationdev) \n", "9. [Problems](#problems)\n", "10. [References](#references)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Introduction \n", "\n", "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. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The discrete Fourier transform acts on a vector $(x_0, ..., x_{N-1})$ and maps it to the vector $(y_0, ..., y_{N-1})$ according to the formula\n", "\n", "\n", "$$y_k = \\frac{1}{\\sqrt{N}}\\sum_{j=0}^{N-1}x_j\\omega_N^{jk}$$\n", "\n", "\n", "where $\\omega_N^{jk} = e^{2\\pi i \\frac{jk}{N}}$.\n", "\n", "Similarly, the quantum Fourier transform acts on a quantum state $\\vert X\\rangle = \\sum_{j=0}^{N-1} x_j \\vert j \\rangle$ and maps it to the quantum state $\\vert Y\\rangle = \\sum_{k=0}^{N-1} y_k \\vert k \\rangle$ according to the formula\n", "\n", "\n", "$$y_k = \\frac{1}{\\sqrt{N}}\\sum_{j=0}^{N-1}x_j\\omega_N^{jk}$$\n", "\n", "\n", "with $\\omega_N^{jk}$ defined as above. Note that only the amplitudes of the state were affected by this transformation.\n", "\n", "This can also be expressed as the map:\n", "\n", "\n", "$$\\vert j \\rangle \\mapsto \\frac{1}{\\sqrt{N}}\\sum_{k=0}^{N-1}\\omega_N^{jk} \\vert k \\rangle$$\n", "\n", "\n", "\n", "Or the unitary matrix:\n", "\n", "\n", "$$U_{QFT} = \\frac{1}{\\sqrt{N}} \\sum_{j=0}^{N-1} \\sum_{k=0}^{N-1} \\omega_N^{jk} \\vert k \\rangle \\langle j \\vert$$\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Intuition \n", "\n", "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 $|0\\rangle$ and $|1\\rangle$ to the X-basis states $|{+}\\rangle$ and $|{-}\\rangle$. 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.\n", "\n", "$$\n", "|\\text{State in Computational Basis}\\rangle \\quad \\xrightarrow[]{\\text{QFT}} \\quad |\\text{State in Fourier Basis}\\rangle\n", "$$\n", "\n", "$$\n", "\\text{QFT}|x\\rangle = |\\widetilde{x}\\rangle\n", "$$\n", "\n", "(We often note states in the Fourier basis using the tilde (~)).\n", "\n", "### 2.1 Counting in the Fourier basis: \n", "\n", "In the computational basis, we store numbers in binary using the states $|0\\rangle$ and $|1\\rangle$:\n", "\n", "![zbasiscounting](images/zbasis-counting.gif)\n", "\n", "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:\n", "\n", "![fbasiscounting](images/fourierbasis-counting.gif)\n", "\n", "The number we want to store dictates the angle at which each qubit is rotated around the Z-axis. In the state $|\\widetilde{0}\\rangle$, all qubits are in the state $|{+}\\rangle$. As seen in the example above, to encode the state $|\\widetilde{5}\\rangle$ on 4 qubits, we rotated the leftmost qubit by $\\tfrac{5}{2^n} = \\tfrac{5}{16}$ full turns ($\\tfrac{5}{16}\\times 2\\pi$ radians). The next qubit is turned double this ($\\tfrac{10}{16}\\times 2\\pi$ radians, or $10/16$ full turns), this angle is then doubled for the qubit after, and so on. \n", "\n", "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. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Example 1: 1-qubit QFT \n", "\n", "Consider how the QFT operator as defined above acts on a single qubit state $\\vert\\psi\\rangle = \\alpha \\vert 0 \\rangle + \\beta \\vert 1 \\rangle$. In this case, $x_0 = \\alpha$, $x_1 = \\beta$, and $N = 2$. Then,\n", "\n", "\n", "\n", "$$y_0 = \\frac{1}{\\sqrt{2}}\\left( \\alpha \\exp\\left(2\\pi i\\frac{0\\times0}{2}\\right) + \\beta \\exp\\left(2\\pi i\\frac{1\\times0}{2}\\right) \\right) = \\frac{1}{\\sqrt{2}}\\left(\\alpha + \\beta\\right)$$\n", "\n", "\n", "\n", "and\n", "\n", "\n", "\n", "$$y_1 = \\frac{1}{\\sqrt{2}}\\left( \\alpha \\exp\\left(2\\pi i\\frac{0\\times1}{2}\\right) + \\beta \\exp\\left(2\\pi i\\frac{1\\times1}{2}\\right) \\right) = \\frac{1}{\\sqrt{2}}\\left(\\alpha - \\beta\\right)$$\n", "\n", "\n", "\n", "such that the final result is the state \n", "\n", "\n", "\n", "$$U_{QFT}\\vert\\psi\\rangle = \\frac{1}{\\sqrt{2}}(\\alpha + \\beta) \\vert 0 \\rangle + \\frac{1}{\\sqrt{2}}(\\alpha - \\beta) \\vert 1 \\rangle$$\n", "\n", "\n", "\n", "This operation is exactly the result of applying the Hadamard operator ($H$) on the qubit:\n", "\n", "\n", "\n", "$$H = \\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1 & 1 \\\\ 1 & -1 \\end{bmatrix}$$\n", "\n", "\n", "\n", "If we apply the $H$ operator to the state $\\vert\\psi\\rangle = \\alpha \\vert 0 \\rangle + \\beta \\vert 1 \\rangle$, we obtain the new state:\n", "\n", "$$\\frac{1}{\\sqrt{2}}(\\alpha + \\beta) \\vert 0 \\rangle + \\frac{1}{\\sqrt{2}}(\\alpha - \\beta) \\vert 1 \\rangle \n", "\\equiv \\tilde{\\alpha}\\vert 0 \\rangle + \\tilde{\\beta}\\vert 1 \\rangle$$\n", "\n", "Notice how the Hadamard gate performs the discrete Fourier transform for $N = 2$ on the amplitudes of the state. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. The Quantum Fourier transform" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So what does the quantum Fourier transform look like for larger $N$? Let's derive a transformation for $N=2^n$, $QFT_N$ acting on the state $\\vert x \\rangle = \\vert x_1\\ldots x_n \\rangle$ where $x_1$ 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.\n", "\n", "\n", "\\begin{aligned}\n", "QFT_N\\vert x \\rangle & = \\frac{1}{\\sqrt{N}} \\sum_{y=0}^{N-1}\\omega_N^{xy} \\vert y \\rangle \n", "\\\\\n", "& = \\frac{1}{\\sqrt{N}} \\sum_{y=0}^{N-1} e^{2 \\pi i xy / 2^n} \\vert y \\rangle ~\\text{since}\\: \\omega_N^{xy} = e^{2\\pi i \\frac{xy}{N}} \\:\\text{and}\\: N = 2^n \n", "\\\\\n", "& = \\frac{1}{\\sqrt{N}} \\sum_{y=0}^{N-1} e^{2 \\pi i \\left(\\sum_{k=1}^n y_k/2^k\\right) x} \\vert y_1 \\ldots y_n \\rangle \\:\\text{rewriting in fractional binary notation}\\: y = y_1\\ldots y_n, y/2^n = \\sum_{k=1}^n y_k/2^k \n", "\\\\\n", "& = \\frac{1}{\\sqrt{N}} \\sum_{y=0}^{N-1} \\prod_{k=1}^n e^{2 \\pi i x y_k/2^k } \\vert y_1 \\ldots y_n \\rangle \\:\\text{after expanding the exponential of a sum to a product of exponentials} \n", "\\\\\n", "& = \\frac{1}{\\sqrt{N}} \\bigotimes_{k=1}^n \\left(\\vert0\\rangle + e^{2 \\pi i x /2^k } \\vert1\\rangle \\right) \\:\\text{after rearranging the sum and products, and expanding} \n", "\\sum_{y=0}^{N-1} = \\sum_{y_1=0}^{1}\\sum_{y_2=0}^{1}\\ldots\\sum_{y_n=0}^{1} \n", "\\\\\n", "& = \\frac{1}{\\sqrt{N}}\n", "\\left(\\vert0\\rangle + e^{\\frac{2\\pi i}{2}x} \\vert1\\rangle\\right) \n", "\\otimes\n", "\\left(\\vert0\\rangle + e^{\\frac{2\\pi i}{2^2}x} \\vert1\\rangle\\right) \n", "\\otimes \n", "\\ldots\n", "\\otimes\n", "\\left(\\vert0\\rangle + e^{\\frac{2\\pi i}{2^{n-1}}x} \\vert1\\rangle\\right) \n", "\\otimes\n", "\\left(\\vert0\\rangle + e^{\\frac{2\\pi i}{2^n}x} \\vert1\\rangle\\right) \n", "\\end{aligned}\n", "\n", "\n", "This is a mathematical description of the animation we saw in the intuition section:\n", "\n", "![fbasiscounting](images/fourierbasis-counting.gif)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. The Circuit that Implements the QFT \n", "\n", "The circuit that implements QFT makes use of two gates. The first one is a single-qubit Hadamard gate, $H$, that you already know. From the discussion in [Example 1](#example1) above, you have already seen that the action of $H$ on the single-qubit state $\\vert x_k\\rangle$ is\n", "\n", "\n", "\n", "$$H\\vert x_k \\rangle = \\frac{1}{\\sqrt{2}}\\left(\\vert0\\rangle + \\exp\\left(\\frac{2\\pi i}{2}x_k\\right)\\vert1\\rangle\\right)$$\n", "\n", "\n", "\n", "The second is a two-qubit controlled rotation $CROT_k$ given in block-diagonal form as \n", "\n", "$$CROT_k = \\left[\\begin{matrix}\n", "I&0\\\\\n", "0&UROT_k\\\\\n", "\\end{matrix}\\right]$$\n", "\n", "where \n", "\n", "$$UROT_k = \\left[\\begin{matrix}\n", "1&0\\\\\n", "0&\\exp\\left(\\frac{2\\pi i}{2^k}\\right)\\\\\n", "\\end{matrix}\\right]$$\n", "\n", "The action of $CROT_k$ on a two-qubit state $\\vert x_l x_j\\rangle$ where the first qubit is the control and the second is the target is given by\n", "\n", "\n", "\n", "$$CROT_k\\vert 0x_j\\rangle = \\vert 0x_j\\rangle$$\n", "\n", "\n", "and\n", "\n", "\n", "$$CROT_k\\vert 1x_j\\rangle = \\exp\\left( \\frac{2\\pi i}{2^k}x_j \\right)\\vert 1x_j\\rangle$$\n", "\n", "\n", "\n", "Given these two gates, a circuit that implements [an n-qubit QFT](#qfteqn) is shown below.\n", "\n", "![image1](images/qft.png)\n", "\n", "The circuit operates as follows. We start with an n-qubit input state $\\vert x_1x_2\\ldots x_n\\rangle$.\n", "\n", "
\n", "
1. After the first Hadamard gate on qubit 1, the state is transformed from the input state to \n", "\n", "$$\n", "H_1\\vert x_1x_2\\ldots x_n\\rangle = \n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\\vert0\\rangle + \\exp\\left(\\frac{2\\pi i}{2}x_1\\right)\\vert1\\rangle\\right]\n", "\\otimes\n", "\\vert x_2x_3\\ldots x_n\\rangle\n", "$$\n", "\n", "
2. After the $UROT_2$ gate on qubit 1 controlled by qubit 2, the state is transformed to\n", "\n", "$$\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\\vert0\\rangle + \\exp\\left(\\frac{2\\pi i}{2^2}x_2 + \\frac{2\\pi i}{2}x_1\\right)\\vert1\\rangle\\right]\n", "\\otimes\n", "\\vert x_2x_3\\ldots x_n\\rangle\n", "$$\n", "\n", "
3. After the application of the last $UROT_n$ gate on qubit 1 controlled by qubit $n$, the state becomes\n", "\n", "$$\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^n}x_n + \n", "\\frac{2\\pi i}{2^{n-1}}x_{n-1} + \n", "\\ldots + \n", "\\frac{2\\pi i}{2^2}x_2 + \n", "\\frac{2\\pi i}{2}x_1\n", "\\right)\n", "\\vert1\\rangle\\right]\n", "\\otimes\n", "\\vert x_2x_3\\ldots x_n\\rangle\n", "$$\n", "\n", "Noting that \n", "\n", "$$\n", "x = 2^{n-1}x_1 + 2^{n-2}x_2 + \\ldots + 2^1x_{n-1} + 2^0x_n\n", "$$\n", "\n", "we can write the above state as \n", "\n", "$$\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^n}x \n", "\\right)\n", "\\vert1\\rangle\\right]\n", "\\otimes\n", "\\vert x_2x_3\\ldots x_n\\rangle\n", "$$\n", "\n", "
4. After the application of a similar sequence of gates for qubits $2\\ldots n$, we find the final state to be:\n", "\n", "$$\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^n}x \n", "\\right)\n", "\\vert1\\rangle\\right]\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^{n-1}}x \n", "\\right)\n", "\\vert1\\rangle\\right]\n", "\\otimes\n", "\\ldots\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^{2}}x \n", "\\right)\n", "\\vert1\\rangle\\right]\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^{1}}x \n", "\\right)\n", "\\vert1\\rangle\\right]\n", "$$\n", "\n", "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.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Example 2: 3-qubit QFT \n", "\n", "The steps to creating the circuit for $\\vert y_3y_2y_1\\rangle = QFT_8\\vert x_3x_2x_1\\rangle$ would be:\n", "\n", "
\n", "
1. Apply a Hadamard gate to $\\vert x_1 \\rangle$\n", "\n", "$$\n", "|\\psi_1\\rangle = \n", "\\vert x_3\\rangle\n", "\\otimes\n", "\\vert x_2\\rangle\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\n", "\\vert0\\rangle + \n", "\\exp\\left(\\frac{2\\pi i}{2}x_1\\right) \n", "\\vert1\\rangle\\right]\n", "$$\n", "\n", "
2. Apply a $UROT_2$ gate to $\\vert x_1\\rangle$ depending on $\\vert x_2\\rangle$\n", "\n", "$$\n", "|\\psi_2\\rangle = \n", "\\vert x_3\\rangle\n", "\\otimes\n", "\\vert x_2\\rangle\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\n", "\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^2}x_2 + \\frac{2\\pi i}{2}x_1\n", "\\right) \n", "\\vert1\\rangle\\right]\n", "$$\n", "\n", "
3. Apply a $UROT_3$ gate to $\\vert x_1\\rangle$ depending on $\\vert x_3\\rangle$\n", "\n", "$$\n", "|\\psi_3\\rangle = \n", "\\vert x_3\\rangle\n", "\\otimes\n", "\\vert x_2\\rangle\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\n", "\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^3}x_3 + \\frac{2\\pi i}{2^2}x_2 + \\frac{2\\pi i}{2}x_1\n", "\\right) \n", "\\vert1\\rangle\\right]\n", "$$\n", "\n", "
4. Apply a Hadamard gate to $\\vert x_2 \\rangle$\n", "\n", "$$\n", "|\\psi_4\\rangle = \n", "\\vert x_3\\rangle\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\n", "\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2}x_2\n", "\\right) \n", "\\vert1\\rangle\\right]\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\n", "\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^3}x_3 + \\frac{2\\pi i}{2^2}x_2 + \\frac{2\\pi i}{2}x_1\n", "\\right) \n", "\\vert1\\rangle\\right]\n", "$$\n", "\n", "
5. Apply a $UROT_2$ gate to $\\vert x_2\\rangle$ depending on $\\vert x_3\\rangle$\n", "\n", "$$\n", "|\\psi_5\\rangle = \n", "\\vert x_3\\rangle\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\n", "\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^2}x_3 + \\frac{2\\pi i}{2}x_2\n", "\\right) \n", "\\vert1\\rangle\\right]\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\n", "\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^3}x_3 + \\frac{2\\pi i}{2^2}x_2 + \\frac{2\\pi i}{2}x_1\n", "\\right) \n", "\\vert1\\rangle\\right]\n", "$$\n", "\n", "
6. Apply a Hadamard gate to $\\vert x_3\\rangle$\n", "\n", "$$\n", "|\\psi_6\\rangle = \n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\n", "\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2}x_3\n", "\\right) \n", "\\vert1\\rangle\\right]\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\n", "\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^2}x_3 + \\frac{2\\pi i}{2}x_2\n", "\\right) \n", "\\vert1\\rangle\\right]\n", "\\otimes\n", "\\frac{1}{\\sqrt{2}}\n", "\\left[\n", "\\vert0\\rangle + \n", "\\exp\\left(\n", "\\frac{2\\pi i}{2^3}x_3 + \\frac{2\\pi i}{2^2}x_2 + \\frac{2\\pi i}{2}x_1\n", "\\right) \n", "\\vert1\\rangle\\right]\n", "$$\n", "\n", "\n", "
7. Keep in mind the reverse order of the output state relative to the desired QFT. Therefore, we must reverse the order of the qubits (in this case swap $y_1$ and $y_3$)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7. Some Notes About the Form of the QFT Circuit " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The example above demonstrates a very useful form of the QFT for $N=2^n$. 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.\n", "\n", "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. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8. Qiskit Implementation\n", "\n", "In Qiskit, the implementation of the $CROT$ gate used in the discussion above is a controlled phase rotation gate. This gate is defined in [OpenQASM](https://github.com/QISKit/openqasm) as\n", "\n", "$$\n", "CP(\\theta) =\n", "\\begin{bmatrix} 1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 1 & 0 \\\\ 0 & 0 & 0 & e^{i\\theta}\\end{bmatrix}\n", "$$\n", "\n", "Hence, the mapping from the $CROT_k$ gate in the discussion above into the $CP$ gate is found from the equation\n", "\n", "$$\n", "\\theta = 2\\pi/2^k = \\pi/2^{k-1}\n", "$$\n", "\n", "### 8.1 Example on 3 Qubits " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from numpy import pi\n", "# importing Qiskit\n", "from qiskit import QuantumCircuit, transpile, assemble, Aer, IBMQ\n", "from qiskit.providers.ibmq import least_busy\n", "from qiskit.tools.monitor import job_monitor\n", "from qiskit.visualization import plot_histogram, plot_bloch_multivector" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is useful to work out the relevant code for the 3-qubit case before generalizing to the $n$-qubit case. First, we must define our quantum circuit:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "qc = QuantumCircuit(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**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 :" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n" ], "text/plain": [ "