Quantum computing is an emerging technology that uses the laws of quantum mechanics to solve complex problems which will replace the traditional approach of solving using classical computers.

Unlike traditional computer logics of using bits-and-bytes, on-or-off, true-or-false scenarios, quantum computing makes use of bits called quantum bits or qubits which represents a one, a zero, or both at once. This superposition property makes these qubits to behave in a way which can’t be explained by the individual components. This behavior of qubits is called entanglement. We make use of entanglement and superposition for applications involving complex calculations.

### Qiskit

Qiskit is an open-source SDK developed by IBM for working with quantum computers. It gears up the development of quantum applications by providing the complete set of tools at the level of circuits, pulses, and algorithms that are required for interacting with quantum systems and simulators.

## Getting Started

In this session, we will learn on defining a simple quantum circuit and executing it on both simulators and real quantum computers of the IBM Quantum Experience. Let’s explore quantum computing with Qiskit SDK.

### Install and import dependencies

Let’s install qiskit package via pip

```
pip install qiskit
pip install pylatexenc
```

If you are using Google Colab,

```
!pip install qiskit
!pip install pylatexenc
```

Now, we can import the dependencies to the project.

```
from qiskit import *
from qiskit.visualization import *
from qiskit.tools.monitor import *
```

### Defining the circuit

We can define a simple circuit using the H gate to put a qubit in superposition after which we will measure the state of the circuit.

```
# Create a circuit to generate a superposition state
circ = QuantumCircuit(1,1)
circ.h(0) # We apply the H gate
circ.measure(range(1),range(1))
circ.draw() # Draw the circuit
"""We can also obtain the *qasm* code for the circuit."""
print(circ.qasm())
```

### Running the circuit on simulators

We can execute the circuit on a simulator once we define it.

```
# Executing on the local simulator
backend_sim = Aer.get_backend('qasm_simulator') # Choose the backend
job_sim = execute(circ, backend_sim, shots=1024) # Execute the circuit, selecting the number of repetitions or 'shots'
result_sim = job_sim.result() # Collect the results
counts = result_sim.get_counts(circ) # Obtain the frequency of each result
print(counts)
plot_histogram(counts)
```

We can execute the circuit on a simulator to determine the final state.

```
# Execution to the get the state
vectorcirc2 = QuantumCircuit(1,1)
circ2.h(0)
backend = Aer.get_backend('statevector_simulator') # Change the backend
job = execute(circ2, backend) # Execute the circuit on a simulator. Now, we do not need repetitions
result = job.result() # Collect the results and access the statevector
outputstate = result.get_statevector(circ2)
print(outputstate)
```

We can obtain the unitary matrix that represents the action of the circuit by following the below code.

```
backend = Aer.get_backend('unitary_simulator') # Change the backend
job = execute(circ2, backend) # Execute the circuit
result = job.result() # Collect the results and obtain the matrix
unitary = result.get_unitary()
print(unitary)
```

### IBMQ integration

Create a IBM Quantum account if you don’t have one to utilize quantum computers at the IBM Quantum Experience. Also, create the API token that is available on the dashboard and using the created API token as the enable_account() method argument connect your project with IBMQ instance.

Now, we can use the quantum computers at the IBM Quantum Experience to execute the circuit.

```
# Connecting to the real quantum computers
from qiskit import IBMQ
provider = IBMQ.enable_account("your-ibmq-api-key") # Load account
provider.backends() # Retrieve the backends to check its status
for b in provider.backends():
print(b.status().to_dict())
```

We can execute the circuit on IBM’s quantum simulator (which supports up to 32 qubits). The only requirement is to select the appropriate backend.

```
# Executing on the IBM Q Experience simulator
backend_sim = provider.get_backend('ibmq_qasm_simulator')
job_sim = execute(circ, backend_sim, shots=1024) # Execute the circuit, selecting the number of repetitions or 'shots'
result_sim = job_sim.result() # Collect the results
counts = result_sim.get_counts(circ) # Obtain the frequency of each result
print(counts)
plot_histogram(counts)
```

We can make use of job_monitor to get live job status information.

```
# Executing on the quantum computer
backend = provider.get_backend('ibmq_armonk')
job_exp = execute(circ, backend=backend)
job_monitor(job_exp)
```

We can compare the results from real quantum computers with the results obtained from the simulator once the job is done.

```
result_exp = job_exp.result()
counts_exp = result_exp.get_counts(circ)
plot_histogram([counts_exp,counts], legend=['Device', 'Simulator'])
```

There you have it! Your first quantum computing project using Qiskit in python :)

Thanks for reading this article.

The source code of the project in this article is available on

To get the article in pdf format: Quantum-computing.pdf

The article is also available on Medium

If you enjoyed this article, please click on the heart button ♥ and share to help others find it!

## Top comments (0)