Introduction to Qiskit
In this notebook we will explore how we can program quantum gates and quantum circuits with Qiskit and even how we can execute them on simulators and real quantum computers using Qiskit patterns. Later we will introduce different ways of encoding information and we will finish with a bonus example of Quantum Teleportation.
Before you begin
Follow the Install and set up instructions if you haven't already, including the steps to Set up to use IBM Quantum™ Platform.
It is recommended that you use the Jupyter development environment to interact with quantum computers. Be sure to install the recommended extra visualization support ('qiskit[visualization]'). You'll also need the matplotlib package for the second part of this example.
To learn about quantum computing in general, visit the Basics of quantum information course in IBM Quantum Learning
Imports
# Added by doQumentation — required packages for this notebook
!pip install -q matplotlib numpy qiskit qiskit-aer qiskit-ibm-runtime
# Import necessary modules for this notebook
import time
import qiskit
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
from qiskit.visualization import plot_bloch_multivector, plot_state_qsphere
from qiskit_aer import AerSimulator
from qiskit.quantum_info import SparsePauliOp
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import EstimatorV2 as Estimator
from qiskit_ibm_runtime import SamplerV2 as Sampler
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit.visualization import plot_histogram
print(qiskit.__version__)
2.3.1
To execute your quantum circuits on hardware you need to first set up your account. You can do it as follows:
- Go to the upgraded IBM Quantum® Platform.
- Go to the top right corner (as shown in the above picture), create your API token, and copy it to a secure location.
- In the next cell, replace
deleteThisAndPasteYourAPIKeyHerewith your API key. - Go to the bottom left corner (as shown in the above picture) and create your instance. Make sure to choose the open plan.
- After the instance is created, copy its associated CRN code. You may need to refresh to see the instance.
- In the cell below, replace
deleteThisAndPasteYourCRNHerewith your CRN code.
See this guide for more details on how to set up your IBM Cloud® account.
⚠️ Note: Treat your API key as you would a secure password. See the Cloud setup guide for more information about using your API key in both secure and untrusted environments.
#your_api_key = "deleteThisAndPasteYourAPIKeyHere"
#your_crn = "deleteThisAndPasteYourCRNHere"
QiskitRuntimeService.save_account(
channel="ibm_quantum_platform",
token=your_api_key,
instance=your_crn,
overwrite=True
)
1. Quantum Gates and Quantum Circuits
Quantum circuits are models for quantum computation in which a computation is a sequence of quantum gates. Let's take a look at some of the popular quantum gates.
X Gate
An X gate equates to a rotation around the X-axis of the Bloch sphere by radians. It maps to and to . It is the quantum equivalent of the NOT gate for classical computers and is sometimes called a bit-flip.
# Let's apply an X-gate on a |0> qubit
qc = QuantumCircuit(1)
qc.x(0)
qc.draw(output='mpl')

# Let's see Bloch sphere visualization
sv = Statevector(qc)
plot_bloch_multivector(sv)

H Gate
A Hadamard gate represents a rotation of about the axis that is in the middle of the -axis and -axis. It maps the basis state to