Skip to content

Commit a8bd9a5

Browse files
authored
Change str of ResultDict to print out repeated measurements (#6468)
- ResultDict will now print out repeated measurements on each line, as opposed to throwing ValueError Fixes: #6447
1 parent d432730 commit a8bd9a5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

cirq-core/cirq/study/result.py

+14
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ def _keyed_repeated_bitstrings(vals: Mapping[str, np.ndarray]) -> str:
7474
return '\n'.join(keyed_bitstrings)
7575

7676

77+
def _keyed_repeated_records(vals: Mapping[str, np.ndarray]) -> str:
78+
keyed_bitstrings = []
79+
for key in sorted(vals.keys()):
80+
reps = vals[key]
81+
n = reps.shape[2]
82+
num_records = reps.shape[1]
83+
for j in range(num_records):
84+
all_bits = ', '.join(_bitstring(reps[:, j, i]) for i in range(n))
85+
keyed_bitstrings.append(f'{key}={all_bits}')
86+
return '\n'.join(keyed_bitstrings)
87+
88+
7789
def _key_to_str(key: TMeasurementKey) -> str:
7890
if isinstance(key, str):
7991
return key
@@ -388,6 +400,8 @@ def _repr_pretty_(self, p: Any, cycle: bool) -> None:
388400
p.text(str(self))
389401

390402
def __str__(self) -> str:
403+
if self._records:
404+
return _keyed_repeated_records(self.records)
391405
return _keyed_repeated_bitstrings(self.measurements)
392406

393407
def _json_dict_(self):

cirq-core/cirq/study/result_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ def test_str():
127127
)
128128
assert str(result) == 'ab=13579, 2 4 6 8 10\nc=01234'
129129

130+
result = cirq.ResultDict(records={'c': np.array([[[True], [True]]])})
131+
assert str(result) == 'c=1\nc=1'
132+
133+
result = cirq.ResultDict(records={'c': np.array([[[True, False], [False, True]]])})
134+
assert str(result) == 'c=1, 0\nc=0, 1'
135+
130136

131137
def test_df():
132138
result = cirq.ResultDict(

0 commit comments

Comments
 (0)