# Build a simple Quantum Circuit using IBM Qiskit in Python

**Qiskit** is an open source framework for quantum computing. It provides tools for creating and manipulating quantum programs and running them on prototype quantum devices on IBM Q Experience or on simulators on a local computer. Let’s see how we can create simple Quantum circuit and test it on a real Quantum computer or simulate in our computer locally. Python is a must prerequisite for understanding Quantum programs as **Qiskit **itself is developed using Python.

First and foremost part before entering into the subject is installation of **Qiskit** and **Anaconda. **Refer to the below articles for step by step guide for anaconda installation.

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course. And to begin with your Machine Learning Journey, join the **Machine Learning - Basic Level Course**

#### Installation

To install qiskit follow the below steps:

- Open Anaconda Prompt and type

pip install qiskit

- That’s it, this will install all necessary packages.
- Next, open Jupyter Notebook.
- Import qiskit using the following command.

import qiskit

**To get access to IBM Quantum Systems:**

- Create a free IBM Quantum Experience Account.
- Navigate to
**My Account.** - Click on
**Copy Token**to copy the token to the clipboard(Token represents the API to access IBM Quantum devices). - Run the following commands(Jupyter Notebook) to store your API token locally for later use in a configuration file called
. Replace**qiskitrc**with the API token.**MY_API_TOKEN**

from qiskit import IBMQ IBMQ.save_account('MY_API_TOKEN')

## Getting Started

Firstly, we will import the necessary packages. The import lines import the basic elements (packages and functions) needed for your program.

The imports used in the code example are:

Holds all your quantum operations; the instructions for the quantum system**QuantumCircuit:**: Runs your circuit**execute**: Handles simulator backends**Aer**Enables data visualization, such as**qiskit.visualization:****plot_histogram**

**Initialize variables**

In the next line of code, you initialize two qubits in the zero state, and two classical bits in the zero state, in the quantum circuit called **circuit.**

**Add gates**

The next three lines of code, beginning with * circuit*., add gates that manipulate the qubits in your circuit.

*Detailed Explanation*

): A Hadamard gate on qubit**QuantumCircuit.h(0**, which puts it into a**0****superposition state**): A controlled-NOT operation (**QuantumCircuit.cx(0, 1**) on control qubit**C**_{x}and target qubit**0**, putting the qubits in an**1****entangled state**): The first argument indexes the qubits, the second argument indexes the classical bits. The**QuantumCircuit.measure([0,1], [0,1]***n*th qubit’s measurement result will be stored in the*n*th classical bit.

This particular trio of gates added one-by-one to the circuit forms the Bell state,|

In this state, there is a 50 percent chance of finding both qubits to have the value zero and a 50 percent chance of finding both qubits to have the value one.

**Simulate the experiment**

The next line of code calls a specific simulator framework – in this case, it calls Qiskit Aer, which is a high-performance simulator that provides several backends to achieve different simulation goals. In this program, we will use the * qasm_simulator*. Each run of this circuit will yield either the bit string

*or*

**’00’***. The program specifies the number of times to run the circuit in the*

**’11’***argument of the execute method (*

**shots***). The number of shots of the simulation is set to*

**job = execute(circuit, simulator, shots=1000)****1000**(the default is

*). Once you have a*

**1024***object, you can access the counts via the method*

**result***. This gives you the aggregate outcomes of your experiment. The output bit string is*

**get_counts(circuit)***approximately 50 percent of the time. This simulator does not model noise. Any deviation from 50 percent is due to the small sample size.*

**’00’****Visualize the circuit**

* QuantumCircuit.draw() *(called by

*in the code) displays your circuit in one of the various styles used in textbooks and research articles. In the circuit obtained after visualization command , the qubits are ordered with qubit zero at the top and qubit one below it. The circuit is read from left to right, representing the passage of time.*

**circuit.draw()****Visualize the results**

Qiskit provides many visualizations, including the function* plot_histogram*, to view your results.

plot_histogram(counts)

The probabilities (relative frequencies) of observing the |00? and |11? states are computed by taking the respective counts and dividing by the total number of shots.

**Below is the implementation.**

## Python3

`# python program to create a simple Quantum circuit` ` ` ` ` `%` `matplotlib inline` `from` `qiskit ` `import` `QuantumCircuit, execute, Aer, IBMQ` `from` `qiskit.compiler ` `import` `transpile, assemble` `from` `qiskit.tools.jupyter ` `import` `*` `from` `qiskit.visualization ` `import` `*` ` ` `# Loading your IBM Q account(s)` `provider ` `=` `IBMQ.load_account()` ` ` `# Create a Quantum Circuit acting` `# on the q register` `circuit ` `=` `QuantumCircuit(` `2` `, ` `2` `)` ` ` `# Add a H gate on qubit 0` `circuit.h(` `0` `)` ` ` `# Add a CX (CNOT) gate on control ` `# qubit 0 and target qubit 1` `circuit.cx(` `0` `, ` `1` `)` ` ` `# Map the quantum measurement to the` `# classical bits` `circuit.measure([` `0` `,` `1` `], [` `0` `,` `1` `])` ` ` `# Use Aer's qasm_simulator` `simulator ` `=` `Aer.get_backend(` `'qasm_simulator'` `)` ` ` `# Execute the circuit on the qasm` `# simulator` `job ` `=` `execute(circuit, simulator, shots` `=` `1000` `)` ` ` `# Grab results from the job` `result ` `=` `job.result()` ` ` `# Return counts` `counts ` `=` `result.get_counts(circuit)` `print` `(` `"\nTotal count for 00 and 11 are:"` `,counts)` ` ` `# Draw the circuit` `circuit.draw()` |

**Output:**

**Plot a histogram**

## Python3

`# Plot a histogram` `plot_histogram(counts)` |

**Output:**