Skip to main contentIBM Quantum Documentation

Primitives examples

The examples in this section illustrate some common ways to use primitives. Before running these examples, follow the instructions in Install and set up.

Note

These examples all use the primitives from Qiskit Runtime, but you could use the base primitives instead.


Estimator examples

Efficiently calculate and interpret expectation values of the quantum operators required for many algorithms with Estimator. Explore uses in molecular modeling, machine learning, and complex optimization problems.

Run a single experiment

Use Estimator to determine the expectation value of a single circuit-observable pair.

import numpy as np
from qiskit.circuit.library import IQP
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.quantum_info import SparsePauliOp, random_hermitian
from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator
 
n_qubits = 50
 
service = QiskitRuntimeService()
backend = service.least_busy(
    operational=True, simulator=False, min_num_qubits=n_qubits
)
 
mat = np.real(random_hermitian(n_qubits, seed=1234))
circuit = IQP(mat)
observable = SparsePauliOp("Z" * 50)
 
pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_circuit = pm.run(circuit)
isa_observable = observable.apply_layout(isa_circuit.layout)
 
estimator = Estimator(backend)
job = estimator.run([(isa_circuit, isa_observable)])
result = job.result()
 
print(f" > Expectation value: {result[0].data.evs}")
print(f" > Metadata: {result[0].metadata}")

Output:

 > Expectation value: 0.313953488372093
 > Metadata: {'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}

Run multiple experiments in a single job

Use Estimator to determine the expectation values of multiple circuit-observable pairs.

import numpy as np
from qiskit.circuit.library import IQP
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.quantum_info import SparsePauliOp, random_hermitian
from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator
 
n_qubits = 50
 
service = QiskitRuntimeService()
backend = service.least_busy(
    operational=True, simulator=False, min_num_qubits=n_qubits
)
 
rng = np.random.default_rng()
mats = [np.real(random_hermitian(n_qubits, seed=rng)) for _ in range(3)]
 
pubs = []
circuits = [IQP(mat) for mat in mats]
observables = [
    SparsePauliOp("X" * 50),
    SparsePauliOp("Y" * 50),
    SparsePauliOp("Z" * 50),
]
 
# Get ISA circuits
pm = generate_preset_pass_manager(optimization_level=1, backend=backend)
 
for qc, obs in zip(circuits, observables):
    isa_circuit = pm.run(qc)
    isa_obs = obs.apply_layout(isa_circuit.layout)
    pubs.append((isa_circuit, isa_obs))
 
estimator = Estimator(backend)
job = estimator.run(pubs)
job_result = job.result()
 
for idx in range(len(pubs)):
    pub_result = job_result[idx]
    print(f">>> Expectation values for PUB {idx}: {pub_result.data.evs}")
    print(f">>> Standard errors for PUB {idx}: {pub_result.data.stds}")

Output:

>>> Expectation values for PUB 0: 0.2222222222222222
>>> Standard errors for PUB 0: 1.4996172247918447
>>> Expectation values for PUB 1: -0.01708984375
>>> Standard errors for PUB 1: 0.015752037506918087
>>> Expectation values for PUB 2: 0.13970588235294118
>>> Standard errors for PUB 2: 0.1307160956701727

Run parameterized circuits

Use Estimator to run three experiments in a single job, leveraging parameter values to increase circuit reusability.

import numpy as np
 
from qiskit.circuit import QuantumCircuit, Parameter
from qiskit.quantum_info import SparsePauliOp
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator
 
service = QiskitRuntimeService()
backend = service.least_busy(operational=True, simulator=False)
 
# Step 1: Map classical inputs to a quantum problem
theta = Parameter("θ")
 
chsh_circuit = QuantumCircuit(2)
chsh_circuit.h(0)
chsh_circuit.cx(0, 1)
chsh_circuit.ry(theta, 0)
 
number_of_phases = 21
phases = np.linspace(0, 2 * np.pi, number_of_phases)
individual_phases = [[ph] for ph in phases]
 
ZZ = SparsePauliOp.from_list([("ZZ", 1)])
ZX = SparsePauliOp.from_list([("ZX", 1)])
XZ = SparsePauliOp.from_list([("XZ", 1)])
XX = SparsePauliOp.from_list([("XX", 1)])
ops = [ZZ, ZX, XZ, XX]
 
# Step 2: Optimize problem for quantum execution.
 
pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
chsh_isa_circuit = pm.run(chsh_circuit)
isa_observables = [
    operator.apply_layout(chsh_isa_circuit.layout) for operator in ops
]
 
# Step 3: Execute using Qiskit primitives.
 
# Reshape observable array for broadcasting
reshaped_ops = np.fromiter(isa_observables, dtype=object)
reshaped_ops = reshaped_ops.reshape((4, 1))
 
estimator = Estimator(backend, options={"default_shots": int(1e4)})
job = estimator.run([(chsh_isa_circuit, reshaped_ops, individual_phases)])
# Get results for the first (and only) PUB
pub_result = job.result()[0]
print(f">>> Expectation values: {pub_result.data.evs}")
print(f">>> Standard errors: {pub_result.data.stds}")
print(f">>> Metadata: {pub_result.metadata}")

Output:

>>> Expectation values: [[ 1.0608189   1.00486071  0.83804693  0.60944046  0.29464245 -0.04296317
  -0.36624773 -0.64259105 -0.8812753  -1.02448586 -1.06108411 -0.99690456
  -0.84149459 -0.61209251 -0.29384684  0.04004591  0.35007024  0.62455713
   0.88498817  1.03482884  1.06426656]
 [ 0.0506541   0.34503135  0.63092204  0.86615863  1.00247386  1.05339317
   1.02209901  0.85661126  0.6229659   0.29543806 -0.01750351 -0.36359568
  -0.64656912 -0.86271097 -1.02369024 -1.06744902 -1.0098996  -0.84096418
  -0.59193695 -0.30021175  0.02148158]
 [-0.0617927  -0.37367346 -0.61766181 -0.85555044 -1.00220866 -1.04225457
  -0.96826245 -0.82054342 -0.59087613 -0.29915093  0.02599006  0.34131848
   0.62482233  0.84653348  1.00220866  1.04145896  0.98550076  0.84043378
   0.59856707  0.2919904  -0.04667603]
 [ 1.03323761  0.96932327  0.8306212   0.606258    0.31161555 -0.02545965
  -0.34025766 -0.63861298 -0.85979372 -0.99478293 -1.0435806  -0.99292649
  -0.81709576 -0.59379338 -0.29411204  0.02678568  0.34476614  0.63808257
   0.86536302  0.99610895  1.03562445]]
>>> Standard errors: [[0.02232532 0.02137189 0.02033785 0.01821782 0.01422721 0.01266797
  0.01360232 0.01570656 0.02104389 0.02130923 0.02259611 0.02102648
  0.01956931 0.0175328  0.01595523 0.01117634 0.01412317 0.01768652
  0.01981311 0.02182993 0.02284472]
 [0.01339107 0.01472212 0.01540346 0.02114485 0.02058292 0.02291643
  0.02166682 0.01916034 0.0174089  0.0132438  0.01370239 0.01444912
  0.01528583 0.01947702 0.02272965 0.02281042 0.02125164 0.01814792
  0.0171921  0.01474575 0.01250523]
 [0.01285004 0.01351385 0.0180921  0.0192617  0.02250482 0.02157521
  0.02027414 0.01928141 0.01744964 0.01253114 0.01198848 0.01750823
  0.01615378 0.020237   0.02247042 0.02155206 0.02197293 0.0196541
  0.02070502 0.01240781 0.01214034]
 [0.02174709 0.01993771 0.01925381 0.01573731 0.01436638 0.01376879
  0.0152804  0.01649184 0.01898698 0.02144559 0.02203927 0.0208232
  0.01958299 0.01905422 0.01513992 0.0135359  0.0143405  0.01949263
  0.01937938 0.02179858 0.02326307]]
>>> Metadata: {'shots': 10016, 'target_precision': 0.01, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}

Use sessions and advanced options

Explore sessions and advanced options to optimize circuit performance on QPUs.

import numpy as np
from qiskit.circuit.library import IQP
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.quantum_info import SparsePauliOp, random_hermitian
from qiskit_ibm_runtime import (
    QiskitRuntimeService,
    Session,
    EstimatorV2 as Estimator,
)
 
n_qubits = 50
 
service = QiskitRuntimeService()
backend = service.least_busy(
    operational=True, simulator=False, min_num_qubits=n_qubits
)
 
rng = np.random.default_rng(1234)
mat = np.real(random_hermitian(n_qubits, seed=rng))
circuit = IQP(mat)
mat = np.real(random_hermitian(n_qubits, seed=rng))
another_circuit = IQP(mat)
observable = SparsePauliOp("X" * 50)
another_observable = SparsePauliOp("Y" * 50)
 
pm = generate_preset_pass_manager(optimization_level=1, backend=backend)
isa_circuit = pm.run(circuit)
another_isa_circuit = pm.run(another_circuit)
isa_observable = observable.apply_layout(isa_circuit.layout)
another_isa_observable = another_observable.apply_layout(
    another_isa_circuit.layout
)
 
with Session(backend=backend) as session:
    estimator = Estimator(mode=session)
 
    estimator.options.resilience_level = 1
 
    job = estimator.run([(isa_circuit, isa_observable)])
    another_job = estimator.run(
        [(another_isa_circuit, another_isa_observable)]
    )
    result = job.result()
    another_result = another_job.result()
 
    # first job
    print(f" > Expectation value: {result[0].data.evs}")
    print(f" > Metadata: {result[0].metadata}")
 
    # second job
    print(f" > Another Expectation value: {another_result[0].data.evs}")
    print(f" > More Metadata: {another_result[0].metadata}")

Output:

 > Expectation value: -0.6176470588235294
 > Metadata: {'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}
 > Another Expectation value: -0.038461538461538464
 > More Metadata: {'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}

Sampler examples

Generate entire error-mitigated quasi-probability distributions sampled from quantum circuit outputs. Leverage Sampler’s capabilities for search and classification algorithms like Grover’s and QVSM.

Run a single experiment

Use Sampler to return the measurement outcome as bitstrings or counts of a single circuit.

import numpy as np
from qiskit.circuit.library import IQP
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.quantum_info import random_hermitian
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler
 
n_qubits = 127
 
service = QiskitRuntimeService()
backend = service.least_busy(
    operational=True, simulator=False, min_num_qubits=n_qubits
)
 
mat = np.real(random_hermitian(n_qubits, seed=1234))
circuit = IQP(mat)
circuit.measure_all()
 
pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_circuit = pm.run(circuit)
 
sampler = Sampler(backend)
job = sampler.run([isa_circuit])
result = job.result()
 
# Get results for the first (and only) PUB
pub_result = result[0]
 
print(f" > First ten results: {pub_result.data.meas.get_bitstrings()[:10]}")

Output:

 > First ten results: ['0110101110001010110010111001000100001010111101111101000101101010100001010001010010010010000110100101010111100110010001000110001', '1010110000100111111111000111010101001100011100011010000000001011101000010000000100010000101011010101000000100100100000000010110', '0110101011000011010011101110000001100000101000010101110001011001011000001001000000010111101100010000001100000010101101110111010', '0111111100110010000010010000010001000100110001111111000000010000010010111010000011111111001110110001000100100101000100000111100', '1100000101011000110011100110011101001000101000100011101001111001110001100011110001111001110010110101000101111010000000111010011', '0101110100001101110101101110100001110000011001111101000100001011101001000010110001010001000010011110010000110000000001110001101', '1000100010101011111110101100010000000100000000101010110101100010101000010001111010010011100010001101010000110010010001000000001', '0100110000110011110100110010001001100110001001011010010100100010011001001110110000010100001000101001110000000100110010100111011', '1000001010010010010110100100010100011100101001010001000100100001001110000100010101011001000110110110110001001011110001000111110', '0001000100000100000010110101000001001010001000010110110000011001101000100000110000001011001010110101010001110001000101010010000']

Run multiple experiments in a single job

Use Sampler to return the measurement outcome as bitstrings or counts of multiple circuits in one job.

import numpy as np
from qiskit.circuit.library import IQP
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.quantum_info import random_hermitian
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler
 
n_qubits = 127
 
service = QiskitRuntimeService()
backend = service.least_busy(
    operational=True, simulator=False, min_num_qubits=n_qubits
)
 
rng = np.random.default_rng()
mats = [np.real(random_hermitian(n_qubits, seed=rng)) for _ in range(3)]
circuits = [IQP(mat) for mat in mats]
for circuit in circuits:
    circuit.measure_all()
 
pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_circuits = pm.run(circuits)
 
sampler = Sampler(backend)
job = sampler.run(isa_circuits)
result = job.result()
 
for idx, pub_result in enumerate(result):
    print(
        f" > First ten results for pub {idx}: {pub_result.data.meas.get_bitstrings()[:10]}"
    )

Output:

 > First ten results for pub 0: ['1001001111010010110000000000000000000110001001000000000001101000000001101001101011010000001010000001010110001100011000000100110', '0011100001001001010110000011101101010101011111011010100100111000011110001000110101000011000110000010001011010100010001000101111', '1111000011010001010001001101010111001110110001001111001000000001011001100011111111101000001011001000001010000001010010100100101', '0011101100110010100111010111010000010111001000010100001000001001100000010010010011100011000100110111011010110001000010000100000', '1010111100100100100111001001011110100001011011000000100001101010001011010110011011011100101100100000000000001000000000000001001', '0111010110000000001100010011101010011110001111000010011110100110110100101100100001110010101010010110010001010000000000000100001', '0111011111111001011110000001101011110101010010000101100110101101101101101001101011111111010110100000000001000000110000011100011', '0011100010001110000001110001101111011000000010000001001111100010101101010110110000110001110010000010011110110001101110000000111', '1001110001010010110000010011011010010000000111001110100111111011011111111010100001001001000000001010000011110000010000000111001', '1011010000110101010111001100000101000111011000010111001000001010000101100110001101111010001100101011011110101000010100000111111']
 > First ten results for pub 1: ['1110001111011101101100000001100011000101111000001001101000100100100110001001000010100111110100001000111100010101100000001110110', '0010001010000101000100111001010011011001100000010001000000011100100000001011000110100010111100111100001010010111011100000001100', '0001100000010001011110010100000110010101101110111010000010001101000111010010001000001110110101001110000111010110001101001100100', '0110101100100110000100011010111110010011011110000001000011000110000111000010110011001101101101011000100100010000110101010110011', '0100000010011011011110101001010000010001010101111101001010001100110011110100010001010001011001111110001000000000111110000011001', '0101100100010001111100101011011010000010101010101101010100001000100000010010100110001010100010110100100000000110010101000100100', '0010101010001001111100011110011110011111101000111001111000010000110000010001100100101011001010001100000101000000110100000010000', '1010001110011001111110110010010101100111101100110000001000101000011100000000100101011010001100101101100010000100010011000110001', '0011101011001001110000001100000000011011111110111001100011101110001011011010000011011000000010011000110001001101000000000010010', '1101010011111010011101001011101011010011000110110001111010100000110011000000000000111110100110111010100010010011010101000101101']
 > First ten results for pub 2: ['0111110001111010100100011000110010000110010001100100100010011010011000000010000100000001001100000010000110001000001011011010001', '0001011101000010100000101000101000111011101001000010010010011100100010001000100001010111000001110001101111010011011100001010000', '1001111101010101111101100101111100100000011101100000001101100100011000000011001011001100111110001011001110000111001010111010101', '1100101101100111000011001110100001110010010011000101001110101001001000011010001001001000010000001000010001100010100100000111000', '1100110110110110101010000001100010100110000100001001101010001001001010100010011111001000001100011111001011101000111011000000101', '1000100110101110000011001001110110010001000001100100011001001000011111010000000101000010011110010110000110001000011001011000100', '1100101010110001100010000010110010000111001100110010101110110000111110111101000111011100000000101011011110001001100001011111001', '1001010001101101101101101001011010001110111111101001100001010000100010100110110000000111010111010010000101101000010100001011000', '0001001001110001010110101100001000010100000110111001000111000010100001111010000001001110101000001100001101011011100000000100100', '1111101000100110010110110111100011000100100100000111000110110110000001011011101000001011011000100011001111101010110100001011001']

Run parameterized circuits

Run several experiments in a single job, leveraging parameter values to increase circuit reusability.

import numpy as np
from qiskit.circuit.library import RealAmplitudes
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler
 
n_qubits = 127
 
service = QiskitRuntimeService()
backend = service.least_busy(
    operational=True, simulator=False, min_num_qubits=n_qubits
)
 
# Step 1: Map classical inputs to a quantum problem
circuit = RealAmplitudes(num_qubits=n_qubits, reps=2)
circuit.measure_all()
 
# Define three sets of parameters for the circuit
rng = np.random.default_rng(1234)
parameter_values = [
    rng.uniform(-np.pi, np.pi, size=circuit.num_parameters) for _ in range(3)
]
 
# Step 2: Optimize problem for quantum execution.
 
pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_circuit = pm.run(circuit)
 
# Step 3: Execute using Qiskit primitives.
sampler = Sampler(backend)
job = sampler.run([(isa_circuit, parameter_values)])
result = job.result()
# Get results for the first (and only) PUB
pub_result = result[0]
# Get counts from the classical register "meas".
print(
    f" >> First ten results for the meas output register: {pub_result.data.meas.get_bitstrings()[:10]}"
)

Output:

 >> First ten results for the meas output register: ['0101100001111110110001100100111111100010001010101001000010011101101010110101100000010000100011010101100000111010111001011101110', '0001011011110010000011100111111100010111101011110011111000101001010111101111011001101000101100110011110100110010001001100000001', '0110011001111101110101100100110100010000110001101011110000101110001011110100100010000111110010111000100100110100001110110000110', '0000111011110001000010000100110100101000011001001011101100111000110110101101101100001000110111011000001001001100010111010011001', '0010010011011011011010011101111000100001111010001001111110111110000110110110001110001011101100110110101100101011100010010011101', '1111001001011110110011000110111010110101100111010100001001001001010010100100100010100010110101010010111010001111100110111000101', '0101001011010101101101010100011110100000000100011001000000101000000000100011011010010010110010101101110101011100010100010110101', '0000010001010111111001001111010111100001000001001101101000001011001011000111101001111100001111110001010011011100011100101000110', '0101001001011110101011000111110110000001100001010001001100010010010001010101001000101000010100100010110010101111111100010101101', '1101101110111111110010001100010011110110101011011111011101111011001010100001111101010000001011111010101100100011111001000000001']

Use sessions and advanced options

Explore sessions and advanced options to optimize circuit performance on QPUs.

import numpy as np
from qiskit.circuit.library import IQP
from qiskit.quantum_info import random_hermitian
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import Session, SamplerV2 as Sampler
from qiskit_ibm_runtime import QiskitRuntimeService
 
n_qubits = 127
 
service = QiskitRuntimeService()
backend = service.least_busy(
    operational=True, simulator=False, min_num_qubits=n_qubits
)
 
rng = np.random.default_rng(1234)
mat = np.real(random_hermitian(n_qubits, seed=rng))
circuit = IQP(mat)
circuit.measure_all()
mat = np.real(random_hermitian(n_qubits, seed=rng))
another_circuit = IQP(mat)
another_circuit.measure_all()
 
pm = generate_preset_pass_manager(backend=backend, optimization_level=1)
isa_circuit = pm.run(circuit)
another_isa_circuit = pm.run(another_circuit)
 
with Session(backend=backend) as session:
    sampler = Sampler(mode=session)
    job = sampler.run([isa_circuit])
    another_job = sampler.run([another_isa_circuit])
    result = job.result()
    another_result = another_job.result()
 
# first job
 
print(
    f" > The first ten measurement results of job 1: {result[0].data.meas.get_bitstrings()[:10]}"
)

Output:

 > The first ten measurement results of job 1: ['1100100101001101010001010001110000100101110111101011110101000110111000110000001000100001100011000001001111011101100001000100111', '0000101111110000100000001101110100100000010100001010001101001010011101010000100001001000000000000011000111010011100010011001001', '1000110110111011100001000011110001000110110110110100100100101110000101110101001000001001101110010010110010100000110010001001010', '1010000000101011101111100111000110110000110010111001111100100000101101011011100000010011000010001010111100010110101001011011100', '1000010001000100010010000011110000101001010011011001000001010101001100100010100110001000001101011001001100011101111001010101011', '0101001100000001000011010110010101001011000101001001001001101100111111001010001001110000000000010001100011110000110000000001100', '0000111001010111010111000011110100110000100100000000101001110100001100001000100000100000000010101010111010101010000001010000111', '1001011010001111110110000011111100100110010001100011110000001011001010010001000010011001110010000000001101010000100000001010011', '0001010001010001100001011011100000100001010011100101111000010010001001101001010100000000000100010000110100100010000000000110001', '1101011011110011010001010001000100100110100111111000110000010001000100000010101000000100000101001000110010101000110000011001110']
# second job
print(
    " > The first ten measurement results of job 2:",
    another_result[0].data.meas.get_bitstrings()[:10],
)

Output:

 > The first ten measurement results of job 2: ['1001000001001010011100010000000010110011100010011110111001010110101100101101011001100110000100111100010000001100100000000100001', '0011111100110111110000101110010111011001110010001110011100110000010110011110000001110100100101010100100110000100100000010100000', '1010110100000010110100010011101010110100101101101000010111110000101100000110010110001010111100001111001110010101000101000100001', '1000111100000000001101011001111010000101101110111010011001010100100000000000000001011110111000110000001010000100101001010100000', '0101011101111001110110101111100000110100001000101010010100110111100100100000000001011010001000010100001110011000000110000010100', '0001000010011010100001011001000011110100001100111010110000100011000110010101100010101111100001000000000101011001100001010000011', '0101101111000000010111110010011011000100000111010000111010010001101101010010100101000111000100100001111100010000000001000000101', '0001001001101011110101110111010110111011100010100000110101000001000100000010000011001000011001110000001100110000100000010000000', '1001010100000001001001011011100000111100101001011000110001110000000101110000010011000110100100100000011110000101100001011000111', '0101000100110010010101110011100001100000110000001110011110000110100111101000100000001100010110000100110001100000100000000000001']

Next steps

Recommendations
Was this page helpful?
Report a bug or request content on GitHub.