Qiskit is IBM’s open source, quantum computing environment for Python. We can use Qiskit to write programs to run in our local environment and run them on a simulator. We can also run them on IBM’s quantum computing resources via the IBM Q project. This currently includes six 5-qubit machines and one 14-qubit machine.
First we’ll need to install Qiskit, then create an account at the IBM Q site so that we can also run our programs on their quantum computers. You can skip this if you have a quantum computer of your own to use instead. We covered getting started with IBM Q Experience in the previous post. We used the quantum circuit builder to create two quantum registers and apply gates, then measure the result.
I recommend using Jupyter notebooks as well, since this will make it easy to play around and graph results, and to explore a few examples I want to point to. Go to anaconda.com and click on the link to download it. Be sure to get the Python 3 version. On linux you’ll get a shell script that will install the needed components – run it with
or use another shell, then follow the instructions to activate conda’s base environment in that shell session. If you’ve used virtualenv before you’ll probably recognize the
(base) indication at the beginning of your prompt.
Similarly there is an
.exe executable for Windows that will install it, and a
.pkg package for OSX and *BSD. Once anaconda is installed, you’ll need to activate the environment before installing and using Qiskit. From your shell, type:
$ pip install qiskit
to install this package and you’ll be ready to go.
These are just python programs, so use your regular editor or IDE if you like. We’re going to use the Jupyter notebook interface which allows us to easily graph results, but it’s not a hard requirement. So from your shell with anaconda’s base environment activated, type:
$ jupyter notebook
and it’ll create a new session and start a local webserver to interact with.
Qiskit is fairly low level, so instead of having high-level operations like
entangle(0,1) we write code to create circuits and implement logic like the
Hadamard gates. More on this later, as we actually construct a circuit. Hopefully you got this far, got Qiskit installed, and got your IBM account. Next we’ll write a program, debug it on our local simulator and then we plan to run it on a big bad quantum computer at IBM.
Your account allows you to interact with the quantum computers and other resources that IBM has online. You’ll find your account number on the dashboard page, by clicking on “My Account” (top right on the page) once logged in. You need to store this to disk on your local machine, and from then on you can use it to access everything:
Notice we first imported IBMQ from the qiskit library, which we installed earlier. The next step is simply a way to save our account token to disk, which is a one-time task, and from then on, we just load it from disk.
Backends are simulators or quantum computers we use to run jobs on, and we can choose to see a list of all or some of the ones we have access to. Qiskit comes with a capable simulator, but where is the fun in just using that – we’re doing this to feel the thrill of running our code on a real quantum computer, right?
Below you see us listing the providers that are not simulators and are operational – 7 results are returned and we can run our programs on any of them. We can query the jobs backlog, or use a provided function called
least_busy() to find one that has the fewest waiting jobs – in this case, the machine called
ibmq_burlington has only 3 jobs queued up according to the
There’s more to say about the programming environment provided by IBM, but it will have to wait because we need to actually write and run some code before calling it quits for this blog post. We already stalled last time in order to quickly talk about IBM accounts, the visual circuit builder and now it’s time to dive in. Ready?
Let’s start out nice and slow, by coding the same quantum entanglement we created in the visual circuit builder by dragging and dropping gates in the previous article. We’ll need a couple of quantum registers, and a couple classical registers to hold the results. Then we’ll apply our gates and then measure the results. Finally we’ll run it on a simulator, then on the real deal!
There – we have our quantum circuit, ready to do something cool. The methods to create registers and circuits comes from qiskit in case you were wondering. So now lets apply our Hadamard and CNOT gates. They’re just as easy as creating our circuit, which we stored in the variable
c so that we can work with it.
That’s the logic to create a state of quantum entanglement between two particles. Time to mention a thing or two about qiskit. Qiskit provides a quantum simulator we can run locally, and this is handy to do first, before queueing up a job for a real quantum computer. It also provides the mechanism by which we can submit those jobs to the real quantum computer.
This is what it takes to run our program on a simulator. Aer is the set of methods to handle simulations and running against real backends; we’ll see it again. The
execute() method takes a quantum circuit
c, and a backend as arguments. In our case, qasm_simulator is the backend we’ll use. We did it! But to see results, we need some display logic.
We can get a nice diagram of our circuit using the circuit’s
draw() method with matplotlib, as shown on the right.
For the results we want to use a histogram. We ran this program and expect to see the results be almost evenly split between (0,0) and (1,1) with few or no results shown for (0,1) and (1,0). qiskit.visualization has a method called
plot_histogram() that will sum up the results and plot them in a histogram for us:
We’ve successfully entangled these two qubits. In fact, on our simulator every time one is measured to be a specific value the other is measured to be the same. Next we’ll run this on a quantum computer and look at some nice helpers that can make our life easier, so stay tuned!