{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quantum Phase Estimation\n", "\n", "**Download this notebook - {nb-download}`phase_estimation.ipynb`**\n", "\n", "When constructing circuits for quantum algorithms it is useful to think of higher level operations than just individual quantum gates. In `pytket` we can construct circuits using box structures which abstract away the complexity of the underlying circuit. This notebook is intended to complement the [boxes section](https://docs.quantinuum.com/tket/user-guide/manual/manual_circuit.html#boxes) of the user manual which introduces the different box types.\n", "\n", "To demonstrate boxes in `pytket` we will consider the Quantum Phase Estimation algorithm (QPE). This is an important subroutine in several quantum algorithms including Shor's algorithm and fault-tolerant approaches to quantum chemistry.\n", "\n", "## Overview of Phase Estimation\n", "\n", "The Quantum Phase Estimation algorithm can be used to estimate the eigenvalues of some unitary operator $U$ to some desired precision.\n", "\n", "The eigenvalues of $U$ lie on the unit circle, giving us the following eigenvalue equation\n", "\n", "$$\n", "\\begin{equation}\n", "U |\\psi \\rangle = e^{2 \\pi i \\theta} |\\psi\\rangle\\,, \\quad 0 \\leq \\theta \\leq 1\n", "\\end{equation}\n", "$$\n", "\n", "Here $|\\psi \\rangle$ is an eigenstate of the operator $U$. In phase estimation we estimate the eigenvalue $e^{2 \\pi i \\theta}$ by approximating $\\theta$.\n", "\n", "\n", "The circuit for Quantum phase estimation is itself composed of several subroutines which we can realise as circuit boxes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "{align=center}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "QPE is generally split up into three stages\n", "\n", "1. Firstly we prepare an initial state $|\\psi\\rangle$ in one register. In parallel we prepare a uniform superposition state using Hadamard gates on some ancilla (measurement) qubits. The number of ancilla qubits determines how precisely we can estimate the phase $\\theta$.\n", "\n", "2. Secondly we apply successive controlled $U$ gates. This has the effect of \"kicking back\" phases onto the ancilla qubits according to the eigenvalue equation above.\n", "\n", "3. Finally we apply the inverse Quantum Fourier Transform ($QFT^\\dagger$). This essentially plays the role of destructive interference, suppressing amplitudes from \"undesirable states\" and hopefully allowing us to measure a single outcome (or a small number of outcomes) with high probability.\n", "\n", "\n", "There is some subtlety around the first point. The initial state used can be an exact eigenstate of $U$ however this may be difficult to prepare if we don't know the eigenvalues of $U$ in advance. Alternatively we could use an initial state that is a linear combination of eigenstates, as the phase estimation will project into the eigenspace of $U$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We also assume that we can implement $U$ with a quantum circuit. In chemistry applications $U$ could be of the form $U=e^{-iHt}$ where $H$ is the Hamiltonian of some system of interest. In the textbook algorithm, the number of controlled unitaries we apply scales exponentially with the number of measurement qubits. This allows more precision at the expense of a larger quantum circuit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Quantum Fourier Transform" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before considering the other parts of the QPE algorithm, lets focus on the Quantum Fourier Transform (QFT) subroutine.\n", "\n", "Mathematically, the QFT has the following action.\n", "\n", "$$\n", "\\begin{equation}\n", "QFT : |j\\rangle\\ \\longmapsto \\frac{1}{\\sqrt{N}} \\sum_{k=0}^{N - 1} e^{2 \\pi ijk/N}|k\\rangle, \\quad N= 2^n\n", "\\end{equation}\n", "$$\n", "\n", "This is essentially the Discrete Fourier transform except the input is a quantum state $|j\\rangle$.\n", "\n", "We can build the circuit for the $n$ qubit QFT using $n$ Hadamard gates $\\lfloor{\\frac{n}{2}}\\rfloor$ swap gates and $\\frac{n(n-1)}{2}$ controlled unitary rotations $\\text{CU1}$.\n", "\n", "$$\n", " \\begin{equation}\n", "U1(\\phi) =\n", " \\begin{pmatrix}\n", " 1 & 0 \\\\\n", " 0 & e^{i \\pi \\phi}\n", " \\end{pmatrix}\\, , \\quad\n", " CU1(\\phi) =\n", " \\begin{pmatrix}\n", " 1 & 0 & 0 & 0 \\\\\n", " 0 & 1 & 0 & 0 \\\\\n", " 0 & 0 & 1 & 0 \\\\\n", " 0 & 0 & 0 & e^{i \\pi \\phi}\n", " \\end{pmatrix}\n", " \\end{equation}\n", "$$\n", "\n", "The circuit for the Quantum Fourier transform on three qubits is the following\n", "\n", "{align=center}\n", "\n", "We can build this circuit in `pytket` by adding gate operations manually:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "lets build the QFT for three qubits" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "