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

✅ Put your name here

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

Pre-Class Assignment 24: Software for Quantum Computing

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this pre class assignment, we'll continue our unit on quantum computing. We'll use specialized software for quantum computing to get more practice with qubits, bits, and operations. We'll use this same software in the next in class assignment to write quantum circuits and execute them on real quantum computers." ] }, { "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": [ "##

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", "\n", "1. Describe what the software Qiskit is useful for.\n", "1. Recognize other software for quantum computing.\n", "1. Translate between quantum circuits in Qiskit and quantum circuit diagrams." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

Qiskit

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Qiskit, short for Quantum Information Software Kit, was developed by researchers at IBM to allow users to work with quantum circuits and execute them on real and simulated quantum computers. You should have tried to install Qiskit at the end of In Class Assignment 23 so you can import it here. If you didn't get to this point, try to install Qiskit below. If you're still having issues, consult the documentation for installation instructions/tips, ask us on Slack, or come to help room hours. \n", "\n", "The documentation will be a good place to refer to throughout the assignment for how to do certain operations in Qiskit. We'll explain all of the quantum computing concepts you need to understand within the notebook, however." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"Try to install Qiskit here if you haven't already.\"\"\"\n", "#pip install qiskit==0.7.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once Qiskit is installed, we can import it below, with the rest of the packages we'll need for today's notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Imports for the notebook.\"\"\"\n", "import qiskit\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: Qiskit is not the only software for working with quantum computers in Python. Do a web search and list at least two other Python packages here. 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": [ "##

Bits and Qubits in Qiskit

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the last in class assignment, we wrote our own classes for working with bits and qubits. Qiskit provides this functionality as well, but written in terms of registers, which are collections of bits or qubits." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Example: Get a qubit in qiskit using a QuantumRegister.\"\"\"\n", "qubit = qiskit.QuantumRegister(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Example: Get a bit in qiskit using a ClassicalRegister.\"\"\"\n", "bit = qiskit.ClassicalRegister(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Registers of bits and qubits aren't very useful by themselves until we create a quantum circuit with them. A quantum circuit is a collection of qubits, bits, and operations.\n", "\n", "In Qiskit, we can use a `QuantumCircuit` object for creating a quantum circuit with our bit and qubit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "circuit = qiskit.QuantumCircuit(qubit, bit, name=\"Example\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A `QuantumCircuit` is one of the main tools we will use from Qiskit. You should be comfortable creating a `QuantumCircuit` object. In the next exercises, you'll learn more about `QuantumCircuit`s in Qiskit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Operations on Qubits

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the previous in class assignment, we wrote a `Qubit` class that could perform a few operations on the qubit's wavefunction. Qiskit also allows operations to be performed on qubits. Specifically, a `QuantumCircuit` object contains built-in methods for operating on qubits.\n", "\n", "Here's a link to the Documentation for Qubit Operations in Qiskit. Refer to this for the following questions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: Our `Qubit` class could implement a NOT operation. What is the name of the method for this in Qiskit? (Hint: You can try a ctrl+f search for \"bit-flip\" to quickly find the method.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Answer: Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: Our `Qubit` class could also implement a Hadamard operation. What is the name of the method for this in Qiskit? (Hint: You can try a ctrl+f search for \"Hadamard\" to quickly find the method.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Answer: Erase the contents of this cell and put your answer here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can code up these operations now. Here's an example, implementing the NOT operation." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Perform the NOT operation on a qubit.\"\"\"\n", "circuit.x(qubit)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Question: Complete the following code to implement the Hadamard operation on a qubit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Complete the following code.\"\"\"\n", "qubit = qiskit.QuantumRegister(1)\n", "bit = qiskit.ClassicalRegister(1)\n", "circuit = qiskit.QuantumCircuit(qubit, bit)\n", "\n", "# TODO: put your code here for implementing a Hadamard operation\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Quantum Circuit Diagrams

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because describing quantum circuits with words (NOT gate here, then a Hadmard gate, etc.) is not very illuminating, Qiskit allows us to draw graphical representations of circuits, known as quantum circuit diagrams. An example of drawing a quantum circuit diagram is shown below." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"Example of drawing a quantum circuit diagram.\"\"\"\n", "circuit.draw()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's what it means:\n", "\n", "* Names for qubits and bits appear on the far left. These are handled automatically by Qiskit.\n", "* Moving to the right, initial states are next. Conventionally, these are always $|0\\rangle$ for qubits and 0 for bits.\n", "* Single lines represent qubits evolving in time. Double lines represent bits evolving in time.\n", "* Operations appear in boxes with a symbol identifying the operation. (For example, H for Hadamard.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These are useful for circuits with multiple operations." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do this: Add a NOT operation to the circuit, then draw it again." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"Add a NOT operation to the circuit, then draw it again.\"\"\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If done correctly, you should see another box (gate) with an X after the Hadamard gate." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Measurements in Qiskit

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the last in class assignment, we discussed what happens when qubits are measured. Qiskit provides built-in methods for handling measurements." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Do this: Add a measurement to your quantum circuit, then draw it again. (You may want to consult the Qiskit documentation for operations.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Put your code here.\"\"\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Measurement gates are denoted by \"M\" and are connected to classical registers by two vertical lines. You can think of this as writing the value of the measurement into the classical bit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Executing a Circuit

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A quantum circuit is like a recipe. It contains instructions for which operations to perform on which qubits. A backend is like a cook. It performs the operations and returns the result to us.\n", "\n", "In Qiskit, a `backend` object can be either a real quantum computer or a quantum computer simulator. A quantum computer simulator is a program designed to mimic how a real quantum computer would behave. (Our `Qubit` class is a very basic example of a quantum computer simulator. It simulates a quantum computer with only one qubit.)\n", "\n", "Here's an example of how to get a quantum computer simulator backend in Qiskit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Example of getting a backend in Qiskit.\"\"\"\n", "backend = qiskit.Aer.get_backend(\"statevector_simulator\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once we have a backend, we can execute our quantum circuit. Example code for doing this is shown below." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"Executing a circuit in Qiskit.\"\"\"\n", "job = qiskit.execute(circuit, backend, shots=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, the `shots` is the number of times to execute the circuit, which is here set to 100." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result of a circuit execution contains a lot of information. Here's how to get what we usually care about -- what bits were measured, and how often." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"Parsing the outcome of a circuit execution.\"\"\"\n", "result = job.result()\n", "counts = result.get_counts()\n", "print(counts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The format of this outcome is a dictionary of bit strings measured and how many times they were measured.\n", "\n", "Last, we can visualize the outcome of a circuit execution, as follows." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "\"\"\"Visualize the outcome of a circuit execution.\"\"\"\n", "qiskit.tools.visualization.plot_histogram(counts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Optional Video on Qiskit

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you're interested, watch the following video on \"Getting started with Qiskit.\" This video contains much of the information in this notebook, plus some topics we'll cover in the next In Class Assignment. Since the language and examples are slightly more advanced than where we're at now, this video is optional and included at the end of today's assignment." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"Optional video on Qiskit.\"\"\"\n", "from IPython.display import YouTubeVideo\n", "YouTubeVideo(\"V3hXSftZuoc\",width=640,height=360)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#

Assignment Wrapup

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the next in class assignment, you'll use these tools to write quantum circuits that perform useful computation. You'll also get the chance to run a quantum circuit on a real quantum computer. To do so, you'll have to register to get an API key on IBM's website. The instructions for doing this are given below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##

Instructions to Get an API Key to Use a Quantum Computer

" ] }, { "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.\"\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 }