Skip to content

Commit

Permalink
Cleaning up the backend manager (#311)
Browse files Browse the repository at this point in the history
**Description of the Change:**
BackendManager is a singleton. As such, we can replace many of its `cls`
to `self`
  • Loading branch information
SamFerracin authored Dec 5, 2023
1 parent d6a9c32 commit fd82ccb
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions mrmustard/math/backend_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ def __repr__(self) -> str:
return f"Backend({self.backend_name})"

@property
def backend(cls) -> BackendBase:
def backend(self) -> BackendBase:
r"""
The backend that is being used.
"""
cls._is_immutable = True
return cls._backend
self._is_immutable = True
return self._backend

@property
def backend_name(self) -> str:
Expand All @@ -147,7 +147,7 @@ def backend_name(self) -> str:
"""
return self._backend.name

def change_backend(cls, name: str) -> None:
def change_backend(self, name: str) -> None:
r"""
Changes the backend to a different one.
Expand All @@ -158,8 +158,8 @@ def change_backend(cls, name: str) -> None:
msg = "Backend must be either ``numpy`` or ``tensorflow``"
raise ValueError(msg)

if cls.backend_name != name:
if cls._is_immutable:
if self.backend_name != name:
if self._is_immutable:
msg = "Can no longer change the backend in this session."
raise ValueError(msg)

Expand All @@ -174,10 +174,10 @@ def change_backend(cls, name: str) -> None:
backend = getattr(module, object)()

# switch backend
cls._backend = backend
self._backend = backend

# bind
cls._bind()
self._bind()

# ~~~~~~~
# Methods
Expand Down Expand Up @@ -1166,11 +1166,11 @@ def DefaultEuclideanOptimizer(self):
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@property
def euclidean_opt(cls):
def euclidean_opt(self):
r"""The configured Euclidean optimizer."""
if not cls._euclidean_opt:
cls._euclidean_opt = cls.DefaultEuclideanOptimizer()
return cls._euclidean_opt
if not self._euclidean_opt:
self._euclidean_opt = self.DefaultEuclideanOptimizer()
return self._euclidean_opt

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Methods that build on the basic ops and don't need to be overridden in the backend implementation
Expand Down

0 comments on commit fd82ccb

Please sign in to comment.