From 60ab9ca8c34af7b1e72b75ee3be052f44db149b5 Mon Sep 17 00:00:00 2001 From: Eric Giguere Date: Fri, 16 Aug 2024 11:02:14 -0400 Subject: [PATCH 1/2] numpy_backend as a property --- qutip/core/numpy_backend.py | 9 +++------ qutip/core/options.py | 31 +++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/qutip/core/numpy_backend.py b/qutip/core/numpy_backend.py index bbeb247fe8..e887527e9a 100644 --- a/qutip/core/numpy_backend.py +++ b/qutip/core/numpy_backend.py @@ -1,14 +1,11 @@ from ..settings import settings - class NumpyBackend: - @property - def backend(self): - return settings.core["numpy_backend"] + def _qutip_setting_backend(self, np): + self.np = np def __getattr__(self, name): - backend = object.__getattribute__(self, 'backend') - return getattr(backend, name) + return getattr(self.np, name) # Initialize the numpy backend diff --git a/qutip/core/options.py b/qutip/core/options.py index c9f156729d..a55d79877d 100644 --- a/qutip/core/options.py +++ b/qutip/core/options.py @@ -1,5 +1,6 @@ from ..settings import settings -import numpy as np +from .numpy_backend import np as qt_np +import numpy __all__ = ["CoreOptions"] @@ -9,8 +10,12 @@ class QutipOptions: Define basic method to wrap an ``options`` dict. Default options are in a class _options dict. + + Options can also act as properties. The ``_properties`` map options keys to + a function to call when the ``QutipOptions`` become the default. """ _options = {} + _properties = {} _settings_name = None # Where the default is in settings def __init__(self, **options): @@ -24,12 +29,17 @@ def __contains__(self, key): return key in self.options def __getitem__(self, key): - # Let the dict catch the KeyError + # Let the dict catch the return self.options[key] def __setitem__(self, key, value): # Let the dict catch the KeyError self.options[key] = value + if ( + key in self._properties + and self is getattr(settings, self._settings_name) + ): + self._properties[key](value) def __repr__(self, full=True): out = [f"<{self.__class__.__name__}("] @@ -44,10 +54,15 @@ def __repr__(self, full=True): def __enter__(self): self._backup = getattr(settings, self._settings_name) - setattr(settings, self._settings_name, self) + self._set_as_global_default() def __exit__(self, exc_type, exc_value, exc_traceback): - setattr(settings, self._settings_name, self._backup) + self._backup._set_as_global_default() + + def _set_as_global_default(self): + setattr(settings, self._settings_name, self) + for key in self._properties: + self._properties[key](self.options[key]) class CoreOptions(QutipOptions): @@ -129,10 +144,14 @@ class CoreOptions(QutipOptions): # Hermiticity checks can be slow, stop jitting, etc. "auto_real_casting": True, # Default backend is numpy - "numpy_backend": np + "numpy_backend": numpy } _settings_name = "core" + _properties = { + "numpy_backend": qt_np._qutip_setting_backend, + } # Creating the instance of core options to use everywhere. -settings.core = CoreOptions() +# settings.core = CoreOptions() +CoreOptions()._set_as_global_default() From cc9808b71f314530b8b43bf3f9fcf59f942ef90c Mon Sep 17 00:00:00 2001 From: Eric Giguere Date: Fri, 16 Aug 2024 11:17:58 -0400 Subject: [PATCH 2/2] change attribute names --- qutip/core/numpy_backend.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/qutip/core/numpy_backend.py b/qutip/core/numpy_backend.py index e887527e9a..39fa1d0947 100644 --- a/qutip/core/numpy_backend.py +++ b/qutip/core/numpy_backend.py @@ -1,11 +1,12 @@ from ..settings import settings + class NumpyBackend: def _qutip_setting_backend(self, np): - self.np = np + self._qt_np = np def __getattr__(self, name): - return getattr(self.np, name) + return getattr(self._qt_np, name) # Initialize the numpy backend