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

✅ Put your name here

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

Pre-Class Assignment 23: Background for Quantum Computing

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook starts a unit introducing a different model of computation, a model that plays by the rules of quantum physics rather than classical physics.\n", "\n", "I hope you're not intimidated! Unfortunately, quantum physics gets a bad rap of being inherently confusing. This is not at all the case! Quantum physics sounds strange due to some weird consequences that we'll see shortly, but it's actually really easy to do, involving some simple mathematics that you have probably seen before. And it doesn't require you to know any classical physics! (If you do, you'll see why quantum physics is a strange theory.)\n", "\n", "Quantum computing is a relatively new field that started in the 1980s and 1990s. Due to recent advances in experimental physics and engineering, we have today some of the world's first quantum computers, and the field has received a lot of attention recently. At the end of this unit, you'll have the opportunity to program a quantum computer!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Itinerary for Quantum Computing Unit

" ] }, { "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": [ "###

Before you start...

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Take ten seconds to answer these survey questions:" ] }, { "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": [ "##

Learning Goals for Today's Pre-Class Assignment

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By the end of today's pre-class assignment, you should be able to:\n", "\n", "1. Describe how computers store information using binary digits.\n", "1. State the fundamental difference between classical and quantum computers in terms of how they store information.\n", "1. Review/learn complex numbers, probability distributions, and vectors to more deeply understand quantum binary digits." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

How Computers Store Information

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Watch the following video to learn about binary digits, or bits, the fundamental unit of information for all data in a computer." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"How computers work: binary & data.\"\"\"\n", "from IPython.display import YouTubeVideo\n", "YouTubeVideo(\"USCBCmwMCDA\", width=640, height=360)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: What are the possible values of a bit?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The video mentioned that 1001 in binary is equal to 9 in decimal. You should understand how to convert from binary to decimal ($1001$ means $1 \\cdot 2^3 + 0 \\cdot 2^2 + 0 \\cdot 2^1 + 1 \\cdot 2^0 = 9$). There's a cool trick for doing this in Python, shown below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Cool trick! Converting from binary to decimal.\"\"\"\n", "int(\"1001\", 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, the first argument to `int` is what gets converted to a number. The second argument to `int` represents the base of the number system to use (binary = base 2). You can change the first argument to get different resulting numbers and test your understanding of binary." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: All data on a computer--including text, images, and sound--is stored in bits. Pick one of these (text, images, or sound) and explain how bits are used to represent this information." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: What do we use to physically represent bits in computers?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

How Quantum Computers Store Information

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recall the last statement from the above video on bits and data:\n", "\n", "
\"If you want to understand how computers work on the inside, it all comes down to these simple ones and zeros and the electrical signals in the circuits behind them.\"
\n", "\n", "In the same way, if you want to understand how quantum computers work, it all comes down to how information is stored.\n", "\n", "
Quantum computers store information in quantum bits, or qubits (pronounced \"CUE bits\") for short.
\n", "\n", "Watch the following short video to get introduced to qubits." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Introduction to qubits.\"\"\"\n", "from IPython.display import YouTubeVideo\n", "YouTubeVideo(\"KBpYK3i3kDs\",width=640,height=360)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

Understanding Qubits: Three Key Concepts

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To understand a qubit, we only have to understand three concepts.\n", "\n", "1. Complex numbers.\n", "1. Probability.\n", "1. Vectors.\n", "\n", "Watch the next three videos to see each concept in turn, and complete the exercises to test your understanding. \n", "\n", "The goal of these concepts is to understand a qubit at a deeper level. Each may seem unrelated, but everything will tie together at the end of the notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Imports for the notebook.\"\"\"\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Concept #1: Complex Numbers

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Watch the following video on complex numbers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Complex numbers.\"\"\"\n", "from IPython.display import YouTubeVideo\n", "YouTubeVideo(\"3AmdT0CsLbk\",width=640,height=360)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###

Video Recap

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* The imaginary unit, which we'll denote $i$, is defined by the property that $i^2 = -1$. (Note: In Python, `j` is used for the imaginary unit.)\n", "\n", "* A complex number has the form\n", "\n", "\\begin{equation}\n", " \\alpha = a + b i\n", "\\end{equation}\n", "\n", "where $a$ and $b$ are real numbers. (The symbol $\\alpha$ is the Greek letter alpha. We'll use Greek letters for complex numbers to not confuse them with real numbers.)\n", "\n", "* The addition of two complex numbers is defined by\n", "\n", "\\begin{equation}\n", " \\alpha + \\beta = (a + b i) + (c + d i) := (a + c) + (b + d)i .\n", "\\end{equation}\n", "\n", "* We define the complex conjugate of a complex number $\\alpha = a + bi$ to be\n", "\n", "\\begin{equation}\n", " \\alpha^* := a - bi .\n", "\\end{equation}\n", "\n", "(That is, we flip the sign of the imaginary part.) \n", "\n", "* The modulus squared of $\\alpha$ is defined to be the product of itself with its complex conjugate:\n", "\n", "\\begin{equation}\n", " |\\alpha|^2 := \\alpha^* \\alpha = a^2 + b^2\n", "\\end{equation}\n", "\n", "As you might guess, the modulus is just the square root of the modulus squared." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###

Exercise: Working with Complex Numbers

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Do this:** Run the cell below to see how to perform some operations on complex numbers in Python." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Working with complex numbers in Python.\"\"\"\n", "# define two complex numbers\n", "alpha = 1 + 2j # note: j is used for the imaginary unit in Python\n", "beta = 3 - 4j\n", "print(\"alpha =\", alpha)\n", "print(\"beta =\", beta)\n", "\n", "# print out the type of alpha\n", "print(\"\\ntype(alpha) =\", type(alpha))\n", "\n", "# print out the real and imaginary part of alpha\n", "print(\"\\nThe real part of alpha is\", alpha.real)\n", "print(\"The imaginary part of alpha is\", alpha.imag)\n", "\n", "# print out the sum of alpha and beta\n", "print(\"\\nalpha + beta =\", alpha + beta)\n", "\n", "# print out the complex conjugate of alpha and beta\n", "print(\"\\nalpha* =\", alpha.conjugate())\n", "print(\"beta* =\", beta.conjugate())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Do this:** Write a function called `modulus_squared` that inputs a complex number $\\alpha$ and returns its modulus squared $|\\alpha|^2 = \\alpha^* \\alpha$. \n", "\n", "Important: Make sure your function returns a `float`, not a `complex` number. You can do this by using the `real` part of the modulus squared." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Put code for implementing your function here!\"\"\"\n", "def modulus_squared(alpha):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"ANSWER.\"\"\"\n", "def modulus_squared(alpha):\n", " # one way\n", " return (alpha.conjugate() * alpha).real\n", "\n", " # another way\n", " return abs(alpha)**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The next cell contains test cases for your function. If your function is correct, this cell will execute without error. (Note: `assert EXPRESSION` throws an error if the `EXPRESSION` is `False`. Otherwise, nothing happens. For this reason, it's often used to test code.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Test cases: run this cell to ensure your function is correct.\"\"\"\n", "assert np.isclose(modulus_squared(3+4j), 25.0)\n", "assert np.isclose(modulus_squared(1), 1.0)\n", "assert np.isclose(modulus_squared(1j), 1.0)\n", "assert np.isclose(modulus_squared(-3 - 4j), 25.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Concept #2: Probability

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Watch the following video on probability distributions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Probability.\"\"\"\n", "from IPython.display import YouTubeVideo\n", "YouTubeVideo(\"rfmmhXzi5lk\",width=640,height=360)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###

Video Recap

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A probability distribution is a list of numbers $p_1, ..., p_n$ that satisfy the following conditions:\n", "\n", "* Each probability is non-negative.\n", "\n", "\\begin{equation}\n", "p_i \\ge 0\n", "\\end{equation}\n", "\n", "* The sum over all probabilites is equal to one.\n", "\n", "\\begin{equation}\n", "\\sum_{i = 1}^{n} p_i = 1 .\n", "\\end{equation}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###

Exercise: Working with Probabilities

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question:** Could the following list of numbers be a probability distribution? Why or why not?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Potential probability distribution.\"\"\"\n", "distribution = np.array([0.1, 0.3, 0.2, 0.2, 0.1, 0.2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question:** Write a function, called `is_valid`, that inputs a numpy array and returns `True` if the list of numbers defines a valid probability distribution, else returns `False`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Put code for implementing your function here!\"\"\"\n", "def is_valid(array):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"ANSWER.\"\"\"\n", "def is_valid(array):\n", " if any(array < 0) or sum(array) != 1:\n", " return False\n", " return True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the next cell to test your function. If your function is correct, no errors should be thrown." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Run this cell to test your function.\"\"\"\n", "assert is_valid(np.array([0.5, 0.3, 0.2]))\n", "assert not is_valid(np.array([0.2, 0.4, 0.2]))\n", "assert not is_valid(np.array([1.0, -1.0, 1.0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Concept #3: Linear Algebra & Vectors

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Watch the following video on vectors." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Linear algebra and vectors.\"\"\"\n", "from IPython.display import YouTubeVideo\n", "YouTubeVideo(\"klDm1eC1gxg\",width=640,height=360)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###

Video Recap

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* A vector is the formal mathematical term for a list of numbers. (You may understand vectors as objects with size and direction, which is an equally valid definition. For the purposes of quantum computing, it's more convenient to think of vectors as just lists of numbers.) \n", "\n", "* An example of a vector 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 another example of a vector 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 angled-bracket notation $|\\rangle$ denotes that an object is a vector. The number inside of the angled brackets is a label for which vector it is. (You'll see why we label the vectors 0 and 1 in the next In Class Assignment. In principle, though, any symbol could be used to label the vector.)\n", "\n", "* Vector addition is defined component-wise. For example,\n", "\n", "\\begin{equation}\n", " |0\\rangle + |1\\rangle = \\left[ \\begin{matrix}\n", " 1 \\\\\n", " 0 \\\\\n", " \\end{matrix} \\right] +\n", " \\left[ \\begin{matrix}\n", " 0 \\\\\n", " 1 \\\\\n", " \\end{matrix} \\right]\n", " =\n", " \\left[ \\begin{matrix}\n", " 1 + 0 \\\\\n", " 0 + 1 \\\\\n", " \\end{matrix} \\right]\n", " =\n", " \\left[ \\begin{matrix}\n", " 1 \\\\\n", " 1 \\\\\n", " \\end{matrix} \\right]\n", "\\end{equation}\n", "\n", "* We can also take scalar multiples of vectors, for example\n", "\n", "\\begin{equation}\n", " \\alpha |0\\rangle = \\alpha \\left[ \\begin{matrix}\n", " 1 \\\\\n", " 0 \\\\\n", " \\end{matrix} \\right]\n", " =\n", " \\left[ \\begin{matrix}\n", " \\alpha \\cdot 1 \\\\\n", " \\alpha \\cdot 0 \\\\\n", " \\end{matrix} \\right]\n", " =\n", " \\left[ \\begin{matrix}\n", " \\alpha \\\\\n", " 0 \\\\\n", " \\end{matrix} \\right].\n", "\\end{equation}\n", "\n", "In general, we multiply each component of the vector by the number $\\alpha$. \n", "\n", "* This allows us to write superpositions, which are scalar multiples and sums of vectors. That is, equations of the form\n", "\n", "\\begin{equation}\n", " \\alpha |0\\rangle + \\beta |1\\rangle\n", "\\end{equation}\n", "\n", "* In Python, Numpy arrays handle vector operations for us." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###

Exercise: Working with Vectors

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following cell shows how we use Numpy arrays to work with vectors in Python." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Using numpy to perform vector operations.\"\"\"\n", "# the |0> == zero vector and |1> == one vector from above\n", "zero = np.array([1, 0], dtype=np.complex64)\n", "one = np.array([0, 1], dtype=np.complex64)\n", "\n", "# print out the vectors\n", "print(\"|0> =\", zero)\n", "print(\"|1> =\", one)\n", "\n", "# some complex numbers\n", "alpha = 0.5 + 0.5j\n", "beta = 1 - 2j" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Do this:** Run the following code cell to see how Numpy arrays handle vector operations for us. Complete the last portion, labeled `TODO`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Run this cell. Complete the last portion.\"\"\"\n", "# print out the sum of zero and one\n", "print(\"|0> + |1> =\", zero + one)\n", "\n", "# compute and print out alpha * |0>\n", "print(\"alpha |0> =\", alpha * zero)\n", "\n", "# compute and print out beta * |1>\n", "print(\"beta |1>> =\", beta * one)\n", "\n", "# TODO: print out the superposition alpha |0> + beta |1>\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"ANSWER.\"\"\"\n", "# TODO: print out the superposition alpha |0> + beta |1>\n", "print(\"alpha |0> + beta |1> =\", alpha * zero + beta * one)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: Is this output of the cell above what you expect based on the definition of vector addition and scalar multiples of vectors?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " **Answer:** Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

Tying Together the Concepts

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we introduced a qubit, we said it could be the state $|0\\rangle$, $|1\\rangle$, or superpositions of $|0\\rangle$ and $|1\\rangle$. We can now fully understand this statement.\n", "\n", "A superposition is a sum of scalar multiples of vectors. So, the most general state of a qubit can be written\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", "where $|\\alpha|^2 + |\\beta|^2 = 1$.\n", "\n", "That is, a qubit is a vector of complex numbers. These complex numbers determine the probability of measuring a particular state, as we'll discuss in upcoming assignments.\n", "\n", "Unlike bits, which are only 0 or 1, qubits can exist in superposition states. This is the first idea that there is \"more\" processing power with qubits (quantum computers) than with bits (classical computers).\n", "\n", "\n", "However, this isn't the entire story. Teaser of what's to come: When we measure qubits, we record either 0 with a probability that depends on $\\alpha$, the coefficient of $|0\\rangle$. In particular,\n", "\n", "\\begin{equation}\n", " p(\\text{measuring 0}) = |\\alpha|^2\n", "\\end{equation}\n", "\n", "Similarly for measuring 1:\n", "\n", "\\begin{equation}\n", " p(\\text{measuring 1}) = |\\beta|^2\n", "\\end{equation}\n", "\n", "This is why we requre $|\\alpha|^2 + |\\beta|^2 = 1$ for qubits. The next in class assignment will explore measurements further and give you more practice working with qubits.\n", "\n", "(Brief remark for those interested: A qubit is an example of a wavefunction in quantum physics. A wavefunction is a mathematical description of a quantum system. In the discrete case (like a qubit), it consists of a vector of complex numbers which determine the probability of measuring particular states.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

Assignment Wrapup

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

Survey

" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "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 }