- Now that you've seen that this execution gives us
the same result that we explained before,
I want to show you two additional ways
that people discuss the quantum state.
(upbeat music)
Hi, welcome back to "Coding with Qiskit."
This is Abe again.
You might remember in our last video,
we talked about how we can build quantum circuits
using Qiskit to manipulate qubits.
Quantum circuits can be used
to make qubits do all sorts of interesting things.
The key to understanding quantum circuits,
whether complicated or simple,
will be to understand how quantum gates act
on the individual qubits, and how they work together.
The point of this video will be to enable you
to learn how quantum gates work on your own.
Luckily, with the right tools,
you can understand how all of Qiskit's quantum gates work.
As open-source software, Qiskit has a number of gates
programmed into it already.
So looking at my screen here, I'm showing you an overview
of the quantum gates available in Qiskit.
The link to this page will be available
in the description below.
So you can see here this list contains gates like
the H gate, also called the Hadamard gate,
CX, Identity, U3 gates, U2 gates, U1 gates, and so on.
And in fact, in Qiskit
you can even create your own custom gates.
There are many different ways to think about quantum gates.
Some of them are more visual,
and some of them are mathematical.
So one of these gates, for example, is the X gate.
So let's get right into it.
Saying the X gate applied to state zero
gives you the state one,
and the X gate applied to state one
gives you the state zero.
So this is one representation that you'll see around.
The other representation is to take note of the fact
that these gates are actually unitary operations.
And all that means is that they can be represented
as matrices, unitary matrices in particular,
and their operations can also be seen
as operations on vectors, which are the states.
So by doing that, the X gate is represented in this way.
(laid-back music)
The zero state is represented in this way.
And if you write out the matrix multiplication,
you'll find out that the result is the following.
So this is exactly equal to that representation.
The third and fourth ways that you'll see people talking
about how a quantum circuit works,
is in terms of both its visual representation,
and how the circuit works as a result of measurements.
To see these, we'll go straight into Qiskit.
As always, I'll start off
by importing Qiskit into my workspace.
So that can be done by typing out
from Qiskit import everything.
I will also be using the visualization tools in Qiskit.
So I can do that by writing out
from Qiskit.tools.visualization,
import plot, bloch, multivector.
Okay, so now that we've imported Qiskit
and the right tools that we need to do our work,
I'm going to do the following.
So I'll create a quantum circuit
with one qubit, and one classical bit.
So that quantum circuit will have one operation on it.
It'll just be circuit.x applied to that qubit.
So what I'm going to do is
take this quantum circuit and simulate it
and see what the output of that quantum circuit is.
So the way to do that is to say
the simulator is aer.get, backend.
This time I'm going to use
what's called the state vector simulator.
(laid-back music)
If you're curious, the state vector is the vector
that describes the quantum state of our qubit.
So, that's where the name state vector simulator comes from.
And I'll say,
execute the circuit
on the backend that I choose to be the simulator,
and I'll say the results from this execution
will be stored in a variable called result.
And the state vector
will be result.get state vector.
And just so we can see that this is working,
I'll say print state vector.
All right, so as you can see,
the state vector that's returned from the circuit,
when it only has one X gate on it, is the vector zero,
with zero imaginary component,
and one with zero imaginary component.
This state vector is exactly what you saw
in our representation before,
where we wrote out X applied on the state zero
is the state one.
So, let's look at our quantum circuit.
So what I'm going to do is say circuit.draw,
and I'll be using the mat plot lib output,
and I'll be saying, just before that,
mat plot lib inline.
So what we're saying is the output of this quantum circuit
is the state vector zero, one.
Now that you've seen that this execution
gives us the same result that we explained before,
I want to show you two additional ways
that people discuss the quantum state.
So the next way that I'm going to show you
is a bit more visual,
and tends to be the favorite of a lot of people.
What I can do is say plot, bloch, multivector.
And the thing I'm going to describe with this
is the state vector.
(laid-back music)
And this is what the state vector looks like
when it's plotted on a bloch sphere.
So, one thing you'll notice
is that people refer to operations on quantum states
as rotations on the sphere.
And in fact, what that X gate has done
is take us from the state zero to the state one.
And that's why that vector is pointing down
in the direction of the state one.
So it turns out that quantum states for individual qubits
can be represented on the sphere,
as any point throughout the surface of the sphere.
And in particular, the one that we're working on here,
the state one, is the very bottom point of the sphere.
And finally, remember that you can always
run this quantum circuit, execute measurements,
and find out what the results of those measurements are.
So in Qiskit, this is done as follows.
So what I'm going to do is add a measurement to the circuit,
and I'm going to measure qubit zero,
and put that result in classical bit zero,
and I'm going to say,
backend is aer.get backend, qasm simulator,
and I'm going to say execute this circuit on the backend,
and I would like to do 1,024 shots.
And the result of this execution, I'd like to take out,
and put in a variable called result.
And I would like to take the counts from this result,
(laid-back music)
and plot them out by using the plot histogram tool,
which is available from Qiskit.tools.visualization,
import plot histogram.
(laid-back music)
And there you have it.
The result is that for 100% of the results,
you get the result one.
And that's because we have the state one represented
by the output of the circuit.
And just real quick, I also want to show you
how to get the matrix representation of a circuit.
And the way to do this is very simple.
What I'll do is go back here,
and instead of state vector simulator,
what I'm going to do is say I want the unitary simulator.
So I'll just copy over this section of code,
(laid-back music)
and I'll say unitary simulator here, and I'll say,
the unitary is the output,
and I'll change get state vector to get unitary.
And I'll print out the unitary
that results from this operation.
And as you see here, the matrix that's printed out
is zero, one, one, zero.
And this is exactly the matrix that we used
when we described the second method
of talking about this quantum circuit,
which is that the X gate is represented
by the matrix zero, one, one, zero.
And if you're curious, the meaning of these items, zero, J,
is simply because each of these matrix elements
is actually a complex number.
So it has a real part and an imaginary part.
And the number that you see here, for example,
zero plus zero J means the real part is zero
and the imaginary part is also zero.
This particular number has a real part one
and an imaginary part zero.
So, that corresponds to the one
that we've been seeing in our matrices before.
So we showed four different ways
to talk about a quantum gate.
One way is to describe its operation
by writing down, as we saw before,
for example, for the X gate, by saying,
X applied on the state zero gives you the state one.
The second way that we used was
to describe the linear algebra behind how this gate works,
by saying the matrix for the X gate,
which is zero, one, one, zero,
acting on the vector for the zero state, which is one, zero,
gives you the vector for the one state, which is zero, one.
The third way that we used
to describe the operation of the X gate
was by showing you something more visual,
by plotting out the state vector
that results from applying that gate on a bloch vector.
And the fourth way that we used to describe its operation
is by running a circuit through a measurement,
and seeing what the measurement outcomes are.
So hopefully with all these tools,
you're now equipped to answer the question
of how a quantum gate works.
And every time you encounter a new one,
you can ask yourself the question
of how it's actually working in a quantum circuit.
So now that you know
how to understand all of Qiskit's quantum gates,
it's important for you to know that you're ready to move on
to building more complicated quantum circuits.
Next week we'll be covering your first quantum algorithm.
And this will be quantum teleportation.
Let us know which method
of talking about quantum gates made more sense to you.
For me personally, I like to see the quantum circuit
represented as a matrix.
So I like to write code in Qiskit,
and find out what the matrix corresponding
to a quantum gate looks like.
Thanks again for watching,
and we'll see you in the next video.
(upbeat music)