Quantum mechanics, Quantum computing, Qubit Programming – Quantum Gates I
Rather, we coded on our system and then run the code on a quantum computer and made an entangled state and we did all sorts of cool things like measuring it and seeing that these are actually entangled or not so um, so yeah. So that’s that. But now we will get into some nitty gritty of quantum computing. So in the previous videos we had seen properties of quantum mechanics, but now we will shift gears a bit and focus on the tools or the methods we need for quantum computation, so um for regular computer systems and not or gates. These forms form the building blocks of everything. Similarly um gates in this quantum computing realm are called quantum gates and we will deal with them. We will start with what do they represent and how does it work and some examples of them so so again, like the next three videos will be a bit more theory leaning. I promise that i will keep it to a minimum, but we still need to get some understanding of these things uh to understand complicated algorithms, so so let’s jump right in so in this video i will talk about um quantum gates that act on one qubit and Then um in the next video i will talk about physical, meaning of quantum bits like um, because quantum bits have these complex numbers. So how can i give a physical understanding of it and that’s through a 3d sphere? I will again talk about it later and then third video, i will uh basically go over two qubit gates and i think with that we should be ready to jump into the first proper quantum algorithm, all right, so so lots of learnings to do till we get There i have my beer, so let’s begin great.
So, first of all, let’s look at this um, any quantum state you would mathematically, represent it as a matrix, so the state 0 can be represented as this 2 by 1 matrix and the first bit is one and the next one is zero and again the state. One could be represented as zero one so mathematically. This forms the basis of all two cross, one matrices so that’s. Why um that’s the mathematical term for it, but then um don’t worry about it. Just this is the representation of zero state, and this is the representation of one state. Now, a regular qubit is in a superposition of zero and one which is alpha times 0 plus beta times 1. So you would represent it like this, and this is pretty straightforward. You multiply alpha to this one you multiply beta to this one and then because there’s an add, you add them up, and this is what you get so this represents the most general um quantum state in this matrix form, which is just a representation all right. So now let’s look at some more examples, so um this state, 0 plus 1, where both 0 and 1 have the factor of square root. 2 1 over square root 2, would be a matrix of 1 over square root of 2 1 over square root. 2.. This this state, 0 minus 1 over square root 2 – would be. This particular would be represented by this particular matrix, where the first element is 1 over square root 2, and the second over second element is negative: 1 over square root.
2.. Now these two, why did i single out this example is because these are pretty important and they will come later down the line quite a lot so that’s why they have been given a special designation, the plus and the minus state, so that’s? Why now uh? Just jumping ahead a bit so 0 and one form the bases like so everything can be built on zero and one. A quantum state has these two building blocks, and on top of that it could be any combination of these two, so they represent the basis. So, for example, what does the word basis mean? Um, a 2d plane is for a 2d plane. The basis would be the x and y directions, because, given those two coordinates, you can identify any point right. So so the minimal amount of dimensions you need to explain any space is the basis or to pinpoint a point in any space is the basis, so plus and minus are again two orthogonal states, and these are a different representation of bases. So these are as good as zero and one, so this is a different way of writing the basis states. If this explanation went over your head, um don’t worry too much about it. Just think of this, as some peculiar state, all right, that’s, all that matters now quantum gates, just like classical gates which are and not or and all that jazz are basically matrix operations on states, so they’re, basically matrices that multiply on a state physically.
They correspond to certain operations. We don’t want to get into that, but then, like uh mathematically, all they mean is matrix multiplication on states, so some fundamental gates let’s talk about some fundamental gates which are known as polygates, and these come from again. This is an aside. These come from the sco2 poly matrices, which basically form the group of rotation of a sphere so that’s where the sphere terminology comes in, and i will talk about that in the next video. So just don’t worry about all of that. But then these are, you can say the building blocks or the building block gates. There is the x gate. 0. 1. 1. 0. There is this y gate, which has some imaginary numbers. So this is minus. I, and this is i, and there is z gate now. Let’S, look at the action of say one of them x gate. So what, when x, acts on zero it flips it zero becomes one when x, acts on one one becomes zero. So this is the flip gate, so x flips the bit let’s look at this operation in detail so, like i said, the action of the gates on my qubit is just matrix multiplication. So the x gate is 0. 1. 1. 0. I put it. I write it like this, and the zero state is one zero. When i perform the multiplication over here, i get zero one which is state one, and the same would happen for um this operation as well.
So the job of x gate is just to flip the bit. So this is one important gate: there’s, a more general gate. There are more representations of more general gates, called the: u gate, the only the only criteria gates need to satisfy. Are that? U times? U say if you have a gate? U, u times u, transpo complex, conjugated or uh should be uh transposed and conjugated should be equal to identity um. So there are more general gates available as well, but then again, i’m skipping over that. So another important one that you will see quite a lot is this hadamard gate um. I hope i got the spinning right. So the jaw – and this gate looks like this and what it does is it takes the zero state to make it the plus state, and it takes the one state to make it the negative state so plus and negative states are these. So that is why i said 0 is equivalent to plus, and one is equal to minus, because um action of hadamard gates um takes from zero one basis to this plus and minus basis, and you will see hadamard gate quite a lot all right. So i think um. This is all the theory that i wanted to talk about in this video. So let me now directly jump into code. So how do you represent these gates, so uh let’s see? Let me bring up my notebook all right, so i have already written out um the code, so i will just walk you through it and run it again.
This code is available on my github repo and the links for that are in the video description. So please check it out and also please like and subscribe, because i want my channel to grow, so i can make more cool things so um if you liked it like and subscribe all right, so let’s let’s jump in so first we um we import everything. We need well, we don’t need the visualization, so let’s get rid of that. We import kiskit and i do not want to run my code on the quantum computer because we are just trying out very simple things. So let’s just um get a simulator running because i just want to execute it on my system right, so let’s first implement the x gate and the way i implement the x gate is let’s. I first create in this line. I first create a quantum circuit of one qubit and i initialize it let’s say with state one, because alpha is zero and beta is one. So this is state one now um this command just initializes it to this value. And if you don’t remember all these commands are executed sequentially. So whenever i so, if i put x above initialization, then after the x gate, the initialize initialization step would act. So all these commands need to be done in the precise, sequential manner in which you want the circuit to be executed or circuit to be built. So so first we want the initialization that’s why i type it out first next, i want the action of x gate.
So you can add an x gate by just simply typing out: dot x, your circuit dot x, where circuit is a quantum circuit object and then, as an argument, you need to specify which qubit we only have one. So i specified its number, which begins with zero, and then i just specify that whatever we have just measure everything and then draw out the circuit, so here you can see, i have initialized it to uh state one. I have an x gate and then i will measure it out now. What should happen x. Gate is supposed to flip my bit so that my measurement should be zero, so let’s try it out um. Here i have the same code that you might have seen it before, which basically transpiles the code for the simulator and then runs the simulation and gets the results out. So, instead of plotting it out, i i’m just printing out the numbers that i see and this simulator by default runs one zero two four times so let’s print it out all right, so we get zero all the times because x, gate flips the bit. So what was one would now become zero and that’s. What we expected let’s, try it again um this time. Let me supply it gate zero. So alpha is one beta zero. I specify it and i run my simulator and you see we get one because zero got flipped to one so that’s awesome um.
Similarly, we can apply the z gate, the action of z, gate, doesn’t change, the state so uh again, i have the same circuit as before. Instead of x, i’ve replaced it with z, so you can also try it out. So here i have x state over here and when i measure it, i get x state again the zero state back again so z doesn’t really change the state, but it does something subtly different, which i will talk about in the next video. The thing is, like these states live in a complex space, so it you cannot. I cannot draw a 2d graph and talk about it because they live in a three dimensional space kind of fashion, so there’s an extra hidden dimensions. A qubit has because of this of its complexity so um i will again get into it in a in a later video, or rather the next video. Finally, let’s talk about the hadamard gate. Again, the the code is as before. Instead of x, i have the hadamard gate, which is just dot h, and you can see my my qubit and then the gate and then i’m performing the measurement. So what does the mod gate do it takes? 0 makes it into the plus state, and if you don’t remember, the plus state is a mixture of both 1 and 0 with the same probability. So if i run this state, it would become 0 and 1 with the same probability.
So when i run it and you can see both of 1 and 0 come with roughly the same number of times, the measurement is the same. The same number of times, roughly same, should happen if i run it for with the state 1 and boom. There is a slight difference: it’s because it’s, a simulator it’s and again, even if you run into a quantum computer, there would be difference because uh it’s, like a coin, toss right like if you toss the coin 10 times it doesn’t mean just because the probability is Half doesn’t mean you’re gon na get five heads and five tails it’s, sometimes gon na go one way or the other a bit so but more or less these numbers are equivalent, and that is what i wanted to show. So in this small notebook, which again is uploaded on my github page, you can see how to play with one qubit gates. There are more of them, you can again uh, look them up in cascade documentation, but then i think for now we only need these and i will talk about more complex gates as and when we need them. We don’t need them right now, so i think that’s. It for this video – and i will see you the next time with uh the next video i’ll just talk about won’t particularly be about coding, but it will be more about this representation of qubits and how to physically make sense of this action of gates all right.
So i hope you enjoyed this short one and uh. If you like this video, please comment below.