# 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)