Hello World — Programming on Quantum Computers Ep 3

– Call got get counts and pass in our circuit. And there it is; that’s the result of executing our quantum circuit. (funky music) Hello, everyone. Welcome back to Coding with Qiskit. So traditionally when we learn
new programming languages, the first application that we write is what’s called the
Hello World application. And the purpose of this
application is to show, first, that you’ve installed the
programming language correctly on your computer and,
second, to walk you through what a regular application
cycle looks like. So how you program an application and get it to execution,
that kind of workflow. So in this video what I
hope to achieve is show you that workflow by running
your first Hello World application in quantum
mechanics using Qiskit. So in the previous episode
we showed you how to install Qiskit on your computer,
and in this episode what we’ll do is get you to
run your first quantum program. So hopefully by now
you’ve installed Qiskit. And what I’m going to do is I
have my terminal window open, and I’m going to start a Jupyter Notebook where I’ll start writing
code to execute in Qiskit. So I have my terminal window
open as you can see here and I’m going to write
Jupyter Notebook and hit enter and that gives me a new Jupyter window on which I can start programming. Okay, so now that we’ve
opened a blank notebook, what we’re going to do is
start writing code in Qiskit. Now for those of you who
are familiar with Python, Qiskit is written in Python, so a lot of it will look
very familiar to you. And for those of you that
don’t know, don’t worry; we’ll get through the
fundamentals that we need before building complicated
quantum circuits. So follow along. So the first thing that I’m going to do is to do from Qiskit import everything. Once I’ve imported everything from Qiskit, the next part is to build
the quantum circuit. And to build this quantum circuit, as I promised we’re going
to be building a two qubit quantum circuit; so what
I am going to do is first to create a two qubit quantum register, and then I’m going to
build a two classical bit classical register so that I can take measurements from these quantum bits. If I can just type better. So now we’ve built a quantum register and a classical register, and now we can build a
circuit using those two. (funky music) Now we’ve created a quantum circuit. And at any point that
we modify the circuit, if you want to look at what
the circuit looks like, you can draw it out by
doing the following. (funky music) I’ll do this one line here, so
that we can see our drawings. And then I’ll just do circuit.draw. So as you can see, what we
have is two quantum bits in the circuit and two
classical bits in the circuit. The quantum bits are Q zero
sub zero and Q zero sub one, and the classical bits are C zero sub zero and C zero sub one. All right, now this circuit
is not very interesting. There aren’t too many
interesting things going on here. There are no gates, so
what we’re going to do is now build up the
gates into the circuit. So in order to create entanglement, the first step is to apply what’s known as a Hadamard gate onto the first qubit. And so what I’m going to
do is circuit.hadamart, which is H for short, and I’m going to apply it on the first qubit. So once we’ve done that, again, you can apply your drawing function. And this time what I’m going
to do is show you a way to make a better plot of your circuit instead of using something
that’s text based to use something that
plots out in matplotlib. So now that you have a
Hadamard gate applied to the first qubit, you
see that H that showed up. We’re going to then create a two qubit operation called a controlled X, and that’s done by doing the following. (funky music) This two qubit operation
is like a logical if, so it’s going to be the
quantum version of if this, then that; and the control is
going to be the first qubit. And the target of that operation is going to be the second qubit, sorry. And there we go. Let’s look at how our circuit looks. (funky music) There we go. Now our quantum circuit is composed of a Hadamard gate and
a controlled (mumbles). And the idea here is now with
these two simple operations, we’re able to generate entanglement between Q zero sub zero
and Q zero sub one. So now that we’ve built
our quantum circuit using this Hadamard gate
and controlled X gate, what we’re going to do is
measure the quantum bits, or qubits, and take those measurements and store them into the classical bits. And what we’re going to do is write out that code to do that, circuit.measure. What I’m going to do is take the results from measuring the qubits
in the quantum register and store them in the classical register. Okay, so we’ve done this. By the way, those outputs that show up are simply related to a
quirk of Jupyter Notebooks. I wouldn’t worry about them for now. So, your circuit has
measurements built into it. Again, as always, you can
look at what your circuit looks like at any moment
by doing circuit.draw. Okay, so this is what our
circuit looks like finally. We have our quantum operations, the Hadamard and controlled X gate; and we have the measurement
so that we can understand what happened in the
quantum circuit at the end. So the next step is to run the circuit, and I’m going to do two things. The first thing I’m going to
do is run this quantum circuit on my classical computer
and see what happens when I simulate a quantum computer here. And then when I’m, once I’m confident with the quantum circuit,
what I’m going to do is send it off to a real device at IBM and then see the results that come out of running this quantum
circuit on a real device. So in order to simulate the circuit, what I’m going to do is use
the air component of Qiskit. The idea here is that air is
what we use when we need to do simulations of our quantum
circuits on our local computer. So the simulator can be imported from air by doing air.get_backend, and the name of the
simulator is QASM simulator. If you’re curious, the name QASM comes from quantum assembly language. Okay, now that we’ve
imported our simulator it’s time to execute the circuit. So what I’ll do is call execute. What I’m doing is executing
the circuit that I’ve built so far and what I’ll be using
is the simulator as a backend. So the backend on which I’m
executing is the simulator that I’ve imported, and that’s it. So now that we’ve executed
our quantum circuit, the question is what are the results that came out of that execution. Once we’ve executed our quantum circuit, let’s get the results
back by doing .result and assign them to a
variable called result. So now the result variable
holds the information that came back from
executing that circuit. Let’s look at what result itself contains. So to do that what I’m going to do is import visualization tools from Qiskit. So what I’m going to do is from qiskit.tools.visualization import plot_histogram. Once I have this plot
histogram function imported from Qiskit, what I’m going
to do is take that result, call .get_counts and pass in our circuit. And there it is; that’s the result of executing our quantum circuit. So as you can see, we get roughly 50% or with 0.5 probability, zero zero. And with almost 0.5 probability, one one. So these small errors
are because we’re running a limited number of
shots on our simulation instead of an infinite number of shots. Now that we’re confident
that our circuit is doing what we expect, let’s run
it on a quantum computer at IBM and see what happens. So in the previous
video you learned how to take your API token from
the IBM quantum experience and save it on your
computer so that you can access IBM’s quantum devices. So let’s load our account here. So I’m going to write
out IBMQ.load_account. Okay, so once our account is loaded, we’re ready to choose the device on which to run our code and then
to continue to get results. So once I’ve loaded my account, I am going to say provider is
equal to IBMQ.get_provider. And I’m going to say the provider
is IBM-Q, hit Shift Enter, and then I’m going to
say the quantum computer with which we’re working
is provider.get_backend. The name of that particular device we’ll be working with
is IBMQ_16_melbourne. This is just what we name our devices. The device isn’t actually
located anywhere in Melbourne. We’re going to say the job is execute the circuit that we’ve built on the backend, which is called qcomp. We’re going to remove these spaces and spell circuit properly
and then I am going to say from qiskit.tools.monitor import job_monitor. So the idea here is that
these jobs are submitted to the real devices and because they’re public devices, there’s a queue. So the job monitor allows
me to say job_monitor taking in the argument of the
current job that we started, and we see that the job is queued now. And, in fact, it’s job
number two in the queue. Depending on how busy the
quantum devices are at the time, the job might take a few minutes. And once you get this message that the job has successfully run, you
can say result is job.result. Next we’ll plot the result by saying plot_histogram of the result.got_count and I’ll pass in the
circuit as an argument. So looking at these results, let’s compare how they look against what
we saw with a simulator. So you see the difference
between the two is that in a simulated case, you only
got zero zero and one one, but when the code was run
on a real quantum device, we also had a small number of results showing up in zero one and one zero. Even though the majority
of results are still at the zero zero state
and the one one state. So let’s think a little
bit about why that happens. The difference between the
simulated case and the code that was run on the real quantum device is that the simulator simulates
a perfect quantum device. In the meantime, the real quantum device is susceptible to small quantum errors. These quantum errors are
getting improved every day as a result of the technology improving. So we expect to see these
counts getting lower and lower and the results getting closer and closer to the ideal simulations. However, as you can see here, the devices are not perfect today, and that is why you see
a difference in results, a small change from what you see with the perfect quantum
computer simulated on our laptop. So there you have it. We’ve gone from start to finish in creating our first quantum
Hello World application. So now the question becomes
where do we go from here? Would you like to see more
focus on quantum games, quantum applications,
the devices themselves? Would you like to learn more about how these applications come in industry? Where do we go from here? Let us know in the comments down below, and we’ll see you in the next video. (funky music)

49 thoughts on “Hello World — Programming on Quantum Computers Ep 3

  1. Would be very interesting to see how to improve scientific calculations via qiskit and maybe some info about how experts today are using it to make actual experiments!

  2. quantum applications would be very useful for those like me who are starting and trying to understand how much we can do with qiskit, you are doing such a great job btw !!

  3. Please do at least 3 videos a week I am so much interested in it that I can't wait you can do quantum applications first. You can use the circuit simulator in IBMQ what is the difference between QISKIT and IBMQ Expirence Simulator.

  4. 1:42 Open Jupiter Notebook
    $jupiter notebook
    On notebook cell
    [python3][cell] from qiskit import *
    Creating a Quantum Circuit
    2:10 Build 2 Qbit Register(2)
    [cell] qr = QuantumRegistry

    2: 30 Clasical Register Mesurments
    [cell] cr = ClasscalRegister(2)

    2:55 Draw our circuit
    [cell] %metplotlib inline
    [cel] circuit.draw()
    Build up gates : Create entaglement
    3: 30 [H] "Hadamard Gate"
    [cell] circuit.h(qr[0])

    Plot Circuit
    3: 57 Better Plot result
    [cell] circuit.draw(output='mpl')

    Controlled X
    4:14 Two Q operation (Logical if )
    [cell] circuit.cx[0], circuit.cx[1]

    4:46 Plotter Circuit
    [cell] circuit.draw(output='mpl')

    4:49 Q Circuit is [H] + Cx Gate
    5:21 Messuring Qbits
    [cell] circuit.mesure(qr, cr)
    [cell] [circuit.mesure(QuantumRegister ->, ClasicalRegister)]

    Plot Circuit
    5:47 Better Plot result
    [cell] circuit.draw(output='mpl')

    6:49 Simulate Circuit and exe
    [cell] simulator = Aer.get_backend('qasm_simulator')
    6:58 Run Circuit
    [cell] execute(circuit, backend = simulator)

    7:33 Result visualization and plot
    [cell] result = execute(circuit, backend = simulator)
    [cell] from qiskit.visualization import plot_histogram

    Run on in a device
    8:53 Log on IBM account
    [cell] IBM.load_account()
    [cell] provider = IBMQ.get_provider('ibm-q')
    [cell] qcomp = provider.get__backend('ibm_16_melbourne')
    [cell] job = execute(circuit, backend=qcomp)

    10:20 Qued Job on device
    [cell] from qiskit.tools.monitor import job_monitor

    10:28 Checking Job qued status
    [cell] job_monitor(job)

    10:42 Looking at results
    [cell] result = job.result()

    11: 03 Result Plotters
    [cell] plot_histogram(result.get_counts(circuits)

    Thanks @QiskitTeam Paul, Catherine ,Leron and Abraham

  5. Great introductory vids! Good job!
    Would be also nice to see more examples of quantum algorithms that solve some specific, simple problems in applied mathematics or computer science.

  6. Teach us higher level programmings sir ,like some games ,or what are the other things we can do with this .,if we catch the language its our task to apply it in our respective fields .And a great video by the way !!

  7. A couple of comments. For people who don't know python, it is hard to listen and follow along with your typing and actually type at the same time. So I end up frantically typing and miss what you are saying. If you can explain what the step is and why it is being done before you start typing it would be helpful. Also, if you can leave more time or a pause in between typing lines it would also be helpful so I can process what each line is doing. Or maybe most people are experienced enough that this is not an issue…don't want to hold folks up that are able to work quickly.

    I would like to see in future episodes:

    – more on gates and what they do
    – additional programming exercises and interpreting the output – so summarizing the steps and why it resulted in the particular output

    – potential real world applications, and basic education about how researchers turn "use cases" into algorithms for problem solving, or how they relate in quantum computing

    Thank you!! I am really enjoying these videos.

  8. I haven't been this interested in a series in a while. I dont care what y'all say this is a love story.
    (Also Algos please!)

  9. As a programmer I have a quibble: I inadvertently discovered that 'circuit' is the name of a module in qiskit; in fact the one from which QuantumRegister and ClassicalRegister are derived. It's not prudent to use a name that's already in use, especially in cases such as this one where everything is imported at once (from qiskit import *) — another practice that's generally frowned upon unless it's clear what one is doing.

    That said, these videos are otherwise very clear and well-produced. I look forward to future episodes.

  10. 1:52 woah, my notebook doesn't look anything like that!
    how do I make mine look the same, and why does it say qiskitdevl on the top right corner?

  11. How quantum entanglement is useful in quantum computing? It takes part in making a quantum computer superior than a classical one?

  12. We should definitely focus on how Quantum Computation will be applicable in industries and specifically Quantum Machine Learning. The next videos should concentrate on Quantum application development.

  13. It took forever for me to get a slot. I was 12th in the queue. Be patient is all I can say as it was almost three hours before I could get my results. I'd like to see more on quantum algorithms and the devices itself.

  14. Personally I suggest not stacking a python application on top of a complex machine and referring to everything as circuits as it is somewhat hard to follow. We're coming from the ground up on this, not top down. I understand the content management and marketing aspect to attract a community and position yourself as a some type of leader to establish credibility but leading people to believe this is an actual SDK and that they can do anything other than toy around is disingenuous and somewhat frustrating. Fools ignore complexity, geniuses remove it and you're not even close to scratching the surface. Kernighan and Ritchie did not require you to understand the physics of a binary machine to compile code. I hope the graduate academic community has less of a role to play in the development of new machines, marketing management and the logic behind them

  15. Ok. Rotating the nucleus of a Phosphorous atom and reading the state of the tied electron is interesting. Seeing that A –> B has a 50-50 probability, based on what you give it, is barely more than a gravity-check, but seeing the superposition states is interesting. I think I understand that the speed of the solution is nearly instantaneous because the speed of quantum entanglement is … quite fast. This seems, though like little more than constructing an analog computer (system of resistors and capacitors to replicate a physical system). I'm curious to see a few, more interesting, examples: 1) A group of Qubits executing an XOR operation to encrypt and decrypt an 8-bit character. 2) A Qubit model of a cantilevered beam with at least 4 finite elements along its length and 3) an implementation of a symmetric ternary logic operation on two Qubytes (8 Qubits). The whole notion of a quantum hashing algorithm is a topic for a later discussion, but just seeing something that can be tangibly understood and how it ties the behavior of the Qubits to the problem I'm trying to solve would be very helpful for those of us more leaning toward the application side of this technology. I can envision some very sophisticated hybrid algorithms of classical and quantum portions of very large problems (imagine using Prolog as a skeleton to drive routines in R, but in this case the R routines might be solved by qubits). …Just some food for thought.

  16. Problem with line 'result=job.result()'. I run this after success message, but it goes away for about 15 minutes and then gives 'RequestsApiError: 403 Client Error: Forbidden for url'. Is anyone else having this problem? Do you have a solution? I am unable to get my results even though I can run the job on ibmq_16_melbourne

  17. plot_histogram(result.get_counts(circuit)) saying AttributeError: 'function' object has no attribute 'get_counts' (qiskit version: 0.12.1)

  18. What really scares me is error propagation. Am I correct if I say that displacement between the "perfect simulated" qc and the "real" qc is enormous ? On a such a simple circuit ? What does it happen when I have hundred of definitions, interactions, factors etc. ?

  19. Great Initiative.. seamless work flow, However When I tried it today, the program did not gave results even though job status showed the program is running active. How long do we need to wait to get the results?

Leave a Reply

Your email address will not be published. Required fields are marked *