✅ Put your name here

Pre-Class Assignment 24: Software for Quantum Computing

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.

Itinerary for Quantum Computing Unit

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 Pre-Class Assignment

By the end of today's pre-class assignment, you should be able to:

  1. Describe what the software Qiskit is useful for.
  2. Recognize other software for quantum computing.
  3. Translate between quantum circuits in Qiskit and quantum circuit diagrams.


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.

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.

In [ ]:
"""Try to install Qiskit here if you haven't already."""
#pip install qiskit==0.7.0

Once Qiskit is installed, we can import it below, with the rest of the packages we'll need for today's notebook.

In [ ]:
"""Imports for the notebook."""
import qiskit
%matplotlib inline

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).

Answer: Erase the contents of this cell and put your answer here!

Bits and Qubits in Qiskit

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.

In [ ]:
"""Example: Get a qubit in qiskit using a QuantumRegister."""
qubit = qiskit.QuantumRegister(1)
In [ ]:
"""Example: Get a bit in qiskit using a ClassicalRegister."""
bit = qiskit.ClassicalRegister(1)

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.

In Qiskit, we can use a QuantumCircuit object for creating a quantum circuit with our bit and qubit.

In [ ]:
circuit = qiskit.QuantumCircuit(qubit, bit, name="Example")

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 QuantumCircuits in Qiskit.

Operations on Qubits

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.

Here's a link to the Documentation for Qubit Operations in Qiskit. Refer to this for the following questions.

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.)

Answer: Erase the contents of this cell and put your answer here!

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.)

Answer: Erase the contents of this cell and put your answer here!

We can code up these operations now. Here's an example, implementing the NOT operation.

In [ ]:
"""Perform the NOT operation on a qubit."""

Question: Complete the following code to implement the Hadamard operation on a qubit.

In [ ]:
"""Complete the following code."""
qubit = qiskit.QuantumRegister(1)
bit = qiskit.ClassicalRegister(1)
circuit = qiskit.QuantumCircuit(qubit, bit)

# TODO: put your code here for implementing a Hadamard operation

Quantum Circuit Diagrams

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.

In [ ]:
"""Example of drawing a quantum circuit diagram."""

Here's what it means:

  • Names for qubits and bits appear on the far left. These are handled automatically by Qiskit.
  • Moving to the right, initial states are next. Conventionally, these are always $|0\rangle$ for qubits and 0 for bits.
  • Single lines represent qubits evolving in time. Double lines represent bits evolving in time.
  • Operations appear in boxes with a symbol identifying the operation. (For example, H for Hadamard.)

These are useful for circuits with multiple operations.

Do this: Add a NOT operation to the circuit, then draw it again.

In [ ]:
"""Add a NOT operation to the circuit, then draw it again."""

If done correctly, you should see another box (gate) with an X after the Hadamard gate.

Measurements in Qiskit

In the last in class assignment, we discussed what happens when qubits are measured. Qiskit provides built-in methods for handling measurements.

Do this: Add a measurement to your quantum circuit, then draw it again. (You may want to consult the Qiskit documentation for operations.)

In [ ]:
"""Put your code here."""

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.

Executing a Circuit

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.

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.)

Here's an example of how to get a quantum computer simulator backend in Qiskit.

In [ ]:
"""Example of getting a backend in Qiskit."""
backend = qiskit.Aer.get_backend("statevector_simulator")

Once we have a backend, we can execute our quantum circuit. Example code for doing this is shown below.

In [ ]:
"""Executing a circuit in Qiskit."""
job = qiskit.execute(circuit, backend, shots=100)

Here, the shots is the number of times to execute the circuit, which is here set to 100.

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.

In [ ]:
"""Parsing the outcome of a circuit execution."""
result = job.result()
counts = result.get_counts()

The format of this outcome is a dictionary of bit strings measured and how many times they were measured.

Last, we can visualize the outcome of a circuit execution, as follows.

In [ ]:
"""Visualize the outcome of a circuit execution."""

Optional Video on Qiskit

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.

In [ ]:
"""Optional video on Qiskit."""
from IPython.display import YouTubeVideo

Assignment Wrapup

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.

Instructions to Get an API Key to Use a Quantum Computer

  1. Navigate to the IBM Quantum Experience website https://quantumexperience.ng.bluemix.net/qx.

  2. Click "Sign In" in the upper right hand corner of the page (blue box with white text).

  3. In the pop-up screen, select "Sign Up."

  4. Fill out the form, then click "Sign up" at the bottom.

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.


In [ ]:
from IPython.display import HTML

Congrats, You're Finished!

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.)

© Copyright 2019, Michigan State University Board of Trustees.