✅ 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", "Assignment | Topic | Description |

Pre Class 23 | Background for Quantum Computing | How Computers Store Information |

In Class 23 | Classsical and Quantum Bits | Information in Quantum States |

Pre Class 24 | Software for Quantum Computing | High Level Software and the Circuit Model |

In Class 24 | Programming Quantum Computers | Manipulating Quantum Bits to Perform Useful Computations |

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", "* AWorking 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": [ "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 aOperations 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 ifCopying 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 withWorking 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", "AWriting 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": "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 toMeasuring a Qubit

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's write a method to measure our `Qubit`. TheOperations 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": [ "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 asAssignment 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.**