Quantum Programming Tutorial #2: Basic Qubit Operations
Hey guys, this is Macad 101, and today I'm going to be talking about how we manipulate a single cubit in QCL.
Now, a qubit is the quantum version of a regular bit in a real computer. In your classical computer, it stores data with a single bit at a time. A bit on your computer is either a one or zero, and it definitely has a value of one or value of zero.
Now, when we measure a cubit off the output of a quantum program, we'll either measure a value of Z or one. So in that sense, it's actually identical to how a regular bit works. However, while the quantum program is running, the qubit has extra properties. It has a probability of being measured as a zero and a probability of being measured as a one, and it also has something called a phase, which we're going to be talking about probably in the next video. But we won't get that far into it in this video.
What I'm going to be showing you is how we can use QCL to manipulate a single cubit and to basically explore the differences between a regular cubit and a bit on a computer. So let's go ahead and fire up QCL.
Our first step is to declare a cubit that we can actually manipulate and change around. This might seem weird to you who are familiar with other programming languages because in a language like C or Java, you might declare an integer. An integer is actually 32 bits. Well, here we're declaring just one cubit. The reason we're not declaring a big data type like an integer is that in order for a regular computer to represent 32 cubits, it would take 4 GB of RAM to do so.
What we actually need to do is declare a single cubit at a time so that way we save memory. A great deal, whereas in a real program on a regular computer, or as I anticipate it'll be like on a quantum computer, you won't be that worried about single bits taking up storage. But in this case, we will worry about it because we're not on a quantum computer.
So we're going to declare something called a quantum register, and we'll give it a size of one. Now, a quantum register is designed to hold multiple cubits if we want. It's like an array of cubits, but in this case, we're going to be declaring just one cubit in the quantum register.
The data type for quantum register is qg. So to declare a quantum register, we just type qg
space the variable name, which is the name we'll be referenced. We're basically giving our cubit a name. I'll call it X. I'll put the number of cubits we want to allocate in brackets; in that case, it's one.
We'll hit enter, and if I type dump right now, it says 1 out of 64 cubits allocated, which means we now have one cubit to work with. Underneath this little thing that's printed out, we see that it says one, and then this zero here. Now, what is that actually telling us?
Well, this thing where it's a vertical line, then a zero, and then a close you know – I guess that's a greater than symbol, but I just think of it as like a close kind of bracket. This is our actual state. So this is, for instance, the zero state. There's a difference between a state and its coefficient.
We could call this no or down or off or something like that. We just choose to call it zero because we're thinking about it like a regular bit. But it's actually not a number at all. Don't think of it like a number; it's just a name that we're assigning to our off state.
Now this number here that's not in any kind of fancy decoration, this right here is our coefficient, this one. So the coefficient of the zero state is one in this case, but not in every case. I'll show you why in a moment. In this case, we'll think of one as the probability of measuring the zero state.
Now, we're saying one instead of 100% because when you're working with actual numbers, it makes more sense to go from 0 to 1 than to go from 0 to 100. Just because if you multiply two probabilities, you'll actually get the probability of the other outcome. It's just so much more nice to go from 0 to one than to go from, you know, 0 to 100.
So don't forget about percentages. In this video, we're just going to be dealing with probabilities from 0 to one, where one is 100% and 0 is 0%. So in this case, our state starts out as a 100% chance, one chance that we'll measure a zero.
Now, what if I want to change it to being a complete chance that we'll measure a one instead of a zero? All we have to do is use the NOT function. What the NOT function actually will do is it'll swap the coefficients between the zero state and the one state.
So right now, because there's no chance, we'll measure a one. The one state actually has a coefficient of zero, and that's why QCL isn't showing it in this list because it doesn't show things that aren't going to happen. But it's still there; it just has a probability of zero.
So we're going to swap the coefficient between the one state with this coefficient here. So now zero will have no probability, and one will have a complete probability of happening.
So I'm going to type NOT
with the capital N, and then this is a function that we're calling. We pass it X in parentheses like this, and now I'll hit enter. You'll see it prints out the state here, and we can actually just type dump again if that makes more comfortable.
But now you see the one coefficient has moved to the one state, and it's not showing us the zero state now because it has a coefficient of zero. And I'm going to NOT X one more time to get it back to having a full probability of being a zero.
Now, the next function I'm going to show you actually will be the first function we're going to see that will mix the state of a cubit. What it's going to do is it's going to set the probability of zero to be a half and the probability of one to be a half. It's not going to show us what you might expect, and I'll show you why right now.
So to call this function, it's called the Hadamard function. We just use a capital H as the function name, probably because no one wants to type out Hadamard every time; I know I don't. But I'm calling the Hadamard function on X, and to do this, I have X set to zero.
As we can see, the Hadamard function when applied to zero, as we've just done, returns 0.707 and then it goes on * 0 + 0.707 * 1. You can already see these two are equal, so the odds are probably equal that we're going to measure a zero or one.
But why isn't this 0.5 or a half or whatever? Why is it this weird number? What it actually turns out is, you have to think of a cubit kind of as a line on a unit circle. If you're not familiar with trigonometry at all, I won't bother you with this, but essentially it means that when we add the square of each state together, we're going to get a length of one.
But not when we add each coefficient together. The reason why this is just like a cubit you might want to think of it for now as a particle on a unit circle that's moving around. For instance, the x-axis is the zero state, and the y-axis is the one state.
You know, the x value and the y value might not add up to one, but when you use the Pythagorean theorem, you add up the squares of the two, you'll always get the radius. We just define the radius of our cubit to be one.
But if you don't want to think about it geometrically, just know that when you square the coefficient of a given state, that'll be the probability. So just to verify, when I square this, it is actually 0.5. So what this tells us is this is ( \frac{1}{\sqrt{2}} ) because when you square it, you get 1/2.
So that's just how the math works behind it, and if I show you, ( \frac{1}{\sqrt{2}} ) is in fact the number there. Although the number here is rounded, the number here isn't. So that's actually ( \frac{1}{\sqrt{2}} ). But now I'll go ahead and I'll take the Hadamard of this junk, and we'll just see what we get.
I haven't already showed you how to evaluate a quantum function, for instance, on a mixed state, but I'll show you how we do that in a second. So if I take the Hadamard of X, you probably have no idea what to expect.
But if I hit enter, you see what just happened; it's back at a zero state. So here I took the Hadamard of zero and it gave me this junk, and I took the Hadamard of the junk and it gave me back zero.
So what it turns out is Hadamard is its own inverse. But first, I'm going to show you how we figure that out, and then I'll show you why. So let's say I take the NOT function of X. Now X will have a value of one instead of zero.
And right here, we took the Hadamard of zero. What happens when we take the Hadamard of one? Well, let's see. It has the same probabilities because if we square this, we get 0.5, and if we square this, even though it's negative, we still get 0.5.
But the difference is the one coefficient is actually negative. So what's going on there? Well, it turns out the Hadamard function when you apply it to a one, it actually will be different than the Hadamard function of zero in that the coefficient for one is actually negative and not positive.
So we can actually use this information to figure out what the Hadamard is of any mixed state at all. To do this, for instance, let's say I wanted to take the Hadamard of this junk up here and figure out that it would always give me a zero.
I'm first going to reset X back to be one. I'm going to write up a little proof right now to show you how we take the Hadamard of any mixed state. So I'm going to open up text edit and I'm going to quickly create a program or project, not even none of those things. I'm going to quickly create a text document.
So here's our text document, and I'm going to write down what we already know. So ( H(1) ) * our zero state will yield us what was it? It was this, and ( H(1) ) * our one state will yield us this right here, which is the same thing except the coefficient is negative.
Now this number right here, 0.707 blah blah blah, is actually just the square root of 1/2. So I'm going to write, let's call it ( s = \sqrt{1/2} ).
Now that we know, now that we have this variable, I'm just going to replace all the 0.707s with s to make our lives easier and cleaner, and I'm also just going to note that ( s^2 = 1/2 ).
So here we go; we have our Hadamard of zero and our Hadamard of one. Let's try to figure out what this value is when we take the Hadamard of it. So the Hadamard of any mixed state and I'll just write down s, s here, the Hadamard of any mixed state will actually be the Hadamard of this portion plus the Hadamard of this portion.
It works that way with any quantum operator. So I'm going to go ahead and type this out right here. Here, it’s ( s * H(0) + s * H(1) ).
So already, we know that we're just going to take the Hadamard of zero and take the Hadamard of one and multiply them by s, which is our ( 1/\sqrt{2} ) and add them together.
Now our next step will be, I'm going to write this out pretty quickly, ( s * ) and then I'm going to paste in the Hadamard of zero here, which we know is this, plus ( s * ) the Hadamard of one, which is that, except this is negative.
And our next step will be distributing the s's here. So remember ( s * s = 1/2 ) and ( s * s = 1/2 ). So it's going to be ( 1/2 * 0 + 1/2 * 1 + ) and now I'm going to do this side right here ( 1/2 * 0 - 1/2 * 1 ).
Yeah, that minus is important; I don't want to forget that. So if we add the like terms here, you can just treat zero like a variable, so these are like terms, and this is a like term with this.
So I'm going to put these in bold because they actually cancel out, and you'll see why it’s positive, and then negative ( 1/2 ) there. So the one coefficient becomes zero, and meanwhile, the ( 1/2 )s, and I'm going to take this out of B, the ( 1/2 )s of zero are both positive, so that becomes one.
So it's going to be ( 1 * 0 + 0 * 1 ), and that's why the Hadamard of this stuff actually comes out to be zero.
Now, I could show you the same thing for one, but I think you know how to evaluate it for one. You know, you would just change this to a minus, change the minus there, add a minus there, blah blah blah, and you'll see it comes out to be completely one and completely not zero.
So the idea is that when you take the Hadamard and then you take it again, it'll be its own inverse.
Now, what happens if we take the Hadamard of a state and then we apply some other thing to it, and then we take the Hadamard again? Well, this will be the basis of our next video when it comes to rotation.
But I'm going to really quickly show you what happens if I take the ( H(1) ) right here. X is already one in this program, and I'm going to NOT this state. As we remember, NOTing will just swap the coefficients, so the minus will move to zero from one.
So as you can see, it moved from one to zero, and now we can figure out what the Hadamard of this is. Let's just do it and hope we don't make a math mistake before we take the Hadamard in this actual thing just to see if we can predict what we'll get.
So the Hadamard of what's this state right here? Well, it's this and we'll change this to s because that's easier to deal with. Well, that'll be minus ( s * H(0) ), and I'm just going to write this right down here. It's going to be minus ( s * 0 + s * 1 ).
Now we can go ahead and distribute this. So it's going to be ( -0 -1 ), so ( -2 * 0 - 2 * 1 ), and then plus ( 1/2 * 0 - 1/2 * 1 ), correct?
Now what you'll see is these minuses are constructive because they're both negative, so but then the zeros cancel out because it's minus ( 1/2 ) here and plus ( 1/2 ) here.
So it's going to be ( 0 * ) our zero state minus ( 1 * ) our one state. So let's go ahead and actually run this now, and you can see it's minus ( 1 * ) our one state.
So we actually did calculate correctly what the Hadamard would be if we took the Hadamard of this value. And of course, we don't need to use s here; it could be any random mixed state.
And we could take the Hadamard of it using this same kind of formulation. So that I think that’s really cool, and it's neat that it works.
All you actually need to know for a single cubit operation, all you need to know to calculate it for any value is you need to know what happens if you do that operation on a zero and if you do that operation on a one.
If you know that, you can calculate any value of that function, and that's called the basis of the function. It's what it does to zero, and it's what it does to one.
We're going to be thinking about bases a lot when it gets further down the line, and that's a linear algebra concept that you'll get familiar with even if you don't know linear algebra throughout these tutorials.
So this was quite a bit of math, I think, for a programming video, but I think it was necessary, and I think it's cool to see.
One last thing I'll note is what would be the probability of measuring a one in this scenario? Well, it's the square of this coefficient, so it's a one. So we're completely likely to measure a one in this scenario.
So when our quantum program is finished, there's no difference between measuring minus one of the one state and a positive one of the one state because, well, you know, the odds are the same; it's just the phase that's different.
So just keep that in mind and think about that maybe a little bit because in our next video, we're going to be talking completely about phase, and we're going to be doing changes that won't be observable. But then suddenly, when we do some other change, the non-observable change will become observable.
So anyway, thanks for watching! Hope you really enjoyed this video. Subscribe, and goodbye!