Fix zero-repetition measurement record shapes in simulator - #8316
Open
Prahalad-ship-it wants to merge 2 commits into
Open
Fix zero-repetition measurement record shapes in simulator#8316Prahalad-ship-it wants to merge 2 commits into
Prahalad-ship-it wants to merge 2 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
Summary
This PR fixes an issue in Cirq’s sampling interface where the simulator returned incorrectly shaped empty measurement records when circuits were run with
repetitions=0.Previously, the zero-repetition path bypassed execution but hardcoded an empty record shape of
(0, 1, 1). This occurred regardless of how many qubits were measured or how many times a key was repeated.This change updates the zero-repetition path so that
Result.recordsaccurately preserves the circuit's structural dimensions:(repetitions, measurement_instances, measured_qubits)->(0, measurement_instances, measured_qubits)Problem
Cirq’s
SimulatesSamples.run_sweep_itermethod includes a fast path for zero repetitions to avoid unnecessary simulation. However, it still must construct a validResultDictcontaining accurately dimensioned empty measurement records.Before this change, the implementation initialized these records using
np.empty([0, 1, 1]). This fallback shape failed to represent circuits utilizing multi-qubit measurement operations or repeated measurement keys.Minimal Reproducible Example
Previous Behavior
Corrected Behavior
02"m"occurs twice in the circuit2q0,q1)Note: This is a correctness fix, not a performance optimization. No unverified simulator optimizations have been introduced.
Tests Added
Added a dedicated regression test in
simulator_test.pythat constructs a circuit containing multiple qubits and repeated measurement keys. The test runs withrepetitions=0and asserts:Prior to this fix, the shape assertion would fail with a layout mismatch.
Validation Performed
The following checks passed locally:
ruffchecks passed successfully on all modified files.cirq-core/cirq/sim.Files Changed
cirq-core/cirq/sim/simulator.pyUpdated the zero-repetition execution pipeline to:
cirq-core/cirq/sim/simulator_test.pyAdded regression coverage validating repeated multi-qubit measurement outputs under zero repetitions.
API Compatibility
This change is fully backwards-compatible.