Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cirq-core/cirq/study/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion cirq-core/cirq/study/result_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +90 to 91

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the PR description says

After ResultDict.measurements raises ValueError, a second call would pass instead of raising ValueError again

should this test be repeated here? I.e.,

Suggested change
with pytest.raises(ValueError, match="Cannot extract 2D measurements"):
_ = r.measurements
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
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
Expand Down
Loading