From 031019926557d9752cabf9c39ede765975b111eb Mon Sep 17 00:00:00 2001 From: HGSilveri Date: Fri, 15 Nov 2024 16:13:21 +0100 Subject: [PATCH] Define the EmulatorBackend ABC --- pulser-core/pulser/backend/abc.py | 42 ++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/pulser-core/pulser/backend/abc.py b/pulser-core/pulser/backend/abc.py index 8c1ec754..13264a0f 100644 --- a/pulser-core/pulser/backend/abc.py +++ b/pulser-core/pulser/backend/abc.py @@ -14,32 +14,38 @@ """Base class for the backend interface.""" from __future__ import annotations -import typing from abc import ABC, abstractmethod +from collections.abc import Sequence +from typing import ClassVar +import pulser +from pulser.backend.config import EmulationConfig from pulser.backend.results import Results from pulser.devices import Device -from pulser.sequence import Sequence class Backend(ABC): """The backend abstract base class.""" - def __init__(self, sequence: Sequence, mimic_qpu: bool = False) -> None: + def __init__( + self, sequence: pulser.Sequence, mimic_qpu: bool = False + ) -> None: """Starts a new backend instance.""" self.validate_sequence(sequence, mimic_qpu=mimic_qpu) self._sequence = sequence self._mimic_qpu = bool(mimic_qpu) @abstractmethod - def run(self) -> Results | typing.Sequence[Results]: + def run(self) -> Results | Sequence[Results]: """Executes the sequence on the backend.""" pass @staticmethod - def validate_sequence(sequence: Sequence, mimic_qpu: bool = False) -> None: + def validate_sequence( + sequence: pulser.Sequence, mimic_qpu: bool = False + ) -> None: """Validates a sequence prior to submission.""" - if not isinstance(sequence, Sequence): + if not isinstance(sequence, pulser.Sequence): raise TypeError( "'sequence' should be a `Sequence` instance" f", not {type(sequence)}." @@ -68,3 +74,27 @@ def validate_sequence(sequence: Sequence, mimic_qpu: bool = False) -> None: "the register's layout must be one of the layouts available " f"in '{device.name}.calibrated_register_layouts'." ) + + +class EmulatorBackend(Backend): + """The emulator backend parent class.""" + + default_config: ClassVar[EmulationConfig] + + def __init__( + self, + sequence: pulser.Sequence, + config: EmulationConfig | None = None, + mimic_qpu: bool = False, + ) -> None: + """Initializes the backend.""" + super().__init__(sequence, mimic_qpu=mimic_qpu) + config = config or self.default_config + if not isinstance(config, EmulationConfig): + raise TypeError( + "'config' must be an instance of 'EmulationConfig', " + f"not {type(config)}." + ) + self._config = config + # See the BackendConfig definition to see how this would work + self._config = type(self.default_config)(**config._backend_options)