Skip to content

Commit

Permalink
Add empty results fallback for Result.repetitions (#3875)
Browse files Browse the repository at this point in the history
Fixes: #3852
  • Loading branch information
vtomole committed Mar 4, 2021
1 parent 3da5fa9 commit 41fcc6a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cirq/study/result.py
Expand Up @@ -68,7 +68,7 @@ def _keyed_repeated_bitstrings(vals: Dict[str, np.ndarray]) -> str:
reps = vals[key]
n = 0 if len(reps) == 0 else len(reps[0])
all_bits = ', '.join(_bitstring(reps[:, i]) for i in range(n))
keyed_bitstrings.append('{}={}'.format(key, all_bits))
keyed_bitstrings.append(f'{key}={all_bits}')
return '\n'.join(keyed_bitstrings)


Expand Down Expand Up @@ -150,6 +150,8 @@ def measurements(self) -> Dict[str, np.ndarray]:

@property
def repetitions(self) -> int:
if not self.measurements:
return 0
# Get the length quickly from one of the keyed results.
return len(next(iter(self.measurements.values())))

Expand Down Expand Up @@ -263,7 +265,7 @@ def histogram( # type: ignore
def __repr__(self) -> str:
def item_repr(entry):
key, val = entry
return '{!r}: {}'.format(key, proper_repr(val))
return f'{key!r}: {proper_repr(val)}'

measurement_dict_repr = (
'{' + ', '.join([item_repr(e) for e in self.measurements.items()]) + '}'
Expand Down
5 changes: 5 additions & 0 deletions cirq/study/result_test.py
Expand Up @@ -22,6 +22,11 @@
from cirq.study.result import _pack_digits


def test_result_init():
assert cirq.Result(params=cirq.ParamResolver({}), measurements=None).repetitions == 0
assert cirq.Result(params=cirq.ParamResolver({}), measurements={}).repetitions == 0


def test_repr():
v = cirq.Result.from_single_parameter_set(
params=cirq.ParamResolver({'a': 2}), measurements={'xy': np.array([[1, 0], [0, 1]])}
Expand Down

0 comments on commit 41fcc6a

Please sign in to comment.