MeasurementStage¶
- class MeasurementStage(grouping_strategy='qwc', result_format_override=None, shot_distribution=None)[source]¶
Bases:
BundleStageUnified measurement stage for all circuit measurement types.
Handles both
probs()andexpval(H)measurements, auto-detecting the measurement type from the MetaCircuit. Forexpval(H), groups observables using the configured strategy (or delegates to expval-native backends viaham_ops).During
expand, setsenv.result_formatto communicate the expected result format topipeline.run():ResultFormat.PROBSforprobs()measurementsResultFormat.EXPVALSforexpval(H)measurementsResultFormat.COUNTSis never set here (reserved for PCE)
During
reduce, combines measurement groups using the postprocessing function from_compute_measurement_groups.- Parameters:
grouping_strategy (
Literal['wires','default','qwc','_backend_expval'] |None) – Grouping strategy for expval observables. Ignored forprobs()measurements. Defaults to “qwc” (qubit-wise commuting).result_format_override (
ResultFormat|None) – If set, overrides the auto-detected result format. For example, passResultFormat.COUNTSto get raw shot counts even when an observable is present.shot_distribution (
Literal['uniform','weighted','weighted_random'] |Callable[[list[float],int],list[int]] |None) – How to split the backend’s total shot budget across measurement groups ("uniform","weighted","weighted_random", or a callable). Only valid for sampling-based grouping strategies ("qwc","wires", orNone); rejected for"_backend_expval"because the backend computes expectation values analytically and ignores shots. WhenNone(default), every group is submitted with the backend’s full shot count.
Attributes Summary
Axis name introduced by this stage.
Whether this stage reads
meta.circuit_bodiesduringexpand.Whether this stage sets up measurement circuits and result format.
True only for genuinely non-deterministic shot-distribution strategies.
Methods Summary
cache_key_extras(env)Fold
env.backend.shotsinto the forward-pass cache key.dry_expand(batch, env)Analytic path: keep grouping + shot allocation, skip QASM rendering.
expand(batch, env)Set up measurements and declare the result format on env.
introspect(batch, env, token)Return stage-specific metadata for dry-run reporting.
reduce(results, env, token)Combine measurement groups (expval) or strip meas axis (probs).
Attributes Documentation
- axis_name¶
- consumes_dag_bodies¶
- handles_measurement¶
- stateful¶
True only for genuinely non-deterministic shot-distribution strategies.
"weighted_random"and user-supplied callables draw fromenv.rngand must re-run on every call. The built-in deterministic strategies ("uniform","weighted") are pure functions of the observable and the shot count, so they stay cacheable; cache invalidation on shot-count changes is delegated tocache_key_extras().
Methods Documentation
- cache_key_extras(env)[source]¶
Fold
env.backend.shotsinto the forward-pass cache key.Any configured shot distribution (even the deterministic ones) reads
env.backend.shotsduringexpand()to compute the per-group allocation. Including the current shot budget in the cache key means a re-run with a different budget triggers fresh allocation rather than replaying the stale one. Returns()whenshot_distributionisNone.
- dry_expand(batch, env)[source]¶
Analytic path: keep grouping + shot allocation, skip QASM rendering.
Group count is the source of truth for the measurement fan-out, so we always run
_compute_measurement_groupsand_allocate_per_group_shots(both cheap, pure numpy / analytic). Only the per-group QASM string generation is swapped for a placeholder so the emitted batch has correct shape without materialising measurement circuits.- Return type: