Skip to content

Commit

Permalink
Merge pull request qutip#2509 from Ericgig/feature.qutip_np_faster
Browse files Browse the repository at this point in the history
Optimize `numpy_backend`
  • Loading branch information
Ericgig authored Aug 19, 2024
2 parents b0ec492 + cc9808b commit 7bb0603
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
8 changes: 3 additions & 5 deletions qutip/core/numpy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@


class NumpyBackend:
@property
def backend(self):
return settings.core["numpy_backend"]
def _qutip_setting_backend(self, np):
self._qt_np = np

def __getattr__(self, name):
backend = object.__getattribute__(self, 'backend')
return getattr(backend, name)
return getattr(self._qt_np, name)


# Initialize the numpy backend
Expand Down
31 changes: 25 additions & 6 deletions qutip/core/options.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ..settings import settings
import numpy as np
from .numpy_backend import np as qt_np
import numpy
__all__ = ["CoreOptions"]


Expand All @@ -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):
Expand All @@ -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__}("]
Expand All @@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit 7bb0603

Please sign in to comment.