Start your quantum computing journey with Qiskit
Tags: quantumcomputing
Published: 2022-06-19 12:54:54

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.org)
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())
```
![Create a circuit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2yjdv2jydwpz4pgixn4.png)

### 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)
```
![histogram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hzhic3an85pf2zzwhxe.PNG)
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)
```
![outputstate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vqq2qt71q8peilprufx.PNG)
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)
```
![unitary matrix](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/foexigu78mkrbuygs204.PNG)

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

- [IBM Quantum](quantum-computing.ibm.com)
- [IBMQProvider issue](https://stackoverflow.com/questions/62001137/ibmqprovider-issue/69892602#69892602)

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())
```
![output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0c08w5gnt0h2n3n9pmr.PNG)
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)
```
![plot_histogram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wnln3k756onz6dzplnp4.PNG)
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'])
```
![output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1b0yvh10owhyvyf9jow.PNG)
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 
- [Codemaker2015 Google Colaboratory](https://colab.research.google.com/drive/1tbWxbmSYKZkTVQTPWKfbIYJmMcT4UCeV?usp=sharing)
- [GitHub - codemaker2015/quantum-computing-first-project](https://github.com/codemaker2015/quantum-computing-first-project)

To get the article in pdf format: [Quantum-computing.pdf](https://drive.google.com/file/d/1KcnG5I-O5r2iPG-nfkFGGAr2edHUntYU/view?usp=sharing)

The article is also available on [Medium](https://medium.com/@codemaker2016/start-your-quantum-computing-journey-with-qiskit-568f1fa14d3f)

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