Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bosonic fitting #2260

Draft
wants to merge 107 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
1a2e5d0
Added FitBath and OhmicBath classes, fix spelling on solver_base
gsuarezr Oct 11, 2023
27ed772
notebooks as example
gsuarezr Oct 11, 2023
3ba219d
formatting
gsuarezr Oct 11, 2023
40a914d
added tests
gsuarezr Oct 13, 2023
3edecc0
Merge branch 'qutip:master' into master
gsuarezr Oct 19, 2023
f51033a
Make fit classes inherit, which broke tests, refactored functions and…
gsuarezr Oct 19, 2023
c880375
minor refactoring
gsuarezr Oct 24, 2023
7080102
replaced tests to match new classes
gsuarezr Oct 24, 2023
390c596
added parity support changed prints to __str__
gsuarezr Oct 25, 2023
fa563b5
deleted innecessary comments and code
gsuarezr Oct 25, 2023
c6a7d4e
Rewrote summary methods,added comments,eliminated unnecessary code
gsuarezr Oct 26, 2023
5c03d92
Rewrote summary methods,added comments,eliminated unnecessary code
gsuarezr Oct 26, 2023
5f0003c
moving parity to another branch
gsuarezr Oct 27, 2023
3c1fcbe
Merge branch 'qutip:master' into master
gsuarezr Oct 27, 2023
76a135b
aligned the text in summary
gsuarezr Oct 30, 2023
3242c97
removed rwa stuff
gsuarezr Oct 31, 2023
228d763
deleted unnecessary code
gsuarezr Nov 2, 2023
13a0b1c
changed formatting from black to autopep8
gsuarezr Nov 2, 2023
02c6d99
changed formatting from black to autopep8
gsuarezr Nov 2, 2023
2361a6c
removed typo from the tests file
gsuarezr Nov 2, 2023
3a0c493
moved plots, added functions so tutorial looks better
gsuarezr Nov 7, 2023
cd4b034
Merge branch 'qutip:master' into bosonic_fitting
gsuarezr Nov 8, 2023
eaa7bd7
modified code to satisfy pycodestyle
gsuarezr Nov 8, 2023
82ae9f3
modified code to satisfy pycodestyle
gsuarezr Nov 8, 2023
5fe5932
fixed string formatting
gsuarezr Nov 8, 2023
e36c9ab
added functions for correlation functions,spectral densities and powe…
gsuarezr Nov 8, 2023
a507467
try catch on mpmath to avoid tests failing
gsuarezr Nov 8, 2023
6463371
removed warning
gsuarezr Nov 9, 2023
a2219c7
fixed tests after refactoring
gsuarezr Nov 9, 2023
013fb2c
refactored according to codeclimate
gsuarezr Nov 10, 2023
336a04d
fixed tests that fail due to refactoring
gsuarezr Nov 10, 2023
b5dfdfc
fixed failed tests
gsuarezr Nov 13, 2023
c3e3457
added changelog, fix tests issue
gsuarezr Nov 13, 2023
7300aad
pep8 issues
gsuarezr Nov 13, 2023
7f75616
Remove Bosonic class instances, renamed functions, fixed tests
gsuarezr Nov 20, 2023
357104b
skip tests where mpmath is required
gsuarezr Nov 20, 2023
662b7e3
removed exception
gsuarezr Nov 20, 2023
ac174ca
codeclimate refactoring suggestions
gsuarezr Nov 20, 2023
f899877
changes proposed monday
gsuarezr Nov 29, 2023
1ef2240
Merge branch 'qutip:master' into bosonic_fitting
gsuarezr Dec 4, 2023
2ee86b7
Fixed typo in file name
pmenczel Dec 7, 2023
382b63a
correction of tests
gsuarezr Dec 8, 2023
b3dfeac
broken after pull, commit to see diff
gsuarezr Dec 8, 2023
dfa31e0
changed tests to match refactor
gsuarezr Dec 11, 2023
7cc95ec
refactored/ changed tests
gsuarezr Dec 11, 2023
67a7178
I had forgotten pytest fixtures on some tests
gsuarezr Dec 11, 2023
94b192f
fixed spectral density on Pade Bath
gsuarezr Dec 11, 2023
b7ce167
refactored and added tests
gsuarezr Dec 18, 2023
1f76959
cleaned visualization.py
gsuarezr Dec 18, 2023
50f771b
pep8 formatting
gsuarezr Dec 18, 2023
e398051
codeclimate issues
gsuarezr Dec 18, 2023
a60cf8f
filtered overflow warning on integrals
gsuarezr Dec 18, 2023
38f003d
Merge branch 'qutip:master' into bosonic_fitting
gsuarezr Dec 18, 2023
2724e22
filtered overflow warning on integrals
gsuarezr Dec 18, 2023
a240f35
Fixing small mistakes and adding some todo comments
pmenczel Dec 19, 2023
d4a579d
modified docstrings
gsuarezr Dec 20, 2023
712a492
correlation test
gsuarezr Dec 21, 2023
b039a10
suggestions from tuesday
gsuarezr Dec 22, 2023
122c3b2
skip test if mp not available
gsuarezr Dec 22, 2023
8e526c3
added zero temperature support to ohmic correlations
gsuarezr Jan 9, 2024
5db3ce7
Merge branch 'qutip:master' into bosonic_fitting
gsuarezr Jan 15, 2024
85fa0d2
minor typos, failing fit test fix
gsuarezr Jan 16, 2024
9557c82
reduce tests time, mark generate bath as slow
gsuarezr Jan 16, 2024
fa3c8f7
Moved Nk of the SpectralFitter class from initialization to get_fit, …
gsuarezr Jan 24, 2024
4595496
Added a few lines so fitting correlation functions with no imaginary …
gsuarezr Jan 24, 2024
27646f3
accidentaly used wrong formatter
gsuarezr Jan 24, 2024
3b45396
accidentaly used wrong formatter
gsuarezr Jan 25, 2024
c218a89
codeclimate details
gsuarezr Jan 25, 2024
20af021
fix broken tests
gsuarezr Jan 25, 2024
98c08e6
Made some docstrings more clear
pmenczel Jan 25, 2024
28cc484
Merge branch 'bosonic_fitting' of github.com:mcditoos/qutip_gsoc_app …
pmenczel Jan 25, 2024
70cc2f5
Simplify correlation function / power spectrum calculations by assumi…
pmenczel Jan 25, 2024
cbf4629
Small updates for D-L bath classes
pmenczel Jan 25, 2024
0033ea2
Changed default number of exponents in exact correlation function to …
pmenczel Jan 26, 2024
72b0018
Updated docstrings, formatting and minor improvements in bofin_fit mo…
pmenczel Jan 26, 2024
6827344
Bugfixes
pmenczel Jan 26, 2024
55d5226
HEOM fitting: minor fixes
pmenczel Jan 26, 2024
1a60dc6
Work on docstrings
gsuarezr Feb 3, 2024
9a8e3c8
Modified ansatz and better guesses
gsuarezr Feb 4, 2024
18e17d1
new ansatz
gsuarezr Feb 4, 2024
45211a3
docstrings
gsuarezr Feb 4, 2024
0e322f2
Some TODOS
gsuarezr Feb 4, 2024
556a4ec
puntuation on docstrings
gsuarezr Feb 4, 2024
88cae59
Working version before refactoring
gsuarezr Feb 4, 2024
96568b4
Merged fix correlation ansatz
gsuarezr Feb 4, 2024
9952111
codeclimate suggested refactoring
gsuarezr Feb 4, 2024
68139e5
Update qutip/solver/heom/bofin_baths.py
gsuarezr Feb 4, 2024
9ed0f7b
failed tests
gsuarezr Feb 4, 2024
bbc0af5
Prefer classmethod over staticmethod
pmenczel Feb 6, 2024
da6172f
Some docstring formatting
pmenczel Feb 6, 2024
15c2bab
Work on Docstrings, made test that fails due to time to a simpler one
gsuarezr Feb 10, 2024
3928d4d
Merge branch 'qutip:master' into bosonic_fitting
gsuarezr Feb 19, 2024
12d694b
minor pep8 issues
gsuarezr Feb 19, 2024
e2e40b4
changes in documentation
gsuarezr Feb 19, 2024
498badd
removed returns that make documentation fail
gsuarezr Feb 19, 2024
fa8f237
reverted default guesses
gsuarezr Feb 19, 2024
bacffdd
Modified Ansatz to be general when Imaginary part of the correlation …
gsuarezr Feb 19, 2024
d953dcf
changed handling of user guesses
gsuarezr Feb 20, 2024
6803c61
readded returns that made documentation fail in another format
gsuarezr Feb 21, 2024
33b3889
codeclimate suggested refactor
gsuarezr Feb 21, 2024
dd3cfdf
Changed selection of correlation ansatz from imaginary part different…
gsuarezr Feb 25, 2024
bb4c763
latex documentation fix
gsuarezr Feb 25, 2024
937dae6
pop8 formatting
gsuarezr Feb 25, 2024
d42ebe7
Testing if latexpdf documentation breaks
gsuarezr Feb 28, 2024
1504426
documentation issues
gsuarezr Feb 29, 2024
d586a9d
added changes to missing docstrings
gsuarezr Mar 4, 2024
421b475
Merge branch 'qutip:master' into bosonic_fitting
gsuarezr Mar 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/2260.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for arbitrary spectral densities in HEOM by fitting underdamped modes, added an Ohmic Bath class
9 changes: 9 additions & 0 deletions qutip/solver/heom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"HSolverDL",
"HierarchyADOs",
"HierarchyADOsState",
"SpectralFitter",
"CorrelationFitter",
"OhmicBath",
]

from .bofin_baths import (
Expand All @@ -53,3 +56,9 @@
HierarchyADOs,
HierarchyADOsState,
)

from .bofin_fit import (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great that these got split out into their own module.

CorrelationFitter,
SpectralFitter,
OhmicBath,
)
138 changes: 118 additions & 20 deletions qutip/solver/heom/bofin_baths.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from qutip.core import data as _data
from qutip.core.qobj import Qobj
from qutip.core.superoperator import spre, spost
from scipy.integrate import quad_vec

__all__ = [
"BathExponent",
Expand Down Expand Up @@ -243,8 +244,9 @@ def _check_coup_op(self, Q):

def __init__(
self, Q, ck_real, vk_real, ck_imag, vk_imag, combine=True,
tag=None,
tag=None, T=None
):
self.T = T
self._check_cks_and_vks(ck_real, vk_real, ck_imag, vk_imag)
self._check_coup_op(Q)

Expand Down Expand Up @@ -337,6 +339,85 @@ def combine(cls, exponents, rtol=1e-5, atol=1e-7):

return new_exponents

def spectral_density(self, w):
raise NotImplemented

def correlation_function(
self, t, epsabs=1e-6, epsrel=1e-6, method='gk15'):
""" Calculates the correlation function
by numerically computing the integral
Parameters
----------
t : np.array or float
the time at which to evaluare the correlation function
epsabs : float
Absolute error tolerance
epsrel : float
Relative error tolerance
"""
def c_i(w, t): return (1/np.pi)*self.spectral_density(w)*(
(1/np.tanh(w/(2*self.T)))*np.cos(w*t) - 1j*np.sin(w*t))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to also have a check for zerodivisionerror here, and if it catches it use np.sign() or something instead?


def c(t): return quad_vec(
lambda x: c_i(x, t),
0,
np.Inf,
epsabs=epsabs,
epsrel=epsrel,
quadrature=method,
)[0]
return c(t)

def bose_einstein(self, w):
try:
return (1 / (np.e**(w / self.T) - 1))
except ZeroDivisionError:
return 0

def power_spectrum(self, w):
"""Calculates the power spectrum from the spectral density
"""
S = self.spectral_density(
w)*((self.bose_einstein(w) + 1) * 2)
return S

def correlation_function_approx(self, t):
"""Computes the correlation function from
the exponents"""
corr = 0+0j
for exp in self.exponents:
if (
exp.type == BathExponent.types['R'] or
exp.type == BathExponent.types['RI']
):
corr += exp.ck * np.exp(-exp.vk * t)
if exp.type == BathExponent.types['I']:
corr += 1j*exp.ck * np.exp(-exp.vk * t)
if exp.type == BathExponent.types['RI']:
corr += 1j*exp.ck2 * np.exp(-exp.vk * t)
return corr

def power_spectrum_approx(self, w):
S = 0+0j
for exp in self.exponents:
if exp.ck is None:
exp.ck = 0
if exp.ck2 is None:
exp.ck2 = 0
if exp.type == BathExponent.types['I']:
S += 2*np.real((1j*exp.ck-exp.ck2)/(exp.vk - 1j*w))
else:
S += 2*np.real((exp.ck+1j*exp.ck2)/(exp.vk - 1j*w))

return S

def spectral_density_approx(self, w):
J = np.real(
self.power_spectrum_approx(w) /
((self.bose_einstein(w) + 1) * 2)
)
return J


class DrudeLorentzBath(BosonicBath):
"""
Expand Down Expand Up @@ -373,16 +454,18 @@ class DrudeLorentzBath(BosonicBath):
def __init__(
self, Q, lam, gamma, T, Nk, combine=True, tag=None,
):
self.lam = lam
self.gamma = gamma
gsuarezr marked this conversation as resolved.
Show resolved Hide resolved
self.T = T
ck_real, vk_real, ck_imag, vk_imag = self._matsubara_params(
lam=lam,
gamma=gamma,
T=T,
Nk=Nk,
)

super().__init__(
Q, ck_real, vk_real, ck_imag, vk_imag, combine=combine, tag=tag,
)
super().__init__(Q, ck_real, vk_real, ck_imag,
vk_imag, combine=combine, tag=tag, T=T)

self._dl_terminator = _DrudeLorentzTerminator(
Q=Q, lam=lam, gamma=gamma, T=T,
Expand Down Expand Up @@ -427,6 +510,9 @@ def _matsubara_params(self, lam, gamma, T, Nk):
vk_imag = [gamma]

return ck_real, vk_real, ck_imag, vk_imag

def spectral_density(self, w):
return 2*self.lam*self.gamma*w/(self.gamma**2 + w**2)


class DrudeLorentzPadeBath(BosonicBath):
Expand All @@ -437,7 +523,7 @@ class DrudeLorentzPadeBath(BosonicBath):
A Padé approximant is a sum-over-poles expansion (
see https://en.wikipedia.org/wiki/Pad%C3%A9_approximant).

The application of the Padé method to spectrum decompoisitions is described
The application of the Padé method to spectrum decompositions is described
in "Padé spectrum decompositions of quantum distribution functions and
optimal hierarchical equations of motion construction for quantum open
systems" [1].
Expand Down Expand Up @@ -479,6 +565,8 @@ class DrudeLorentzPadeBath(BosonicBath):
def __init__(
self, Q, lam, gamma, T, Nk, combine=True, tag=None
):
self.lam = lam
self.gamma = gamma
eta_p, gamma_p = self._corr(lam=lam, gamma=gamma, T=T, Nk=Nk)

ck_real = [np.real(eta) for eta in eta_p]
Expand All @@ -488,9 +576,8 @@ def __init__(
ck_imag = [np.imag(eta_p[0])]
vk_imag = [gamma_p[0]]

super().__init__(
Q, ck_real, vk_real, ck_imag, vk_imag, combine=combine, tag=tag,
)
super().__init__(Q, ck_real, vk_real, ck_imag,
vk_imag, combine=combine, tag=tag, T=T)

self._dl_terminator = _DrudeLorentzTerminator(
Q=Q, lam=lam, gamma=gamma, T=T,
Expand Down Expand Up @@ -565,8 +652,8 @@ def _delta(self, i, j):

def _calc_eps(self, Nk):
alpha = np.diag([
1. / np.sqrt((2 * k + 5) * (2 * k + 3))
for k in range(2 * Nk - 1)
1. / np.sqrt((2 * k + 5) * (2 * k + 3))
for k in range(2 * Nk - 1)
], k=1)
alpha += alpha.transpose()
evals = eigvalsh(alpha)
Expand All @@ -575,14 +662,17 @@ def _calc_eps(self, Nk):

def _calc_chi(self, Nk):
alpha_p = np.diag([
1. / np.sqrt((2 * k + 7) * (2 * k + 5))
for k in range(2 * Nk - 2)
1. / np.sqrt((2 * k + 7) * (2 * k + 5))
for k in range(2 * Nk - 2)
], k=1)
alpha_p += alpha_p.transpose()
evals = eigvalsh(alpha_p)
chi = [-2. / val for val in evals[0: Nk - 1]]
return chi

def spectral_density(self, w):
return 2*self.lam*self.gamma*w/(self.gamma**2 + w**2)


class _DrudeLorentzTerminator:
""" A class for calculating the terminator of a Drude-Lorentz bath
Expand Down Expand Up @@ -662,12 +752,16 @@ def __init__(
T=T,
Nk=Nk,
)
self.lam = lam
self.gamma = gamma
self.w0 = w0
self.T = T

super().__init__(
Q, ck_real, vk_real, ck_imag, vk_imag, combine=combine, tag=tag,
)
super().__init__(Q, ck_real, vk_real, ck_imag,
vk_imag, combine=combine, tag=tag, T=T)

def _matsubara_params(self, lam, gamma, w0, T, Nk):
@staticmethod
def _matsubara_params(lam, gamma, w0, T, Nk):
""" Calculate the Matsubara coefficents and frequencies. """
beta = 1/T
Om = np.sqrt(w0**2 - (gamma/2)**2)
Expand Down Expand Up @@ -704,6 +798,10 @@ def _matsubara_params(self, lam, gamma, w0, T, Nk):

return ck_real, vk_real, ck_imag, vk_imag

def spectral_density(self, w):
return self.lam**2 * self.gamma * w / ((w**2 - self.w0**2)**2
+ (self.gamma*w)**2)


class FermionicBath(Bath):
"""
Expand Down Expand Up @@ -962,8 +1060,8 @@ def _delta(self, i, j):

def _calc_eps(self, Nk):
alpha = np.diag([
1. / np.sqrt((2 * k + 3) * (2 * k + 1))
for k in range(2 * Nk - 1)
1. / np.sqrt((2 * k + 3) * (2 * k + 1))
for k in range(2 * Nk - 1)
], k=1)
alpha += alpha.transpose()

Expand All @@ -973,8 +1071,8 @@ def _calc_eps(self, Nk):

def _calc_chi(self, Nk):
alpha_p = np.diag([
1. / np.sqrt((2 * k + 5) * (2 * k + 3))
for k in range(2 * Nk - 2)
1. / np.sqrt((2 * k + 5) * (2 * k + 3))
for k in range(2 * Nk - 2)
], k=1)
alpha_p += alpha_p.transpose()
evals = eigvalsh(alpha_p)
Expand Down