In [26]:
# Introduction to Quantum Computing through qiskit 2.1.1 - Session 1
# In this code session, we accomplish the following:
# 1. Define a quantum circuit with 1, 3, and 8 qubits
# 2. Draw the quantum circuit and visualize the quantum circuits
# 33. We measure the quantum circuit and simulate the quantum circuit using quantum simulator
In [1]:
import qiskit
In [2]:
import numpy as np # for numbers analysis
from qiskit import * # importing the required libraries from qiskit
In [3]:
from qiskit.circuit import QuantumCircuit # for drawing quantum circuits
from qiskit_aer import AerSimulator # for simulations
from qiskit.visualization import plot_histogram
In [4]:
# PART I
# We create a quantum circuit using 8 qubits
In [5]:
qcirc = QuantumCircuit(8)
In [6]:
#measure the 8 qubits and specify an output
qcirc.measure_all()
In [7]:
# draw the circuit
qcirc.draw('mpl')
Out[7]:
In [8]:
# the output, as expected is, 00000000
In [10]:
# We can initialize the quantum state too; In this cell, I create a quantum circuit with one qubit
# and initialize it as 0.
qcirc2 = QuantumCircuit(1) # an one qubit quantum circuit
qcirc2.initialize([1,0])
qcirc2.draw('mpl')
Out[10]:
In [12]:
# PART II- Here, I create a quantum circuit with 3 qubits and run the quantum circuit with 8 qubits on a local simulator
In [13]:
from qiskit_aer import Aer
In [14]:
# Run the quantum circuit on a statevector simulator backend
backend = Aer.get_backend('statevector_simulator')
In [16]:
# Create a Quantum Program for execution
# create a quantum circuit with a quantum register of three qubits
circ3 = QuantumCircuit (3)
In [17]:
# Add a Hamdard gate qubit 0, putting this qubit in superposition.
circ3.h(0)
# Add a CX (CNOT) gate between qubit 0 and qubit 1.
circ3.cx(0, 1)
# Add a CX (CNOT) gate between qubit 0 and qubit 2.
circ3.cx(0, 2)
Out[17]:
<qiskit.circuit.instructionset.InstructionSet at 0x285fcb2f1c0>
In [18]:
circ3.draw ('mpl')
Out[18]:
In [19]:
# Create a Quantum Program for execution
job = backend.run(circ3)
In [20]:
result = job.result() # store the result object
In [22]:
outputstate = result.get_statevector(circ3, decimals=3)
print(outputstate)
Statevector([0.707+0.j, 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j, 0. +0.j, 0.707+0.j], dims=(2, 2, 2))
In [23]:
# we plot the real and imaginary components of the state density matrix
from qiskit.visualization import plot_state_city
plot_state_city(outputstate)
Out[23]:
In [25]:
## End of code
## It is noted that the qiskit official documentation
## at https://github.com/Qiskit/qiskit-tutorials/blob/master/tutorials/circuits/1_getting_started_with_qiskit.ipynb
## has code for various versions of qiskit 0.45, qiskit 0.46, and qiskit 1.1;
## However, official documentation for running the quantum code in qiskit 2.1.1 is lacking which is placing serious constraints for
## professional developers.
## As of now, there is no authentic documentation on how to import and use IBMQ, sampler, and estimator in qiskit 2.1.1
## By B. Sundar, IFS, Special Secretary to Government, ITE&C department, Government of Andhra Pradesh
### End of Code####
In [ ]: