Sampler inputs and outputs
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
This page gives an overview of the inputs and outputs of the Qiskit Runtime Sampler primitive, which executes workloads on IBM Quantum® compute resources. Sampler lets you efficiently define vectorized workloads by using a data structure known as a Primitive Unified Bloc (PUB). They are used as inputs to the run() method for the Sampler primitive, which executes the defined workload as a job. Then, after the job has completed, the results are returned in a format that is dependent on both the PUBs used as well as the runtime options specified from the primitive.
Inputs
Each PUB is in the format:
(<single circuit>, <one or more optional parameter value>, <optional shots>),
There can be multiple parameter values items, and each item can be either an array or a single parameter, depending on the chosen circuit. Additionally, the input must contain measurements.
For the Sampler primitive, a PUB can contain at most three values:
- A single
QuantumCircuit, which may contain one or moreParameterobjects Note: These circuits should also include measurement instructions for each of the qubits to be sampled. - A collection of parameter values to bind the circuit against (only needed if any
Parameterobjects are used that must be bound at runtime) - (Optionally) a number of shots to measure the circuit with
The following code demonstrates an example set of vectorized inputs to the Sampler primitive and executes them on an IBM® backend as a single RuntimeJobV2 object.
from qiskit.circuit import (
Parameter,
QuantumCircuit,
ClassicalRegister,
QuantumRegister,
)
from qiskit.transpiler import generate_preset_pass_manager
from qiskit.quantum_info import SparsePauliOp
from qiskit.primitives.containers import BitArray
from qiskit_ibm_runtime import (
QiskitRuntimeService,
SamplerV2 as Sampler,
)
import numpy as np
# Instantiate runtime service and get
# the least busy backend
service = QiskitRuntimeService()
backend = service.least_busy(operational=True, simulator=False)
# Define a circuit with two parameters.
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.ry(Parameter("a"), 0)
circuit.rz(Parameter("b"), 0)
circuit.cx(0, 1)
circuit.h(0)
circuit.measure_all()
# Transpile the circuit
pm = generate_preset_pass_manager(optimization_level=1, backend=backend)
transpiled_circuit = pm.run(circuit)
layout = transpiled_circuit.layout
# Now define a sweep over parameter values, the last axis of dimension 2 is
# for the two parameters "a" and "b"
params = np.vstack(
[
np.linspace(-np.pi, np.pi, 100),
np.linspace(-4 * np.pi, 4 * np.pi, 100),
]
).T
sampler_pub = (transpiled_circuit, params)
# Instantiate the new Sampler object, then run the transpiled circuit
# using the set of parameters and observables.
sampler = Sampler(mode=backend)
job = sampler.run([sampler_pub])
result = job.result()Outputs
After one or more PUBs are sent to a QPU for execution and a job successfully completes, the data is returned as a PrimitiveResult container object accessed by calling the RuntimeJobV2.result() method. The PrimitiveResult contains an iterable list of SamplerPubResult objects that contain the execution results for each PUB. These data are samples of the circuit output.
Each element of this list corresponds to a PUB submitted to the primitive's run() method (for example, a job submitted with 20 PUBs will return a PrimitiveResult object that contains a list of 20 SamplerPubResult objects, one corresponding to each PUB).
Each SamplerPubResult object possesses both a data and a metadata attribute.
- The
dataattribute is a customizedDataBinthat contains the actual measurement values, standard deviations, and so forth. The data bins are dict-like objects that contain oneBitArrayperClassicalRegisterin the circuit. - The
BitArrayclass is a container for ordered shot data. It stores the sampled bitstrings as bytes inside a two-dimensional array. The left-most axis of this array runs over ordered shots, while the right-most axis runs over bytes. - The
metadataattribute contains information about the runtime options used (explained later in the Result metadata section of this page).
The following is a visual outline of the PrimitiveResult data structure:
└── PrimitiveResult
├── SamplerPubResult[0]
│ ├── metadata
│ └── data ## In the form of a DataBin object
│ ├── NAME_OF_CLASSICAL_REGISTER
│ │ └── BitArray of count data (default is 'meas')
| |
│ └── NAME_OF_ANOTHER_CLASSICAL_REGISTER
│ └── BitArray of count data (exists only if more than one
| ClassicalRegister was specified in the circuit)
├── SamplerPubResult[1]
| ├── metadata
| └── data ## In the form of a DataBin object
| └── NAME_OF_CLASSICAL_REGISTER
| └── BitArray of count data for second pub
├── ...
├── ...
└── ...
Put simply, a single job returns a PrimitiveResult object and contains a list of one or more SamplerPubResult objects. These SamplerPubResult objects then store the measurement data for each PUB that was submitted to the job.
As a first example, let us look at the following ten-qubit circuit:
# generate a ten-qubit GHZ circuit
circuit = QuantumCircuit(10)
circuit.h(0)
circuit.cx(range(0, 9), range(1, 10))
# append measurements with the `measure_all` method
circuit.measure_all()
# transpile the circuit
transpiled_circuit = pm.run(circuit)
# run the Sampler job and retrieve the results
sampler = Sampler(mode=backend)
job = sampler.run([transpiled_circuit])
result = job.result()
# the data bin contains one BitArray
data = result[0].data
print(f"Databin: {data}\n")
# to access the BitArray, use the key "meas", which is the default name of
# the classical register when this is added by the `measure_all` method
array = data.meas
print(f"BitArray: {array}\n")
print(f"The shape of register `meas` is {data.meas.array.shape}.\n")
print(f"The bytes in register `alpha`, shot by shot:\n{data.meas.array}\n")Output:
Databin: DataBin(meas=BitArray(<shape=(), num_shots=4096, num_bits=10>))
BitArray: BitArray(<shape=(), num_shots=4096, num_bits=10>)
The shape of register `meas` is (4096, 2).
The bytes in register `alpha`, shot by shot:
[[ 3 255]
[ 3 255]
[ 2 0]
...
[ 0 0]
[ 0 0]
[ 3 255]]
It can sometimes be convenient to convert away from the bytes format in the BitArray to bitstrings. The get_count method returns a dictionary mapping bitstrings to the number of times that they occurred.
# optionally, convert away from the native BitArray format to a dictionary format
counts = data.meas.get_counts()
print(f"Counts: {counts}")Output:
Counts: {'1111111111': 1345, '1000000000': 42, '0000000000': 1538, '0000000001': 36, '0000001111': 31, '1101111111': 57, '1110110111': 1, '1111110110': 4, '1111110000': 15, '1111111101': 33, '1111111110': 94, '1111110111': 50, '0000000101': 4, '0001000000': 11, '1111000000': 19, '1111011111': 68, '0000000010': 20, '0000001101': 4, '0000011111': 54, '0000100001': 1, '1111011000': 2, '0000000111': 38, '0000100000': 40, '0001110111': 2, '1101111101': 3, '1110000000': 32, '1100000000': 35, '0010000000': 28, '0000001000': 30, '1101000000': 1, '1111100000': 62, '0000111111': 21, '0111111111': 26, '1111111000': 27, '1110111111': 15, '0000000011': 17, '0011111111': 18, '0001111111': 43, '1111111100': 15, '1111001111': 4, '1111111011': 14, '0111111110': 1, '0011111000': 1, '1111011101': 2, '1111011110': 6, '1111110101': 1, '1000000001': 1, '0010000001': 4, '0001100000': 2, '1000011111': 3, '0010000010': 2, '0100100000': 1, '0001110000': 1, '0011111011': 1, '0000010110': 2, '1111101111': 12, '0011011111': 2, '1011111111': 7, '0000001010': 1, '0001000001': 1, '1101011111': 4, '1101100000': 3, '1110100111': 1, '0000100010': 1, '1101111000': 3, '0101111110': 1, '1111101000': 1, '0011110000': 2, '1111100010': 2, '0001111101': 3, '0001000011': 1, '0000010111': 1, '0000010000': 6, '0000111110': 4, '0111011111': 4, '1011100000': 2, '1110011111': 2, '0000011110': 6, '0011111110': 1, '0000011011': 2, '1110100001': 1, '0011110111': 1, '0000001110': 2, '0011111100': 2, '1100000001': 2, '0111110000': 2, '0010000011': 1, '1111010111': 2, '1101110111': 2, '0000110000': 1, '0000011100': 1, '0111011101': 1, '0010011110': 1, '0000011101': 3, '0100000000': 3, '1111111010': 2, '0000000100': 1, '1100000010': 1, '1110000111': 2, '1110111000': 1, '1111101011': 1, '1011111110': 2, '1000001000': 1, '1111100111': 2, '1111110011': 1, '1001111111': 2, '0111011110': 1, '1110100000': 2, '1110111101': 1, '0010011111': 3, '0001011111': 3, '1101110000': 1, '1111101100': 1, '1000001111': 1, '0000011000': 1, '1111100110': 1, '1011111101': 2, '1111101110': 1, '1110000011': 2, '1111100011': 2, '0111111101': 1, '1111110001': 1, '1010000000': 1, '1111111001': 1, '0000101111': 2, '0011101111': 1, '1111100101': 1, '1111000001': 1, '1111100001': 2, '1100111111': 1, '0000111000': 1, '0000000110': 2, '1110001000': 1, '0000001011': 1, '1001000000': 1, '1111011011': 2, '1101111110': 1, '0111111000': 1, '0111100000': 2, '0000110111': 1, '1110111110': 1, '1001011110': 1, '0011110001': 1, '0000001001': 1, '0001111011': 1, '0001111000': 1, '1100001101': 1}
When a circuit contains more than one classical register, the results are stored in different BitArray objects. The following example modifies the previous snippet by splitting the classical register into two distinct registers:
# generate a ten-qubit GHZ circuit with two classical registers
circuit = QuantumCircuit(
qreg := QuantumRegister(10),
alpha := ClassicalRegister(1, "alpha"),
beta := ClassicalRegister(9, "beta"),
)
circuit.h(0)
circuit.cx(range(0, 9), range(1, 10))
# append measurements with the `measure_all` method
circuit.measure([0], alpha)
circuit.measure(range(1, 10), beta)
# transpile the circuit
transpiled_circuit = pm.run(circuit)
# run the Sampler job and retrieve the results
sampler = Sampler(mode=backend)
job = sampler.run([transpiled_circuit])
result = job.result()
# the data bin contains two BitArrays, one per register, and can be accessed
# as attributes using the registers' names
data = result[0].data
print(f"BitArray for register 'alpha': {data.alpha}")
print(f"BitArray for register 'beta': {data.beta}")Output:
BitArray for register 'alpha': BitArray(<shape=(), num_shots=4096, num_bits=1>)
BitArray for register 'beta': BitArray(<shape=(), num_shots=4096, num_bits=9>)
Use BitArray objects for performant post-processing
Since arrays generally offer better performance compared to dictionaries, it is advisable to perform any post-processing directly on the BitArray objects rather than on dictionaries of counts. The BitArray class offers a range of methods to perform some common post-processing operations:
print(f"The shape of register `alpha` is {data.alpha.array.shape}.")
print(f"The bytes in register `alpha`, shot by shot:\n{data.alpha.array}\n")
print(f"The shape of register `beta` is {data.beta.array.shape}.")
print(f"The bytes in register `beta`, shot by shot:\n{data.beta.array}\n")
# post-select the bitstrings of `beta` based on having sampled "1" in `alpha`
mask = data.alpha.array == "0b1"
ps_beta = data.beta[mask[:, 0]]
print(f"The shape of `beta` after post-selection is {ps_beta.array.shape}.")
print(f"The bytes in `beta` after post-selection:\n{ps_beta.array}")
# get a slice of `beta` to retrieve the first three bits
beta_sl_bits = data.beta.slice_bits([0, 1, 2])
print(
f"The shape of `beta` after bit-wise slicing is {beta_sl_bits.array.shape}."
)
print(f"The bytes in `beta` after bit-wise slicing:\n{beta_sl_bits.array}\n")
# get a slice of `beta` to retrieve the bytes of the first five shots
beta_sl_shots = data.beta.slice_shots([0, 1, 2, 3, 4])
print(
f"The shape of `beta` after shot-wise slicing is {beta_sl_shots.array.shape}."
)
print(
f"The bytes in `beta` after shot-wise slicing:\n{beta_sl_shots.array}\n"
)
# calculate the expectation value of diagonal operators on `beta`
ops = [SparsePauliOp("ZZZZZZZZZ"), SparsePauliOp("IIIIIIIIZ")]
exp_vals = data.beta.expectation_values(ops)
for o, e in zip(ops, exp_vals):
print(f"Exp. val. for observable `{o}` is: {e}")
# concatenate the bitstrings in `alpha` and `beta` to "merge" the results of the two
# registers
merged_results = BitArray.concatenate_bits([data.alpha, data.beta])
print(f"\nThe shape of the merged results is {merged_results.array.shape}.")
print(f"The bytes of the merged results:\n{merged_results.array}\n")Output:
The shape of register `alpha` is (4096, 1).
The bytes in register `alpha`, shot by shot:
[[1]
[0]
[0]
...
[0]
[1]
[0]]
The shape of register `beta` is (4096, 2).
The bytes in register `beta`, shot by shot:
[[ 0 255]
[ 1 255]
[ 1 254]
...
[ 0 0]
[ 1 255]
[ 0 0]]
The shape of `beta` after post-selection is (0, 2).
The bytes in `beta` after post-selection:
[]
The shape of `beta` after bit-wise slicing is (4096, 1).
The bytes in `beta` after bit-wise slicing:
[[7]
[7]
[6]
...
[0]
[7]
[0]]
The shape of `beta` after shot-wise slicing is (5, 2).
The bytes in `beta` after shot-wise slicing:
[[ 0 255]
[ 1 255]
[ 1 254]
[ 0 0]
[ 0 255]]
Exp. val. for observable `SparsePauliOp(['ZZZZZZZZZ'],
coeffs=[1.+0.j])` is: 0.11962890625
Exp. val. for observable `SparsePauliOp(['IIIIIIIIZ'],
coeffs=[1.+0.j])` is: 0.04931640625
The shape of the merged results is (4096, 2).
The bytes of the merged results:
[[ 1 255]
[ 3 254]
[ 3 252]
...
[ 0 0]
[ 3 255]
[ 0 0]]
Result metadata
In addition to the execution results, both the PrimitiveResult and SamplerPubResult objects contain a metadata attribute about the job that was submitted. The metadata containing information for all submitted PUBs (such as the various runtime options available) can be found in the PrimitiveResult.metatada, while the metadata specific to each PUB is found in SamplerPubResult.metadata.
The Sampler result metadata also includes execution timing information called the execution span.
In the metadata field, primitive implementations can return any information about execution that is relevant to them, and there are no key-value pairs that are guaranteed by the base primitive. Thus, the returned metadata might be different in different primitive implementations.
# Print out the results metadata
print("The metadata of the PrimitiveResult is:")
for key, val in result.metadata.items():
print(f"'{key}' : {val},")
print("\nThe metadata of the PubResult result is:")
for key, val in result[0].metadata.items():
print(f"'{key}' : {val},")Output:
The metadata of the PrimitiveResult is:
'execution' : {'execution_spans': ExecutionSpans([DoubleSliceSpan(<start='2026-04-14 19:58:08', stop='2026-04-14 19:58:10', size=4096>)])},
'version' : 2,
The metadata of the PubResult result is:
'circuit_metadata' : {},
View execution spans
The results of SamplerV2 jobs executed in Qiskit Runtime contain execution timing information in their metadata.
This timing information can be used to place upper and lower timestamp bounds on when particular shots were executed on the QPU.
Shots are grouped into ExecutionSpan objects, each of which indicates a start time, a stop time, and a specification of which shots were collected in the span.
An execution span specifies which data was executed during its window by providing an ExecutionSpan.mask method. This method, given any Primitive Unified Block (PUB) index, returns a boolean mask that is True for all shots executed during its window. PUBs are indexed by the order in which they were given to the Sampler run call. If, for example, a PUB has shape (2, 3) and was run with four shots, then the mask's shape is (2, 3, 4). See the execution_span API page for full details.
To view execution span information, review the metadata of the result returned by SamplerV2, which comes in the form of an ExecutionSpans object. This object is a list-like container containing instances of subclasses of ExecutionSpan, such as SliceSpan.
Example:
# Define two circuits, each with one parameter with two parameters.
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.ry(Parameter("a"), 0)
circuit.cx(0, 1)
circuit.h(0)
circuit.measure_all()
pm = generate_preset_pass_manager(optimization_level=1, backend=backend)
transpiled_circuit = pm.run(circuit)
params = np.random.uniform(size=(2, 3)).T
sampler_pub = (transpiled_circuit, params)
# Instantiate the new Estimator object, then run the transpiled circuit
# using the set of parameters and observables.
job = sampler.run([sampler_pub], shots=4)
result = job.result()
spans = job.result().metadata["execution"]["execution_spans"]
print(spans)Output:
ExecutionSpans([DoubleSliceSpan(<start='2026-04-14 20:16:08', stop='2026-04-14 20:16:09', size=24>)])
from qiskit.primitives import BitArray
# Get the mask of the 1st PUB for the 0th span.
mask = spans[0].mask(0)
# Decide whether the 0th shot of parameter set (1, 2) occurred in this span.
in_this_span = mask[2, 1, 0]
# Create a new bit array containing only the PUB-1 data collected during this span.
bits = result[0].data.meas
filtered_data = BitArray(bits.array[mask], bits.num_bits)Execution spans can be filtered to include information pertaining to specific PUBs, selected by their indices:
# take the subset of spans that reference data in PUBs 0 or 2
spans.filter_by_pub([0, 2])Output:
ExecutionSpans([DoubleSliceSpan(<start='2026-04-14 20:16:08', stop='2026-04-14 20:16:09', size=24>)])
View global information about the collection of execution spans:
print("Number of execution spans:", len(spans))
print(" Start of the first span:", spans.start)
print(" End of the last span:", spans.stop)
print(" Total duration (s):", spans.duration)Output:
Number of execution spans: 1
Start of the first span: 2026-04-14 20:16:08.452553
End of the last span: 2026-04-14 20:16:09.434031
Total duration (s): 0.981478
Extract and inspect a particular span:
spans.sort()
print(" Start of first span:", spans[0].start)
print(" End of first span:", spans[0].stop)
print("#shots in first span:", spans[0].size)Output:
Start of first span: 2026-04-14 20:16:08.452553
End of first span: 2026-04-14 20:16:09.434031
#shots in first span: 24
It is possible for time windows specified by distinct execution spans to overlap. This is not because a QPU was performing multiple executions at once, but is instead an artifact of certain classical processing that might happen concurrently with quantum execution. The guarantee being made is that the referenced data definitely occurred in the reported execution span, but not necessarily that the limits of the time window are as tight as possible.