In the last article about quantum computing, we discovered what qubits are with their specific characteristics and how these compare to those of classical bits. Today, we will continue our journey learning about quantum computing by understanding how one can use qubits to actually make computations. We will see how one can create quantum programs, and you will be able to write and run your own programs on actual quantum computers. By the end of the article, you will have written your first quantum program, how cool is that!
Welcome back to this introduction to quantum computing! The goal of this post is for you to:
- Continue your learning of the fundamentals of quantum computing.
- Register on the IBM Q Experience platform which will enable you to use an actual quantum computer.
- Create your first quantum programs using the Python programming language.
If you directly want to jump to the programming part, click here. The only requirement is basic knowledge of Python, and it will be easier if you are already familiar with Jupyter notebooks.
Computing with Qubits
As we learned in the previous article, qubits are the unit of information in quantum systems. They obey the law of quantum mechanics which allows for a variety of interesting behaviors (superposition, entanglement, …).
The whole point of a system is not to only represent the information, but also process it in order to make computations and potentially solve some problems. In the next few sections, you will discover how this is done in a quantum system.
Those of you who are familiar with computer science certainly have worked with circuits. Indeed, in the simplest form, one can represent a computation using a circuit just like the one below.
Here, the starting bit values A, B and C are carried over the wires and go through blocks which are also called gates. Each of these gates outputs a new value which depends on its inputs, and when assembling all of this in a certain way we can achieve a desired result, for example compute the sum of the inputs. Typically in classical computation, we only use a limited amount of such blocks (in the image we used two different blocks only), because even with that we can basically compute anything we can imagine.
To sum up, wires carry the information, gates transform this information, and at the end of the circuit the wire contains the solution we want if the circuit was designed accordingly.
The good news is that in quantum computing, the very same idea of circuit is used! There are a few differences as we will see, but the model of computation is the same.
Just as in classical computation, we perform transformations on qubits using gates. If you remember from previous article, a mathematical way of writing the state of a qubit (which can be a superposition of 0 and 1) is to use a vector. That is, we can write . If you remember from your linear algebra class, transformations are made on vectors using matrices. So in quantum computing, quantum gates are represented by matrices.
This is interesting, because unlike classical gates, quantum gates can for example create superpositions or entangle qubits together. In fact, since the number of matrices is basically infinite, so is the number of potential gates. However just like in classical computing, there are gates which are very often used to perform basic operations.
Gates with Single Qubit
Single qubit gates are gates that have one qubit at their input and also output a single qubit. Note that in classical computing, there are only two such gates. Indeed, either it is the gate that performs no operation (also called identity gate), or it is the gate which flips the value of the input bit (also called NOT gate).
The way we represent quantum gates pictorially is pretty much the same as in a classical circuit, we use a block with the name of the gate. For example in the image below, the gate called H-gate is depicted together with the operation it performs. More explanation about this gate will come in a second.
We present now two famous single qubit gates that will be used later when programming. The first one is the equivalent of the classical NOT gate, and it is usually represented in quantum computing books as a matrix called X.
That gates acts as follows: When given input qubit with value |0⟩, it outputs |1⟩, i.e., . And when given input qubit with value |1⟩, it outputs |0⟩, i.e., . This also means that when we apply this gate to a qubit which is in the superposition , it outputs (it basically switches and ).
Perhaps one of the most famous gate in quantum computing is the Hadamard gate (or H-gate), which is able to create superpositions.
That gates acts as follows: When given input qubit with value |0⟩, it outputs , i.e., . And when given input qubit with value |1⟩, it outputs , i.e., .
So starting with a qubit which is not in superposition, we are able to get a qubit whose state is now an equal superposition of |0⟩ and |1⟩!
Gates with Multiple Qubits
Of course, gates can be applied to more than one qubit, which makes it even more interesting because the output depends on more than just a single qubit state.
A type of such gates which are used very often are called controlled gates. What they do is to apply a certain single-qubit operation to a qubit, depending on the values of other qubits. In a sense, these other qubits “control” whether the single-qubit operation is applied. We present here an example with only one control qubit, and using the NOT gate (or X gate) for the single qubit operation.
This results in the controlled-NOT gate (or C-X gate). It is represented as shown in the images below.
The top qubit in the circuit represent the control qubit, and it acts as follows: If the control qubit value is |0⟩, then the X gate is not applied to the bottom qubit. However if the control qubit value is |1⟩, then the X gate is applied on the bottom qubit. This might seem simple, but now imagine that the top qubit is in a superposition (a mix of 0 and 1). Then it means that the X-gate will at the same time not be applied and be applied! Yes I know, strange things tend to happen in the quantum world…
Mathematically, we write the action of the controlled NOT gate on the four possible states of the two qubit system as:
So here, the value of the second qubit is flipped only if the value of the first one is 1. Note that given the action of the gate on these four states, we can always determine what happens when we are given superposition of these states.
There are a LOT more gates but these are those which are used in the quantum program we will design. If you want to know more about these and about the mathematics behind, you can refer to the books mentioned at the end of the article or read this Wikipedia article.
You may remember that all these quantum effects are great, but we can only understand things in a classical way so we somehow need to convert qubits to classical bits to be able to read the output. Indeed, imagine a circuit with superpositions, which might mean that some gates are not applied and applied at the same time, how could we possibly comprehend the output of such a design?
Thus we use measurements at the end of the circuits, that is we measure the value of some qubits by forcing them to collapse to either a 0 or a 1, giving us back classical bits. In a quantum circuit, a measurement is represented by a block as shown in the image below.
Note that the single wire on the left carry qubits, while the double wire on the right represent a classical bit.
A Recap of Circuits
To sum up, one way of representing a quantum computation is by using the circuit model of computation. In circuits, we get input qubits that have some initial states (we do not care how we arrived to these states) and this information is carried over wires.
Transformations can then be applied by using quantum gates, which can act on one or more qubits at the same time. After this processing which can use quantum effects, we must read the values of at least some qubits to get an output that we can read classically, and measurements are used in that case. After getting classical bits, we can read them and decide if further classical processing is necessary or if we are content with the output.
Programming on Quantum Computers
I have two good news for you:
- If you read both articles then you should know all the basics to understand how quantum computations are done, so you are ready to get your hands dirty!
- For this practical part, you will not need to install anything on your computer, which makes it super convenient.
Alright let’s get started!
In order for you to experiment with programming, I created a Jupyter notebook which contains a mix of reminders about the theory together with how this is implemented. For those of you who do not know what a Jupyter notebook is, I let you checkout the website of the Project Jupyter. As they succintly explain there, “The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text.”
You can download the notebook here.
Platforms for Quantum Computing
There are a lot of companies that created platforms to create quantum programs, including Microsoft, IBM, Google, Rigetti and many more.
In this article, we use the platform provided by IBM. It is simple to use because it uses the Python programming language which is widely used and has a huge community.
Register on the IBMQ Experience Platform
First things first, you will need to register on the IBMQ platform in order to run the Jupyter notebook that you downloaded a second ago. I let you go to this link and create an account.
Once connected, you should see the following screen:
You can then click on the tab which is highlighted in red which will bring you to a page where you can see Jupyter notebooks (in your case you should not have any yet). Here, you will have the possibility to import a Jupyter notebook. This is highlighted in red in the image below.
Click on “Import” and choose the Jupyter notebook you just downloaded. After that, you should be able to open it directly on the IBMQ platform and run it just like any usual Jupyter notebook.
I hope you had fun learning a bit about quantum computing and experimenting with it. You should be equipped with the basics to at least better understand videos about the topic for example.
This is such a huge field with so many real-life application that one could spend hours reading about it. So do not hesitate to search further, there are some great people that write very interesting articles about the subject which go in more depth about specific quantum algorithms, all the maths behind it, and much more.
Please let me know if you would like me to write about something specific in the future, I will be happy to hear about your ideas!
Until then, I wish you all the best 😀
Let me give again the references to the books that will teach you in a lot more depth about quantum computing:
- Quantum Computation and Quantum Information, written by M. A. Nielsen and I. L. Chuang
- Quantum Information Theory, written by M. M. Wilde
- Quantum Computing Since Democritus, written by S. Aaronson
Cover image was taken from Wikipedia