Writing a Qiskit Program
An example of a qiskit program from start to finish
Last updated
An example of a qiskit program from start to finish
Last updated
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).
First we need to go through and import all our libraries and set up our IBM account.
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.
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:
Now you'll be able to run jobs on IBM's system when you're ready.
We'll be working with two qubits, so we'll need two classical and two quantum registers:
Our circuit will include both of those:
And our output:
Let's apply a Hadamard gate to the first qubit and a controlled not gate to both - afterward we'll measure the result.
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.
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 and about half - that is, half of the time both qubits are in state and half the time both qubits are in state .
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.
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.
Let's check what we got from the real quantum system:
How strange - we're expecting about half and about half ... where are and 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.