Standardize how tutorials use local simulators

Abierto
#5,703 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
45/100
Tipo de issue
Refactorización
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
jupyter-notebook, python

Línea de trabajo

Start with the affected notebooks under docs/tutorials/ and compare them with docs/guides/local-testing-mode and docs/guides/local-simulators. Apply the proposed Tier 1 or Tier 2 choice to each listed tutorial, preserving the explicitly out-of-scope tensor-network methods; done means simulator setup, transpile targets, naming, noise rationale, and exact-reference usage are consistent across the audit.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

tutorial 📒

Summary

About 33 of the 60 tutorials in docs/tutorials/ run a local simulation somewhere, usually as a small-scale example before the hardware run. They do it in at least six different ways, with different primitives, noise choices, transpile targets, and variable names. A reader who goes through several tutorials sees a different simulator pattern almost every time, and many of the patterns don't match what the guides teach.

This issue lists what each tutorial currently does, the inconsistencies, and a proposed standard that aligns with the Local testing mode and Migrate to local simulators guides.

Current state

A. Qiskit reference primitives (StatevectorSampler / StatevectorEstimator)
Tutorial What it does
grovers-algorithm StatevectorSampler() for the small-scale run
krylov-quantum-diagonalization Transpiles against FakeMarrakesh, then runs the untranspiled circuits on StatevectorEstimator()
loop-string-hadron-dynamics StatevectorEstimator()
operator-back-propagation StatevectorEstimator(seed=rng) for exact reference values
quantum-kernel-training StatevectorSampler()
sample-based-krylov-quantum-diagonalization Transpiles against GenericBackendV2, runs on StatevectorSampler()
sample-based-quantum-diagonalization Transpiles against a custom GenericBackendV2 (LUCJ basis gates), runs on StatevectorSampler(seed=rng)
warm-start-qaoa StatevectorEstimator() and StatevectorSampler()
simulate-neutron-scattering StatevectorEstimator(), alongside Quimb tensor-network simulation
B. Runtime primitive + noiseless AerSimulator() (local testing mode)
Tutorial What it does
edc-cut-bell-pair-benchmarking aer_backend = AerSimulator(); Sampler(mode=aer_backend)
long-range-entanglement aer_backend = AerSimulator(); Sampler(mode=aer_backend)
multi-product-formula aer_sim = AerSimulator(); Estimator(mode=aer_sim)
wire-cutting SamplerV2(mode=AerSimulator())
dc-hex-ising aer_sim = AerSimulator(); Sampler inside Batch(backend=aer_sim)
qedma-2d-ising-with-qesem simulator = AerSimulator(); Estimator(simulator) (positional, no mode=)
C. Runtime primitive + AerSimulator.from_backend(real_backend) (device noise model)
Tutorial What it does
chsh-inequality aer_sim = AerSimulator.from_backend(backend); Estimator(mode=aer_sim); transpiles to aer_sim.target
nishimori-phase-transition aer_backend = AerSimulator.from_backend(backend), passed to GemExperiment
pauli-correlation-encoding-for-qaoa backend = AerSimulator.from_backend(real_backend), which reuses the name backend for the simulator
D. Runtime primitive + AerSimulator with a hand-built noise model
Tutorial What it does
ai-transpiler-introduction Depolarizing NoiseModel; aer_sim = AerSimulator(noise_model=...); SamplerV2(mode=aer_sim)
compilation-methods-for-hamiltonian-simulation-circuits Same pattern as ai-transpiler-introduction
E. Qiskit Aer primitives (qiskit_aer.primitives)
Tutorial What it does
fractional-gates AerSampler.from_backend(backend_f) and AerSampler.from_backend(backend_c)
transpilation-optimizations-with-sabre AerEstimator.from_backend(backend)
dc-hex-ising Aer_Sampler.from_backend(AerSimulator(method="matrix_product_state", ...)) for the MPS run
F. AerSimulator.run() directly (no primitives)
Tutorial What it does
sqdrift simulator = AerSimulator(); transpile(...); simulator.run(...)
transverse-field-ising-model backend_sim = AerSimulator(method="matrix_product_state"); .run(...)
quantum-phase-estimation-qctrl aer_simulator = AerSimulator(method="matrix_product_state"); .run(...)
spacetime-codes noisy_sim = AerSimulator(method="stabilizer", noise_model=...); .run(..., seed_simulator=...) per circuit
G. Other (not circuit-execution simulators)
Tutorial What it does
approximate-quantum-compilation-for-time-evolution Quimb MPS via qiskit-addon-aqc-tensor; FakeKyiv used only as a transpile target
simulate-neutron-scattering, simulate-neutron-scattering-with-a-serverless-workflow Quimb MPS via QuimbSimulator
multi-product-formula tenpy MPS/TEBD via qiskit-addon-mpf
depth-reduction-with-circuit-cutting, probabilistic-error-cancellation-with-logical-noise-models, multi-product-formula qiskit.quantum_info.Statevector for exact reference values
Leftovers found during the audit
  • depth-reduction-with-circuit-cutting: comment says "For a fake backend, this will use a local simulator," but the tutorial only uses a real backend.
  • readout-error-mitigation-sampler: commented-out FakeMelbourneV2.

Inconsistencies

  1. Four ways to run the simulation. The tutorials use Qiskit reference primitives, Runtime primitives in local testing mode, qiskit_aer.primitives, and raw AerSimulator.run(). The raw .run() calls (group F) bypass primitives entirely, even though every hardware section uses them.
  2. No stated noise choice. Some examples are noiseless, some copy a device's noise with from_backend, and some use a hand-built depolarizing model. Tutorials rarely explain why they chose one.
  3. Different transpile targets. These include GenericBackendV2, FakeKyiv or FakeMarrakesh, AerSimulator(), aer_sim.target, and in krylov-quantum-diagonalization, a target that the simulated circuit isn't even transpiled for.
  4. API style. Most tutorials pass mode=aer_sim; qedma-2d-ising-with-qesem passes the simulator positionally.
  5. Variable names. The simulator is called aer_sim, aer_backend, simulator, backend_sim, aer_simulator, noisy_sim, or backend.
  6. Problems in individual tutorials. These are the krylov-quantum-diagonalization transpile mismatch, the backend reuse in pauli-correlation-encoding-for-qaoa, and the leftovers listed above.

Proposed standard

The standard has two tiers, because one simulator path can't cover every tutorial (see "Why two tiers").

Tier 1: Simulate the hardware workflow (default)

Use local testing mode: the same qiskit_ibm_runtime SamplerV2/EstimatorV2 used for the hardware run, with an AerSimulator as the mode. Transpile against that same simulator object.

from qiskit_aer import AerSimulator
from qiskit.transpiler import generate_preset_pass_manager
from qiskit_ibm_runtime import SamplerV2 as Sampler

# Pick ONE and say why in the markdown:
aer_sim = AerSimulator()                              # noiseless
# aer_sim = AerSimulator.from_backend(backend)        # noise model from a real QPU
# aer_sim = AerSimulator(noise_model=noise_model)     # custom noise model
# aer_sim = AerSimulator(method="matrix_product_state")  # larger, low-entanglement circuits
# aer_sim = AerSimulator(method="stabilizer")         # Clifford circuits

pm = generate_preset_pass_manager(backend=aer_sim, optimization_level=1)
isa_circuits = pm.run(circuits)

sampler = Sampler(mode=aer_sim)
result = sampler.run(isa_circuits).result()

Rules:

  • Always pass the simulator with mode=, not positionally.
  • Name it aer_sim. Never reuse backend, which stays the real QPU.
  • Transpile against the object passed to mode, not a separate fake or generic backend.
  • State the noise choice (noiseless, device noise, or custom) and the reason in the markdown.
  • For reproducible output, set sampler.options.simulator.seed_simulator (supported in local testing mode).
  • Session and Batch are accepted but ignored in local testing mode. They're fine when they mirror the hardware code, but not required.

When the hardware section switches from simulator to QPU, only the mode argument (and the transpile target) should change. This is the main selling point of local testing mode in the guides.

Tier 2: Exact reference values

When the tutorial needs an exact value (for example, a ground-truth expectation value to compare against, or an exact kernel entry), use the Qiskit reference primitives StatevectorEstimator / StatevectorSampler on the logical (untranspiled) circuit, and label it as the exact reference. Use qiskit.quantum_info.Statevector directly only when the tutorial needs the state itself rather than a primitive result.

Out of scope

Tensor-network methods that are part of the algorithm itself (Quimb in AQC and neutron scattering, tenpy in MPF) are not simulator choices and stay as they are.

Why two tiers

I verified this locally (qiskit-ibm-runtime 0.46.1, qiskit-aer 0.17.2). Local testing mode wraps the simulator in Qiskit's shot-based BackendEstimatorV2, so:

  • Estimator(mode=AerSimulator()) returned 0.9507 for an observable whose exact value is 0.9553.
  • default_precision=0 is rejected ("must be >0").

So local testing mode can't produce exact values, and tutorials that need them must keep a statevector path. The standard makes that an explicit, labeled choice instead of an accident.

What changes per tutorial

Tutorial Change
edc-cut-bell-pair-benchmarking, long-range-entanglement Rename aer_backend → aer_sim
multi-product-formula, dc-hex-ising (noiseless part) Already compliant
wire-cutting Assign aer_sim = AerSimulator() instead of constructing it inline in SamplerV2(mode=AerSimulator())
qedma-2d-ising-with-qesem Estimator(mode=aer_sim); rename simulator → aer_sim
chsh-inequality, ai-transpiler-introduction, compilation-methods-for-hamiltonian-simulation-circuits Already compliant
nishimori-phase-transition Rename aer_backend → aer_sim. GemExperiment takes a backend, so the primitive isn't under our control; keep AerSimulator.from_backend
pauli-correlation-encoding-for-qaoa Rename so backend is the QPU and aer_sim is the simulator
fractional-gates AerSampler.from_backend(backend_f) → Sampler(mode=AerSimulator.from_backend(backend_f)); confirm that Aer handles the fractional-gate target
transpilation-optimizations-with-sabre AerEstimator.from_backend(backend) → Estimator(mode=AerSimulator.from_backend(backend)); expect small numerical changes because results become shot-based
dc-hex-ising (MPS part) Aer_Sampler.from_backend(mps_backend) → Sampler(mode=AerSimulator(method="matrix_product_state", ...))
sqdrift simulator.run(...) → Sampler(mode=aer_sim); read counts from result[0].data.<creg>.get_counts()
transverse-field-ising-model, quantum-phase-estimation-qctrl .run(...) → Sampler(mode=AerSimulator(method="matrix_product_state"))
spacetime-codes .run(...) → Sampler(mode=AerSimulator(method="stabilizer", noise_model=...)) with options.simulator.seed_simulator. The postselection logic reads combined counts, so use result[0].join_data().get_counts()
grovers-algorithm, quantum-kernel-training, sample-based-krylov-quantum-diagonalization, sample-based-quantum-diagonalization Decide per tutorial: if the example demonstrates the hardware workflow → Tier 1; if it needs exact output → Tier 2, labeled as such. sample-based-quantum-diagonalization needs a custom LUCJ basis-gate GenericBackendV2 for ffsim's pass manager; keep it and add a sentence explaining why
krylov-quantum-diagonalization Either run the transpiled circuits in Tier 1, or drop the FakeMarrakesh transpile from the simulator section and use Tier 2 on the logical circuits. Don't transpile for one target and run another
operator-back-propagation, loop-string-hadron-dynamics, warm-start-qaoa, simulate-neutron-scattering Tier 2 (exact values); add a label that these are exact references
approximate-quantum-compilation-for-time-evolution Out of scope (Quimb); FakeKyiv transpile-only use is fine
depth-reduction-with-circuit-cutting Remove the stale fake-backend comment
readout-error-mitigation-sampler Remove the commented-out FakeMelbourneV2

Alignment with the guides

  • Local testing mode and Migrate to local simulators teach exactly Tier 1: Sampler(mode=aer_sim) with AerSimulator(), AerSimulator.from_backend(real_backend), or AerSimulator(method="stabilizer"), transpiled with generate_preset_pass_manager(backend=aer_sim, ...). The guide also says "For most users, AerSimulator is a good choice," and uses the name aer_sim.
  • Quickstart and Exact simulation with Qiskit SDK primitives teach StatevectorSampler/StatevectorEstimator for exact, small simulations, which matches Tier 2.
  • Run your first circuit on hardware offers a fake backend in local testing mode as its simulator alternative. That is the same mechanism with a fake backend instead of Aer. Tier 1 prefers AerSimulator to match the "most users" recommendation, but fake backends remain valid when a tutorial specifically targets a device snapshot.
  • One mismatch: Exact and noisy simulation with Qiskit Aer primitives teaches qiskit_aer.primitives (group E). The proposal moves tutorials away from that pattern. The guide can stay as a reference for Aer's own primitives, but tutorials should use local testing mode so the simulator code matches the hardware code.
  • Nothing in the guides teaches raw AerSimulator.run(), so group F is the clearest case for migration.

Open questions

  • Should the tutorial template/guidelines codify this (for example, a "Small-scale simulator example" section with the Tier 1 snippet)?
  • Should simulate-with-qiskit-aer get a note pointing to local testing mode as the default for workflows that end on hardware?
Lenguaje dominante
Jupyter Notebook
Estrellas
111
Forks
247
Merge medio
1 d 1 h
PR fusionados (30 d)
96

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Qiskit/documentation

Todos los issues de Qiskit/documentation

Issues similares

Más issues de Documentation

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.