- Device count, so those
were the device noisy.
I'm going to label the second one
device count and those were mitigated.
(upbeat electronic music)
Take a look at this!
Hello and welcome back
to coding with Qiskit.
This is Abe.
In today's episode we will
be talking about noise
and we'll be discovering a technique
called measurement error of mitigation,
which allows us to squeeze
the best performance
out of today's quantum computers.
You might recall from
our Hello World video
that we executed a simple circuit.
Both on a simulator as well
as a real quantum computer.
In that episode we saw
a very clear difference
between the two cases.
So in particular,
we saw that while the simulator,
which simulates a
perfect quantum computer,
tells us to expect a result
zero zero and one one
we found that running the
code on real quantum computers
gave us small number of results
that were also zero one and one zero.
The reason for the small number
of zero one and one zero results
is the presence of errors
in our quantum computation.
And in fact, these errors are caused
by various different types of noise.
So far in our work
we've been using two elements of Qiskit.
So we've been building
circuits in Qiskit Terra
and simulating the circuits on Aer
and running the circuits on real hardware
using Terra as well.
In this video we'll be talking about
a third component called Qiskit Ignis.
Ignis allows us to understand
and even sometimes mitigate
the types of errors that
we see in our computations.
And so in this way Ignis is the package
for working with and
characterizing quantum hardware.
One type of issue that
we have to deal with
on a daily basis is
called energy relaxation.
So think of a qubit that you prepare
in the excited state or the one state.
If you leave the qubit in this state
after some time you'll find
that it decays in energy
and goes back to the zero state.
The time scale for this kind of error,
which takes you from the excited
state to the ground state,
is known as T1.
There are several other kinds of errors
that we could talk about.
But lets do something
really cool with Ignis.
And the thing I want to focus on today,
is called measurement error mitigation.
The idea here is the following;
So let's imagine that we create
a two qubit quantum circuit
and we prepare the state one one.
and we immediately
measure those two qubits.
What we'll find is that the outcome
is mostly one one on real hardware.
But you'll also find a
small number of results
in the zero zero, zero
one, and one zero states.
Similarly if you prepared
another starting state,
lets say zero zero.
You'll find that your measurements shows
you a small number of results
in all the other states.
Now what if you knew
the chances of seeing the wrong answer
for each of these initial states.
Could you use that information
to then invert the results
of an unknown computation?
And try to get it as close as possible
to a simulation.
Let's see how this works.
So I'm going to start off by
saying from qiskit import everything.
And then I'm going to build
a simple three qubit quantum circuit.
So I'll say,
number of qubits is three.
And I'll create a circuit
which is quantum circuit.
Composed of three qubits
and three classical bits.
To the circuit I'll add
the operation of a Hadamard
gate on the first qubit
and then I'll do a controlled x
on the second qubit
controlled by the first qubit.
And I'll do another controlled x
on the third qubit
controlled by the second qubit.
Alright!
And finally I'll do a measurement
on all three qubits.
Okay!
So we can, as always, draw the circuit.
And before I draw, I'm going to say
mat plot lib inline.
So that's the circuit
that we'll be working with today.
You might recall that this circuit
is very similar to the one that we built
in the Hello World video.
So if this was a bit too fast,
please go back to that video
and take a look about how
we built that circuit.
So I'm going to proceed here.
So now the question becomes
running the circuit on
a real quantum computer.
So how do we do that?
The next step is to look at those results.
So what I'm going to do is say,
from Qiskit. visualization
import
plot histogram.
And then I'm going to say plot histogram
of the simulation result .get_counts.
And in particular for this circuit.
So this is what the
ideal results look like.
So, we expect roughly 50% of our results
to be in the zero zero zero state
and 50% of our results
to be in the one one one state.
So, this gives you an idea
of what a perfect quantum computer should
give for this circuit.
Alright!
Let's now run this code
on a real quantum computer.
And to do that I'm going to first begin
by importing my account.
So I'm going to say
IBMQ.load_account.
And I'm going to say the provider
is IBMQ.get_provider.
And the hub is going to be
IBM-Q, and the device that I'd like to use
for this work is going to
be provider.get_backend.
The name of the device is IBMQX2.
So this is a York Town device.
Once that executes,
the next step is to run
the job on this back end.
So, I'll say job is
execute the circuit
on the backend being the device.
And I'm going to ask for
1024 shots again.
Alright!
So it's a good idea when your running
on these real quantum computers
to first print the job ID.
So, I like to do print job.job_id
and import the job monitoring tool.
So, from Qiskit.tools.monitor
import job_monitor.
And then start the process
of job_monitor job.
So once the job is done
we can get the results back by saying
device_result is job.result.
And what I can do then
is plot those results.
So I'll say plot histogram
of device_result.get_counts
passing in the circuit.
So this is what the results look like
when you run them on a
real quantum computer.
And as we discussed before
while the large number of your results
are in the zero zero zero
and one one one states
just like in the simulator,
you still get a small number of results
in these other states.
So now let's do the interesting part.
So what I'm going to say
is from
Qiskit.ignis.mitigation.measurement
import
complete measurement cal.
And complete measurement fitter.
Then what I'll say is calibration circuits
and state_labels
are given by complete measurement cal
passing in the quantum register.
To be the circuit that I'm
working with dot quantum register
number zero so that's the
three qubits that I have.
And I'm going to give this a label
measurement error mitigation
calibration circuits
Okay!
So now, the idea here is
that Ignis has given us
a series of calibration circuits to run,
in order to find out what the errors are
as you run each of these circuits.
So in particular you
could do something like,
cal_circuits
maybe number two .draw.
And say output in mat plot light mode.
So you see here,
it gave you the circuit zero one zero.
So if you run the circuit,
what kind of outcomes do you get?
So in total there are
eight of these circuits.
And that's expected because we're working
with a circuit that has three qubits
so you can prepare a zero zero zero,
zero zero one, all the
way up to one one one
and there are eight such circuits.
Okay!
Now let me just run these circuits
and see what the outcomes are.
So what I'm going to say is
the calibration job is execute.
The circuits, in particular
the calibration circuits.
And I'm going to pass a few parameters.
So the first thing I'm going to say
is the backend is the device.
The number of shots I want.
I'm going to make this 1024 shots.
The optimization level is zero.
And that makes sure these circuits
are not changed as we transfer them
from what we have here
onto the real device.
And then we can say the job ID is
cal_job.job_id.
And we can start job_monitor of cal_job.
Okay!
So that'll allow us to see
what state the job is in,
and then I'm going to say
as soon as it's done
the calibration results
are cal_job.result.
And then we hit enter
and wait until the device run is finished.
If your curious to see what the outcome
of these results are.
So again, we can do plot_histogram,
and in particular we
can show, for example,
the cal_results.get_counts
for just one of these circuits
so cal_circuits, maybe number,
let's say three.
So you see this is the state
that was prepared in zero one one.
And in fact when we
measured this state we find
yes, the large number of results
are still zero one one.
But there are still the smaller number
of results that we need to account for.
So the work flow in Ignis
is very typically like what you've seen,
where we generate a series of circuits,
run them on the hardware.
And then take those
results back and fit them.
So try to understand
what parameters come out of these results.
So, I'm going to create
a measurement fitter.
And that's going to become
complete measurement fitter
of the calibration results.
And associated with these results
I need to tell the function
what states were generated.
So I'll pass the state labels as well.
Okay!
And if you're curious,
you can also visualize
the entire set of results
by saying something
like measurement fitter
.plot calibration.
So you see now the results make sense.
When you prepare your
state in any of these cases
the results that you
get in the measurements
look like the following.
So, seeing the diagonal components
tells you if you're
preparing zero zero zero,
the largest number of results.
So dark means near probability one
and white means near probability zero.
So, the largest number of results
are still the state that your preparing.
But you have these off
diagonal components.
They're very small
but they do matter in the computation.
Once you've created
the measurement fitter,
which extracts the parameters
out of the calibration results.
We can create an object called a filter.
So we'll create a measurement filter
from the measurement fitter.
By saying measurement fitter.filter.
So what you can do
once you've created the measurement filter
is say, mitigated_result
is the measurement filter.apply
on our device_result.
Okay!
So let's take a look
at what these results look like.
So the device counts
were device_results.get_counts
for the circuit.
The mitigated_counts
were mitigated_result.get_counts
for the circuit.
And let's plot them side by side.
So I'm going to say plot_histogram
of the device_counts
and the mitigated_counts.
And I'm going to label them
device_counts so those
were the device, noisy.
I'm going to label the second one
device count and those were mitigated.
Take a look at this!
So we've gone from the blue chart
which is the result from running
our quantum circuit at the beginning
on our quantum computer.
To these results
so that measurement error mitigation
has accounted for what kind of results
we expect to see for the
different starting states.
And then inverted the results
that we see in blue
into the ones in red.
So you see a lot of the
errors have disappeared here.
And all we have is just a
small amount of error here.
And the results look very close
to what you expect to see
from a perfect simulator.
Remember, now you've taken
all those calibration measurements
and saved the measurement filter.
So what you can do then
is now if you build
another quantum circuit
of three qubit you could mitigate results
from that circuit as well.
So let's just build another circuit.
So I'll say circuit2
is a quantum circuit
with three qubit again
and three classical bits.
And I'll say circuit2.x
on second qubit circuit2.h on first qubit.
And circuit2.cx
from zero to one,
circuit2.cx from one to two.
And then I'll say circuit2.measure
qubit zero one two
into classical bits zero one two.
And then do circuit2.draw_output
is mat plot line.
So this is a slightly different circuit
from the one that we've created before.
Let's quickly run it and see what happens.
So I'm going to execute,
do this all in one line.
So I'm going to say plot histogram
from executing
the circuit2 on the back
end being the simulator.
And the number of shots being 1024,
and I'm going to get
the results from this.
And I'm going to get the counts for it.
For this particular circuit.
Okay!
So this time we see that for the circuit,
that we've created,
the ideal results are all in zero zero one
And one one zero.
Lets run the circuit on real hardware
and see what happens.
So again the procedure to
do this is just like before.
We will say device_counts_2
is
execute circuit2
on the backend being the device
and the number of shots being 1024
.result.get_counts
for circuit2.
Okay!
Let's wait some time
for this job to finish
and to get the results back.
Once the job finishes we
can say plot histogram
of device_counts_2.
And this is what the results look like.
So again just like you'd expect.
Most results are in zero zero one
and one one zero
but you do see a few
results in the other states.
But now because you've created
your measurement filter already,
what you can do
is say measurement filter.apply
to these counts.
Let's save these results
as device_mitigated_counts_2.
And if we were to do plot histogram
of exactly this,
the results look much cleaner now
So, you have very little
results in the other states.
And your very close to what an ideal
quantum computer would have.
So lets summarize,
what have we learned today?
So we started off by
seeing how small errors
in our quantum computation
introduced measurement results
that take us far away from
what an ideal quantum
computer would give us.
And then we used a package
in Qiskit called Ignis
to understand what those
kinds of errors are.
And then to find out what
the parameters involved are
to generate a fitter
and then to use that
fitter to generate a filter
which allowed us to mitigate the errors
in our measurement outcomes.
So at the end of the day
What we've done is go
through a hardware run
of our quantum circuit.
And then taken the
results from that circuit
and then effectively applied
an inverting procedure
to find what the ideal outcomes
would be for that run.
So as an exercise,
why don't you try the following,
so you've noticed in our code
we created a three qubit quantum circuit.
And we tried to do
measurement error mitigation
on that quantum circuit.
See what happens if you
go now to four qubits.
The natural thing to happen is that
the amount of errors that you see,
as the numbers of qubits
increases should go up.
And you should be able to still do
measurement error mitigation
by generating the calibrations
circuits from Ignis.
Show us what you get in the comments.
Tell us what kind of
measurement results you find.
And tell us in the comments down below
what you've learned from this process.
If you'd like to learn some more
about the theory behind
the measurement error
mitigation work that we did,
you can go to our Qiskit textbook
where you'll find a nice section
about all the underlying
mathematics behind this procedure.
And as always thanks for watching.
And we'll see you in the next episode
of coding with Qiskit.
(upbeat music)
