Skip to content

Commit

Permalink
Remove Simulation() and pulser.simulation (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
HGSilveri authored Oct 7, 2024
1 parent f04682b commit dd68ac3
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 274 deletions.
4 changes: 1 addition & 3 deletions docs/source/apidoc/simulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ QutipEmulator

:class:`QutipEmulator` is the class to simulate :class:`SequenceSamples`, that are samples of a :class:`Sequence`.
It is possible to simulate directly a :class:`Sequence` object by using the class method
``QutipEmulator.from_sequence``. Since version 0.14.0, the :class:`Simulation` class is deprecated
in favour of :class:`QutipEmulator`.
``QutipEmulator.from_sequence``.

.. autoclass:: pulser_simulation.simulation.QutipEmulator
:members:

.. autoclass:: pulser_simulation.simulation.Simulation

SimConfig
----------------------
Expand Down
2 changes: 1 addition & 1 deletion pulser-core/pulser/sampler/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def to_nested_dict(
) -> dict:
"""Format in the nested dictionary form.
This is the format expected by `pulser_simulation.Simulation()`.
This is the format expected by `pulser_simulation.QutipEmulator()`.
Args:
all_local: Forces all samples to be distributed by their
Expand Down
2 changes: 1 addition & 1 deletion pulser-core/pulser/sequence/_seq_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ def phase_str(phi: Any) -> str:
alpha=0.3,
hatch="////",
)
else:
else: # pragma: no cover
ax.plot(
t,
ys_mod[i][:total_duration],
Expand Down
4 changes: 2 additions & 2 deletions pulser-core/pulser/sequence/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -1886,8 +1886,8 @@ def draw(
need to set this flag to False.
See Also:
Simulation.draw(): Draws the provided sequence and the one used by
the solver.
QutipEmulator.draw(): Draws the provided sequence and the one used
by the solver.
"""
valid_modes = ("input", "output", "input+output")
if mode not in valid_modes:
Expand Down
37 changes: 0 additions & 37 deletions pulser-core/pulser/simulation/__init__.py

This file was deleted.

2 changes: 1 addition & 1 deletion pulser-simulation/pulser_simulation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pulser_simulation._version import __version__ as __version__
from pulser_simulation.qutip_backend import QutipBackend
from pulser_simulation.simconfig import SimConfig
from pulser_simulation.simulation import QutipEmulator, Simulation
from pulser_simulation.simulation import QutipEmulator

__all__ = [
"EmulatorConfig",
Expand Down
10 changes: 5 additions & 5 deletions pulser-simulation/pulser_simulation/simresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _get_index_from_time(self, t_float: float, tol: float = 1.0e-3) -> int:
return int(np.where(abs(t_float - self._sim_times) < tol)[0][0])
except IndexError:
raise IndexError(
f"Given time {t_float} is absent from Simulation times within"
f"Given time {t_float} is absent from simulation times within"
+ f" tolerance {tol}."
)

Expand Down Expand Up @@ -229,7 +229,7 @@ class NoisyResults(SimulationResults):
Contrary to a CoherentResults object, this object contains a list of
Counters describing the state distribution at the time it was created by
using Simulation.run() with a noisy simulation.
using QutipEmulator.run() with a noisy simulation.
Contains methods for studying the populations and extracting useful
information from them.
"""
Expand Down Expand Up @@ -266,7 +266,7 @@ def __init__(
'all' or 'all_with_error', and to 'ground-rydberg', 'XY',
'digital' if given respectively 'ground-rydberg_with_error',
'XY_with_error' or 'digital_with_error'.
sim_times: Times at which Simulation object returned
sim_times: Times at which QutipEmulator object returned
the results.
n_measures: Number of measurements needed to compute this
result when doing the simulation.
Expand Down Expand Up @@ -381,7 +381,7 @@ def __init__(
basis_name: The basis indicating the addressed atoms after
the pulse sequence ('ground-rydberg', 'digital' or 'all' or
one of these bases with the suffix "_with_error").
sim_times: Times at which Simulation object returned the
sim_times: Times at which QutipEmulator object returned the
results.
meas_basis: The basis in which a sampling measurement
is desired (must be in "ground-rydberg" or "digital").
Expand Down Expand Up @@ -463,7 +463,7 @@ def get_final_state(
tol: float = 1e-6,
normalize: bool = True,
) -> qutip.Qobj:
"""Returns the final state of the Simulation.
"""Returns the final state of the simulation.
Args:
reduce_to_basis: Reduces the full state vector
Expand Down
168 changes: 1 addition & 167 deletions pulser-simulation/pulser_simulation/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from pulser.register.base_register import BaseRegister
from pulser.result import SampledResult
from pulser.sampler.samples import ChannelSamples, SequenceSamples
from pulser.sequence._seq_drawer import draw_samples, draw_sequence
from pulser.sequence._seq_drawer import draw_samples
from pulser_simulation.hamiltonian import Hamiltonian
from pulser_simulation.qutip_result import QutipResult
from pulser_simulation.simconfig import SimConfig
Expand Down Expand Up @@ -805,169 +805,3 @@ def from_sequence(
config,
evaluation_times,
)


class Simulation:
r"""Simulation of a pulse sequence using QuTiP.
Warning:
This class is deprecated in favour of ``QutipEmulator.from_sequence``.
Args:
sequence: An instance of a Pulser Sequence that we
want to simulate.
sampling_rate: The fraction of samples that we wish to
extract from the pulse sequence to simulate. Has to be a
value between 0.05 and 1.0.
config: Configuration to be used for this simulation.
evaluation_times: Choose between:
- "Full": The times are set to be the ones used to define the
Hamiltonian to the solver.
- "Minimal": The times are set to only include initial and final
times.
- An ArrayLike object of times in µs if you wish to only include
those specific times.
- A float to act as a sampling rate for the resulting state.
with_modulation: Whether to simulated the sequence with the programmed
input or the expected output.
"""

def __init__(
self,
sequence: Sequence,
sampling_rate: float = 1.0,
config: Optional[SimConfig] = None,
evaluation_times: Union[float, str, ArrayLike] = "Full",
with_modulation: bool = False,
) -> None:
"""Instantiates a Simulation object."""
with warnings.catch_warnings():
warnings.simplefilter("always")
warnings.warn(
DeprecationWarning(
"The `Simulation` class is deprecated,"
" use `QutipEmulator.from_sequence` instead."
)
)
self._seq = sequence
self._modulated = with_modulation
self._emulator = QutipEmulator.from_sequence(
self._seq, sampling_rate, config, evaluation_times, self._modulated
)

@property
def evaluation_times(self) -> np.ndarray:
"""The times at which the results of this simulation are returned.
Args:
value: Choose between:
- "Full": The times are set to be the ones used to define the
Hamiltonian to the solver.
- "Minimal": The times are set to only include initial and
final times.
- An ArrayLike object of times in µs if you wish to only
include those specific times.
- A float to act as a sampling rate for the resulting state.
"""
return self._emulator.evaluation_times

@evaluation_times.setter
def evaluation_times(self, value: Union[str, ArrayLike, float]) -> None:
"""Sets times at which the results of this simulation are returned."""
with warnings.catch_warnings():
warnings.simplefilter("always")
warnings.warn(
DeprecationWarning(
"Setting `evaluation_times` is deprecated,"
" use `set_evaluation_times` instead."
)
)
self._emulator.set_evaluation_times(value)

@property
def initial_state(self) -> qutip.Qobj:
"""The initial state of the simulation.
Args:
state: The initial state.
Choose between:
- "all-ground" for all atoms in ground state
- An ArrayLike with a shape compatible with the system
- A Qobj object
"""
return self._emulator.initial_state

@initial_state.setter
def initial_state(self, value: Union[str, np.ndarray, qutip.Qobj]) -> None:
"""Sets the initial state of the simulation."""
with warnings.catch_warnings():
warnings.simplefilter("always")
warnings.warn(
DeprecationWarning(
"Setting `initial_state` is deprecated,"
" use `set_initial_state` instead."
)
)
self._emulator.set_initial_state(value)

def draw(
self,
draw_phase_area: bool = False,
draw_interp_pts: bool = False,
draw_phase_shifts: bool = False,
draw_phase_curve: bool = False,
fig_name: str | None = None,
kwargs_savefig: dict = {},
) -> None:
"""Draws the input sequence and the one used by the solver.
Args:
draw_phase_area: Whether phase and area values need
to be shown as text on the plot, defaults to False.
draw_interp_pts: When the sequence has pulses with waveforms
of type InterpolatedWaveform, draws the points of interpolation
on top of the respective waveforms (defaults to False). Can't
be used if the sequence is modulated.
draw_phase_shifts: Whether phase shift and reference
information should be added to the plot, defaults to False.
draw_phase_curve: Draws the changes in phase in its own curve
(ignored if the phase doesn't change throughout the channel).
fig_name: The name on which to save the figure.
If None the figure will not be saved.
kwargs_savefig: Keywords arguments for
``matplotlib.pyplot.savefig``. Not applicable if `fig_name`
is ``None``.
See Also:
Sequence.draw(): Draws the sequence in its current state.
"""
if draw_interp_pts and self._modulated:
raise ValueError(
"Can't draw the interpolation points when the sequence is "
"modulated; `draw_interp_pts` must be `False`."
)
draw_sequence(
self._seq,
self._emulator._sampling_rate,
draw_input=not self._modulated,
draw_modulation=self._modulated,
draw_phase_area=draw_phase_area,
draw_interp_pts=draw_interp_pts,
draw_phase_shifts=draw_phase_shifts,
draw_phase_curve=draw_phase_curve,
)
if fig_name is not None:
plt.savefig(fig_name, **kwargs_savefig)
plt.show()

def __getattr__(self, name: str) -> Any:
return getattr(self._emulator, name)
Loading

0 comments on commit dd68ac3

Please sign in to comment.