and this time for realzies

This commit is contained in:
Valentin Boettcher 2022-03-23 14:47:47 +01:00
parent becb9c09e3
commit e6bb56c6d0
2 changed files with 201 additions and 0 deletions

View file

@ -0,0 +1,68 @@
from two_qubit_model import TwoQubitModel
from typing import Optional
import tempfile
from hops.core.integration import HOPSSupervisor
from hops.core.hierarchy_parameters import HIParams
from dataclasses import dataclass
import numpy as np
@dataclass
class TruncationToleranceData:
tol: float
"""The tolerance parameter for ``BathMemory`` truncation scheme."""
number_of_indices: int
"""The number of indices given by the ``BathMemory`` truncation scheme"""
number_of_indices_ref: int
"""The reference number of indices given by the ``Simplex`` truncation scheme"""
relative_error: float
"""The maximum norm deviation of the ``BathMemory`` trajectory
from the reference ``Simplex`` trajectory divided by the maximum
of the norm of the reverence trajectory."""
k_max_ref: int
"""The depth of the reference ``Simplex`` truncation scheme."""
def get_one_trajctory(config: HIParams) -> np.ndarray:
"""The"""
def find_tol(
model: TwoQubitModel,
k_max_ref: int = 6,
start_tol: int = 4,
relative_error: float = 0.1,
) -> Optional[float]:
"""Find the tolerance for the ``BathMemory`` truncation scheme.
:param model: The model configuration.
:param k_max_ref: The depth of the reference ``Simplex``
truncation scheme.
:param start_tol: The start tolerance of the ``BathMemory``
truncation scheme.
:param relative_error: The maximum allowed norm deviation of the
``BathMemory`` trajectory from the reference ``Simplex``
trajectory divided by the maximum of the norm of the
reverence trajectory.
:returns: The required tolerance parameter for ``BathMemory``
truncation scheme or ``None`` if none could be found.
"""
simplex_model = model.copy()
simplex_model.truncation_scheme = "simplex"
simplex_model.k_max = k_max_ref
simplex_config = simplex_model.hops_config
with tempfile.TemporaryDirectory() as data_dir:
simplex_sup = HOPSSupervisor(
simplex_config,
number_of_samples=1,
data_path=data_dir,
data_name="data_conv",
)

133
hiro_models/utility.py Normal file
View file

@ -0,0 +1,133 @@
from functools import singledispatchmethod
import dataclasses
from dataclasses import dataclass
from beartype import beartype
import json
import numpy as np
import qutip as qt
from typing import Any, Union
import hashlib
@beartype
@dataclass
class StocProcTolerances:
"""
An object to hold tolerances for :any:`stocproc.StocProc`
instances.
"""
integration: float = 1e-4
"""Integration tolerance."""
interpolation: float = 1e-4
"""Interpolation tolerance."""
class JSONEncoder(json.JSONEncoder):
"""
A custom encoder to serialize objects occuring in
:any:`TwoQubitModel`.
"""
@singledispatchmethod
def default(self, obj: Any):
return super().default(obj)
@default.register
def _(self, arr: np.ndarray):
return {"type": "array", "value": arr.tolist()}
@default.register
def _(self, obj: qt.Qobj):
return {
"type": "Qobj",
"value": obj.full(),
"dims": obj.dims,
"obj_type": obj.type,
}
@default.register
def _(self, obj: complex):
return {"type": "complex", "re": obj.real, "im": obj.imag}
@default.register
def _(self, obj: StocProcTolerances):
return {"type": "StocProcTolerances", "value": dataclasses.asdict(obj)}
@classmethod
def dumps(cls, data, **kwargs) -> str:
"""Like :any:`json.dumps`, just for this encoder.
The ``kwargs`` are passed on to :any:`json.dumps`.
"""
return json.dumps(
data,
**kwargs,
cls=cls,
ensure_ascii=False,
)
@classmethod
def loads(cls, string: str, **kwargs) -> dict[str, Any]:
"""Like :any:`json.loads`, just for this encoder.
The ``kwargs`` are passed on to :any:`json.loads`.
"""
return json.loads(string, object_hook=object_hook, **kwargs)
@classmethod
def hash(cls, data, **kwargs):
"""
Like :any:`dumps`, only that the result is being piped into
:any:`hashlib.sha256`. A ``sha256`` hash is being returned.
"""
return hashlib.sha256(cls.dumps(data, **kwargs).encode("utf-8"))
@classmethod
def hexhash(cls, data, **kwargs) -> str:
"""
Like :any:`hash`, only that a hexdigest is being returned.
"""
return cls.hash(data, **kwargs).hexdigest()
def object_hook(dct: dict[str, Any]):
"""A custom decoder for the types introduced in :any:`JSONEncoder`."""
if "type" in dct:
type = dct["type"]
if type == "array":
return np.array(dct["value"])
if type == "Qobj":
return qt.Qobj(dct["value"], dims=dct["dims"], type=dct["obj_type"])
if type == "complex":
return dct["re"] + 1j * dct["im"]
if type == "StocProcTolerances":
return StocProcTolerances(**dct["value"])
return dct
def operator_norm(obj: qt.Qobj) -> float:
"""Returns the operator norm of ``obj``."""
return np.sqrt(max(np.abs((obj.dag() * obj).eigenenergies())))
def assert_serializable(model):
"""
Serialize and restore ``model`` into json asserting that the
objects stay the same.
"""
assert model == model.__class__.from_json(
model.to_json()
), "Serialization should not change the model."