Quantum Computing : Basics of Qiskit and Cirq (Part 1)

Quantum Computing: Quantum computing is a type of computing that uses quantum-mechanical phenomena, such as superposition and entanglement, to perform operations on data. Quantum computers use quantum bits, or qubits, which can represent and process a much larger amount of information than traditional bits. This allows them to solve complex problems much faster than classical computers in certain scenarios.

Cirq: Cirq is an open-source Python library developed by Google Quantum AI for designing, simulating, and running quantum circuits on quantum computers and simulators. It provides tools to create quantum circuits that work with noisy intermediate-scale quantum (NISQ) devices, which are characterized by having a modest number of qubits that can perform a limited set of operations.

Qiskit: Qiskit is an open-source software development kit (SDK) created by IBM. It allows users to work with quantum computers at the level of circuits, operators, and algorithms. Qiskit provides tools for building and running quantum programs on both simulators and real quantum machines provided by IBM and other providers.

Both Cirq and Qiskit are instrumental in advancing the field of quantum computing by making it more accessible to researchers and developers. They each have their own set of features and are compatible with different quantum processors and simulators, catering to a wide range of quantum computing applications.

Installing Qiskit and Cirq

Firstly install qiskit by IBM: Check the documentation

pip install qiskit

Secondly install cirq by Google: Check documentation

!pip install cirq --quiet

import cirq

import cirq_web

print("Libraries imported successfully!")

Create a qubit and apply X gate

In qiskit:

import qiskit

from qiskit import *

qc= QuantumCircuit(1)

qc.x(0)

qc.draw()

To get more good drawing, you may do this:

pip install pylatexenc

Now, if you write: qc.draw('mpl') , you will see this

Note: We have just used MatPlotLib

In Cirq:

!pip install cirq --quiet

import cirq

import cirq_web

qubit = cirq.NamedQubit("q0")

qc = cirq.Circuit()

qc.append( cirq.X(qubit) )

print( qc )

H gate

In cirq:

qc.append(cirq.H(qubit)) #adding H gate to the circuit

print(qc)

In qiskit:

Bloch Sphere

In qiskit:

from qiskit import QuantumCircuit from qiskit.quantum_info import Statevector from qiskit.visualization import plot_bloch_multivector

qc = QuantumCircuit(1) # one qubit creation qc.x(0) #applying x gate to the first qubit

state = Statevector(qc) #creating state vector for the circuit plot_bloch_multivector(state) #bloch sphere

In cirq:

state_vector =cirq.final_state_vector(qc) #creating state vector

ket = cirq.dirac_notation( state_vector=state_vector ) #generating ket notation print( state_vector) # state_vector = [0.+0.j 1.+0.j]

print(ket) # ket= 0.71|0⟩ - 0.71|1⟩ as we have applied X gate & H gate cirq_web.bloch_sphere.BlochSphere( state_vector=state_vector )

Histogram

In qiskit:

from qiskit.visualization import plot_histogram

counts1 = {'1': 1}

legend = ['First execution']

plot_histogram([counts1], legend=legend, color=['lightblue'], title="New Histogram")

In cirq:

qc.append(cirq.measure(qubit)) #histogram is not possible without measurement print(qc)

sim = cirq.Simulator() result =sim.run(qc, repetitions = 100) #100 times repetition

hist = cirq.plot_state_histogram(result, plt.subplot(), title = 'Qubit States', xlabel = 'States', ylabel = 'Occurrences')

plt.show()

Restart the kernel if you get errors

Try the qiskit code

Try the cirq code

Done!

1
Subscribe to my newsletter

Read articles from Md Shahriyar Al Mustakim Mitul directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Md Shahriyar Al Mustakim Mitul
Md Shahriyar Al Mustakim Mitul