Quantum Pathfinder: Blast Through Beginning Roadblocks & Join the Revolution! Part 2

Kathie Wang
5 min readMar 23, 2024

--

Hi my name is Kathie, and I have been exploring quantum algorithms and using Qiskit for close to two years. I have my own independent project about predicting mental health status of individuals using Quantum Support Vector Machines, and am a high school intern at Dr. Mark Wilde at Cornell University and Dr. Matthew Sullivan at Ithaca College.

Today I will be answering two main questions: 1) how do I run my code on a Jupyter Notebook, and 2) what is a simple quantum computing project I can do using Qiskit?

Once you have installed and opened the Jupyter Notebook, you can first practice getting used to the notebook by coding a non-quantum program to reinforce your understanding of Python data types.

For this example, we can create a dictionary, and print each element while traversing through it (traversing means going through the dictionary from beginning to end). First, we can create a dictionary object and assign the variable name my_dict to it. We can then initialize our dictionary by setting key value pairs to my_dict as shown below:

Keys and values are what makes dictionaries unique, because you can access the corresponding value of a key by just knowing the key. To print all the keys of my_dict, we can write and run the following (run this cell by either clicking the run button on top, or using the shortcut Shift + Enter).

We can also access only the keys and values of a dictionary using the keys() and values() methods. Before running our program, we should also make sure to periodically save our code to make sure we don’t lose our progress.

Being able to iterate through a dictionary can be very important when you are observing the results of quantum algorithm outputs because you may only need to retrieve one element of the dictionary, and you can do this by traversing through a dictionary.

Now, we can try writing our first quantum program — constructing a quantum circuit and measuring the counts! Though you may have seen similar tutorials before, I will include common issues you might encounter and how to fix them.

In a new cell, we will need to import the necessary libraries:

If you run this cell and get an error saying something similar to “cannot import name Execute from ‘qiskit’ ”, this means that the name was typed incorrectly. In this case, you can see that we were trying to import Execute; however, the correct name is the word in lowercase, execute. It’s important to remember that Python is a case-sensitive language, meaning that uppercase and lowercase words represent different things.

The bottom cell shows the incorrect import from qiskit, while the top cell shows the correct import names

Next, we can construct a quantum circuit qc by creating two quantum registers (the first number in the parenthesis) and two classical registers (the second number in the parenthesis). To add gates or operators to our circuit, we can use the format qc.[gate name] — this example shows adding a Hadamard gate to quantum register or qubit 0, and a CNOT gate, where qubit 0 is the control qubit and qubit 1 is the target qubit.

We can then measure the qubits in our circuit by measuring qubit 0 to classical register 0, and qubit 1 to classical register 1. Finally, we can print our circuit by using the qc.draw() method, and include “mpl” and style=“clifford” as a parameters to generate a colorful circuit:

Now, we can simulate running our circuit on a real device and get the results. In a new cell, we can create a backend to run our quantum circuit on. We can then use the execute() method to return a job, which keeps track of the results of our experiment. Lastly, we can get the results of our experiment, and get the counts by using their respective methods. We then print the counts, and after saving our file and running all cells, we get the counts display, and see that its data type is a dictionary!

From the printout, we see that the outcome 00 was measured 505 out of 1024 times, and 11 was measured 519 out of 1024 times.

If you can’t see your circuit drawing, it may be because you wrote the code for the circuit simulation in the same cell as drawing the circuit. You can either add the end section of code in another cell and run both cells, or change the qc.draw() line to display(qc.draw()).

Line 8 can be changed to display(qc.draw()) if you want to keep your code all in one cell

It’s also important to run your cells in the correct order, because if a variable is updated in another cell, you will have to run all the other cells that refer to the variable to get the accurate output.

When I first started learning Qiskit on my own, something that was frustrating for me was forgetting to run the blocks in the correct order. I wanted to separate some lines of code from others to see how each line worked, but it quickly became inconvenient and overwhelming. I’m here to tell you, don’t worry, you will figure this out!! An important skill quantum programmers have is being able to carefully examine your own work and see what may be the bug; so remember to not get frustrated when something does not work. Another thing to note is that sometimes you just need to step away from your code for 5 minutes and shift your focus, because you might see your code in a different perspective later. The more projects you do, the more comfortable you will become with programming using Qiskit!

Check out this link about accessing the IBM quantum lab, downloading your Jupyter notebook, and how to use the lab environment!

https://medium.com/@vidur.jannapureddy/quantum-pathfinder-blast-through-beginning-roadblocks-join-the-revolution-part-1-19db051acf85

--

--