Qiskit primitives provide a consistent way to run quantum circuits for two common tasks: estimating observable values and collecting measurement samples. This guide explains when to choose Estimator or Sampler, how to make shot counts and parameter sweeps comparable, and how to structure experiments that can move from a local simulator to managed quantum hardware without changing the scientific question.
Overview
Qiskit primitives sit between a circuit and the backend that executes it. Instead of treating every backend as a separate programming target, you describe a circuit, its parameters, and the result you need. The primitive then submits that workload to a compatible simulator or quantum device.
Use Estimator when the output you need is an expectation value such as the average energy of a Hamiltonian, the mean value of a Pauli observable, or an objective function used by VQE and related hybrid algorithms. Conceptually, it evaluates:
<O> = <psi(theta)|O|psi(theta)>
Use Sampler when the output you need is a distribution of measurement results. Sampling is the natural choice for studying bitstrings, measuring a classifier’s output, evaluating a QAOA solution distribution, or inspecting how often each computational-basis result occurs.
The distinction is practical:
| Question | Primitive | Typical output |
|---|---|---|
| What is the average value of this observable? | Estimator | Expectation value and uncertainty information |
| Which bitstrings does this circuit produce? | Sampler | Counts, probabilities, or sampled bit arrays |
| How does an objective change as parameters move? | Estimator | One value per circuit-parameter-observable combination |
| How stable is a measurement distribution? | Sampler | One distribution per circuit and parameter set |
Qiskit has introduced multiple generations of primitive interfaces, and method names, result objects, and provider integration can differ between releases. Treat the examples below as workflow templates: confirm the exact constructor, submission method, and result accessors against the Qiskit version pinned by your project.
How to estimate
A repeatable experiment starts by separating the quantum workload from backend configuration. Build the circuit and define its parameters first. Then choose the primitive, set shots deliberately, submit a batch, and normalize the returned data into a format your analysis code controls.
1. Define the circuit and observable
For an Estimator experiment, keep the observable explicit rather than hiding it inside the circuit. A simple Pauli observable might be represented as Z on one qubit, or as a weighted sum such as 0.5 * ZI - 0.2 * XX. The exact operator class depends on the Qiskit release, but the experimental meaning is stable.
For a Sampler experiment, add the measurements required by your question. A circuit intended for sampling should expose the relevant classical registers or measurement data. Do not compare a measured circuit with an unmeasured circuit and assume they represent the same workload.
2. Choose the primitive by the result you need
An Estimator workflow is conceptually:
circuits = [parameterized_circuit]
observables = [observable]
parameter_values = [[0.1, 0.7]]
job = estimator.run(circuits, observables, parameter_values=parameter_values, shots=shots)
result = job.result()
value = extract_expectation_value(result)A Sampler workflow is conceptually:
circuits = [measured_parameterized_circuit]
parameter_values = [[0.1, 0.7]]
job = sampler.run(circuits, parameter_values=parameter_values, shots=shots)
result = job.result()
samples = extract_measurements(result)Newer primitive interfaces commonly group circuits, observables, parameter values, and options into a structured publication or batch object. Older interfaces may accept separate positional arguments. The important design choice is to submit related cases together when the interface supports batching, while retaining an index that maps every returned result to its circuit, observable, and parameter vector.
3. Make shot selection part of the experiment
Shots are repeated circuit executions used to estimate a distribution or expectation value. More shots generally reduce sampling uncertainty, but they also increase execution work. Use a fixed shot count when comparing parameter values, backends, or software versions. If you change shots between runs, record that change beside the result.
For a parameter sweep, do not silently use a different shot count for each point. A simple experiment record should include the parameter vector, observable, shots, backend identifier, transpilation settings, optimization level, and any error-mitigation or resilience options.
4. Normalize result handling
Primitive result objects are useful for preserving backend-specific metadata, but they are not always convenient for analysis. Convert them into a small internal schema such as:
{
"circuit_id": "ansatz_01",
"observable_id": "hamiltonian_01",
"parameters": [0.1, 0.7],
"shots": 4096,
"backend": "local-or-remote-id",
"value": 0.42,
"counts": null
}For Sampler results, store counts or bit arrays according to the interface you use, then derive probabilities in your own analysis layer. This makes later comparisons less dependent on whether a release returns quasi-probabilities, counts, or structured measurement arrays.
Inputs and assumptions
A useful Qiskit primitives experiment has four groups of inputs.
- Quantum definition: circuit structure, measurements, observables, parameter order, and parameter values.
- Statistical definition: shots, repetitions, random seeds where supported, and the uncertainty measure used in analysis.
- Compilation definition: target backend, basis gates or target instruction set, qubit layout, routing choices, and optimization settings.
- Execution definition: simulator or hardware mode, authentication and provider configuration, batching, queue behavior, and runtime options.
Keep the parameter order explicit. A frequent source of misleading results is passing [theta_2, theta_1] to a circuit whose parameters were defined as [theta_1, theta_2]. Bind values by a stable parameter list or named mapping, and test one known parameter assignment before launching a sweep.
Also distinguish three kinds of comparison:
- Algorithm comparison: same logical circuit and inputs, possibly on different backends.
- Backend comparison: same compiled workload and shot budget, with backend-specific noise and connectivity acknowledged.
- Statistical comparison: same circuit and backend, repeated with different shot counts or seeds.
These comparisons answer different questions. A circuit transpiled independently for two devices may have different depth and gate counts, so a difference in output cannot be attributed only to the hardware.
Worked examples
Example 1: estimating an observable
Suppose a two-qubit variational circuit prepares a state and you want the expectation value of Z ⊗ I. Use Estimator, choose a parameter vector such as [0.1, 0.7], and start with a fixed shot budget. Submit the circuit, observable, and parameter vector as one logical experiment.
For a parameter sweep, create a table before execution:
| Point | Parameters | Shots | Purpose |
|---|---|---|---|
| A | [0.1, 0.7] | 4,096 | Baseline |
| B | [0.2, 0.7] | 4,096 | Change first parameter |
| C | [0.1, 0.8] | 4,096 | Change second parameter |
After execution, record the expectation value for each row and preserve any standard-error or metadata fields returned by the primitive. If the result will drive a classical optimizer, pass the value through a function that also logs the parameter vector and evaluation number. That record is essential when an optimizer revisits a point or when a later SDK upgrade changes result formatting.
Example 2: sampling a measured circuit
Suppose a two-qubit circuit prepares a candidate solution for a small optimization problem. Add measurements, run it with Sampler, and inspect the frequency of bitstrings such as 00, 01, 10, and 11. Convert returned data to counts or probabilities, then apply the problem’s objective function to each bitstring.
For example, if the objective is a cut value, keep the raw distribution and a derived summary:
{
"counts": {"00": 1200, "01": 1700, "10": 1600, "11": 596},
"shots": 5096,
"best_bitstring": "01",
"best_probability": 0.3336
}The numbers in this illustration are an assumed local result, not a hardware benchmark. In a real run, calculate the probability as count / shots and state whether bitstrings were normalized for register order. Register and endianness conventions can make a correct result appear reversed if they are not documented.
Example 3: simulator-to-hardware progression
First run a small, shot-based simulator with the same measurements and shot count planned for hardware. Then transpile for the target device and compare logical depth, two-qubit gate count, and mapped qubits. Finally, submit to hardware only after the simulator result and data extraction have been tested. This progression separates circuit bugs from compilation effects and device noise.
When to recalculate
Recalculate and revalidate the experiment whenever an input that affects interpretation changes. The most obvious triggers are a Qiskit upgrade, a changed primitive interface, a new provider integration, a different backend, modified transpilation settings, or a changed shot budget. Also revisit results when a simulator, runtime option, error-mitigation setting, or observable definition changes.
Do not treat a rerun as a simple replacement of the old result. Keep the previous environment and compare both the numerical output and the execution metadata. A useful regression check runs a small deterministic or high-confidence circuit, verifies parameter binding, checks measurement-register order, and confirms that the normalized result schema is still populated.
Before publishing or using a result in a decision, use this practical checklist:
- Write down whether the task requires an expectation value or a measurement distribution.
- Pin the Qiskit and provider versions used by the experiment.
- Record circuit, observable, parameter order, shots, backend, and transpilation settings.
- Run a small simulator test and inspect raw primitive results.
- Batch comparable cases, but preserve an index for every circuit and parameter vector.
- Store raw results alongside normalized values and derived metrics.
- Recalculate after SDK, backend, pricing, latency, or execution-policy changes relevant to your project.
Estimator and Sampler are not competing versions of the same tool. Estimator answers an observable question; Sampler answers a distribution question. Once that distinction is reflected in your circuit design, shot policy, metadata, and result schema, Qiskit experiments become easier to debug, compare, and rerun across simulators and quantum hardware.