Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change str of ResultDict to print out repeated measurements #6468

Merged
merged 2 commits into from
Feb 16, 2024
Merged
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
14 changes: 14 additions & 0 deletions cirq-core/cirq/study/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def _keyed_repeated_bitstrings(vals: Mapping[str, np.ndarray]) -> str:
return '\n'.join(keyed_bitstrings)


def _keyed_repeated_records(vals: Mapping[str, np.ndarray]) -> str:
keyed_bitstrings = []
for key in sorted(vals.keys()):
reps = vals[key]
n = reps.shape[2]
num_records = reps.shape[1]
for j in range(num_records):
all_bits = ', '.join(_bitstring(reps[:, j, i]) for i in range(n))
keyed_bitstrings.append(f'{key}={all_bits}')
return '\n'.join(keyed_bitstrings)


def _key_to_str(key: TMeasurementKey) -> str:
if isinstance(key, str):
return key
Expand Down Expand Up @@ -388,6 +400,8 @@ def _repr_pretty_(self, p: Any, cycle: bool) -> None:
p.text(str(self))

def __str__(self) -> str:
if self._records:
return _keyed_repeated_records(self.records)
return _keyed_repeated_bitstrings(self.measurements)

def _json_dict_(self):
Expand Down
6 changes: 6 additions & 0 deletions cirq-core/cirq/study/result_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def test_str():
)
assert str(result) == 'ab=13579, 2 4 6 8 10\nc=01234'

result = cirq.ResultDict(records={'c': np.array([[[True], [True]]])})
assert str(result) == 'c=1\nc=1'

result = cirq.ResultDict(records={'c': np.array([[[True, False], [False, True]]])})
assert str(result) == 'c=1, 0\nc=0, 1'


def test_df():
result = cirq.ResultDict(
Expand Down