Skip to content

Commit

Permalink
Fix printing of results object when it contains no state (#367)
Browse files Browse the repository at this point in the history
* Fix bug affecting printing of results class

* fixed

* fixed

* Update .github/CHANGELOG.md

Co-Authored-By: antalszava <antalszava@gmail.com>

* Update strawberryfields/api/connection.py

Co-authored-by: antalszava <antalszava@gmail.com>
  • Loading branch information
josh146 and antalszava committed Apr 15, 2020
1 parent a144b4c commit fce4595
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@
due to floating point precision error.
[(#364)](https://github.com/XanaduAI/strawberryfields/pull/364)

* Fixed a bug that caused an exception when printing results with no state.
[(#367)](https://github.com/XanaduAI/strawberryfields/pull/367)

* Improves the Takagi decomposition, by making explicit use of the eigendecomposition of real symmetric matrices. [(#352)](https://github.com/XanaduAI/strawberryfields/pull/352)

<h3>Contributors</h3>
Expand Down
8 changes: 8 additions & 0 deletions strawberryfields/api/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,15 @@ def get_job_result(self, job_id: str) -> Result:
with io.BytesIO() as buf:
buf.write(response.content)
buf.seek(0)

samples = np.load(buf, allow_pickle=False)

if np.issubdtype(samples.dtype, np.integer):
# Samples represent photon numbers.
# Convert to int64, to avoid unexpected behaviour
# when users postprocess these samples.
samples = samples.astype(np.int64)

return Result(samples, is_stateful=False)
raise RequestFailedError(
"Failed to get job result: {}".format(self._format_error_message(response))
Expand Down
7 changes: 4 additions & 3 deletions strawberryfields/api/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ def state(self):
raise AttributeError("The state is undefined for a stateless computation.")
return self._state

def __str__(self):
def __repr__(self):
"""String representation."""
return "Result: {} subsystems, state: {}\n samples: {}".format(
len(self.samples), self.state, self.samples
shots, modes = self.samples.shape
return "<Result: num_modes={}, shots={}, contains state={}>".format(
modes, shots, self._is_stateful
)
21 changes: 20 additions & 1 deletion tests/api/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
Unit tests for strawberryfields.api.result
"""
import numpy as np
import pytest

from strawberryfields.api import Result
Expand All @@ -29,9 +30,27 @@ class TestResult:
def test_stateless_result_raises_on_state_access(self):
"""Tests that `result.state` raises an error for a stateless result.
"""
result = Result([[1, 2], [3, 4]], is_stateful=False)
result = Result(np.array([[1, 2], [3, 4]]), is_stateful=False)

with pytest.raises(
AttributeError, match="The state is undefined for a stateless computation."
):
result.state

def test_stateless_print(self, capfd):
"""Test that printing a result object with no state provides the correct output."""
result = Result(np.array([[1, 2], [3, 4], [5, 6]]), is_stateful=False)
print(result)
out, err = capfd.readouterr()
assert "modes=2" in out
assert "shots=3" in out
assert "contains state=False" in out

def test_state_print(self, capfd):
"""Test that printing a result object with a state provides the correct output."""
result = Result(np.array([[1, 2], [3, 4], [5, 6]]), is_stateful=True)
print(result)
out, err = capfd.readouterr()
assert "modes=2" in out
assert "shots=3" in out
assert "contains state=True" in out

0 comments on commit fce4595

Please sign in to comment.