Skip to content

Commit

Permalink
Include total sequence duration in Results
Browse files Browse the repository at this point in the history
  • Loading branch information
HGSilveri committed Nov 19, 2024
1 parent 6e49225 commit 4527cbf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
11 changes: 4 additions & 7 deletions pulser-core/pulser/backend/observable.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def __call__(
state: State,
hamiltonian: Operator,
result: Results,
time_tol: float,
) -> None:
"""Specifies a call to the callback at a specific time.
Expand All @@ -53,8 +52,6 @@ def __call__(
state: The current state.
hamiltonian: The Hamiltonian at this time.
result: The Results object to store the result in.
time_tol: Tolerance below which two time values are considered
equal.
"""
pass

Expand All @@ -79,7 +76,6 @@ def __call__(
state: State,
hamiltonian: Operator,
result: Results,
time_tol: float,
) -> None:
"""Specifies a call to the observable at a specific time.
Expand All @@ -98,14 +94,17 @@ def __call__(
time_tol: Tolerance below which two time values are considered
equal.
"""
time_tol = (
(0.5 / result.total_duration) if result.total_duration else 1e-6
)
if (
self.evaluation_times is not None
and config.is_time_in_evaluation_times(
t, self.evaluation_times, tol=time_tol
)
) or config.is_evaluation_time(t, tol=time_tol):
value_to_store = self.apply(
config=config, t=t, state=state, hamiltonian=hamiltonian
config=config, state=state, hamiltonian=hamiltonian
)
result._store(
observable_name=self.name(), time=t, value=value_to_store
Expand All @@ -129,15 +128,13 @@ def apply(
self,
*,
config: EmulationConfig,
t: float,
state: State,
hamiltonian: Operator,
) -> Any:
"""Calculates the observable to store in the Results.
Args:
config: The config object passed to the backend.
t: The relative time as a float between 0 and 1.
state: The current state.
hamiltonian: The Hamiltonian at this time.
Expand Down
9 changes: 8 additions & 1 deletion pulser-core/pulser/backend/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@

@dataclass
class Results:
"""A collection of results."""
"""A collection of results.
Args:
atoms_order: The order of the atoms/qudits in the results.
meas_basis: The measurement basis.
total_duration: The total duration of the sequence, in ns.
"""

atom_order: tuple[QubitId, ...]
meas_basis: str
total_duration: int
_results: dict[str, dict[float, Any]] = field(init=False)

def __post_init__(self) -> None:
Expand Down
4 changes: 3 additions & 1 deletion pulser-core/pulser/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import warnings
from abc import ABC, abstractmethod
from collections import Counter
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import Any

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -47,6 +47,8 @@ def __getattr__(name: str) -> Any:
class Result(ABC, backend_results.Results):
"""Base class for storing the result of an observable at specific time."""

total_duration: int = field(default=0, init=False)

@property
def sampling_dist(self) -> dict[str, float]:
"""Sampling distribution of the measured bitstring.
Expand Down
4 changes: 2 additions & 2 deletions pulser-simulation/pulser_simulation/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ def _run_solver() -> CoherentResults:
self._meas_basis,
state,
self._meas_basis in self.basis_name,
evaluation_time=t / self._tot_duration * 1e-3,
evaluation_time=t / self._tot_duration * 1e3,
)
for state, t in zip(result.states, self._eval_times_array)
]
Expand Down Expand Up @@ -685,7 +685,7 @@ def _run_solver() -> CoherentResults:
tuple(self._hamiltonian._qdict),
self._meas_basis,
total_count[ind],
evaluation_time=t / self._tot_duration * 1e-3,
evaluation_time=t / self._tot_duration * 1e3,
)
for ind, t in enumerate(self._eval_times_array)
]
Expand Down

0 comments on commit 4527cbf

Please sign in to comment.