{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

✅ Put your name here

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

In-Class Assignment 23: Classical and Quantum Bits

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In yesterday's pre-class assignment, we got introduced to bits and qubits. Today, we'll get more practice working with both units of information." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Itinerary

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AssignmentTopicDescription
Pre Class 23Background for Quantum ComputingHow Computers Store Information
In Class 23Classsical and Quantum BitsInformation in Quantum States
Pre Class 24Software for Quantum ComputingHigh Level Software and the Circuit Model
In Class 24Programming Quantum ComputersManipulating Quantum Bits to Perform Useful Computations
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Learning Goals for Today's In-Class Assignment

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The purpose of this notebook is to understand how bits are used in classical computers and how qubits are used in quantum computers. In particular, by the end of today's assignment, you should:\n", "\n", "1. Know the difference between a bit and a qubit. (And thus the difference between classical and quantum computing.)\n", "1. Understand how information is stored and retrieved from bits, and what operations can be done on a bit.\n", "1. Understand how information is stored and retrieved from qubits, and what operations can be done on a qubit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Imports for the assignment.\"\"\"\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Recap of the Pre-Class Assignment

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the pre-class assignment, we learned that all information is stored in bits on (classical) computers. The key difference in quantum computers is that they store information in quantum bits, or qubits.\n", "\n", "To get a deeper understanding of qubits, we reviewed/learned three important topics:\n", "\n", "* A complex number has real and imaginary parts and a complex conjugate that allows us to compute it's modulus squared.\n", "\n", "* A probability distribution is a list of numbers (or vector, now that we know what that is) that add up to one. \n", "\n", "* A vector is a list of numbers (real, complex, etc.) that we can form linear combinations, or superpositions, with." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: Is this a valid probability distribution?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Exercise: is this a valid probability distribution?\"\"\"\n", "distribution = np.array([0.6, 0.4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: If you answered yes, what could this probability distribution represent? (Give an example.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: What's the complex conjugate and modulus squared of\n", "\n", "\\begin{equation}\n", " \\alpha = 2 + 4i?\n", "\\end{equation}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Write code in the following cell to answer the above question." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Exercise: compute the complex conjugate and modulus squared of the given complex number.\"\"\"\n", "# complex number\n", "alpha = 2 + 4j\n", "\n", "# TODO: compute and print out the complex conjugate\n", "\n", "\n", "# TODO: compute and print out the modulus squared\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"ANSWER.\"\"\"\n", "# complex number\n", "alpha = 2 + 4j\n", "\n", "# TODO: compute and print out the complex conjugate\n", "print(alpha.conjugate())\n", "\n", "# TODO: compute and print out the modulus squared\n", "print(abs(alpha)**2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

Working with Bits

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember that a bit can have the values of either 1 or 0 (True or False, on or off, yes or no.) All information on (classical) computers is stored in bits. Computers process information by operating on bits." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: How can you encode one letter of the alphabet (a, b, c, ..., x, y, z) using only bits? How many bits do you need at minimum?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: In our laptops, bits are represented by electrical signals. Think of other physical systems that we could use to represent bits of information. List as many as you can (at least three)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Writing a Bit Class

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we know what binary digits are and how to use them to represent information (like letters in the alphabet), let's do a bit of coding (excuse the pun). Specifically, let's code up a `Bit` class to understand them better. A skeleton of the class is provided below, with some methods implemented, which you should not change.\n", "\n", "You're recommended to make all edits to the class here (rather than copying and pasting the class several times below). Unfortunately, this requires some scrolling back and forth between directions and the code. You may wish to have another copy of the notebook open and use one for reading instructions and the other for writing code." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Code cell with a bit class. Keep your class here and modify it as you work through the notebook. \n", "A skeleton of the class is provided for you.\"\"\"\n", "\n", "class Bit:\n", " \"\"\"Binary digit class.\"\"\"\n", " \n", "\n", " def display_value(self):# <-- do not modify this method\n", " \"\"\"Displays the value of the Bit.\"\"\"\n", " print(\"The bit's value is \", self.value, \".\", sep=\"\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"ANSWER: Final class after completing all instructions.\"\"\"\n", "class Bit:\n", " \"\"\"Binary digit class.\"\"\"\n", " \n", " def __init__(self, initial_value=0):\n", " \"\"\"Initialize a Bit to the 0 state.\"\"\"\n", " self.value = initial_value\n", " \n", " def NOT(self):\n", " \"\"\"Performs the NOT operation on the Bit.\"\"\"\n", " self.value = (self.value + 1) % 2\n", "\n", " def measure(self):\n", " \"\"\"Returns the value of the Bit.\"\"\"\n", " return self.value\n", " \n", " def copy(self):\n", " \"\"\"Returns a copy of the current bit.\"\"\"\n", " return Bit(self.value)\n", "\n", " def display_value(self):# <-- do not modify this method\n", " \"\"\"Displays the value of the Bit.\"\"\"\n", " print(\"The bit's value is \", self.value, \".\", sep=\"\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We know that every class needs an `__init__` method, which here will create our `Bit`. Let's agree by convention to always start our bit in the \"off\" state, represented by 0, unless a different initial value is provided. We'll do this by using keyword arguments as described below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do the following to your class:\n", "\n", "(1) Define an `__init__` method. This method should have a keyword argument (input to the function) called `initial_value` whose default value is 0. Create a class attribute called `value` and set it to be `initial_value`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Create a bit and display it's value.\"\"\"\n", "b = Bit()\n", "b.display_value()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do the following to your class:\n", "\n", "(2) Write a method called `measure` which returns the `value` of the bit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now run the following code block." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Create a bit, display its value, and print out its state.\"\"\"\n", "b = Bit()\n", "b.display_value()\n", "print(\"The measured state of the bit is {}\".format(b.measure()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I know what you're thinking! \"Well duh! The measured state of a bit is just going to be it's value...\" Hold that thought! We're going to see a big difference when we write a class for a quantum bit.\n", "\n", "First, let's talk about the operations we can perform on a bit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###

Operations on a Bit

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There's only one non-trivial operation that can be performed on a bit -- negating, or flipping, its value. We'll call this operation the `NOT` operation, which has the following effect:\n", "\n", "\\begin{align}\n", " \\text{NOT(0)} &= 1 \\\\\n", " \\text{NOT(1)} &= 0\n", "\\end{align}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do the following to your class:\n", "\n", "(3) Define a method called `NOT` which negates the `value` of the bit. This method should NOT return a value (no pun intended)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now run the following code with your class." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"Perform operations on a bit.\"\"\"\n", "# create a bit\n", "b = Bit()\n", "b.display_value()\n", "print(\"The measured state of the bit is {}.\\n\".format(b.measure()))\n", "\n", "# apply a NOT operation\n", "b.NOT()\n", "b.display_value()\n", "print(\"The measured state of the bit is {}.\\n\".format(b.measure()))\n", "\n", "# apply another NOT operation\n", "b.NOT()\n", "b.display_value()\n", "print(\"The measured state of the bit is {}.\\n\".format(b.measure()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that applying two `NOT` gates in a row gets us back to the same state, as you might expect.\n", "\n", "We can certainly do more operations with multiple bits of information. For example, if we have two bits, we can take the `AND` or the `OR` of them. The `AND` takes in two bits and returns one if both input bits are one and zero otherwise. The `OR` returns one if either of the input bits is one (including both).\n", "\n", "Operations on multiple bits are crucial for information processing (i.e., computation), but for simplicity we won't discuss them in more detail here. \n", "\n", "One reason we do mention multiple bits is for copying information, another crucial component of (classical) computation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Copying Bits

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can copy a single classical bit into as many bits as we want. How? Well, we just look at the bit, record its value, then prepare another bit with that value." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do the following to your class:\n", "\n", "(4) Define a method called `copy` which copies the `value` of the `Bit` to a new `Bit` and returns the new `Bit`. Then execute the following cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"Copy a bit.\"\"\"\n", "b = Bit()\n", "new_bit = b.copy()\n", "\n", "b.display_value()\n", "new_bit.display_value()\n", "\n", "print(b == new_bit)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the bits have the same value, but they are not equivalent, since they are different objects. With bits, we are able to directly \"measure\" the bits value and write it into a new bit of information.\n", "\n", "We highlighted this feature, as well as the others, to now contrast bits with qubits." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

Working with Qubits

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Whereas classical computers represent information using bits, quantum computers represent information using qubits. Here's a short refresher on what a qubit is from the Pre-Class Assignment.\n", "\n", "A qubit is a vector of complex numbers\n", "\n", "\\begin{equation}\n", " |\\psi\\rangle = \\alpha |0\\rangle + \\beta |1\\rangle\n", " =\n", " \\left[ \\begin{matrix}\n", " \\alpha \\\\\n", " \\beta \\\\\n", " \\end{matrix} \\right] .\n", "\\end{equation}\n", "\n", "These complex numbers determine the probability of measuring 0 or 1, as we'll see today, so we require that $|\\alpha|^2 + |\\beta|^2 = 1$.\n", "\n", "As a reminder, the vector $|0\\rangle$, sometimes called the ground state, is\n", "\n", "\\begin{equation}\n", " |0\\rangle = \\left[ \\begin{matrix}\n", " 1 \\\\\n", " 0 \\\\\n", " \\end{matrix} \\right]\n", "\\end{equation}\n", "\n", "and the vector $|1\\rangle$, sometimes called the excited state, is \n", "\n", "\\begin{equation}\n", " |1\\rangle = \\left[ \\begin{matrix}\n", " 0 \\\\\n", " 1 \\\\\n", " \\end{matrix} \\right] .\n", "\\end{equation}\n", "\n", "The Greek symbol $\\psi$ (psi, pronounced: \"sigh\") is commonly used to represent qubits." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: Bits are made of classical physical systems like light switches or electricity. What kind of quantum systems do people make qubits out of? Search the web to find out and record at least three of your findings below. Cite your source(s)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Writing a Qubit Class

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's now get more practice with qubits by writing a `Qubit` class in the same way that we wrote a `Bit` class. This will allow you to see the similarites and differences between classical and quantum bits." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Code cell with a qubit class. Keep your class here and modify it as you work through the notebook. \n", "A skeleton of the class is provided for you.\"\"\"\n", "\n", "class Qubit:\n", " \"\"\"Quantum bit class.\"\"\"\n", "\n", "\n", " def display_wavefunction(self):# <-- do not modify this method!\n", " \"\"\"Prints the wavefunction of the Qubit.\"\"\"\n", " print(\"The Qubit's wavefunction is\", self.wavefunction[0], self.wavefunction[1], sep=\"\\n\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"ANSWER: complete class is shown below.\"\"\"\n", "\n", "class Qubit:\n", " \"\"\"Quantum bit class.\"\"\"\n", " # data -- states. NOTE: THESE CAN BE DEFINED ELSEWHERE IN THE CLASS\n", " zero = np.array([1, 0], dtype=np.complex64)\n", " one = np.array([0, 1], dtype=np.complex64)\n", " \n", " # data -- gates. NOTE: THESE CAN BE DEFINED ELSEWHERE IN THE CLASS\n", " xmat = np.array([[0, 1], [1, 0]], dtype=np.complex64)\n", " hmat = 1 / np.sqrt(2) * np.array([[1, 1], [1, -1]], dtype=np.complex64)\n", " \n", " def __init__(self):\n", " \"\"\"Initializes a Qubit to the ground state |0>.\"\"\"\n", " self.wavefunction = self.zero\n", "\n", " def measure(self):\n", " \"\"\"Returns the state obtained by measuring the Qubit and modifies the wavefunction accordingly.\"\"\"\n", " prob_ground_state = abs(self.wavefunction[0])**2\n", " if np.random.uniform() < prob_ground_state:\n", " self.wavefunction = self.zero\n", " return 0\n", " else:\n", " self.wavefunction = self.one\n", " return 1\n", "\n", " def NOT(self):\n", " \"\"\"Performs a NOT operation on the Qubit's wavefunction.\"\"\"\n", " self.wavefunction = np.dot(self.xmat, self.wavefunction)\n", "\n", " def H(self):\n", " \"\"\"Performs a Hadamard operation on the Qubit's wavefunction\"\"\"\n", " self.wavefunction = np.dot(self.hmat, self.wavefunction)\n", "\n", " def display_wavefunction(self):# <-- do not modify this method!\n", " \"\"\"Prints the wavefunction of the Qubit.\"\"\"\n", " print(\"The Qubit's wavefunction is\", self.wavefunction[0], self.wavefunction[1], sep=\"\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do the following to your `Qubit` class:\n", "\n", "(1) Define an `__init__` method. In this method:\n", "\n", "(a) Create a class attribute called `zero` which represents the vector $|0\\rangle$ above. This attribute should be a Numpy array. Make sure the datatype (`dtype`) is complex, for example using `np.complex64`.\n", "\n", "(b) Create a class attribute called `one` which represents the vector $|1\\rangle$ above. This attribute should be a Numpy array. Make sure the datatype (`dtype`) is complex, for example using `np.complex64`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By convention, let's agree to always initialize a `Qubit` in the ground state $|0\\rangle$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do the following to your `Qubit` class:\n", "\n", "(1) In the `__init__` method, create an attribute called `wavefunction` of the `Qubit` and set it to be equal to the $|0\\rangle$ state. (The term wavefunction is physics jargon. We can say \"a qubit is...\" or \"a qubit's wavefunction is...\" interchangeably.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now run the following code." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Initialize a qubit and display its wavefunction.\"\"\"\n", "q = Qubit()\n", "q.display_wavefunction()\n", "print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above code, we initialize a qubit and then use the provided `display_wavefunction` method to print out it's wavefunction. If your `__init__` method is correct, you should see the qubit's wavefunction as the $|0\\rangle$ vector." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###

Measuring a Qubit

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's write a method to measure our `Qubit`. The two measurement rules of a qubit \n", "\n", "\\begin{equation}\n", " |\\psi\\rangle = \\alpha |0\\rangle + \\beta |1\\rangle\n", " =\n", " \\left[ \\begin{matrix}\n", " \\alpha \\\\\n", " \\beta \\\\\n", " \\end{matrix} \\right] .\n", "\\end{equation}\n", "\n", "are listed below:\n", "\n", "The First Measurement Rule\n", "\n", "(1) The probability that $|\\psi\\rangle$ is in the ground state $|0\\rangle$ is $|\\alpha|^2$. The probability that $|\\psi\\rangle$ is in the excited state $|1\\rangle$ is $|\\beta|^2 = 1 - |\\alpha|^2$.\n", "\n", "Key Concept: There are two possible measurement outcomes of a qubit. Thus, the measurement outcome of a qubit is a bit. This is why we used 0 and 1 as labels for the vectors all along! When we measure the ground state $|0\\rangle$, we call this outcome $0$. When we measure the excited state $|1\\rangle$, we call this outcome $1$.\n", "\n", "The Second Measurement Rule\n", "\n", "(2) If we measure the ground state, the wavefunction becomes $|0\\rangle$. If we measure the excited state, the wavefunction becomes $|1\\rangle$.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do the following to your `Qubit` class:\n", "\n", "(1) Write a method called `measure` which measures a `Qubit` according to the above rules. Specifically, your method should return a bit (0 if the ground state was measured, or 1 if the excited state was measured) and modify the `wavefunction` of the `Qubit` appropriately.\n", "\n", "Hints:\n", "\n", "(i) Compute the probability the qubit is in the ground state ($|\\alpha|^2$).\n", "\n", "(ii) Generate a random number between 0 and 1.\n", "\n", "(iii) If the random number is less than $|\\alpha|^2$, set the `wavefunction` to be the `zero` vector, and return the number (bit) 0. Otherwise, set the `wavefunction` to be the `one` vector, and return the number (bit) 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now run the following code." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"Initialize a qubit and measure it.\"\"\"\n", "q = Qubit()\n", "q.display_wavefunction()\n", "print(\"The bit we obtain from measuring the qubit is {}.\\n\".format(q.measure()))\n", "q.display_wavefunction()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should have seen the measurement result 0.\n", "\n", "Question: Run the cell above many times and note the measurement result (i.e., bit) after each time you run it. (The keyboard shortcut \"control + enter\" is useful here.) What measured state do you always get? Why?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Operations on a Qubit

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On a classical bit, we could only do one operation, the `NOT` operation, because the bit only had two states. With our `Qubit`, we have an underlying `wavefunction` that helps determine what our measured state will be, as we have seen above. Operations on a `Qubit` act on its `wavefunction`. As such, there's a lot more operations we can do to it! (In fact, there's infinitely many operations we can do on a qubit.)\n", "\n", "One example of an operation is called the `X` or `NOT` operation. Why is it called this? Well, it has the effect\n", "\n", "\\begin{align}\n", " \\text{NOT($|0\\rangle$)} &= |1\\rangle \\\\\n", " \\text{NOT($|1\\rangle$)} &= |0\\rangle\n", "\\end{align}\n", "\n", "Qubit operations can be written as matrices that act on a qubit's wavefunction to implement the operation. You don't have to know how to do matrix-vector multiplication, just how to do it in Python. An example is shown below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Example of a matrix vector multiplication using numpy.\"\"\"\n", "# an example matrix\n", "matrix = np.array([[1, 1], [1, 1]])\n", "\n", "# an example vector\n", "vector = np.array([1, 0])\n", "\n", "# the matrix-vector product\n", "print(np.dot(matrix, vector))\n", "\n", "# another way to do the matrix-vector product\n", "print(matrix @ vector)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It can be shown that a matrix representation for `NOT` is\n", "\n", "\\begin{equation}\n", " \\text{NOT} = \\left[ \\begin{matrix}\n", " 0 & 1 \\\\\n", " 1 & 0 \\\\\n", " \\end{matrix} \\right] .\n", "\\end{equation}\n", "\n", "(If you know linear algebra, prove this to youreself. If not, just take our word for it.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do the following to your `Qubit` class:\n", "\n", "(1) Write a method called `NOT` which multiplies the `wavefunction` the `NOT` matrix given above. (This method should NOT return a value (still no pun intended), only modify the `wavefunction`.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now run the following code." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"Perform a NOT operation on a qubit.\"\"\"\n", "q = Qubit()\n", "q.display_wavefunction()\n", "print()\n", "\n", "q.NOT()\n", "q.display_wavefunction()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should have a `Qubit` whose wavefunction is $|1\\rangle$ after executing the above cell. Now let's measure such a qubit to see what we get." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Measure a qubit with wavefunction |1>.\"\"\"\n", "q = Qubit()\n", "q.NOT()\n", "q.display_wavefunction()\n", "print(\"The bit we obtain from measuring the qubit is {}.\\n\".format(q.measure()))\n", "q.display_wavefunction()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should have seen 1 as the measurement result." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: Run the above cell many times and observe your measurement results after each run. What measured state do you always get? Why?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another quantum operation is called the Hadamard operation or Hadamard gate. A matrix representation for the Hadamard gate is given by\n", "\n", "\\begin{equation}\n", " H = \\frac{1}{\\sqrt{2}}\\left[ \\begin{matrix}\n", " 1 & 1 \\\\\n", " 1 & -1 \\\\\n", " \\end{matrix} \\right]\n", "\\end{equation}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do the following to your `Qubit` class:\n", "\n", "(1) Write a method called `H` that applies the Hadamard gate given above to the `Qubit`s `wavefunction`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now run the following code." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Performing a Hadamard transform on a qubit.\"\"\"\n", "q = Qubit()\n", "q.H()\n", "q.display_wavefunction()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should see that the qubit has equal amplitudes (components of the wavefunction). Now let's measure the `Qubit` to see what we get." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Measuring a qubit after performing a Hadamard gate.\"\"\"\n", "q = Qubit()\n", "q.H()\n", "q.display_wavefunction()\n", "print(\"The bit we obtain from measuring the qubit is {}.\\n\".format(q.measure()))\n", "q.display_wavefunction()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What measurement result did you get?\n", "\n", "Question: Run the above cell several times and observe your measurement results after each run. Write a sentence describing your observation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: Write code to create a qubit, perform the Hadamard gate, and measure the qubit 1000 times. Record each measurement outcome, then make a histogram of the probability of measuring 0 and the probability of measuring 1." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Put your code here.\"\"\"\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"ANSWER.\"\"\"\n", "# dictionary to store measurement outcomes\n", "measurements = {0: 0, 1: 0}\n", "\n", "# loop over 1000 iterations\n", "for _ in range(1000):\n", " # get a qubit\n", " q = Qubit()\n", " \n", " # apply a Hadamard gate\n", " q.H()\n", " \n", " # measure the qubit to get a bit\n", " bit = q.measure()\n", " \n", " # store the result\n", " measurements[bit] += 1.0 / 1000\n", "\n", "# make a histogram\n", "_ = plt.bar(list(measurements.keys()), measurements.values(), color='g')\n", "plt.xlabel(\"Measurement Outcome\");\n", "plt.ylabel(\"Probability\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: Reflect on your results. What can you say about the probability of measuring 0 and the probability of measuring 1?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Copying Qubits

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember the question of how to copy a bit? Well, now let's ask this for qubits:\n", "\n", "Question: How do you copy a qubit?\n", "\n", "Attempted Answer 1: You just copy it's wavefunction?\n", "\n", "This won't work! Remember the wavefunction is just a mathematical tool that we use to help us calculate probabilities of what state the system is in. If we have a particle, say an electron, there's no wavefunction that we can just look at and then copy over it's information. (Unlike a light switch, a classical system, which we could look at and see with no issues.\n", "\n", "Attempted Answer 2: Well what if we just measure it then copy the measurement result into a new qubit?\n", "\n", "This also won't work! Remember the measurement rules above. When we measure a qubit, we inherently change its wavefunction. The wavefunction it changes to is not, in general, the same as it was before measurement." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is a way to copy one qubit to another, which is known as quantum teleportation, and involves a total of three qubits to get the job done. You'll get a chance to look at this in an upcoming assignment!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

Assignment Wrap-up

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Installing Qiskit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the next assignments, we'll be using the Quantum Information Science Kit, or Qiskit, which is a Python package for quantum computing. Try to install Qiskit v0.7.0 on your computer now by executing the following cell. We'll be walking around to troubleshoot problems.\n", "\n", "Note: Why version 0.7.0? These quantum software packages are new and tend to change a bit. We'll use this version to make sure all the code in future assignments works as anticipated." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Attempt to install Qiskit using pip. Uncomment the following two lines and run the cell.\"\"\"\n", "# !pip install --upgrade pip\n", "# !pip install qiskit==0.7.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Instructions to Get an API Key to Use a Quantum Computer

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use Qiskit to program an actual quantum computer. To do so, one needs to register for an API key. If this interests you, follow the instructions below. (These instructions are also in the next Pre Class Assignment if you're short on time.) This is optional, not required." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Navigate to the IBM Quantum Experience website https://quantumexperience.ng.bluemix.net/qx.\n", "\n", "1. Click \"Sign In\" in the upper right hand corner of the page (blue box with white text).\n", "\n", "1. In the pop-up screen, select \"Sign Up\" or \"Sign up using Github.\" The first requires an email, the second requires you to log into GitHub and authorize access to your account (to get an email).\n", "\n", "1. Fill out the form, then click \"Sign up\" at the bottom.\n", "\n", "Once you have created an account, you can sign in (follow the first two steps above). Then, click the user icon in the upper right hand corner of the page, then click \"My Account.\" On the new screen, click the \"Advanced\" tab. Here, you can see your API key and copy it to your clipboard. You'll need to enter this in your notebook to use the real quantum computer backends." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Survey

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import HTML\n", "HTML(\n", "\"\"\"\n", "\n", "\"\"\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Congrats, You're Finished!

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, you just need to submit this assignment by uploading it to the course Desire2Learn web page for today's submission folder. (Don't forget to add your name in the first cell.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

© Copyright 2019, Michigan State University Board of Trustees.

" ] } ], "metadata": { "kernelspec": { "display_name": "env", "language": "python", "name": "env" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }