This practical Qiskit primitives tutorial shows how to structure a hybrid quantum-classical workflow: prepare a problem, define a parameterised circuit, submit measurements, return results to a classical optimiser, and validate the outcome. Use the checklist when moving from a simulator to hardware or when Qiskit APIs and backend requirements change.
Overview
Hybrid quantum-classical computing divides a workload between a classical program and a quantum circuit. The classical side prepares data, chooses circuit parameters, controls the optimisation loop and records results. The quantum side evaluates a circuit or samples its output. Neither part is expected to perform the entire application alone.
A typical workflow looks like this:
- Define the business, scientific or optimisation objective.
- Encode the relevant inputs in a circuit or observable.
- Build a parameterised ansatz, rather than recreating a circuit for every trial.
- Submit the circuit through a Qiskit primitive or compatible execution interface.
- Collect expectation values or sampled outcomes.
- Pass those results to a classical optimiser.
- Repeat until the stopping rule is reached, then validate independently.
Qiskit primitives provide a useful abstraction for this loop. An estimator-style primitive is suited to evaluating expectation values, which is common in variational quantum eigensolvers and quantum optimisation. A sampler-style primitive is suited to obtaining measurement samples, counts or quasi-probability-style results, depending on the interface and execution mode. Exact class names, option names and backend setup can vary between Qiskit and provider releases, so treat the code pattern below as a workflow template and check the documentation for the version installed in your environment.
Start with a small simulator experiment. Once the circuit, objective function and stopping criteria behave as expected, move to a selected cloud or hardware backend. For planning questions around execution budgets, see How Much Does It Cost to Run Quantum Workloads in the Cloud?
Checklist by scenario
Scenario 1: Establish a reproducible local baseline
- Create an isolated Python environment and record the Python, Qiskit and provider package versions.
- Set a fixed random seed where the simulator, transpiler and optimiser support one.
- Choose a small input whose expected behaviour can be checked classically.
- Write down the circuit width, depth, number of shots and objective definition.
- Save the initial parameter vector and every objective value returned by the loop.
A minimal parameterised workflow can be expressed as follows:
parameters = initialise_parameters()
problem = prepare_problem(data)
circuit = build_parameterised_circuit(problem)
observable = build_observable(problem)
for step in range(max_steps):
bound_circuit = bind_parameters(circuit, parameters)
result = estimator.run(bound_circuit, observable)
value = extract_expectation_value(result)
history.append({"step": step, "value": value})
parameters = classical_optimizer.update(parameters, value)
final_parameters = parametersThe exact binding, submission and result-extraction methods depend on the primitive interface in use. Keep those details behind small functions such as bind_parameters and extract_expectation_value. This makes an API migration less disruptive than scattering provider-specific calls throughout the application.
Scenario 2: Use a sampler when outcomes matter
Choose a sampler-style workflow when the output is a distribution or when the objective is calculated from measured bitstrings. This is common in demonstrations of QAOA, constraint satisfaction and circuit classification. The checklist is:
- Add measurement operations to the circuit where required by the selected interface.
- Confirm which classical registers contain the returned bits and how bit order is represented.
- Set a shot count appropriate to the signal you need; more shots reduce sampling uncertainty but increase execution work.
- Convert returned outcomes into the objective format used by the classical code.
- Compare the best observed samples with a simple classical baseline.
Do not treat the most frequently observed bitstring as automatically optimal. Calculate its objective value and verify that it satisfies the original constraints.
Scenario 3: Move from simulation to a remote backend
Before submitting remotely, separate application logic from backend configuration. Your workflow should make it possible to replace a simulator with a provider-managed backend without changing the problem definition or optimiser.
- Confirm that the backend supports the circuit width, required gates and primitive mode.
- Transpile or compile the circuit for the target backend and record the resulting depth and gate count.
- Check whether the backend uses shots, exact simulation, an approximation mode or another execution model.
- Estimate the number of primitive calls in the full optimisation loop, not just one circuit evaluation.
- Handle queue, authentication, timeout and transient service errors separately from invalid circuit errors.
For an enterprise pilot, document the data boundary, credentials, job identifiers and retention requirements before connecting the workflow to production systems. A useful way to frame that assessment is the quantum computing use-case and business-value checklist.
Scenario 4: Optimise the loop without hiding its behaviour
Once the baseline is correct, improve efficiency carefully. Batch compatible circuit evaluations where the primitive supports it, reduce unnecessary recompilation, cache invariant observables and use an optimiser suited to noisy objective values. Keep a record of each change so a faster run can be compared with the baseline on equal terms.
What to double-check
Shots and statistical variation
A measured expectation value is an estimate when it is obtained from finite shots. The optimiser may therefore respond to measurement noise rather than a genuine improvement. Repeat selected evaluations, report variability and avoid declaring convergence from a single unusually good result. Shot allocation can also be uneven across observables; review the practical workflow for measurement and shot allocation when an objective contains several terms.
Parameter and observable alignment
Check that the parameter order used by the optimiser matches the parameter order expected by the circuit. Confirm that the observable acts on the intended qubits and that any Hamiltonian coefficients use the same convention as the problem formulation. A circuit can execute successfully while calculating the wrong quantity.
Noise and transpilation
Compare the ideal or noiseless result with the noisy result before adding mitigation. Inspect circuit depth, two-qubit gate count, layout and measurement mapping. If mitigation is appropriate, test it against known reference cases rather than assuming that every corrected value is more reliable. The guide to ZNE, readout correction and probabilistic error cancellation provides a framework for that comparison.
Reproducibility
Record the source data, circuit-generation code, parameter initialisation, transpilation settings, primitive options, backend identifier, shot count and optimiser configuration. Remote results may still vary because of calibration, queue timing or service changes, so reproducibility means preserving enough context to explain a difference rather than promising identical output forever.
Common mistakes
- Using a quantum circuit before defining a measurable objective. Start with the quantity the classical routine must minimise or maximise.
- Changing several variables at once. Altering the ansatz, optimiser, shots and backend together makes failures difficult to diagnose.
- Ignoring constraint validity. A low numerical objective is not useful if the returned solution violates the problem rules.
- Assuming simulator success predicts hardware success. Simulation validates logic; it does not reproduce every hardware limitation or noise effect.
- Submitting one remote job per tiny operation. Group compatible evaluations where possible and measure the cost of orchestration as well as circuit execution.
- Hard-coding provider-specific result formats. Normalise primitive results at the edge of the application.
- Stopping without a classical comparison. Benchmark against a transparent heuristic, exact solution for a small instance or another established baseline.
When to revisit
Revisit this workflow before each substantial experiment, seasonal planning cycle or production change. First check the installed Qiskit and provider versions, then confirm primitive names, result objects, option fields and backend requirements. Re-run the local baseline after upgrading dependencies. If the results change, compare circuit structure, transpilation, shots, seeds and backend configuration before changing the algorithm.
Return to the checklist when moving from estimator to sampler, from simulation to hardware, from a single circuit to batched evaluations, or from a research notebook to a service. Also revisit it when the business problem changes: a new constraint may require a different encoding, observable or validation test.
A practical next step is to create a small repository containing four components: problem preparation, circuit construction, primitive execution and classical optimisation. Add a baseline test with a known input, log every run, and define a failure rule for invalid solutions. That structure keeps the hybrid quantum-classical workflow understandable while the surrounding tools evolve.