{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "remove_cell" ] }, "source": [ "# Single Qubit Gates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the previous section we looked at all the possible states a qubit could be in. We saw that qubits could be represented by 2D vectors, and that their states are limited to the form:\n", "\n", "$$|q\\rangle = \\cos{\\tfrac{\\theta}{2}}|0\\rangle + e^{i\\phi}\\sin{\\tfrac{\\theta}{2}}|1\\rangle$$\n", "\n", "Where $\\theta$ and $\\phi$ are real numbers. In this section we will cover _gates,_ the operations that change a qubit between these states. Due to the number of gates and the similarities between them, this chapter is at risk of becoming a list. To counter this, we have included a few digressions to introduce important ideas at appropriate places throughout the chapter. \n", "\n", "## Contents \n", "\n", "1. [The Pauli Gates](#pauli) \n", " 1.1 [The X-Gate](#xgate) \n", " 1.2 [The Y & Z-Gates](#ynzgatez) \n", "2. [Digression: The X, Y & Z-Bases](#xyzbases) \n", "3. [The Hadamard Gate](#hgate) \n", "4. [Digression: Measuring in Different Bases](#measuring) \n", "5. [The P-gate](#rzgate) \n", "6. [The I, S and T-gates](#istgates) \n", " 6.1 [The I-Gate](#igate) \n", " 6.2 [The S-Gate](#sgate) \n", " 6.3 [The T_Gate](#tgate) \n", "7. [The General U-gate](#generalU) \n", "\n", "In _The Atoms of Computation_ we came across some gates and used them to perform a classical computation. An important feature of quantum circuits is that, between initialising the qubits and measuring them, the operations (gates) are *_always_* reversible! These reversible gates can be represented as matrices, and as rotations around the Bloch sphere. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "thebelab-init" ] }, "outputs": [], "source": [ "from qiskit import QuantumCircuit, assemble, Aer\n", "from math import pi, sqrt\n", "from qiskit.visualization import plot_bloch_multivector, plot_histogram\n", "sim = Aer.get_backend('aer_simulator')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. The Pauli Gates \n", "You should be familiar with the Pauli matrices from the linear algebra section. If any of the maths here is new to you, you should use the linear algebra section to bring yourself up to speed. We will see here that the Pauli matrices can represent some very commonly used quantum gates.\n", "\n", "### 1.1 The X-Gate \n", "The X-gate is represented by the Pauli-X matrix:\n", "\n", "$$X = \\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix} = |0\\rangle\\langle1| + |1\\rangle\\langle0|$$\n", "\n", "To see the effect a gate has on a qubit, we simply multiply the qubit’s statevector by the gate. We can see that the X-gate switches the amplitudes of the states $|0\\rangle$ and $|1\\rangle$:\n", "\n", "$$X|0\\rangle = \\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}\\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} = \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} = |1\\rangle$$\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", " Reminder: Multiplying Vectors by Matrices (Click here to expand)\n", "

Matrix multiplication is a generalisation of the inner product we saw in the last chapter. In the specific case of multiplying a vector by a matrix (as seen above), we always get a vector back:\n", " $$M|v\\rangle = \\begin{bmatrix}a & b \\\\ c & d \\\\\\end{bmatrix}\\begin{bmatrix}v_0 \\\\ v_1 \\\\\\end{bmatrix}\n", " = \\begin{bmatrix}a\\cdot v_0 + b \\cdot v_1 \\\\ c \\cdot v_0 + d \\cdot v_1\\end{bmatrix}$$\n", " In quantum computing, we can write our matrices in terms of basis vectors:\n", " $$X = |0\\rangle\\langle1| + |1\\rangle\\langle0|$$\n", " This can sometimes be clearer than using a box matrix as we can see what different multiplications will result in:\n", " \n", " \\begin{aligned}\n", " X|1\\rangle & = (|0\\rangle\\langle1| + |1\\rangle\\langle0|)|1\\rangle \\\\\n", " & = |0\\rangle\\langle1|1\\rangle + |1\\rangle\\langle0|1\\rangle \\\\\n", " & = |0\\rangle \\times 1 + |1\\rangle \\times 0 \\\\\n", " & = |0\\rangle\n", " \\end{aligned}\n", "\n", " In fact, when we see a ket and a bra multiplied like this:\n", " $$|a\\rangle\\langle b|$$\n", " this is called the outer product, which follows the rule:\n", " $$\n", " |a\\rangle\\langle b| = \n", " \\begin{bmatrix}\n", " a_0 b_0^* & a_0 b_1^* & \\dots & a_0 b_n^*\\\\ \n", " a_1 b_0^* & \\ddots & & \\vdots \\\\\n", " \\vdots & & \\ddots & \\vdots \\\\\n", " a_n b_0^* & \\dots & \\dots & a_n b_n^* \\\\\n", " \\end{bmatrix}\n", "$$\n", " where $*$ denotes the complex conjugation. We can see this does indeed result in the X-matrix as seen above:\n", " $$\n", " |0\\rangle\\langle1| + |1\\rangle\\langle0| = \n", " \\begin{bmatrix}0 & 1 \\\\ 0 & 0 \\\\\\end{bmatrix} +\n", " \\begin{bmatrix}0 & 0 \\\\ 1 & 0 \\\\\\end{bmatrix} = \n", " \\begin{bmatrix}0 & 1 \\\\ 1 & 0 \\\\\\end{bmatrix} = X\n", "$$\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Qiskit, we can create a short circuit to verify this:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHMAAABOCAYAAAATpymVAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8rg+JYAAAACXBIWXMAAAsTAAALEwEAmpwYAAADXklEQVR4nO3cT0iTcRzH8c/v2UTwz8EoFC/bRYPwz0WIFsQo8SCEshEdSrQ6JNUts11U8CCBlYdOkZiFaBddYAwPHR4aCMVAvQThYfNmHhLRIbT5/DqYiuhhk9Hv8dvnBbs8g+f5wJs9Y2NMaa01SATL9AAqHMYUhDEFYUxBGFMQxhSEMQVhTEEYUxDGFIQxBWFMQRhTEMYUhDEFYUxBGFMQxhSEMQVhTEEYUxDGFIQxBWFMQRhTEMYUhDEF8ZoeYMrjH9+xtLlp5NqN5eV4cf5Cwc/738Zc2tzEl/VfpmcUFG+zgjCmIIwpCGMKwpiCMKYgjCkIYwrCmDnSmQwy3Y+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