Util¶
Utilities for the energy flow calculation.
- class hopsflow.util.BCF(t_max: float, function: Optional[Callable[[numpy.ndarray], numpy.ndarray]] = None, num_terms: Optional[int] = None, resolution: Optional[float] = None, factors: Optional[numpy.ndarray] = None, exponents: Optional[numpy.ndarray] = None)[source]¶
A parameter object to hold information about a BCF.
The BCFs will be expanded into a sum of exponentials like \(\alpha(\tau) = \sum_k G_k \cdot \exp(-W_k\cdot\tau)\). You can either give the BCFs as parameter or the coefficients. If you give the BCFs, the fit will be performed automatically.
Calling this object will call the wrapped BCF function.
- Parameters
resolution (Optional[float]) – the precision in the sampling for the fit,
t_max/precisionpoints will be usednum_terms (Optional[int]) – the number of terms of the expansion of the BCF expansion
t_max (float) –
function (Optional[Callable[[numpy.ndarray], numpy.ndarray]]) –
factors (Optional[numpy.ndarray]) –
exponents (Optional[numpy.ndarray]) –
- approx(t: numpy.ndarray) numpy.ndarray[source]¶
The BCF as exponential expansion.
- Parameters
t (numpy.ndarray) –
- Return type
- exponents¶
the exponents in the BCF expansion
- factors¶
the factors in the BCF expansion
- function¶
the BCF as python function, will be set to the exponential expansion if the BCF coefficients are given.
- t_max¶
the maximum simulation time
- class hopsflow.util.BCFDist(α: Callable[[numpy.ndarray], numpy.ndarray], **kwargs)[source]¶
A distribution based on the absolute value of the BCF.
- Parameters
α (Callable[[numpy.ndarray], numpy.ndarray]) –
- class hopsflow.util.EnsembleValue(value: Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]])[source]¶
- Parameters
value (Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]]) –
- property N¶
- property Ns¶
- property aggregate_iterator¶
- property ensemble_value_iterator¶
- property final_aggregate¶
- insert(value: tuple[int, numpy.ndarray, numpy.ndarray])[source]¶
- Parameters
value (tuple[int, numpy.ndarray, numpy.ndarray]) –
- insert_multi(values: list[tuple[int, numpy.ndarray, numpy.ndarray]])[source]¶
- Parameters
values (list[tuple[int, numpy.ndarray, numpy.ndarray]]) –
- property value¶
- property values¶
- property σ¶
- property σs¶
- class hopsflow.util.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]¶
A custom encoder to serialize objects occuring in
ensemble_mean.- default(obj: Any)[source]¶
Implement this method in a subclass such that it returns a serializable object for
o, or calls the base implementation (to raise aTypeError).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- Parameters
obj (Any) –
- class hopsflow.util.WelfordAggregator(first_value: numpy.ndarray)[source]¶
- Parameters
first_value (numpy.ndarray) –
- property ensemble_std: numpy.ndarray¶
- property ensemble_variance: numpy.ndarray¶
- mean¶
- n¶
- property sample_variance: numpy.ndarray¶
- update(new_value: numpy.ndarray)[source]¶
- Parameters
new_value (numpy.ndarray) –
- hopsflow.util.apply_operator(ψ: numpy.ndarray, op: numpy.ndarray) numpy.ndarray[source]¶
Applies the operator
opto each element of the time series ψ of the dimensions(*, dim)wheredimis the hilbert space dimension.- Parameters
ψ (numpy.ndarray) –
op (numpy.ndarray) –
- Return type
- hopsflow.util.dot_with_hierarchy(left: numpy.ndarray, right: numpy.ndarray) numpy.ndarray[source]¶
Calculates \(\sum_k \langle\mathrm{left} | \mathrm{right}^{(e_k)}\rangle\) for each time step.
- Parameters
left (numpy.ndarray) – array of shape
(time-steps, system-dimension, hierarchy-width,)right (numpy.ndarray) – array of shape
(time-steps, hierarchy-width, system-dimension)
- Return type
- hopsflow.util.ensemble_mean(arg_iter: Iterator[Any], function: Callable[[...], numpy.ndarray], N: Optional[int] = None, every: Optional[int] = None, save: Optional[str] = None, overwrite_cache: bool = False, chunk_size: int = 20) hopsflow.util.EnsembleValue[source]¶
- hopsflow.util.ensemble_return_add(left: Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]], right: Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]]) Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]][source]¶
Adds the values of
leftandright. The standard deviations are calculated correctly by adding the variances.- Parameters
left (Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]]) –
right (Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]]) –
- Return type
Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]]
- hopsflow.util.ensemble_return_scale(left: float, right: Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]]) Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]][source]¶
Scales
rightbyleft.- Parameters
left (float) –
right (Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]]) –
- Return type
Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]]
- hopsflow.util.except_element(array: numpy.ndarray, index: int) numpy.ndarray[source]¶
Returns the
arrayexcept the element withindex.- Parameters
array (numpy.ndarray) –
index (int) –
- Return type
- hopsflow.util.fit_α(α: Callable[[numpy.ndarray], numpy.ndarray], n: int, t_max: float, support_points: int = 1000, with_cache: bool = True) tuple[numpy.ndarray, numpy.ndarray][source]¶
Fit the BCF
αto a sum ofnexponentials up tot_maxusing a number ofsupport_points.The fit result will be cached if
with_cacheisTrue.- Parameters
α (Callable[[numpy.ndarray], numpy.ndarray]) –
n (int) –
t_max (float) –
support_points (int) –
with_cache (bool) –
- Return type
- hopsflow.util.integrate_array(arr: numpy.ndarray, t: numpy.ndarray, err: Optional[numpy.ndarray]) Union[numpy.ndarray, tuple[numpy.ndarray, numpy.ndarray]][source]¶
Calculates the antiderivative of the function sampled in
arralongt. Optionally the errorerris being integrated alongside.- Parameters
arr (numpy.ndarray) –
t (numpy.ndarray) –
err (Optional[numpy.ndarray]) –
- Return type
- hopsflow.util.mulitply_hierarchy(left: numpy.ndarray, right: numpy.ndarray) numpy.ndarray[source]¶
Multiply each hierarchy member with a member of
leftfor each time step.- Parameters
left (numpy.ndarray) – array of shape
(hierarchy-width,)right (numpy.ndarray) – array of shape
(time-steps, hierarchy-width, system-dimension)
- Return type
- hopsflow.util.operator_expectation(ρ: numpy.ndarray, op: numpy.ndarray, real: bool = False) numpy.ndarray[source]¶
Calculates the expecation value of
opas a time series.- Parameters
ρ (numpy.ndarray) – The state as time series.
(time, dim-sys, dim-sys)op (numpy.ndarray) – The operator.
real (bool) – Whether to take the real part.
- Returns
the expectation value
- Return type
- hopsflow.util.operator_expectation_ensemble(ψs: Iterator[numpy.ndarray], op: numpy.ndarray, normalize: bool = False, real: bool = False, **kwargs) Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]][source]¶
Calculates the expecation value of
opas a time series.- Parameters
ψs (Iterator[numpy.ndarray]) – A collection of stochastic trajectories. Each element should have the shape
(time, dim-sys).op (numpy.ndarray) – The operator.
N – Number of samples to take.
real (bool) – Whether to take the real part.
normalize (bool) –
- Return type
Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]]
All the other kwargs are passed on to
ensemble_mean.- Returns
the expectation value
- Parameters
ψs (Iterator[numpy.ndarray]) –
op (numpy.ndarray) –
normalize (bool) –
real (bool) –
- Return type
Union[tuple[int, numpy.ndarray, numpy.ndarray], list[tuple[int, numpy.ndarray, numpy.ndarray]]]
- hopsflow.util.poly_real(p: numpy.polynomial.polynomial.Polynomial) numpy.polynomial.polynomial.Polynomial[source]¶
Return the real part of
p.- Parameters
- Return type
- hopsflow.util.sandwhich_operator(ψ: numpy.ndarray, op: numpy.ndarray, normalize: bool = False, real: bool = False) numpy.ndarray[source]¶
Applies the operator
opto each element of the time seriesψof the dimensions(*, dim)wheredimis the hilbert space dimension and sandwichesψonto it from the left. IfnormalizeisTruethen the value will be divided by the squared norm. IfrealisTrue, the real part is returned.- Parameters
ψ (numpy.ndarray) –
op (numpy.ndarray) –
normalize (bool) –
real (bool) –
- Return type
- hopsflow.util.uni_to_gauss(x: numpy.ndarray)[source]¶
Transforms
xintolen(x)/2normal distributed numbers.- Parameters
x (numpy.ndarray) –
- hopsflow.util.α_apprx(τ: numpy.ndarray, G: numpy.ndarray, W: numpy.ndarray) numpy.ndarray[source]¶
Calculate exponential expansion $sum_i G_i exp(W_i * τ)$ of the BCF along
τ.- Parameters
τ (numpy.ndarray) – the time
G (numpy.ndarray) – pefactors
W (numpy.ndarray) – exponents
- Returns
the exponential expansion evaluated at
τ- Return type