Learning Quantum
  • Project Scope and Purpose
  • Getting Started
    • Need to Know
    • Resources
  • Linear Algebra
    • Linear Algebra Summary
      • Math References
    • Basics
    • Vector Relationships
    • Span, Basis and Spaces
    • Transformations
  • Physics
    • Physics Summary
      • Physics References
    • Classical Mechanics
    • Quantum Mechanics
  • Qubits
    • Qubits Summary
      • Qubit References
    • Classical Bits
    • Quantum Bits
    • Multi-Qubit Systems
  • Quantum Circuits
    • Quantum Circuit Summary
      • Quantum Circuit References
    • Classical Models of Computation
    • Quantum Information
    • Single Qubit Gates
    • Multi-Qubit Gates
  • IBMQ
    • IBMQ Summary
    • Getting Access to IBM Quantum
    • IBMQ Tools
    • Using Quantum Gates - The Circuit Composer
  • Qiskit
    • Qiskit Overview
    • Installing Qiskit
    • Parts of a Qiskit Program
    • Writing a Qiskit Program
  • Supplementary
  • Quantum Safe Algorithms
Powered by GitBook
On this page
  • Set-up
  • Imports
  • Connecting your IBMQ account
  • Your Quantum Circuit
  • Circuit set-up
  • Applying Gates
  • Running your Circuit
  • Testing on the simulator
  • Running on a real quantum system
  • Results
  • Getting our results
  • Error

Was this helpful?

  1. Qiskit

Writing a Qiskit Program

An example of a qiskit program from start to finish

PreviousParts of a Qiskit ProgramNextQuantum Safe Algorithms

Last updated 5 years ago

Was this helpful?

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∣00⟩ and about half ∣11⟩|11\rangle∣11⟩ - that is, half of the time both qubits are in state ∣0⟩|0\rangle∣0⟩ and half the time both qubits are in state ∣1⟩|1\rangle∣1⟩.

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∣00⟩ and about half ∣11⟩|11\rangle∣11⟩... where are ∣01⟩|01\rangle∣01⟩ and ∣10⟩|10\rangle∣10⟩ coming from?

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.

They come from inaccuracies in our quantum system. In the section about we talked about 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.

More detailed instructions for finding your API token
classical bits
sources of error and noise
A screenshot show where the account page can be found
A screenshot showing where the copy token button can be found
A visualization of our empty circuit
A visualization of our circuit after applying gates and measuring
The graph showing which states were measured in our simulation
The message indicating our job is 6th in the queue
The message indicating the job is running
The message indicating the job has been successfully run
The graph showing which states were measured in our job