From 125bb7e9db4323bf783ba6c8821e4f1aa3cd338e Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Fri, 9 Oct 2026 18:39:49 -0700 Subject: [PATCH 1/3] Keep ResultDict unchanged when ResultDict.measurements cannot be resolved Problem: After `ResultDict.measurements` raises ValueError, a second call would pass instead of raising ValueError again. Solution: Avoid mutating `ResultDict` until the measurements map is successfully resolved. --- cirq-core/cirq/study/result.py | 6 ++++-- cirq-core/cirq/study/result_test.py | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cirq-core/cirq/study/result.py b/cirq-core/cirq/study/result.py index 3a460b75e9..4d01d7eb91 100644 --- a/cirq-core/cirq/study/result.py +++ b/cirq-core/cirq/study/result.py @@ -429,12 +429,14 @@ def params(self) -> cirq.ParamResolver: def measurements(self) -> Mapping[str, np.ndarray]: if self._measurements is None: assert self._records is not None - self._measurements = {} + _measurements = {} for key, data in self._records.items(): reps, instances, qubits = data.shape if instances != 1: raise ValueError('Cannot extract 2D measurements for repeated keys') - self._measurements[key] = data.reshape((reps, qubits)) + _measurements[key] = data.reshape((reps, qubits)) + # assign only after the _measurements are successfully resolved + self._measurements = _measurements return self._measurements @property diff --git a/cirq-core/cirq/study/result_test.py b/cirq-core/cirq/study/result_test.py index 77368cfd12..6992d6deae 100644 --- a/cirq-core/cirq/study/result_test.py +++ b/cirq-core/cirq/study/result_test.py @@ -87,8 +87,10 @@ def test_construct_from_repeated_measurements() -> None: 'b': np.array([[[0, 0, 0]], [[1, 1, 1]]]), }, ) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="Cannot extract 2D measurements"): _ = r.measurements + # ensure r.measurements remains unresolved after ValueError + assert r._measurements is None assert np.all(r.records['a'] == np.array([[[0, 0], [0, 1]], [[1, 0], [1, 1]]])) assert np.all(r.records['b'] == np.array([[[0, 0, 0]], [[1, 1, 1]]])) assert r.repetitions == 2 From 9c398e3b7f73794d7ef2bf67ae1fa827596eaa85 Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Sun, 11 Oct 2026 17:53:32 -0700 Subject: [PATCH 2/3] Sync test with addressed behavior Ensure that the second access raises the same exception again which was not the case before. Co-authored-by: Michael Hucka --- cirq-core/cirq/study/result_test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cirq-core/cirq/study/result_test.py b/cirq-core/cirq/study/result_test.py index 6992d6deae..ff4511e5de 100644 --- a/cirq-core/cirq/study/result_test.py +++ b/cirq-core/cirq/study/result_test.py @@ -87,6 +87,9 @@ def test_construct_from_repeated_measurements() -> None: 'b': np.array([[[0, 0, 0]], [[1, 1, 1]]]), }, ) + with pytest.raises(ValueError, match="Cannot extract 2D measurements"): + _ = r.measurements + # Check that a second access raises ValueError again. with pytest.raises(ValueError, match="Cannot extract 2D measurements"): _ = r.measurements # ensure r.measurements remains unresolved after ValueError From 852e2b563cf8310e2a4827bc5a84cbc9b034804a Mon Sep 17 00:00:00 2001 From: Pavol Juhas Date: Sun, 11 Oct 2026 17:54:41 -0700 Subject: [PATCH 3/3] Drop redundant assertion Already tested above. No need to touch a protected attribute here. --- cirq-core/cirq/study/result_test.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/cirq-core/cirq/study/result_test.py b/cirq-core/cirq/study/result_test.py index ff4511e5de..7b25d9c3ea 100644 --- a/cirq-core/cirq/study/result_test.py +++ b/cirq-core/cirq/study/result_test.py @@ -92,8 +92,6 @@ def test_construct_from_repeated_measurements() -> None: # Check that a second access raises ValueError again. with pytest.raises(ValueError, match="Cannot extract 2D measurements"): _ = r.measurements - # ensure r.measurements remains unresolved after ValueError - assert r._measurements is None assert np.all(r.records['a'] == np.array([[[0, 0], [0, 1]], [[1, 0], [1, 1]]])) assert np.all(r.records['b'] == np.array([[[0, 0, 0]], [[1, 1, 1]]])) assert r.repetitions == 2