Saltar al contenido principal

Hands-on with Qiskit: Hello World - Qiskit Patterns

In this notebook you will walk through a Qiskit Patterns workflow, and use Qiskit's Sampler primitive.

This tutorial partially involves content from IBM Quantum Documentation: Hello World.

Useful links:

  1. Please create an IBM Cloud account to access the IBM Quantum Platform.
  2. We will use a cloud-based platform to set up a coding environment. You may either use QBraid or Google Colab.
  3. After this notebook, we will be looking at the Quantum Teleportation protocol.

More links - Qiskit community and further learning resources:

Install Qiskit

You can use an online jupyter lab environment (see guide Online lab environments) or install Qiskit locally.

Follow the Qiskit installation guide Install the Qiskit SDK and the Qiskit Runtime client to complete the following steps:

  • Install Qiskit including the extra visualization packages: pip install qiskit[visualization]

  • Install qiskit-ibm-runtime: pip install qiskit-ibm-runtime

  • Install jupyter: pip install jupyter Check that the version of Python you are using in your environment is python>=3.10, to make sure that it is compatible with the latest Qiskit version:

# Added by doQumentation — required packages for this notebook
!pip install -q IPython
from platform import python_version

print(python_version())
3.13.7

If you need to upgrade Python and are unsure how to do it, please refer to this guide on how to upgrade Python depending on your OS: How to update Python

%pip install qiskit[visualization]
%pip install qiskit-ibm-runtime
%pip install
%pip install qiskit-aer
zsh:1: no matches found: qiskit[visualization]
Note: you may need to restart the kernel to use updated packages.
Requirement already satisfied: qiskit-ibm-runtime in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (0.43.0)
Requirement already satisfied: requests>=2.19 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-ibm-runtime) (2.32.5)
Requirement already satisfied: requests-ntlm>=1.1.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-ibm-runtime) (1.3.0)
Requirement already satisfied: numpy>=1.13 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-ibm-runtime) (2.3.3)
Requirement already satisfied: urllib3>=1.21.1 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-ibm-runtime) (2.5.0)
Requirement already satisfied: python-dateutil>=2.8.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-ibm-runtime) (2.9.0.post0)
Requirement already satisfied: ibm-platform-services>=0.22.6 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-ibm-runtime) (0.69.0)
Requirement already satisfied: pydantic>=2.5.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-ibm-runtime) (2.12.2)
Requirement already satisfied: qiskit>=1.4.1 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-ibm-runtime) (2.2.1)
Requirement already satisfied: packaging in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-ibm-runtime) (25.0)
Requirement already satisfied: ibm_cloud_sdk_core<4.0.0,>=3.24.2 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from ibm-platform-services>=0.22.6->qiskit-ibm-runtime) (3.24.2)
Requirement already satisfied: PyJWT<3.0.0,>=2.10.1 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from ibm_cloud_sdk_core<4.0.0,>=3.24.2->ibm-platform-services>=0.22.6->qiskit-ibm-runtime) (2.10.1)
Requirement already satisfied: six>=1.5 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from python-dateutil>=2.8.0->qiskit-ibm-runtime) (1.17.0)
Requirement already satisfied: charset_normalizer<4,>=2 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from requests>=2.19->qiskit-ibm-runtime) (3.4.4)
Requirement already satisfied: idna<4,>=2.5 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from requests>=2.19->qiskit-ibm-runtime) (3.11)
Requirement already satisfied: certifi>=2017.4.17 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from requests>=2.19->qiskit-ibm-runtime) (2025.10.5)
Requirement already satisfied: annotated-types>=0.6.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from pydantic>=2.5.0->qiskit-ibm-runtime) (0.7.0)
Requirement already satisfied: pydantic-core==2.41.4 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from pydantic>=2.5.0->qiskit-ibm-runtime) (2.41.4)
Requirement already satisfied: typing-extensions>=4.14.1 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from pydantic>=2.5.0->qiskit-ibm-runtime) (4.15.0)
Requirement already satisfied: typing-inspection>=0.4.2 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from pydantic>=2.5.0->qiskit-ibm-runtime) (0.4.2)
Requirement already satisfied: rustworkx>=0.15.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit>=1.4.1->qiskit-ibm-runtime) (0.17.1)
Requirement already satisfied: scipy>=1.5 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit>=1.4.1->qiskit-ibm-runtime) (1.16.2)
Requirement already satisfied: dill>=0.3 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit>=1.4.1->qiskit-ibm-runtime) (0.4.0)
Requirement already satisfied: stevedore>=3.0.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit>=1.4.1->qiskit-ibm-runtime) (5.5.0)
Requirement already satisfied: cryptography>=1.3 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from requests-ntlm>=1.1.0->qiskit-ibm-runtime) (46.0.2)
Requirement already satisfied: pyspnego>=0.4.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from requests-ntlm>=1.1.0->qiskit-ibm-runtime) (0.12.0)
Requirement already satisfied: cffi>=2.0.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibm-runtime) (2.0.0)
Requirement already satisfied: pycparser in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from cffi>=2.0.0->cryptography>=1.3->requests-ntlm>=1.1.0->qiskit-ibm-runtime) (2.23)
Note: you may need to restart the kernel to use updated packages.
ERROR: You must give at least one requirement to install (see "pip help install")
Note: you may need to restart the kernel to use updated packages.
Requirement already satisfied: qiskit-aer in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (0.17.2)
Requirement already satisfied: qiskit>=1.1.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-aer) (2.2.1)
Requirement already satisfied: numpy>=1.16.3 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-aer) (2.3.3)
Requirement already satisfied: scipy>=1.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-aer) (1.16.2)
Requirement already satisfied: psutil>=5 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-aer) (7.1.0)
Requirement already satisfied: python-dateutil>=2.8.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit-aer) (2.9.0.post0)
Requirement already satisfied: six>=1.5 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from python-dateutil>=2.8.0->qiskit-aer) (1.17.0)
Requirement already satisfied: rustworkx>=0.15.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit>=1.1.0->qiskit-aer) (0.17.1)
Requirement already satisfied: dill>=0.3 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit>=1.1.0->qiskit-aer) (0.4.0)
Requirement already satisfied: stevedore>=3.0.0 in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit>=1.1.0->qiskit-aer) (5.5.0)
Requirement already satisfied: typing-extensions in /Users/astricornish/miniforge3/envs/um-qiskit/lib/python3.13/site-packages (from qiskit>=1.1.0->qiskit-aer) (4.15.0)
Note: you may need to restart the kernel to use updated packages.

Make the necessary imports

Let's make the necessary imports for this tutorial.

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
import qiskit_ibm_runtime
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import SamplerV2 as Sampler
from qiskit.quantum_info import SparsePauliOp
from qiskit.quantum_info import Statevector
from qiskit.visualization import plot_bloch_multivector, plot_state_qsphere
from IPython.display import display, Latex

Set up your IBM Quantum Platform account

In order to execute quantum circuits on real hardware, you will need an IBM Cloud account.

Follow the instructions in this guide Set up your IBM Cloud account to complete the following steps:

  1. Set up an IBM Cloud account if you do not already have one.
  2. Log into or create an IBM Quantum Platform account with an IBMid.
  3. Access your IBM Quantum Platform dashboard, create your API token, and copy it to a secure location. (See first reference image below.)
  4. In the code cell following the reference images, replace deleteThisAndPasteYourAPIKeyHere with your API key.
  5. Go to the Instances page from the ☰ main menu and create your instance. If you are not part of a Network institution, choose the open plan. (See second reference image below.)
  6. After the instance is created, copy its associated CRN code. (CRN stands for Cloud Resource Names) You may need to refresh to see the instance.
  7. In the code cell following the reference images, replace deleteThisAndPasteYourCRNHere with your CRN code.

Note: Treat your API key as you would a secure password. Refer to the Set up your IBM Cloud account guide for more information about using your API key in both secure and untrusted environments.

#@title personals
your_api_key = "YOUR_API_KEY"
your_crn = "YOUR_CRN"
from qiskit_ibm_runtime import QiskitRuntimeService
# Save your API key to access real devices

your_api_key = your_api_key
your_crn = your_crn

QiskitRuntimeService.save_account(
channel="ibm_cloud",
token=your_api_key,
instance=your_crn,
set_as_default=True,
overwrite=True,
)

Create and run a simple quantum algorithm using the Qiskit pattern framework

The Qiskit pattern conceptual framework can be considered the anatomy of a quantum algorithm.

The four steps to writing a quantum program using Qiskit patterns are:

  1. Map the problem to a quantum-native format.

  2. Optimize the circuits and operators.

  3. Execute using a Qiskit primitive function.

  4. Analyze the results.

Step 1. Map the problem to a quantum-native format

In a quantum program, quantum circuits are the native format in which to represent quantum instructions, and operators represent the observables to be measured. When creating a circuit, you'll usually create a new QuantumCircuit object, then add instructions to it in sequence.

Demonstration: Building basic quantum circuits in Qiskit

Let's try building some simple circuits with Qiskit.

# Simple quantum circuit with two qubits and two classical bits

# Create quantum circuit with 2 qubits and 2 classical bits
qc = QuantumCircuit(2)

# Add gates to your circuit
qc.x(0)
qc.s(1)

# Draw the output using MatPlotLib
qc.draw(output='mpl')

Quantum circuit diagram

# Quantum circuit with a Quantum Register named 'qr' that has two qubits, and a Classical Register named 'cr' with two classical bits

# Create a quantum register with 2 qubits, register label is 'qr'
qreg = QuantumRegister(2)

# Create a classical register with 2 qubits, register label is 'cr'
creg = ClassicalRegister(2)

# Create a quantum circuit with registers qreg and creg
qc = QuantumCircuit(qreg, creg)

# Add gates to your registers
qc.x(qreg[0])

# Draw the quantum circuit
qc.draw(output='mpl')

Quantum circuit diagram

Exercise: Building basic quantum circuits in Qiskit

Create a circuit for the Bell state 00+112\frac{|00\rangle + |11\rangle}{\sqrt{2}}

# Create a new circuit with two qubits
qc = QuantumCircuit(2)

# Add a Hadamard gate to qubit 0
qc.h(0)

# Perform a controlled-X gate on qubit 1, controlled by qubit 0
qc.cx(0,1)

# Return a drawing of the circuit using MatPlotLib ("mpl").
qc.draw('mpl')

Quantum circuit diagram

The initial state of the quantum circuit is the 00\ket{00} state.

The final state is:

# Use Statevector to fetch the statevector of the circuit
sv = Statevector(qc)
sv.draw(output='latex')

2200+2211\frac{\sqrt{2}}{2} |00\rangle+\frac{\sqrt{2}}{2} |11\rangle

A note on bit numbering in Qiskit

Qiskit numbers the bits in a string from right to left. The Qiskit SDK uses the LSb 0 bit numbering. When displaying or interpreting a list of nn bits (or qubits) as a string, bit n1n−1 is the leftmost bit, and bit 00 is the rightmost bit. This is because we usually write numbers with the most significant digit on the left, and in Qiskit, bit n1n−1 is interpreted as the most significant bit. For more details, see the Bit-ordering in the Qiskit SDK topic.

#LSB ordering example
qc2 = QuantumCircuit(2)
qc2.x(1)

qc2.draw("mpl")

Quantum circuit diagram

sv2 = Statevector(qc2)
sv2.draw(output='latex')

10 |10\rangle

Do we need measurement gates?

When creating quantum circuits, you must also consider what type of data you want returned after execution. Qiskit provides two ways to return data: you can obtain the expectation value of an observable, or you can obtain a probability distribution for a set of qubits you choose to measure. Prepare your workload to measure your circuit in one of these two ways with Qiskit primitives.

  • Sampler primitive - returns a probability distribution for a set of qubits you choose to measure. E.g.:
  • Estimator primitive - returns the expectation value of an observable. E.g.:

We'll be using the Sampler today, so we need to add measurement gates to our circuit.

# Use measure_all, which adds a barrier, applies measurement gates on all qubits, creates a classical register called `meas`
qc.measure_all()
qc.draw('mpl')

Quantum circuit diagram

Step 2. Optimize the circuits for the target hardware

When executing circuits on a device, it is important to optimize the set of instructions that the circuit contains and minimize the overall depth (roughly the number of instructions) of the circuit. This ensures that you obtain the best results possible by reducing the effects of error and noise. Additionally, the circuit's instructions must conform to a backend device's Instruction Set Architecture (ISA) and must consider the device's basis gates and qubit connectivity.

The following code instantiates a simulator to submit a job to and transforms the circuit and observables to match that backend's ISA. Note that we will use a real device later.

# option:
from qiskit_ibm_runtime.fake_provider import FakeTorino
backend = FakeTorino()
print(
f"Name: {backend.name}\n"
f"Version: {backend.version}\n"
f"Native gate set: {backend.operation_names}\n"
)

#to view other properties you can use properties()
# refer to https://docs.quantum.ibm.com/guides/get-qpu-information
Name: fake_torino
Version: 2
Native gate set: ['for_loop', 'delay', 'cz', 'id', 'sx', 'measure', 'reset', 'switch_case', 'if_else', 'rz', 'x']
# Convert to an ISA circuit
pm = generate_preset_pass_manager(backend=backend, optimization_level=3)

isa_circuit_sampler = pm.run(qc)

isa_circuit_sampler.draw("mpl", idle_wires=False)

Quantum circuit diagram

Step 3. Execute using the Qiskit primitives

Quantum computers can produce random results, so you usually collect a sample of the outputs by running the circuit many times. You can estimate the value of the observable by using the Estimator class. Sampler can be used to get data from a quantum computer. These objects possess a run() method that executes the selection of circuits, observables, and parameters (if applicable), using a primitive unified bloc (PUB).

# Create a sampler instance using the selected backend
sampler = Sampler(backend)

# Run the sampler primitive on ISA circuit for specified number of shots (1024)

job_sampler = sampler.run([isa_circuit_sampler], shots=1024)

# Save the result of the job

result_sampler = job_sampler.result()

Step 4. Post-process the results

This step involves postprocessing your results. You might feed these results into another workflow for further analysis or prepare a plot of the key values and data. In general, this step is specific to your problem.

  • For the Sampler, we plot the probability distribution obtained by sampling the quantum circuit as many times as the shots you specified using plot_histogram.
from qiskit.visualization import plot_histogram

counts = result_sampler[0].data.meas.get_counts()
# Note: meas is the default name of the classical register when using measure_all().
# If you specify a classical register, then use the name you assign

# Plot the result
plot_histogram(counts)

Code output

Run a program on a real device

If you'd like to run this code on a real device, you can use the following code.

from qiskit_ibm_runtime import QiskitRuntimeService

# View the list of backends you have access to

service = QiskitRuntimeService()

service.backends()
management.get:WARNING:2025-11-03 14:24:36,838: Loading default saved account
[<IBMBackend('ibm_fez')>,
<IBMBackend('ibm_brisbane')>,
<IBMBackend('ibm_torino')>,
<IBMBackend('ibm_marrakesh')>]
# Get backend
backend_real = service.least_busy(simulator=False, operational=True)

#backend_real = service.backend(name="insert_backend_name") # use this if you want to choose a specific backend

sampler = Sampler(backend_real)

pm = generate_preset_pass_manager(backend=backend_real, optimization_level=3)
isa_circuit = pm.run(qc)

job = sampler.run([isa_circuit], shots=1024)
print(job.job_id)
<bound method BasePrimitiveJob.job_id of <RuntimeJobV2('d444lcg7i53s73e4n6tg', 'sampler')>>
result = job.result()
print(
f"Name: {backend_real.name}\n"
f"Version: {backend_real.version}\n"
f"Native gate set: {backend_real.operation_names}\n"
)
Name: ibm_fez
Version: 2
Native gate set: ['delay', 'cz', 'id', 'sx', 'measure', 'reset', 'if_else', 'rz', 'x']
counts = result[0].data.meas.get_counts()

plot_histogram(counts)

Code output

Fetching results from a completed job

The cell below demonstrates how you can fetch results from a completed job.

service = QiskitRuntimeService()
retrieved_job = service.job('d40mquhsg33c73dhhs00')
result = retrieved_job.result()
counts = result[0].data.meas.get_counts()
plot_histogram(counts)
management.get:WARNING:2025-11-03 14:24:55,135: Loading default saved account

Code output