Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gsuarezr committed Oct 24, 2024
1 parent d354330 commit b126e97
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
55 changes: 27 additions & 28 deletions qutip/core/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def approx_by_cf_fit(
self, tlist: ArrayLike, target_rsme: float = 2e-5, Nr_max: int = 10,
Ni_max: int = 10, guess: list[float] = None, lower: list[float] = None,
upper: list[float] = None, full_ansatz: bool = False, tag: Any = None,
combine: bool =True,sigma:float= 1e-4,maxfev:int=100_000
combine: bool = True, sigma: float = 1e-4, maxfev: int = 100_000
) -> tuple[ExponentialBosonicEnvironment, dict[str, Any]]:
r"""
Generates an approximation to this environment by fitting its
Expand Down Expand Up @@ -444,10 +444,10 @@ def approx_by_cf_fit(
operator). See :meth:`combine` for details.
sigma : float, default 1e-4
The uncertainty in the date provided, to calculate uncertainty
residuals, it is useful to decrease if the values of the data are
residuals, it is useful to decrease if the values of the data are
too small
maxfev: int, default 100_000
Number of times the parameters of the fit are allowed to vary
Number of times the parameters of the fit are allowed to vary
during the optimization
Returns
-------
Expand Down Expand Up @@ -514,7 +514,7 @@ def approx_by_cf_fit(
start_real = time()
rmse_real, params_real = iterated_fit(
_cf_real_fit_model, num_params, tlist, np.real(clist), target_rsme,
guess_re, Nr_min, Nr_max, lower_re, upper_re,sigma=sigma,
guess_re, Nr_min, Nr_max, lower_re, upper_re, sigma=sigma,
maxfev=maxfev
)
end_real = time()
Expand All @@ -525,7 +525,7 @@ def approx_by_cf_fit(
rmse_imag, params_imag = iterated_fit(
_cf_imag_fit_model, num_params, tlist, np.imag(clist), target_rsme,
guess_im, Ni_min, Ni_max, lower_im, upper_im,
sigma=sigma,maxfev=maxfev
sigma=sigma, maxfev=maxfev
)
end_imag = time()
fit_time_imag = end_imag - start_imag
Expand Down Expand Up @@ -567,14 +567,14 @@ def approx_by_cf_fit(
vkAI.extend([-b - 1j * c, -b + 1j * c])

approx_env = ExponentialBosonicEnvironment(
ckAR, vkAR, ckAI, vkAI, T=self.T, tag=tag,combine=combine)
ckAR, vkAR, ckAI, vkAI, T=self.T, tag=tag, combine=combine)
return approx_env, fit_info

def approx_by_sd_fit(
self, wlist: ArrayLike, Nk: int = 1, target_rsme: float = 5e-6,
Nmax: int = 10, guess: list[float] = None, lower: list[float] = None,
upper: list[float] = None, tag: Any = None,combine: bool=True,
sigma:float =1e-4,maxfev:int=1e-6
upper: list[float] = None, tag: Any = None, combine: bool = True,
sigma: float = 1e-4, maxfev: int = 1e-6
) -> tuple[ExponentialBosonicEnvironment, dict[str, Any]]:
r"""
Generates an approximation to this environment by fitting its spectral
Expand Down Expand Up @@ -631,10 +631,10 @@ def approx_by_sd_fit(
operator). See :meth:`combine` for details.
sigma : float, default 1e-4
The uncertainty in the date provided, to calculate uncertainty
residuals, it is useful to decrease if the values of the data are
residuals, it is useful to decrease if the values of the data are
too small
maxfev: int, default 100_000
Number of times the parameters of the fit are allowed to vary
Number of times the parameters of the fit are allowed to vary
during the optimization
Returns
-------
Expand Down Expand Up @@ -676,7 +676,7 @@ def approx_by_sd_fit(
start = time()
rmse, params = iterated_fit(
_sd_fit_model, 3, wlist, jlist, target_rsme, guess,
Nmin, Nmax, lower, upper,sigma=sigma,maxfev=maxfev
Nmin, Nmax, lower, upper, sigma=sigma, maxfev=maxfev
)
end = time()
fit_time = end - start
Expand Down Expand Up @@ -705,7 +705,7 @@ def approx_by_sd_fit(
vkAI.extend(coeffs[3])

approx_env = ExponentialBosonicEnvironment(
ckAR, vkAR, ckAI, vkAI, T=self.T, tag=tag,combine=combine)
ckAR, vkAR, ckAI, vkAI, T=self.T, tag=tag, combine=combine)
return approx_env, fit_info


Expand Down Expand Up @@ -929,11 +929,11 @@ def approx_by_matsubara(
"""
if tag is None and self.tag is not None:
tag = (self.tag, "Matsubara Truncation")
if self.T==0:
raise warnings.warn("The Matsubara coefficients at T=0 do"
"not improve with the number of exponents."
" Other approaches such as fitting the "
"correlation function provide better results.")
if self.T == 0:
raise warnings.warn("The Matsubara coefficients at T=0 do"
"not improve with the number of exponents."
" Other approaches such as fitting the "
"correlation function provide better results.")

lists = self._matsubara_params(Nk)
approx_env = ExponentialBosonicEnvironment(
Expand Down Expand Up @@ -1023,8 +1023,8 @@ def _matsubara_params(self, Nk):
# --- Pade approx calculation ---

def _corr(self, Nk):
if self.T==0:
beta=np.inf
if self.T == 0:
beta = np.inf
else:
beta = 1. / self.T
kappa, epsilon = self._kappa_epsilon(Nk)
Expand All @@ -1039,7 +1039,7 @@ def _corr(self, Nk):
self.gamma * (epsilon[ll] * self.T)
/ ((epsilon[ll]**2 * self.T**2) - self.gamma**2)
)
gamma_p.append(epsilon[ll] *self.T)
gamma_p.append(epsilon[ll] * self.T)

return eta_p, gamma_p

Expand Down Expand Up @@ -1220,11 +1220,11 @@ def approx_by_matsubara(

def _matsubara_params(self, Nk):
""" Calculate the Matsubara coefficients and frequencies. """
if self.T==0:
raise warnings.warn("The Matsubara coefficients at T=0 do"
"not improve with the number of exponents."
" Other approaches such as fitting the "
"correlation function provide better results.")
if self.T == 0:
raise warnings.warn("The Matsubara coefficients at T=0 do"
"not improve with the number of exponents."
" Other approaches such as fitting the "
"correlation function provide better results.")
Om = np.sqrt(self.w0**2 - (self.gamma / 2)**2)
Gamma = self.gamma / 2

Expand All @@ -1236,7 +1236,7 @@ def _matsubara_params(self, Nk):
])

ck_real.extend([
(-2 * self.lam**2 * self.gamma *self.T) * (2 * np.pi * k * self.T)
(-2 * self.lam**2 * self.gamma * self.T) * (2 * np.pi * k * self.T)
/ (
((Om + 1j * Gamma)**2 + (2 * np.pi * k * self.T)**2)
* ((Om - 1j * Gamma)**2 + (2 * np.pi * k * self.T)**2)
Expand Down Expand Up @@ -1636,7 +1636,6 @@ def __init__(
exponents = self.combine(exponents)
self.exponents = exponents


@classmethod
def combine(
cls, exponents: Sequence[CFExponent],
Expand Down Expand Up @@ -2285,7 +2284,7 @@ def approx_by_matsubara(
ck_minus, vk_minus = self._matsubara_params(Nk, -1)

return ExponentialFermionicEnvironment(
ck_plus, vk_plus, ck_minus, vk_minus, T=self.T, mu=self.mu,
ck_plus, vk_plus, ck_minus, vk_minus, T=self.T, mu=self.mu,
tag=tag
)

Expand Down
27 changes: 16 additions & 11 deletions qutip/solver/heom/bofin_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class HierarchyADOs:
labels: list of tuples
A list of the ADO labels within the hierarchy.
"""

def __init__(self, exponents, max_depth):
self.exponents = exponents
self.max_depth = max_depth
Expand Down Expand Up @@ -716,7 +717,7 @@ def _combine_bath_exponents(self, bath):
""" Combine the exponents for the specified baths. """
# Only one bath provided, not a list of baths
if (not isinstance(bath, (list, tuple))
or self._is_environment_api(bath)):
or self._is_environment_api(bath)):
bath = [bath]
exponents = []
for b in bath:
Expand All @@ -725,10 +726,12 @@ def _combine_bath_exponents(self, bath):
b = self._env_to_bath(b)
except ValueError as ve:
raise ValueError(ve.args[0] +
" When passing environments and baths make sure to"
" include the coupling operators for the environments"
" [(env,Q),bath] is correct while [env,bath] is not"
)
" When passing environments and baths"
" make sure to include the coupling "
"operators for the environments"
" [(env,Q),bath] is correct "
"while [env,bath] is not"
)
exponents.extend(b.exponents)

if not all(exp.Q.dims == exponents[0].Q.dims for exp in exponents):
Expand All @@ -740,18 +743,19 @@ def _combine_bath_exponents(self, bath):
return exponents

def _is_environment_api(self, bath_spec):
is_list =isinstance(bath_spec, (list, tuple))
is_list = isinstance(bath_spec, (list, tuple))
if is_list:
env_syntax= (
env_syntax = (
isinstance(bath_spec[0], ExponentialBosonicEnvironment)
or isinstance(bath_spec[0], ExponentialFermionicEnvironment)
)
)
is_bath = (
isinstance(bath_spec[0], BosonicBath)
or isinstance(bath_spec[0],FermionicBath)
or isinstance(bath_spec[0], FermionicBath)
)
else:
if type(bath_spec) in [ExponentialBosonicEnvironment,ExponentialFermionicEnvironment]:
if ((type(bath_spec) == ExponentialBosonicEnvironment) or
(type(bath_spec) == ExponentialFermionicEnvironment)):
raise ValueError("Enviroments must be passed with their"
" corresponding coupling operator as a list"
" or tuple (env,Q)")
Expand All @@ -760,7 +764,6 @@ def _is_environment_api(self, bath_spec):
return False
else:
return is_list and env_syntax


def _env_to_bath(self, bath_spec):
if isinstance(bath_spec[0], ExponentialBosonicEnvironment):
Expand Down Expand Up @@ -1297,6 +1300,7 @@ class HSolverDL(HEOMSolver):
operator). See :meth:`BosonicBath.combine` for details.
Keyword only. Default: True.
"""

def __init__(
self, H_sys, coup_op, coup_strength, temperature,
N_cut, N_exp, cut_freq, *, bnd_cut_approx=False, options=None,
Expand Down Expand Up @@ -1345,6 +1349,7 @@ class _GatherHEOMRHS:
nhe : int
The number of ADOs in the hierarchy.
"""

def __init__(self, f_idx, block, nhe):
self._block_size = block
self._n_blocks = nhe
Expand Down
11 changes: 6 additions & 5 deletions qutip/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def iterated_fit(
guess: ArrayLike | Callable[[int], ArrayLike] = None,
Nmin: int = 1, Nmax: int = 10,
lower: ArrayLike = None, upper: ArrayLike = None,
sigma:float=1e-4,maxfev:int=100_000
sigma: float = 1e-4, maxfev: int = 100_000
) -> tuple[float, ArrayLike]:
r"""
Iteratively tries to fit the given data with a model of the form
Expand Down Expand Up @@ -436,7 +436,8 @@ def iterated_fit(
lower_repeat = np.tile(lower, N)
upper_repeat = np.tile(upper, N)
rmse1, params = _fit(fun, num_params, xdata, ydata,
guesses, lower_repeat, upper_repeat,sigma,maxfev)
guesses, lower_repeat,
upper_repeat, sigma, maxfev)
N += 1

return rmse1, params
Expand Down Expand Up @@ -476,8 +477,8 @@ def _rmse(fun, xdata, ydata, params):
)


def _fit(fun, num_params, xdata, ydata, guesses, lower, upper,sigma,
maxfev,method='trf'):
def _fit(fun, num_params, xdata, ydata, guesses, lower, upper, sigma,
maxfev, method='trf'):
# fun: model function
# num_params: number of parameters in fun
# xdata, ydata: data to be fit
Expand All @@ -495,7 +496,7 @@ def _fit(fun, num_params, xdata, ydata, guesses, lower, upper,sigma,
fun, x, _unpack(packed_params, num_params)
),
xdata, ydata, p0=_pack(guesses), bounds=(lower, upper),
maxfev=maxfev, method=method,sigma=sigma
maxfev=maxfev, method=method, sigma=sigma
)
params = _unpack(packed_params, num_params)
rmse = _rmse(fun, xdata, ydata, params)
Expand Down

0 comments on commit b126e97

Please sign in to comment.