Skip to main contentIBM Quantum Documentation Preview
This is a preview build of IBM Quantum® documentation. Refer to quantum.cloud.ibm.com/docs for the official documentation.

Estimator examples

Package versions

The code on this page was developed using the following requirements. We recommend using these versions or newer.

qiskit[all]~=2.3.0
qiskit-ibm-runtime~=0.43.1

The examples in this section illustrate some common ways to use Estimator. Before running these examples, follow the instructions in Install Qiskit.

Note

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

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 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(mode=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.007735697018533441
 > 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 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.0185546875
>>> Standard errors for PUB 0: 0.015601139924387178
>>> Expectation values for PUB 1: -0.09142077434760362
>>> Standard errors for PUB 1: 0.03358006320017227
>>> Expectation values for PUB 2: -0.001953125
>>> Standard errors for PUB 2: 0.013875265857795725

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 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: [[ 0.99448627  0.94805756  0.80317513  0.56626438  0.29038667 -0.02010529
  -0.30613927 -0.5911369  -0.81581866 -0.94101035 -0.99303537 -0.94826483
  -0.8098078  -0.59300234 -0.33287723 -0.01222899  0.29100849  0.57351886
   0.80711328  0.95531205  0.99635171]
 [ 0.00808357  0.31152832  0.61663123  0.81208779  0.95178844  0.99199902
   0.9370722   0.79281158  0.56688619  0.28748488 -0.01948347 -0.31857553
  -0.57434795 -0.79737154 -0.95178844 -0.99842442 -0.95427569 -0.79882244
  -0.58761329 -0.27546316  0.00538905]
 [-0.01906893 -0.32313549 -0.58450423 -0.8033824  -0.94930119 -0.99448627
  -0.94121762 -0.80296786 -0.57186069 -0.29764116  0.02445798  0.31753918
   0.6054386   0.81644048  0.94183943  0.98681724  0.93955945  0.80027334
   0.57082434  0.29432482 -0.01513078]
 [ 0.99075539  0.93375586  0.78970252  0.56688619  0.29536118 -0.00870538
  -0.31401557 -0.60688949 -0.81084416 -0.94743575 -0.99344991 -0.94722848
  -0.80110242 -0.57103161 -0.30075022 -0.00103636  0.30551746  0.58533331
   0.80669874  0.94142489  0.99407173]]
>>> Standard errors: [[0.00359184 0.00438386 0.00852894 0.01096216 0.00952795 0.01084166
  0.01092139 0.01112474 0.00835954 0.00493214 0.00305847 0.00505781
  0.0081545  0.00942587 0.01243794 0.01279255 0.01449143 0.01001158
  0.00686416 0.00608612 0.00306803]
 [0.01380567 0.01025795 0.00809922 0.00742183 0.0050425  0.00375401
  0.00555313 0.0079511  0.01037733 0.01328277 0.01431205 0.01218068
  0.01144855 0.00865455 0.00559959 0.00383215 0.0044145  0.00880595
  0.01191713 0.01291607 0.01212646]
 [0.01005963 0.00753399 0.00858187 0.00673876 0.00409901 0.0026568
  0.00439918 0.00771471 0.00939481 0.00991522 0.01046032 0.01036158
  0.00773218 0.00618488 0.00375851 0.00374272 0.0040234  0.00837196
  0.01109589 0.01055713 0.00823229]
 [0.00376252 0.00513786 0.00521975 0.01071544 0.00701596 0.01081168
  0.01136338 0.01045901 0.00709637 0.00554858 0.00372386 0.00470944
  0.0063878  0.00980035 0.01048143 0.00968916 0.01234788 0.00880073
  0.00779767 0.00458181 0.00341765]]
>>> Metadata: {'shots': 10016, 'target_precision': 0.01, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}

Use batches and advanced options

Explore the batch execution mode and advanced options to optimize circuit performance on QPUs.

import numpy as np
from qiskit.circuit.library import iqp
from qiskit.transpiler import generate_preset_pass_manager
from qiskit.quantum_info import SparsePauliOp, random_hermitian
from qiskit_ibm_runtime import (
    QiskitRuntimeService,
    Batch,
    EstimatorV2 as Estimator,
)

n_qubits = 15

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" * n_qubits)
another_observable = SparsePauliOp("Y" * n_qubits)

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
)

# The context manager automatically closes the batch.
with Batch(backend=backend) as batch:
    estimator = Estimator(mode=batch)

    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.04995359041431103
 > Metadata: {'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}
 > Another Expectation value: 0.0
 > More Metadata: {'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}

Next steps

Recommendations