Deduplicate shared preamble in expectation-value sweeps#8123
Open
yurekami wants to merge 1 commit into
Open
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:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.