📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Quantum Computing Coding Tutorial (Step by step)

Lukas's Lab10:36

Transcription

In this video, I'm going to go through an example of programming a quantum computer step by step so that you can follow along. I'm going to use Google Collab for this because it's free and easily accessible. Just look up Google Collab and sign in with your Gmail account and you're ready to go.

In the last video, I quickly went through the coding of the Deutsch Josa algorithm. But after posting this, I realized that some of you may be interested in a more detailed walkthrough style video where I go line by line through the code. So, that's what I'm going to do in this video. If you guys like coding tutorials like this, let me know and I will make more for future videos where I describe quantum algorithms.

All right. So, the first thing we're going to do is pip install the libraries that we're going to use. For those of you who don't know much coding, pip stands for package installer for Python and is what we use to basically download the libraries of code that we want to use that others have written. For this tutorial, we'll mainly use three libraries. KisKit, Kiskuit Air, and Kiskuit IBM runtime. These are libraries written by IBM that allow us to code quantum algorithms and then run them on IBM's hardware.

Next, once we've pip installed the libraries, we just have to tell our code that we need to actually access the libraries. To do this, I'll copy and paste some code that imports all the necessary functions into this notebook. This is just a bunch of import statements where I tell our code what functions we have to access.

Okay, so now we can actually start coding the Deutscha algorithm. The first thing we're going to do is start by defining two random numbers which can take on the value zero or one. This is what random.rand does. We'll use these random numbers to decide whether the function that we're going to run is going to be constant or balanced.

Next, we'll set the number of cubits, which we'll call n, to 23. Next, we set up a conditional to tell us about our function. If the oracle type is zero, then we have a constant function. And otherwise, if oracle type is one, we'll have a balanced function. Note, we haven't actually told our code what the function is that it'll run yet. So this is more just a bookkeeping step on our part. The code doesn't know anything about a function yet.

If the function is balanced, we'll need to define a number called a, which we'll use later when we actually write the balanced function. This is just some random number that fits in the number of bits that we have.

Okay, so now that we have the basics coded up, we can start with the meat of the algorithm. Quantum algorithms in general require both quantum and classical registers. A register is just a set of bits or cubits that we can do operations on. In this case, we define a quantum register called QR and a classical register called CR. We also define a quantum circuit name variable which we call Deutsch Josa and we define a quantum circuit object that uses the cubit and classical registers that we defined before. We're going to use the name that we defined Deutscha to refer to this. So this is going to be the Deutscha circuit with a quantum register and a classical register.

For step one of the algorithm, this is relatively simple. We just apply a not gate on our last cubit which flips its state from 0 to one. The way that we actually do this is by addressing our circuit DJ circuit and using the X gate or the not gate. The Xgate is the bit flip operation. Then we have to tell it which cubit we actually want to do this operation on. So we go into our quantum register QR and we address the nth cubit which is the brackets n in this case.

Next for step two we'll apply a Walsh hatomar transform. The Walsh hatomar transform applies a hatomar transformation to every cubit individually. To do this, we'll write a for loop. A for loop applies some operation in the indented text for a series of iterations. In this case, we loop 23 times up to the number of bits that we have. Our looping variable I counts up starting at zero. And each time the loop runs, it is incremented once until the value of I reaches 23, at which point the loop stops.

Finally, we'll draw a barrier. This part is just for the visualization of the circuit that we'll do later and doesn't actually affect anything in the circuit's operation.

Okay, for step three, we'll actually apply the oracle function. First, we'll use the random numbers that we made before to decide whether the function that we're going to pass to the algorithm is constant or balanced. If the function we pull is constant, then we need to implement an example case of a constant function. Here we can do that by just applying the identity operation. if it's constant at zero or the not operation if the function is constant at one.

On the other hand, if the function is balanced, then we need to implement a function that maps our answer to zero half the time and one half the time. The way that we do this is first to loop through the number of bits that we have. Specifically, we're going to loop through the integer A. And for each bit in the integer a which is again a number that we defined before we loop through and check whether that bit value is set to zero or one. We then implement a controlled notgate between the corresponding cubit and the ancilla cubit which is the nth cubit meaning the one we did the notgate on.

Next we'll draw another barrier. Again this is just for visualization.

[Music]

After that, we're going to do another Walsh hatomar transformation on the first n minus one cubits. So basically all of the cubits except for the last one. Again, we implement this Walsh hatomar transformation by doing a for loop. This is the last step in our algorithm before measurement. And so now we're actually at the final quantum state.

Finally, we're going to measure our cubits and record the result in our classical register. To do this, we'll go through the register of cubits and measure the state of each one. We can then record the value that each cubit took into the classical register.

Now, if we want to see what our quantum algorithm or quantum circuit looks like, we can have KisKit draw it out. When we do this, we can see the gate sequence that we apply, meaning all of the individual logic gates that are applied during our circuit. The barriers break up the different steps. If we shorten the number of cubits to say like seven just so that it's easier to visualize, it's a little more clear what's going on here. We see that we have steps one 2 3 and four arranged and broken up by these barriers. Well, actually steps one and two are kind of in the same barrier together.

Anyways, we can move on and start setting up the back end, which is how we actually connect to IBM's quantum computers to send code to a real machine. There are a couple of options here. First, we can run the algorithm on the quantum simulator. This is useful since there's actually usually a backlog of people running stuff on the publicly accessible quantum computers. So, to make sure that the algorithm you wrote is working as intended, it's good to run a small series of test cases on the simulator first. We can then run on the actual quantum computer later.

We can also pick a quantum back end, i.e. a real quantum computer. In this case, we'll use IBM Brisbane, which is one of their 127 cubit processors that they have made publicly available.

Now, we can transpile the circuit. This means converting the code into a gate sequence for this specific quantum computer and plot the results. I'm going to copy paste in some more code here that plots the result for visualization.

First, I'm going to run this on the quantum simulator. When we run the Deutsch Joa algorithm, we can tell what type of function was implemented above by measuring the output state. If the algorithm outputs a random state containing zeros and ones as most probable, then our function is balanced. But if the algorithm outputs the all zero state as the most probable, then our function is constant. In this case, you can see that our function was balanced because there's some random state containing a bunch of zeros and ones.

Next, we can change the back end to the actual quantum computer and run again. Note here, I disconnected and then reconnected because I had bad Wi-Fi. Uh, so I ended up with a constant function this time when I ran it. Anyways, when we run the algorithm on the real quantum hardware, we expect to predominantly measure the zero state. And indeed, this is what we observe. In addition, most of the states that we measure that are not the zero state with any real frequency have only one single one in them. This means that it's likely that one of the cubits accidentally bit flipped, i.e. there was a small error.

In the balance case, it's probabilistically much more likely that the resulting state will have several ones. So this is actually another good indication that in fact we did have a constant function. Meaning all of the answers that we get with any probability look like they're almost the all zero state.

If we plot everything together, we see that there's pretty significant error in these systems. Like I mentioned before, most of these errors are single bit flips that result in us measuring a state like the all zero state with a one in the middle somewhere. Like here we see in the middle of this graph. As quantum computers improve over time, these error rates will decrease and decrease and hopefully they'll get to the point where quantum computers reach fault tolerance. What this means is that quantum computers will basically behave like the quantum simulator. Although that's a long way off, we can always hope.

If you guys want to play around with this notebook, I'm going to leave a link to it in the description of this video. I know this is a different type of video than what I usually post, but I hope you found it helpful. Please let me know if y'all are interested in more of these types of coding tutorial style videos. Until next time, I've been Lucas. This has been Lucas' Lab, and thanks for watching.

[Music]