Getting Started with Qiskit (explained by a software developer, not a quantum physicist)

Abby Mitchell
Queen of Qiskit
Published in
10 min readOct 19, 2020

--

So you’re a software developer who wants to get your feet wet in the field of quantum computing, but haven’t done any physics since high school? Want to try out IBM’s quantum codebase (Qiskit) but not sure where to start? Maybe you’ve found a few blogs or tutorials but you’re getting bogged down by all the jargon?

Well lucky for you I went through all that pain already and decided to write this blog so you don’t have to!

I’ll be walking you through the following tutorial: Getting Started with Qiskit

Disclaimer! I am not a quantum physicist, my aim with this blog is to help more software developers get started with quantum computing by providing a very high level overview of the concepts in the tutorial. I will only cover the very basic need-to-know information to help you understand the code you write. If you want to progress with quantum computing beyond what’s covered here you will probably at some point need to learn some quantum physics theory in more depth (sorry kiddos).

Before you Start (THIS BIT IS IMPORTANT)

I’m assuming you have some basic knowledge of Quantum Computing, and how it compares to classical computing. If you don’t know what qubits, entanglement and superposition are I suggest you go brush up on the basics here (don’t worry I’ll wait):

I’m also assuming you have some knowledge of python, although you could probably get by with just some general programming knowledge as the actual code used here is not very complex.

In order to get going with the tutorial you need to have Qiskit installed on your machine, if you haven’t done so already you can learn how to do so here:

Once you’ve got Qiskit installed open up a blank jupyter notebook and lets dive in!

Tutorial Part 0 — Getting Started with Qiskit

Ok let’s start with paragraph 1.

If you’ve never seen a visualisation of a quantum circuit before they look something like this:

You may see different style variations depending on what libraries you’re using, the one above is the stylised version you get using qiskit, although you will also see a cruder version like this output from your code:

Put simply, running algorithms on quantum computers essentially involves the following steps:

  1. Passing a specified number of qubits into the system (i.e. the circuit)
  2. Applying a specified combination of gates (which make up an algorithm) to those qubits
  3. Measuring the qubits and storing the results in classical bits (i.e. 0s and 1s)

Lets take a look at that circuit visualisation again:

Before we can measure any results we have to actually set up the circuit (this is referred to as the Build phase). Then we run the experiment (known as the Execute phase) either on a simulation of a quantum computer, or on a real one. We use qiskit code to implement these phases.

There are many different gates that can be applied to qubits, for the purposes of this tutorial you only need to know about 2:

Tutorial Part 1 — Building the circuit

Ok don’t panic. There’s a lot of jargon here but we’ll break it down.

What is a three-qubit GHZ state?

This is a quantum physics term to describe a set of 3 entangled qubits. When the qubits are in this GHZ state we can’t represent them as discrete state of 0s and 1s, so here it’s been represented mathematically like this:

I won’t go into too much depth about the maths (check out the bonus resources section at the end if you want to know more), but as the tutorial shows, we can represent one qubit in its superposition state like this:

So when you entangle that qubit with 2 others using Controlled-Not (CNOT) gates, you get the GHZ state (notice 3 0s and 3 1s in the first equation instead of just 1).

What’s with the weird | symbols?

This is Bra-Ket notation. This notation is very common in quantum computing literature and here is used to represent qubits (as opposed to classical bits or just ordinary numbers). In this context |0〉 represents a qubit in the 0 state, and |1〉 represents a qubit in the 1 state. As you can see in the tutorial, you can use this notation to represent multiple qubits as well. Specifically, this notation is used to represent the qubit states as vectors. I won’t go into much more depth here, but quantum state vectors are definitely a key concept to understand if you want to do more with quantum computing going forwards, if you want to learn more check out the links in the bonus resources section at the end.

What is a Bell State?

Just as a GHZ state represents 3 entangled qubits, a Bell state represents just 2 entangled qubits. AS we build our circuit in this tutorial we first create a Bell state to entangle the first 2 qubits, then we create the GHZ state by entangling the 3rd.

Here is a visual representation of each state:

Tutorial Part 2 — Visualise the Circuit

Ta-da! You’ve just built your circuit and visualised it!

side note: if you run circ.draw() instead of circ.draw('mpl') you will get a less stylised visualisation of your circuit (similar to the one mentioned at the start of this article)

The information in the blue box starts diving into the quantum computing theory. You don’t strictly need to know what this means to complete the tutorial but if you are interested in continuing your quantum journey after this tutorial I recommend learning about tensor products and matrix representations of qubits as a start (check out the links in the bonus resources section at the end).

Tutorial Part 3 — Statevector Backend

This is the part where you get to actually run your circuit and get some results!

While this tutorial doesn’t show you how to run your circuit on a real quantum computer (best to walk before you can run), it does take you through a few different simulators. Simulators are great because they quicker and they produce ‘perfect’ results*. Qiskit offers a bunch of different simulator ‘backends’ to play with, each of which return their results in a different format (e.g. statevector, unitary and QASM)

*Real quantum computers are considered ‘noisy’, meaning that they don’t always produce the right results, so you have to run experiments multiple times to determine the correct outcome. Noise reduction in quantum computing is a hugely important area of research (covering both hardware and software), check out the links in the bonus resources section at the end.

What exactly is a statevector?

Let’s say we have 3 classical bits. At any one time during computation, each bit can be either a 0 or a 1, so there are 8 possible combinations:

However, as we stated earlier, it is impossible to represent qubits in superposition as a combination of exactly 0s and 1s. Instead, we can represent them using statevectors. A state vector is a complex vector representing a given quantum state. By complex we mean it has a ‘real’ part and an ‘imaginary’ part (maybe you remember this bit from high school?). So a statevector representation of our 3 qubit GHZ state looks something like this:

The qiskit statevector simulator will calculate the values of the statevector for us:

Ok, this output state is a bit confusing, but you can kind of tell that it’s just a badly formatted version of the matrix we went through above.

If matrices aren’t really your style qiskit has a visualisation library with a bunch of different options to choose from (Tutorial 2 gives you an overview of some of the most common ones).

Ok, this may not be the most intuitive of visualisations, but essentially the graph on the left represents the ‘real’ part of the statevector, and the graph on the right represents the ‘imaginary’ part. The height of the blue bars represents the probability that the qubits will collapse into that combinations of 0s and 1s when we measure them. In this case theres a roughly 50:50 chance that our qubits will collapse into 000 or 111, and they will never collapse into the other combinations (due to the way we set up the gates of our circuit).

This highlights the awesomeness of quantum computers! By choosing a certain combination of gates, we can manipulate the qubits to give us just 2 possible outcomes instead of 8. This is, at a basic level, what it means to develop quantum algorithms.

Tutorial Part 4 — Unitary Backend

What is unitary?

Here when they say unitary they are referring to the fact that qubit gates can be represented as unitary matrices. A matrix is unitary if ‘its conjugate transpose is also its inverse’. (If that phrase makes no sense to you go watch this video).

In Quantum Physics this concept is often represented like this:

Where U is the original matrix, U† represents the conjugate transpose and I is the identity matrix (you’ll see this notation A LOT in physics textbooks).

Hence, qiskit’s unitary simulator will output a representation of our circuit in the form of a 8x8 unitary matrix. Here again the output isn’t formatted very nicely in the jupyter notebook, but essentially it looks like this:

Tutorial Part 5 — OpenQASM Backend

You’re almost there! Well done for making it this far without your brain melting 👏👏👏

In this last section you get to simulate measuring the output of your circuit by executing it multiple times (the more times you execute the more accurate your results).

What is the computational |0, |1 basis?

This term refers to the way the qubits will be measured. For the purposes of this tutorial all you need to know is that this means when our qubits collapse into classical bits they will have values of either 0 or 1. There are a range of different bases that could be used, but the |0〉, |1〉 basis (sometimes referred to as the Z basis) is the most common.

What does the new equation mean?

This equation is called the Born Rule and its used to algebraically calculate the probability of getting a certain combination of 0s and 1s when we execute the circuit. For our GHZ state this formula tells us that the probability of getting the output 000 is 1/2, 111 is 1/2, and all other combinations are 0. We can then validate these probabilities by executing the circuit many times and seeing how many times we get 000 and 111. You don’t need to understand the mathematical proof for this formula to understand the code in this tutorial, but if you want to learn more about the Born Rule and other quantum mechanics principles check out the links in the bonus resources.

The above section is relatively self-explanatory, just one thing to note is the new lines representing the 3 classical bits that will be used to store the information from the measurements (1 classical bit per measured qubit). In some circuit diagrams these classical bit lines are condensed into one line to save space.

The next steps involve executing the circuit many many times (1024 times to be precise) and aggregating the results. In a hypothetically ‘perfect’ quantum computer (i.e. a simulator) we will only get the outputs 000 and 111, but if we were to run our circuit on a real quantum computer we would also get a few anomalous results, due to the ‘noisiness’ of the real machine.

By using get_counts we can see that out of the 1024 times our circuit was executed on the simulator, 510 times the measured outcome was ‘000’ and 514 times it was ‘111’. And there were no other outcomes measured.

And finally, a nice simple histogram to visualise the results:

🎉🎉🎉 CONGRATULATIONS YOU MADE IT! 🎉🎉🎉

Here are those bonus resources I promised:

--

--

Abby Mitchell
Queen of Qiskit

I’m a Quantum Developer Advocate at IBM. I’m particularly passionate about Quantum Computing and encouraging women to pursue careers in technology.