This video belongs to the openHPI course Introduction to Quantum Computing with Qiskit (with IBM Quantum). Do you want to see more?
An error occurred while loading the video player, or it takes a long time to initialize. You can try clearing your browser cache. Please try again later and contact the helpdesk if the problem persists.
Scroll to current position
- 00:01Welcome back everyone!
- 00:02I'm very happy that now that you learned the basics, we can actually start
- 00:06implementing our very first quantum algorithm.
- 00:09So, before I go into the implementation
- 00:12with Qiskit, I would first like to show you how how the algorithm works and what
- 00:17the goal of it is and how we can get the quantum advantage.
- 00:21So, the Deutsche-Jozsa algorithm was
- 00:23proposed in 1992 and the way it works is that suppose you are given a function f,
- 00:30which takes as an input, an n bit string, and then it outputs either a constant or
- 00:36a balanced function, so we can see it as a black box.
- 00:39We do not know how that function operates, but we are promised that the output is
- 00:44either constant, so it's given by a single bit.
- 00:46So it could be either constantly zero or it could be constantly one,
- 00:50or it could be balanced, which means that half of the inputs lead
- 00:55to a zero and half of the inputs lead to a one.
- 00:58Now, the goal is that we want to determine if f is constant or balanced.
- 01:04So, if we look at a classical algorithm,
- 01:06well, let's say we call the function once and we get a zero,
- 01:10then we have no idea whether the function now is constant or balanced.
- 01:14We actually need to call it again.
- 01:16If we get a one, then we know, OK, we have two different outputs,
- 01:19so it cannot be constant, but it has to be balanced.
- 01:22So we need at least two periods.
- 01:24However, in the case where we get again
- 01:26a zero, we still do not know whether it's constant or balanced.
- 01:29We need to ask again.
- 01:31And if we get a zero again, then again, and so on, at most we need to ask it.
- 01:36Well, we need to give it half of the input
- 01:38bits plus one, half of the possible bit strings that we can feed it plus one.
- 01:44And well, if we look at n bit strings,
- 01:46then in total we could have two to the power n different ones.
- 01:50So two to the power n minus one is then half of it plus one.
- 01:55Now, the quantum algorithm needs only
- 01:57exactly one query, and so this algorithm was one of the very
- 02:02first examples to show the speed up that quantum algorithms can give.
- 02:06You can see here also the circuit
- 02:08representation, as you remember, hopefully we start from the left to the right.
- 02:12So you can see we have here some qubits,
- 02:15we can have n qubits, and they're all in the state zero initially.
- 02:21Then, as I told you,
- 02:22every quantum algorithm starts with a bunch of Hadamard gates.
- 02:25So, on each of these qubits we apply a Hadamard gate given by the orange H.
- 02:31Then we have our oracle, the function UF here.
- 02:35That's a big box.
- 02:37You can think of it as this black box
- 02:39that I'm giving to you, that implements the function that I
- 02:41promise to you is either constant or balance, but that you do not really know
- 02:44what it is that you want to figure out what it does.
- 02:48Then we apply again a bunch of Hadamard
- 02:50gates and in the end we apply measurements.
- 02:53So the green boxes correspond to measurements, so each of the qubits we
- 02:56measure and then even if we are in a superposition before,
- 02:59each of the measurements will give us either a zero or one.
- 03:03So, the output bit string, which we call y would be an n bit string.
- 03:09Now, if we go through the mathematics, we can see that the outcome of this
- 03:13circuit equals exactly the zero bit string.
- 03:18So, all outputs will be zero if and only if the function f is constant.
- 03:23If the output is anything different, then we know it is balanced.
- 03:28We can unfortunately not go through all
- 03:30the mathematics now, but you just have to believe me that
- 03:33now let us look at how we implemented with Qiskit.
- 03:39So we're using the IBM quantum lab that you have seen in previous videos.
- 03:44And while in the very beginning what we
- 03:47have to do is import our basic Qiskit libraries.
- 03:51So, we run the very first cell where we
- 03:53just have some basic imports and then we define our oracle.
- 03:58We call it the Deutsche-Jozsa Oracle.
- 04:00We define it as a function that depends
- 04:02on the number of bits n and on the oracle number.
- 04:06So, that can be just some integer.
- 04:09Now, we actually imported here also a function called Deutsche-Jozsa problem oracle.
- 04:15And this function is really nice because
- 04:17it just directly gives us a random oracle or well actually it's not random,
- 04:21it gives us an oracle, one out of four different oracles that we
- 04:25can specify by giving an oracle number between one and four.
- 04:29Now we call this oracle the Deutsche-Jozsa Oracle.
- 04:33This is just the name that it displays when we display the circuit.
- 04:36And then we return this oracle.
- 04:39Let's run that function, just the definition.
- 04:42Then we define the complete algorithm,
- 04:45the Deutsche-Jozsa algorithm, which again depends on n and on the oracle number.
- 04:49However, I put here oracle number equals
- 04:51zero, which means in the case where you don't specify the number,
- 04:55so where you do not put any number between one and four in there,
- 04:58but leave it just blank, in that case we will have the case zero
- 05:03by default, which I later on defined to just give me a random oracle.
- 05:07So, in that case you can test,
- 05:08if you don't know what your oracle is, you can test it.
- 05:12So, okay, let us start by creating the circuit.
- 05:16We start by creating a quantum circuit just with a basic command quantum circuit
- 05:20and we need n plus one qubits, where I did not yet tell you that,
- 05:25but so for the circuit that you saw before, we have this function that I
- 05:29called UF, that I told you, it's just this black box acting as an oracle.
- 05:33And so this function.
- 05:35Actually we add an additional qubit
- 05:37on that so that we can turn our function rather than having a bit
- 05:43oracle, which the function usually would be that we can turn it into a phase oracle.
- 05:49So, we will encode the outcome
- 05:50of the Oracle rather as instead of having it as a bit, we will encode it as a phase.
- 05:56So, if we input a superposition,
- 05:57we will have different phases for the different states.
- 06:00But don't worry about that too much, it's just why I have here n plus one.
- 06:03It's because of this special extra qubit that we need for the implementation.
- 06:06And then the second number n here
- 06:09shows us how many classical bits we need, which is for the measurement. I told you we
- 06:14want to measure in the end all of our N normal qubits.
- 06:17So for those we need those N outcomes, classical registers to store them.
- 06:24Now, in the beginning of the circuit we do Hadamard gates, which you just do by calling
- 06:30our circuit H for Hadamard and then identify the number of the qubits that we want
- 06:35to apply to and we want to apply to all of our N qubits.
- 06:38That's why we say make a for loop for range n.
- 06:42Then we set up this special output qubit, the one that we don't care about too much,
- 06:47the one that just turns our bit oracle into phase oracle.
- 06:51We need to prepare the minus state.
- 06:53So we need to prepare first apply an X gate and then an H gate.
- 06:57But as I said, don't worry about that too
- 06:58much, it's just to get the superposition to then get some phases.
- 07:02Now, what we do next is here,
- 07:04I mentioned before, if we do not specify the Oracle number
- 07:07but we want to have a surprise, then we just do nothing.
- 07:12We do not identify a number here, we just set at zero.
- 07:15And in that case we take a random number
- 07:17that gives us either a constant or a balanced oracle.
- 07:21Now, of course we've added the Hadamard gates on all our qubits.
- 07:28What we need to do next is we need to call
- 07:30our function the Oracle, the function UF that we defined before.
- 07:34So, we just call the show the Oracle
- 07:37with our inputs and then we appended to the circuit.
- 07:41In the end, if you remember the picture I showed you before we had Hadamard gates
- 07:45the function UF, you have again Hadamard gates and then measurements.
- 07:48So, now we have the Hadamard gates and the Oracle.
- 07:51We add again a bunch of Hadamard gates and measurements on each qubit.
- 07:56Each qubit means here we measure a qubit I
- 07:59and we store it in the classical register I.
- 08:01That's why we have two I's here.
- 08:03And well, in the end we return the circuit.
- 08:07And now we have defined this function, but now we would like to see doesn't
- 08:11actually look what we expect it to look like.
- 08:13So, we specify the number of qubits n equal to four.
- 08:18And then we choose the Oracle number,
- 08:21which we can either, for example, choose to be random between one and four.
- 08:25So, if I put one comma five,
- 08:27since it's with Python, you don't go to the last number.
- 08:30So, that would mean we have a random number between one and four.
- 08:33But I can also just say, okay, I want to have Oracle number one as I wish.
- 08:37I call the circuit and I just say circuit.draw
- 08:40and then it draws the circuit and well, here we go with our nice circuit.
- 08:45We can see the Hadamard gates, as expected on the four qubits.
- 08:48Then the oracle.
- 08:49Here this extra qubit that we don't care
- 08:51about, we don't even measure it, which we needed for the phases.
- 08:55Then again, a bunch of Hadamard gates and the measurements.
- 08:59So, I hope this already gave you a good
- 09:02idea of how we can actually implement a given circuit where we know
- 09:05theoretically how it looks like on a quantum computer.
- 09:09Now, in the next video, you will see how we can then run it both
- 09:13on the simulator and on an actual quantum device.
- 09:16So stay tuned for the next video.
To enable the transcript, please select a language in the video player settings menu.