Skip to content

Commit

Permalink
Define the EmulatorBackend ABC
Browse files Browse the repository at this point in the history
  • Loading branch information
HGSilveri committed Nov 15, 2024
1 parent ab92b0d commit 0310199
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions pulser-core/pulser/backend/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}."
Expand Down Expand Up @@ -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)

0 comments on commit 0310199

Please sign in to comment.