Writing a Qiskit Program

An example of a qiskit program from start to finish

As mentioned before, this tutorial is being written in a jupyter notebook (version 4.6.1) with Anaconda (version 4.6.14) and qiskit (version 0.11.1) installed, running python (version 3.7.3).

Set-up

Imports

First we need to go through and import all our libraries and set up our IBM account.

from qiskit import **
from qiskit.tools.visualization import plot_histogram
from qiskit.tools.monitor import job_monitor

%matplotlib inline

The line %matplotlib inline is used to make matplotlib graphs show up correctly in the jupyter notebook. You won't need this if you're using a different tool.

Connecting your IBMQ account

Next, we need to connect your IBM account so that you can access their quantum systems. You'll need to log into your IBM account and grab your API token. Each token is different, and you shouldn't share it with anyone (that would pose a security risk to your account). To find your token log into your IBM account, and head to the account page:

Once you're there, you'll see the where you can copy your token:

Then we can save the token on our local system:

IBMQ.save_account('example_API_token-replace_me')
IBMQ.load_account()

Now you'll be able to run jobs on IBM's system when you're ready.

Your Quantum Circuit

Circuit set-up

We'll be working with two qubits, so we'll need two classical and two quantum registers:

qr = QuantumRegister(2)
cr = ClassicalRegister(2)

Our circuit will include both of those:

circuit = QuantumCircuit(qr, cr)
circuit.draw(output='mpl')

And our output:

Applying Gates

Let's apply a Hadamard gate to the first qubit and a controlled not gate to both - afterward we'll measure the result.

circuit.h(qr[0])
circuit.cx(qr[0], qr[1])

circuit.measure(qr, cr)

circuit.draw(output='mpl')

Running your Circuit

Testing on the simulator

Now that we have our little circuit set up, we're ready to test it. The first thing we'll need to do it load up our simulator. Once it's loaded, we'll execute it and display the results.

simulator = Aer.get_backend('qasm_simulator')
result = execute(circuit, backend = simulator).result()

plot_histogram(result.get_counts(circuit))

We can see that the results make sense - the Hadamard gate imposed a uniform superposition on the control bit, and the controlled not gate depends on the state of the control bit. Therefore what we should see is about half 00|00\rangle and about half 11|11\rangle - that is, half of the time both qubits are in state 0|0\rangle and half the time both qubits are in state 1|1\rangle.

Running on a real quantum system

Since our test was successful, we're ready to run our code on an actual system. This means we need to load up the quantum system and prepare our job.

provider = IBMQ.get_provider('ibm-q')
qcomp = provider.get_backend('ibmq_16_melbourne')
job = execute(circuit, backend=qcomp)

job_monitor(job)

When we submit our job, we'll see a few different messages:

This way we can track where we are in the queue and if our job is finished.

Results

Getting our results

Let's check what we got from the real quantum system:

result = job.result()
plot_histogram(result.get_counts(circuit))

Error

How strange - we're expecting about half 00|00\rangle and about half 11|11\rangle... where are 01|01\rangle and 10|10\rangle coming from?

They come from inaccuracies in our quantum system. In the section about classical bits we talked about sources of error and noise in typical systems. Because quantum systems are so much more complex, we have more potential sources of noise, and therefore end up with more potential for error. Sometimes, our qubits end up where they shouldn't, and sometimes our measurements are wrong.

As quantum systems get better, we'll get more accurate result. They'll start to be able to check their own work - the noise in the system will be reduced. For now, we'll always end up with a little bit of what we didn't expect, and we'll need to be careful to recognize that when we analyze our results.

Last updated