Deduplicate shared preamble in expectation-value sweeps#8123
Conversation
Factor the identical preamble of DensityMatrixSimulator.simulate_expectation_values_sweep and Simulator.simulate_expectation_values_sweep_iter into a protected _qubit_map_and_pauli_sums helper on SimulatesExpectationValues, and remove the # TODO(quantumlib#4209) marker. Only the shared setup (terminal-measurement guard, qubit-order resolution, observable wrapping) is extracted; the eager and lazy method bodies are unchanged. Fixes quantumlib#4209
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8123 +/- ##
==========================================
- Coverage 99.60% 99.60% -0.01%
==========================================
Files 1118 1118
Lines 100957 100954 -3
==========================================
- Hits 100557 100553 -4
- Misses 400 401 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
pavoljuhas
left a comment
There was a problem hiding this comment.
Thank you for the PR. The primary goal of refactoring tasks like #4209 is to make the code more readable and maintainable. I feel it does not happen for the change here. As a rough approximation the total LOC is essentially unchanged when excluding comments and docstrings and notably increased in terms of raw counts. There is an extra new indirection to follow in the simulate_expectation_values_{iter,sweep} code.
Note these methods cannot be easily replaced with a common base-class method, because one needs to call expectation_from_state_vector whereas the other expectation_from_density_matrix and they also do a different thing to get the sequence of simulation results.
This is still useful to show that #4209 should be probably closed as wontfix; it was open long enough without much complaints.
Fixes #4209.
What
DensityMatrixSimulator.simulate_expectation_values_sweepandSimulator.simulate_expectation_values_sweep_iterbegan with an identical preamble(the one flagged by the
# TODO(#4209)comment): reject terminal measurements unlesspermitted, resolve the qubit order, build the qubit→index map, and wrap the observables
as
cirq.PauliSums.This factors that preamble into a protected helper
_qubit_map_and_pauli_sumsonSimulatesExpectationValues(the base both simulators already inherit) and removes the# TODO(#4209)marker.Design context
Only the preamble is shared; the two methods' bodies are intentionally left as they are.
The base class documents "Prefer overriding
simulate_expectation_values_sweep_iter", andthe sparse
Simulatordeliberately stays a lazy generator that streams oversimulate_sweep_iterand usesexpectation_from_state_vector, whileDensityMatrixSimulatoris eager and usesexpectation_from_density_matrix. Collapsing thetwo into one method would regress the sparse path's streaming behavior, so the helper covers
only the common setup. The helper returns the already-resolved
QubitOrderso the qubit mapand the downstream
simulate(...)call use the same ordering — no behavior change.No public API change (the helper is protected); no serialization changes.
Scope / non-goals
Simulates*interfaces or the Clifford/MPS simulators (theydon't override these methods).
Test
No new behavior is introduced, so the existing expectation-value tests act as the regression
net —
density_matrix_simulator_test.py,sparse_simulator_test.py, andsimulator_test.pyall pass (473 tests), and they already cover the numeric expectation values, the
terminal-measurement
ValueError, and thepermit_terminal_measurementsflag, whichexercise every line of the new helper.