Skip to content

Commit

Permalink
Unit tests for Results
Browse files Browse the repository at this point in the history
  • Loading branch information
HGSilveri committed Jan 3, 2025
1 parent 4bcc8e2 commit dc26f3b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pulser-core/pulser/backend/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get_result_times(self, observable: Observable | str) -> list[float]:
return list(self._results[self._find_uuid(observable)].keys())

def get_result(self, observable: Observable | str, time: float) -> Any:
"""Get the given result at the given time.
"""Get the a specific result at a given time.
Args:
observable: The observable instance used to calculate the result
Expand Down Expand Up @@ -115,6 +115,10 @@ def get_tagged_results(self) -> dict[str, dict[float, Any]]:

def _find_uuid(self, observable: Observable | str) -> uuid.UUID:
if isinstance(observable, Observable):
if observable.uuid not in self._results:
raise ValueError(
f"'{observable!r}' has not been stored in the results"
)
return observable.uuid
try:
return self._tagmap[observable]
Expand Down
66 changes: 65 additions & 1 deletion tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from pulser.devices import AnalogDevice, MockDevice
from pulser.register import SquareLatticeLayout
from pulser.result import Result, SampledResult
from pulser_simulation import QutipState, QutipOperator
from pulser_simulation import QutipOperator, QutipState


@pytest.fixture
Expand Down Expand Up @@ -335,10 +335,74 @@ def test_emulation_config():
),
):
EmulationConfig(interaction_matrix=np.arange(12).reshape((4, 3)))
with pytest.raises(
ValueError,
match=re.escape(
"interaction matrix of shape (2, 2) is incompatible with "
"the received initial state of 3 qudits"
),
):
EmulationConfig(
interaction_matrix=np.eye(2),
initial_state=QutipState.from_state_amplitudes(
eigenstates=("r", "g"), amplitudes={"rrr": 1.0}
),
)
with pytest.raises(TypeError, match="must be a NoiseModel"):
EmulationConfig(noise_model={"p_false_pos": 0.1})


def test_results():
res = Results(atom_order=(), total_duration=0)
assert res.get_result_tags() == []
assert res.get_tagged_results() == {}
with pytest.raises(
AttributeError, match="'bitstrings' is not in the results"
):
assert res.bitstrings
with pytest.raises(
ValueError,
match="'bitstrings' is not an Observable instance nor a known "
"observable tag",
):
assert res.get_result_times("bitstrings")

obs = BitStrings(tag_suffix="test")
with pytest.raises(
ValueError,
match=f"'bitstrings_test:{obs.uuid}' has not been stored",
):
assert res.get_result(obs, 1.0)

obs(
config=EmulationConfig(),
t=1.0,
state=QutipState.from_state_amplitudes(
eigenstates=("r", "g"), amplitudes={"rrr": 1.0}
),
hamiltonian=QutipOperator.from_operator_repr(
eigenstates=("r", "g"), n_qudits=3, operations=[(1.0, [])]
),
result=res,
)
assert res.get_result_tags() == ["bitstrings_test"]
expected_val = {1.0: Counter({"111": obs.num_shots})}
assert res.get_tagged_results() == {"bitstrings_test": expected_val}
assert res.bitstrings_test == expected_val
assert (
res.get_result_times("bitstrings_test")
== res.get_result_times(obs)
== [1.0]
)
assert (
res.get_result(obs, 1.0)
== res.get_result("bitstrings_test", 1.0)
== expected_val[1.0]
)
with pytest.raises(ValueError, match="not available at time 0.912"):
res.get_result(obs, 0.912)


class TestObservables:
@pytest.fixture
def ghz_state(self):
Expand Down

0 comments on commit dc26f3b

Please sign in to comment.