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
approx(t: numpy.ndarray) numpy.ndarray[source]

The BCF as exponential expansion.

Parameters

t (numpy.ndarray) –

Return type

numpy.ndarray

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 a TypeError).

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 op to each element of the time series ψ of the dimensions (*, dim) where dim is the hilbert space dimension.

Parameters
Return type

numpy.ndarray

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

numpy.ndarray

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]
Parameters
Return type

hopsflow.util.EnsembleValue

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 left and right. The standard deviations are calculated correctly by adding the variances.

Parameters
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 right by left.

Parameters
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 array except the element with index.

Parameters
Return type

numpy.ndarray

hopsflow.util.expand_t(f)[source]
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 of n exponentials up to t_max using a number of support_points.

The fit result will be cached if with_cache is True.

Parameters
Return type

tuple[numpy.ndarray, numpy.ndarray]

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 arr along t. Optionally the error err is being integrated alongside.

Parameters
Return type

Union[numpy.ndarray, tuple[numpy.ndarray, numpy.ndarray]]

hopsflow.util.mulitply_hierarchy(left: numpy.ndarray, right: numpy.ndarray) numpy.ndarray[source]

Multiply each hierarchy member with a member of left for 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

numpy.ndarray

hopsflow.util.operator_expectation(ρ: numpy.ndarray, op: numpy.ndarray, real: bool = False) numpy.ndarray[source]

Calculates the expecation value of op as 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

numpy.ndarray

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 op as 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
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

p (numpy.polynomial.polynomial.Polynomial) –

Return type

numpy.polynomial.polynomial.Polynomial

hopsflow.util.sandwhich_operator(ψ: numpy.ndarray, op: numpy.ndarray, normalize: bool = False, real: bool = False) numpy.ndarray[source]

Applies the operator op to each element of the time series ψ of the dimensions (*, dim) where dim is the hilbert space dimension and sandwiches ψ onto it from the left. If normalize is True then the value will be divided by the squared norm. If real is True, the real part is returned.

Parameters
Return type

numpy.ndarray

hopsflow.util.uni_to_gauss(x: numpy.ndarray)[source]

Transforms x into len(x)/2 normal 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
Returns

the exponential expansion evaluated at τ

Return type

numpy.ndarray