Introduction to Quantum Computing using Python

Using Python skills to learn Quantum Computing!

abdulah amer
Towards Data Science

--

The world of Quantum Computing is equal parts new and exciting, as it is overwhelming and difficult. However, it does not have to be! You do not need a Ph.D. in Physics or a lab of superconducting qubits to experiment on your own and get your feet wet in such a burgeoning field. In this post, I will be showing you how easy it can be to get started, and how you can be running quantum simulations of your own, and all you need is a laptop, some patience, and a cup of coffee (optional for all you tea drinkers).

Firstly, I will be using Python and the IBM package Qiskit. Once you have Python up and running you can use the command line and use the following command to install Qiskit. If you have trouble installing Qiskit, look here for more information.

For reference, these will be the versions of Python and Qiskit I used. The import statements at the top are for accessing the version info, we will use similar statements to get the other functionality from Qiskit.

At the top of the code above and any code that lends power from packages, we will need import statements. Specifically, we will need the following imports.

Aer is where the backends for all our quantum simulators exist, that is what the M simulator bit is about, and it is good practice to put that at the top as well since like our import statements we will not be changing them while writing the rest of our code.

Now let us discuss quantum computing a bit before we code something out. A quantum computer differs from a classical computer by its bits. Some readers will be familiar with classical bits, they are represented by 0’s and 1’s and a combination of these bits form binary strings, something like 0010110. Classical bits can only take the values of 0 and 1. Bits can store information like numbers, letters, special characters, this post, and all the future ones I will write. In our quantum circuit, we will use classical bits to store measurements, since they are good at doing that.

Quantum bits are different, they can take the value of 0 and 1 at the same time! This is a little weird to think about if you are unfamiliar with quantum mechanics, let us roll with it right now and leave that for another day. When a qubit (quantum bit) is in between two states, we call that a superposition and this can be represented as the probabilities of finding the qubit in each state. As a quick example, we can set up a qubit to be in a perfect superposition of 0 and 1. This means that before measuring the qubit it is in both states, and when we measure it will be 0 and 1 half of the time.

Before you go scratching your head, think of a simpler example. I flip a coin, catch it, and cover it so that no one can see. If you were to guess what the coin is you would say heads or tails, but the coin is not heads or tails yet, not until I uncover my hand and show you. You would say that before you look at it you would be half as likely to be correct if you guessed heads, and just as likely if you guessed tails. This is a good example to keep in mind, coins are much more familiar to us than qubits.

This weird property of qubits is what makes them so special and interesting, this is what they do best, existing in superpositions. This is what we will be coding today, we will take two qubits that are both in the state 0, and put them both into an equal superposition of 0 and 1. This superposition is special, we will be getting one of the 4 most maximally entangled states also known as the bell states! These are incredibly important in many parts of quantum computation, so it is a practical place to start.

Now that we know that we will need both classical bits and qubits, we can declare them with

qreg and creg are the places where our circuit will get the bits it needs. From qreg, the quantum register, our circuit will find qubits. The same for creg. We fill up our circuit with these

and now we have our quantum circuit named entangler. Three lines of code have gotten us far, but we are not done. How do we do stuff with our qubits? I can hear you asking yourself. This is done via quantum gates. A quantum gate is an operation that transforms one or multiple qubits in some way. We will be looking at two such gates in this example, the Hadamard gate, and the CNOT gate.

The Hadamard gate is a coin flip that is covered, it turns a qubit in state 0, into a state of equal superposition of 0 and 1. The CNOT gate is more complicated and needs two qubits to work its magic. Essentially the CNOT gate changes a “target” qubit depending on what it sees when it looks at the “control” qubit. This is what the CNOT gate does.

In this post, we will not be doing the calculation of the states of our qubits by hand, but feel free to do that or to play around with different gates and qubits on your own and see what they do.

We will be applying the Hadamard gate on the first qubit and the CNOT gate on both the qubits, denoting the first qubit as the control and the second as the target. After we apply these gates all that is left to do is to check our answer and see what we get, this is done by measuring each qubit and leaving each result in a classical bit.

A side note to advert potential confusion, Python, like many programming languages, starts counting with the number 0, not 1. This means that the 0 qubit is the first and the 1 qubit is the second. You can see that the gates are methods of the quantum circuit object, and the gates take qubits as parameters. Measurement is also just a method of a quantum circuit and takes the qubit to be measured as the first parameter and the classical bit where the measurement gets left as the second parameter. The classical bits are also numbered as 0 and 1 for the first and second respectively.

I am a fan of visuals so I will show you how to see the quantum circuit diagram of what you just coded, and to plot the results of our measurement. We can draw the quantum circuit with the .draw() method.

Which yields this diagram.

Quantum Circuit by Author

We follow our quantum circuit from left to right and see the “H” that denotes the Hadamard gate and the bit that has the blue dot and plus sign is the CNOT gate, where the plus tells us which qubit is the target. As well as two mini speedometer looking objects that represent measurements of each qubit.

Now that we have seen our quantum circuit we need to see our results. This is done by executing a measurement simulation of our circuit. The execute() function takes the circuit to measure as its first parameter and the simulator that you want to use to do the simulation as the second.

The simulator does many measurements and takes a tally of each 0 and 1 it finds. Then the hist variable grabs these tallies and we finally use plot_histogram() to give us a histogram of our results.

Histogram of Phi+ state by Author

Here we see that when we look at this system we roughly get both qubits measured to be in the 0 states half of the time, and find both qubits in the 1 states half of the time. This is a maximally entangled Bell State. We took two nonentangled qubits and mixed them up so much that they are both in an equal split of always being found in the same state together. Entanglement is a representation of the von Neumann entropy of the system, in our Bell state we would say the entropy is maximized. Entropy is the measure of disorder or randomness in our system since when we measure our system we have just an equally likely outcome for each possible result, it is considered to be maximally random. Entanglement is a resource that can be “used up” by certain quantum protocols. We can discover more of these in future articles!

I hope you now have a better idea of what quantum computing is and what it is not, as well as how to play around with some of these things from the comfort of your own home. Thank you for reading the article and there will surely be more to come soon.

You can find me on Linkedin and Github. Please message me with any inquiries or comments.

--

--

Solving problems day by day, using my study of physics and programming. Anyone can learn anything!