# Writing a Qiskit Program

As mentioned before, this tutorial is being written in a jupyter notebook (*version 4.6.1*) with Anaconda (*version 4.6.14*) and qiskit (*version 0.11.1*) installed, running python (*version 3.7.3*).

## Set-up

### Imports

First we need to go through and import all our libraries and set up our IBM account.

```python
from qiskit import **
from qiskit.tools.visualization import plot_histogram
from qiskit.tools.monitor import job_monitor

%matplotlib inline
```

The line `%matplotlib inline` is used to make matplotlib graphs show up correctly in the jupyter notebook. You won't need this if you're using a different tool.

### Connecting your IBMQ account

Next, we need to connect your IBM account so that you can access their quantum systems. You'll need to log into your IBM account and grab your API token. Each token is different, and you shouldn't share it with anyone (that would pose a security risk to your account). To find your token log into your IBM account, and head to the account page:

![A screenshot show where the account page can be found](https://3199855145-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-Qqv_c34aARaXqqlB7%2F-M4Qep7z2vQXE2csUK0W%2F-M4QfXg7Spl7xmviMv83%2Fimage.png?alt=media\&token=89e3aca3-fd91-43bd-b002-9a8e1136b501)

Once you're there, you'll see the where you can copy your token:

![A screenshot showing where the copy token button can be found](https://3199855145-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-Qqv_c34aARaXqqlB7%2F-M4Qep7z2vQXE2csUK0W%2F-M4QfuRWirjGn9hLIXMU%2Fimage.png?alt=media\&token=38b57f60-4a6c-4654-a9de-d46dfb27ee79)

Then we can save the token on our local system:

```python
IBMQ.save_account('example_API_token-replace_me')
IBMQ.load_account()
```

Now you'll be able to run jobs on IBM's system when you're ready.

{% hint style="info" %}
[More detailed instructions for finding your API token](https://qiskit.org/documentation/install.html#access-ibm-quantum-systems)
{% endhint %}

## Your Quantum Circuit

### Circuit set-up

We'll be working with two qubits, so we'll need two classical and two quantum registers:

```python
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
```

Our circuit will include both of those:

```python
circuit = QuantumCircuit(qr, cr)
circuit.draw(output='mpl')
```

And our output:

![A visualization of our empty circuit](https://3199855145-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-Qqv_c34aARaXqqlB7%2F-M4QkpJfHuT1Oweh8YwD%2F-M4Qnixk9vciIv2bQ7ou%2Fimage.png?alt=media\&token=58772c89-c606-485b-ac93-5ef03eb2f1ea)

### Applying Gates

Let's apply a Hadamard gate to the first qubit and a controlled not gate to both - afterward we'll measure the result.

```python
circuit.h(qr[0])
circuit.cx(qr[0], qr[1])

circuit.measure(qr, cr)

circuit.draw(output='mpl')
```

![A visualization of our circuit after applying gates and measuring](https://3199855145-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-Qqv_c34aARaXqqlB7%2F-M4QkpJfHuT1Oweh8YwD%2F-M4QuJ2wKyF9rTXtnLzn%2Fimage.png?alt=media\&token=b2498a3c-9e7c-49b3-8cef-726db9efb286)

## Running your Circuit

### Testing on the simulator

Now that we have our little circuit set up, we're ready to test it. The first thing we'll need to do it load up our simulator. Once it's loaded, we'll execute it and display the results.

```python
simulator = Aer.get_backend('qasm_simulator')
result = execute(circuit, backend = simulator).result()

plot_histogram(result.get_counts(circuit))
```

![The graph showing which states were measured in our simulation](https://3199855145-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-Qqv_c34aARaXqqlB7%2F-M4QbfgWxLk5kM2fFmQ0%2F-M4QbohIGY5VN9c3am4P%2Findex.png?alt=media\&token=a10eea41-687b-48a3-87de-6deb92dd0bd8)

We can see that the results make sense - the Hadamard gate imposed a uniform superposition on the control bit, and the controlled not gate depends on the state of the control bit. Therefore what we should see is about half $$|00\rangle$$ and about half $$|11\rangle$$ - that is, half of the time both qubits are in state $$|0\rangle$$ and half the time both qubits are in state $$|1\rangle$$.

### Running on a real quantum system

Since our test was successful, we're ready to run our code on an actual system. This means we need to load up the quantum system and prepare our job.

```python
provider = IBMQ.get_provider('ibm-q')
qcomp = provider.get_backend('ibmq_16_melbourne')
job = execute(circuit, backend=qcomp)

job_monitor(job)
```

When we submit our job, we'll see a few different messages:

![The message indicating our job is 6th in the queue](https://3199855145-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-Qqv_c34aARaXqqlB7%2F-M4R-NojAj9Na4OnKMet%2F-M4R0kXLd_KCjBRi1EsX%2Fimage.png?alt=media\&token=1147a101-1720-4563-be08-62965ce62353)

![The message indicating the job is running](https://3199855145-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-Qqv_c34aARaXqqlB7%2F-M4R0yZLnNXIvSeHRZi6%2F-M4R1By5boq8mPLgy_B7%2Fimage.png?alt=media\&token=a787ffb1-0685-46bc-8a10-5b21f1be6e25)

![The message indicating the job has been successfully run](https://3199855145-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-Qqv_c34aARaXqqlB7%2F-M4R0yZLnNXIvSeHRZi6%2F-M4R1HdUmXN3zSMo4Idj%2Fimage.png?alt=media\&token=82a57eba-8b83-4145-9afa-2a15fb48da81)

This way we can track where we are in the queue and if our job is finished.

## Results

### Getting our results

Let's check what we got from the real quantum system:

```python
result = job.result()
plot_histogram(result.get_counts(circuit))
```

![The graph showing which states were measured in our job](https://3199855145-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M-Qqv_c34aARaXqqlB7%2F-M4R7Qml71FbVUeZIhwZ%2F-M4R7X6KV18mlVpNXagz%2Findex.png?alt=media\&token=ce7fc65c-0fd5-467a-b994-09c7017a4f63)

### Error

How strange - we're expecting about half $$|00\rangle$$ and about half $$|11\rangle$$... where are $$|01\rangle$$ and $$|10\rangle$$ coming from?

They come from inaccuracies in our quantum system. In the section about [*classical bits*](https://lewisla.gitbook.io/learning-quantum/qubits/classical-bits) we talked about [*sources of error and noise*](https://lewisla.gitbook.io/learning-quantum/qubits/classical-bits#fault-detection) in typical systems. Because quantum systems are so much more complex, we have more potential sources of noise, and therefore end up with more potential for error. Sometimes, our *qubits* end up where they shouldn't, and sometimes our *measurements* are wrong.

As quantum systems get better, we'll get more accurate result. They'll start to be able to check their own work - the noise in the system will be reduced. For now, we'll always end up with a little bit of what we didn't expect, and we'll need to be careful to recognize that when we analyze our results.
