Stepping into World of Quantum Computing: QC Basics With Qiskit

Maham Shafiq
Analytics Vidhya
Published in
6 min readJun 12, 2020

Quantum Computers aren’t theoretical devices anymore. Google’s quantum supremacy experiment was performed using 53 noisy qubits. This experiment demonstrates that a Quantum Computer could perform a calculation in 200 seconds that would take 10,000 years on the largest classical computer using existing algorithms.

Noisy Intermediate-Scale Quantum (NISQ) computing era begins from here. In the Upcoming years, quantum devices with tens-to-hundreds of noisy qubits are expected to become a reality.

In this article, I would cover all the basic questions which a newbie has in mind while stepping into the field of quantum Computing. Later we will get our hands dirty with a little bit of coding with IBM’s Qiskit.

What makes Quantum Computers Special?

Classical computers stores information using classical bits that can only possess a value of 1 or 0 and not both at the same time. Here comes the Quantum Magic! In Quantum computers information is represented using Qubits (quantum bit).

Qubit

Qubit is a unit of computing information that is represented by a state of an atom or elementary particle (such as the spin) and can store multiple values at once due to the principles of quantum mechanics

Classial Bit Vs Qubit

Superposition

The Qubits can have both the values 1 and 0 simultaneously and this phenomenon in the language of Quantum Computing is called Superposition.

One more interesting fact about quantum mechanics dictates that a qubit cannot be measured directly without destroying its superposition. The register that said 1 and 0 will randomly collapse into 1 or 0.

Qubits can be represented using bra-ket notations: |0⟩ or |1⟩, pronounced ‘ket 0’, and ‘ket 1’ respectively.

A Quick Look at a Physical Qubit

Interaction of photons and semi-transparent mirror can be used to determine a random sequence of 1’s and 0’s. Photon cannot be split in two, when we send a photon through a mirror there is a 50/50 chance that it reflected or transmitted. If a photon is reflected and registered by the first detector then 1 is marked down if it is transmitted or registered by the second detector then a 0 is marked down. In this way, we generate a stream or sequence of random numbers.

Getting Started with Qiskit

The workflow of using Qiskit consists of three high-level steps:

  • Build: design a quantum circuit that represents the problem you are considering.
  • Execute: run experiments on different backends (which include both systems and simulators).
  • Analyze: calculate summary statistics and visualize the results of the experiments.

Workflow Step–by–Step

The program above can be broken down into six steps:

  1. Import packages
  2. Initialize variables
  3. Add gates
  4. Visualize the circuit
  5. Simulate the experiment
  6. Visualize the results

Step 1: Import Packages

The basic elements needed for your program are imported as follows:

import numpy as np
from qiskit import(
QuantumCircuit,
execute,
Aer)
from qiskit.visualization import plot_histogram

In more detail, the imports are

  • QuantumCircuit can be thought of as the instructions of the quantum system. It holds all your quantum operations.
  • execute: runs your circuit/experiment.
  • Aer: handles simulator backends.
  • plot_histogram: creates histograms.

Step 2: Initialize Variables

Consider the next line of code

circuit = QuantumCircuit(2, 2)

Here, you are initializing with 2 qubits in the zero state; with 2 classical bits set to zero; and circuit is the quantum circuit.

Syntax:QuantumCircuit(int, int)

Step 3: Add Gates

You can add gates (operations) to manipulate the registers of your circuit. Consider the following three lines of code:

circuit.h(0)
circuit.cx(0, 1)
circuit.measure([0,1], [0,1])

The gates are added to the circuit one-by-one to form the Bell state

|ψ⟩=(|00⟩+|11⟩)/2–√.|ψ⟩=(|00⟩+|11⟩)/2.

The code above applies the following gates:

  • QuantumCircuit.h(0): A Hadamard gate HH on qubit 0, which puts it into a superposition state.
  • QuantumCircuit.cx(0, 1): A controlled-Not operation (CXCX) on control qubit 0 and target qubit 1, putting the qubits in an entangled state.
  • QuantumCircuit.measure([0,1], [0,1]): if you pass the entire quantum and classical registers to, measure the ith qubit’s measurement result will be stored in the ith classical bit.

Step 4 : Visualize the Circuit

You can use QuantumCircuit.draw() to view the circuit that you have designed in the various forms used in many textbooks and research articles.

circuit.draw()┌───┐     ┌─┐   
q_0: ┤ H ├──■──┤M├───
└───┘┌─┴─┐└╥┘┌─┐
q_1: ─────┤ X ├─╫─┤M├
└───┘ ║ └╥┘
c_0: ═══════════╩══╬═

c_1: ══════════════╩═

In this circuit, the qubits are ordered with qubit zero at the top and qubit one at the bottom. The circuit is read left-to-right, meaning that gates which are applied earlier in the circuit show up farther to the left.

The default backend for QuantumCircuit.draw() or qiskit.visualization.circuit_drawer() is the text backend. However, depending on your local environment you may want to change these defaults to something better suited for your use case. This is done with the user config file. By default, the user config file should be located in ~/.qiskit/settings.conf and is a .ini file.

For example, a settings.conf file for setting a Matplotlib drawer is:

[default]
circuit_drawer = mpl

You can use any of the valid circuit drawer backends as the value for this config, this includes text, mpl, latex, and latex_source.

Step 5: Simulate the Experiment

Qiskit Aer is a high-performance simulator framework for quantum circuits. It provides several backends to achieve different simulation goals.

If you have issues installing Aer, you can alternatively use the Basic Aer provider by replacing Aer with Basic Aer. Basic Aer is included in Qiskit Terra.

import numpy as np
from qiskit import(
QuantumCircuit,
execute,
BasicAer)
...

To simulate this circuit, you will use the qasm_simulator. Each run of this circuit will yield either the bit string 00 or 11.

simulator = Aer.get_backend('qasm_simulator')
job = execute(circuit, simulator, shots=1000)
result = job.result()
counts = result.get_counts(circuit)
print("\nTotal count for 00 and 11 are:",counts)
Total count for 00 and 11 are: {'00': 461, '11': 539}

As expected, the output bit string is 00 approximately 50 percent of the time. The number of times the circuit is run can be specified via the shots argument of the execute method. The number of shots of the simulation was set to be 1000 (the default is 1024).

Once you have a result object, you can access the counts via the method get_counts(circuit). This gives you the aggregate outcomes of the experiment you ran.

Step 6: Visualize the Results

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

plot_histogram(counts)

The observed probabilities Pr(00)Pr(00) and Pr(11)Pr(11) are computed by taking the respective counts and dividing by the total number of shots.

Half Adder Circuit

Classical

The computer uses binary numbers 0 and 1. An adder circuit uses these binary numbers and calculates the addition. A binary adder circuit can be made using XOR and AND gates. The summation output provides two elements, the first one is the SUM and the second one is the Carry Out.

Quantum

For quantum computers, we use the same basic idea, but we have different conventions for how to represent inputs, outputs, and the symbols used for operations. Here is the quantum circuit that represents the same process as above.

Circuit Diagram would look like this

--

--