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

Electron-phonon extension #294

Open
wants to merge 74 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
f7310b8
fixed typo
m-baumgarten Feb 9, 2024
4ffbe56
holstein with toyozawa trial, coherent state trial todo
m-baumgarten Feb 9, 2024
fa6f13b
working toyozawa implementation for 1 electron, 1D Holstein
m-baumgarten Feb 16, 2024
caf7a34
multi-electron implementation. toyozawa very dodgy
m-baumgarten Feb 23, 2024
e7507f7
Some einsum cleanup
Mar 3, 2024
bf49a31
added variationcal code to ipie and example run file
Mar 4, 2024
2ef4fb8
holstein propagator cleanup
Mar 4, 2024
f0a10a6
moved files to adhere to addons infrastructure
Mar 5, 2024
582a24e
changed folder name from ipie/addons/ephqmc to ipie/addons/eph
Mar 5, 2024
f9d70d5
Merge branch 'develop' into multiel_holstein
fdmalone Mar 5, 2024
69e1ef5
changed directory name finally
Mar 5, 2024
7925ba9
Merge branch 'multiel_holstein' of github.com:m-baumgarten/ipie_holst…
Mar 5, 2024
ccc2649
Cleanup of HolsteinModel class plus some explanation of the model.
Mar 5, 2024
ce14a30
added __init__ files in new directories
m-baumgarten Mar 5, 2024
d839b23
cleaned up comments and added docstring
m-baumgarten Mar 5, 2024
00870e5
added energy estimator, more or less identical to the one in ipie/est…
m-baumgarten Mar 6, 2024
e2e703b
removed addon imports and dispatch to local_energy_holstein
m-baumgarten Mar 6, 2024
dab3687
enabled AFQMC.build by adding some dummy parameters to HolsteinPropag…
m-baumgarten Mar 6, 2024
ad5c2cd
added docstring to EphWalkers and moved initial trial.calc_overlap fr…
m-baumgarten Mar 6, 2024
e782941
renamed Eph classes to EPh
m-baumgarten Mar 7, 2024
096ea3f
fix optimize position
m-baumgarten Mar 7, 2024
b425c44
Docstrings and renaming of HolsteinPropagatorImportance
m-baumgarten Mar 7, 2024
3a578a2
varaitional clean up, removed comments and moved gab to estimators.py
m-baumgarten Mar 7, 2024
f79e95a
docstrings and type specifications
m-baumgarten Mar 8, 2024
4ea65a6
added licensing header
m-baumgarten Mar 8, 2024
7eec46b
renamed phonon displacements stored in EPhWalkers.x to EPhWalkers.pho…
m-baumgarten Mar 8, 2024
c01760f
docstrings with math
m-baumgarten Mar 10, 2024
e59ead6
removed redundant calculation of greens function
m-baumgarten Mar 10, 2024
a3c3675
added abstract functions, required for every new trial
m-baumgarten Mar 10, 2024
317dcf3
intializes greens functions new and sets overlap in build
m-baumgarten Mar 10, 2024
ee14708
extra layer for propagators from addons
m-baumgarten Mar 11, 2024
eeeb44c
reformatted code
m-baumgarten Mar 11, 2024
58ed7af
Merge branch 'develop' into multiel_holstein
fdmalone Mar 11, 2024
93e6a49
renamed EPhWalkers.total_ovlp to EPhWalkers.ovlp_perm
m-baumgarten Mar 11, 2024
3edb5e8
calc_greens_function and calc_overlap are now "purer", calc_overlap s…
m-baumgarten Mar 11, 2024
4c72ba7
Merge branch 'multiel_holstein' of github.com:m-baumgarten/ipie_holst…
m-baumgarten Mar 11, 2024
d66d467
energy estimator unit test
m-baumgarten Mar 11, 2024
20d86a2
no more hamiltonian needed for trial initialisation. pass w0 instead
m-baumgarten Mar 11, 2024
24a99d5
fixed bug with final reshaping causing dimensions to be mixed up
m-baumgarten Mar 11, 2024
dfb3a4d
added variational_energy to toyozawa
m-baumgarten Mar 11, 2024
a0cfab5
added calc_energy to coherent state trial and made it abstractmethod
m-baumgarten Mar 12, 2024
cf8f379
added unittests for propagation and variational
m-baumgarten Mar 12, 2024
28d40df
utils for unittests
m-baumgarten Mar 12, 2024
298ad97
reformatting
m-baumgarten Mar 12, 2024
51b48a1
added verbosity for toyozawa_variational
m-baumgarten Mar 12, 2024
6fb14af
fixed bug with local_energy, where coherent state expectation value o…
m-baumgarten Mar 12, 2024
5a0a70a
added g_tensor in anticipation of generalization
m-baumgarten Mar 12, 2024
9dfd155
unittests for ephwalkers
m-baumgarten Mar 12, 2024
c355ffa
unittests for trial methods
m-baumgarten Mar 12, 2024
40c5ed5
docstring
m-baumgarten Mar 12, 2024
3b1256f
reformatting
m-baumgarten Mar 12, 2024
27b5365
light reformatting
m-baumgarten Mar 12, 2024
a801b80
removed redundant sqrt(m*w)
m-baumgarten Mar 13, 2024
42faa91
minor bug fixes
m-baumgarten Mar 13, 2024
5d7c560
example for holstein model 4 electrons
m-baumgarten Mar 20, 2024
ee78aad
Merge branch 'develop' into multiel_holstein
fdmalone Mar 30, 2024
92e3620
jax imports in tests guarded
m-baumgarten Apr 16, 2024
1a86cb4
Merge branch 'multiel_holstein' of github.com:m-baumgarten/ipie_holst…
m-baumgarten Apr 16, 2024
9bcb208
removed run_test in holstein example and renamed test_estimators
m-baumgarten Apr 16, 2024
0ebe6f2
Merge branch 'develop' into multiel_holstein
m-baumgarten Jul 22, 2024
705d1e6
removed jax dependency in example
m-baumgarten Aug 4, 2024
44bea2d
Merge branch 'multiel_holstein' of github.com:m-baumgarten/ipie_holst…
m-baumgarten Aug 4, 2024
4963d63
accommodates new Propagator dict
m-baumgarten Aug 4, 2024
e02282c
jax workaround like torch
m-baumgarten Aug 5, 2024
44505b5
fixed pytests
m-baumgarten Sep 6, 2024
fab267b
fixed pylint errors
m-baumgarten Sep 6, 2024
03f7b05
fixed remaining health errors
m-baumgarten Sep 6, 2024
40e9128
Merge branch 'develop' into multiel_holstein
linusjoonho Sep 6, 2024
b73fdd7
fixed examples and black
m-baumgarten Sep 7, 2024
cc4d29d
Merge branch 'multiel_holstein' of github.com:m-baumgarten/ipie_holst…
m-baumgarten Sep 7, 2024
fb69584
fixed example
m-baumgarten Sep 7, 2024
530c5ff
Merge branch 'develop' into multiel_holstein
linusjoonho Nov 5, 2024
42cdff4
Merge branch 'develop' into multiel_holstein
linusjoonho Nov 5, 2024
d9582f7
Merge branch 'develop' into multiel_holstein
linusjoonho Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions examples/13-1d_holstein/run_afqmc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import numpy as np
np.random.seed(125)
from mpi4py import MPI

from ipie.qmc.afqmc import AFQMC
from ipie.systems import Generic
from ipie.addons.eph.hamiltonians.holstein import HolsteinModel
m-baumgarten marked this conversation as resolved.
Show resolved Hide resolved
from ipie.addons.eph.trial_wavefunction.toyozawa import ToyozawaTrial
from ipie.addons.eph.trial_wavefunction.variational.toyozawa_variational import variational_trial_toyozawa
from ipie.addons.eph.walkers.eph_walkers import EphWalkers
from ipie.addons.eph.propagation.holstein import HolsteinPropagatorImportance
from ipie.addons.eph.estimators.energy import EnergyEstimator
from ipie.qmc.options import QMCParams

#System Parameters
nup = 2
ndown = 2
nelec = (nup, ndown)

#Hamiltonian Parameters
g = 2.
t = 1.
w0 = 4.
nsites = 4
pbc = True

#Walker Parameters & Setup
comm = MPI.COMM_WORLD
nwalkers = 1000 // comm.size

#Setup initial guess for variational optimization
initial_electron = np.random.random((nsites, nup + ndown))
initial_phonons = np.ones(nsites) * 0.1

#System and Hamiltonian setup
system = Generic(nelec)
ham = HolsteinModel(g=g, t=t, w0=w0, nsites=nsites, pbc=pbc)
ham.build()

#Variational procedure
etrial, beta_shift, el_trial = variational_trial_toyozawa(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are you settings this and not just recomputing it as a check?

Copy link
Collaborator

Choose a reason for hiding this comment

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

(this is a good unit test.)

Copy link
Author

Choose a reason for hiding this comment

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

Are you saying I should load beta_shift and el_trial instead of computing them in the run script (and have a unites for variational_trial_toyozawa)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, computing them in the script is fine. I was saying the trial wavefunction should recompute the energy given your optimized orbitals / parameters. This is a useful sanity check when running jobs as we typically generate trials externally so it's a form of handshake to make sure everything is formatted / interpreted correctly. Here you're just setting them rather than recalculating them via the trial.

The unit test is separate and is just a simple assertion that building a trial wavefunction from your optimized parameters reproduces the variational energy.

initial_phonons, initial_electron, ham, system
)
wavefunction = np.column_stack([beta_shift, el_trial])

#Setup trial
trial = ToyozawaTrial(
wavefunction=wavefunction,
hamiltonian=ham,
num_elec=[nup, ndown],
num_basis=nsites
)
trial.set_etrial(etrial)

fdmalone marked this conversation as resolved.
Show resolved Hide resolved
#Setup walkers
walkers = EphWalkers(
initial_walker=wavefunction,
nup=nup,
ndown=ndown,
nbasis=nsites,
nwalkers=nwalkers
)
walkers.build(trial)

num_steps_per_block = 10
num_blocks = 10000
add_est = {
"energy": EnergyEstimator(
system=system, ham=ham, trial=trial
)
}

seed = 125

# Note nwalkers specifies the number of walkers on each CPU
ephqmc = AFQMC.build(
num_elec=nelec,
hamiltonian=ham,
trial_wavefunction=trial,
walkers=walkers,
num_walkers=nwalkers,
seed=seed,
num_steps_per_block=num_steps_per_block,
num_blocks=num_blocks,
)
ephqmc.run(additional_estimators=add_est, verbose=False)

17 changes: 17 additions & 0 deletions ipie/addons/eph/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2022 The ipie Developers. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Directory for additions to ipie which depend on the core ipie library.
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete lines 15-17. we meant this to be an instruction for contributing to addons.

# New features should mirror the ipie layout e.g.
m-baumgarten marked this conversation as resolved.
Show resolved Hide resolved
# ipie/addons/finite_temperature/qmc/afqmc.py etc.
17 changes: 17 additions & 0 deletions ipie/addons/eph/estimators/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2022 The ipie Developers. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Directory for additions to ipie which depend on the core ipie library.
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete

# New features should mirror the ipie layout e.g.
# ipie/addons/finite_temperature/qmc/afqmc.py etc.
90 changes: 90 additions & 0 deletions ipie/addons/eph/estimators/energy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Copyright 2022 The ipie Developers. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import plum

from ipie.estimators.estimator_base import EstimatorBase
from ipie.addons.eph.estimators.local_energy_holstein import local_energy_holstein
from ipie.addons.eph.hamiltonians.holstein import HolsteinModel
from ipie.systems.generic import Generic
from ipie.addons.eph.trial_wavefunction.eph_trial_base import EphTrialWavefunctionBase
from ipie.utils.backend import arraylib as xp
from ipie.addons.eph.walkers.eph_walkers import EphWalkers

@plum.dispatch
def local_energy(
system: Generic,
hamiltonian: HolsteinModel,
walkers: EphWalkers,
trial: EphTrialWavefunctionBase
):
return local_energy_holstein(system, hamiltonian, walkers, trial)

class EnergyEstimator(EstimatorBase):
def __init__(
self,
system=None,
ham=None,
trial=None,
filename=None,
):
assert system is not None
assert ham is not None
assert trial is not None
super().__init__()
self._eshift = 0.0
self.scalar_estimator = True
self._data = {
"ENumer": 0.0j,
"EDenom": 0.0j,
"ETotal": 0.0j,
"EEl": 0.0j,
"EElPh": 0.0j,
"EPh": 0.0j,
}
self._shape = (len(self.names),)
self._data_index = {k: i for i, k in enumerate(list(self._data.keys()))}
self.print_to_stdout = True
self.ascii_filename = filename

def compute_estimator(self, system, walkers, hamiltonian, trial, istep=1):
trial.calc_greens_function(walkers)
# Need to be able to dispatch here
energy = local_energy(system, hamiltonian, walkers, trial)
self._data["ENumer"] = xp.sum(walkers.weight * energy[:, 0].real)
self._data["EDenom"] = xp.sum(walkers.weight)
self._data["EEl"] = xp.sum(walkers.weight * energy[:, 1].real)
self._data["EElPh"] = xp.sum(walkers.weight * energy[:, 2].real)
self._data["EPh"] = xp.sum(walkers.weight * energy[:, 3].real)

return self.data

def get_index(self, name):
index = self._data_index.get(name, None)
if index is None:
raise RuntimeError(f"Unknown estimator {name}")
return index

def post_reduce_hook(self, data):
ix_proj = self._data_index["ETotal"]
ix_nume = self._data_index["ENumer"]
ix_deno = self._data_index["EDenom"]
data[ix_proj] = data[ix_nume] / data[ix_deno]
ix_nume = self._data_index["EEl"]
data[ix_nume] = data[ix_nume] / data[ix_deno]
ix_nume = self._data_index["EElPh"]
data[ix_nume] = data[ix_nume] / data[ix_deno]
ix_nume = self._data_index["EPh"]
data[ix_nume] = data[ix_nume] / data[ix_deno]
51 changes: 51 additions & 0 deletions ipie/addons/eph/estimators/local_energy_holstein.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import numpy as np

from ipie.addons.eph.hamiltonians.holstein import HolsteinModel
from ipie.addons.eph.trial_wavefunction.eph_trial_base import EphTrialWavefunctionBase
from ipie.addons.eph.walkers.eph_walkers import EphWalkers

from ipie.systems.generic import Generic
from ipie.utils.backend import arraylib as xp


def local_energy_holstein(
system: Generic,
hamiltonian: HolsteinModel,
walkers: EphWalkers,
trial: EphTrialWavefunctionBase
) -> np.ndarray:
r"""Computes the local energy for the Holstein model via

.. math::
\frac{\langle \Psi_\mathrm{T} | \hat{H} | \Phi_\mathrm{w}\rangle}
{\langle \Psi_\mathrm{T} | \Phi_\mathrm{w}\rangle},

where :math:`| \Phi_\mathrm{w}\rangle = \sum_{\tau \in cyclic perms}
|\phi(\tau(r))\rangle \otimes |\beta(\tau(X))\rangle`. In this ansatz for
the walkers :math:`|\beta\rangle` is a coherent state, which corresonds to a
by :math:`\beta` displaced vacuum state.
"""

energy = xp.zeros((walkers.nwalkers, 4), dtype=xp.complex128)

gf = trial.calc_greens_function(walkers)

energy[:, 1] = np.sum(hamiltonian.T[0] * gf[0], axis=(1,2))
if system.ndown > 0:
energy[:, 1] += np.sum(hamiltonian.T[1] * gf[1], axis=(1,2))

energy[:, 2] = np.sum(np.diagonal(gf[0], axis1=1, axis2=2) * walkers.x, axis=1)
if system.ndown > 0:
energy[:, 2] += np.sum(np.diagonal(gf[1], axis1=1, axis2=2) * walkers.x, axis=1)
energy[:, 2] *= hamiltonian.const

energy[:, 3] = 0.5 * hamiltonian.m * hamiltonian.w0**2 * np.sum(walkers.x**2, axis=1)
energy[:, 3] -= 0.5 * hamiltonian.nsites * hamiltonian.w0
energy[:, 3] -= 0.5 * trial.calc_phonon_laplacian_locenergy(walkers) / hamiltonian.m

energy[:, 0] = np.sum(energy[:,1:], axis=1)

return energy



17 changes: 17 additions & 0 deletions ipie/addons/eph/hamiltonians/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2022 The ipie Developers. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Directory for additions to ipie which depend on the core ipie library.
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete

# New features should mirror the ipie layout e.g.
# ipie/addons/finite_temperature/qmc/afqmc.py etc.
57 changes: 57 additions & 0 deletions ipie/addons/eph/hamiltonians/holstein.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import numpy
from ipie.utils.backend import arraylib as xp


class HolsteinModel():
r"""Class carrying parameters specifying a 1D Holstein chain.

The Holstein model is described by the Hamiltonian

.. math::
\hat{H} = -t \sum_{\langle ij\rangle} \hat{a}_i^\dagger \hat{a}_j
- g \sqrt{2 w_0 m} \sum_i \hat{a}_i^\dagger \hat{a}_i \hat{X}_i
+ \bigg(\sum_i \frac{m w_0^2}{2} \hat{X}_i^2 + \frac{1}{2m} \hat{P}_i^2
- \frac{w_0}{2}\bigg),

where :math:`t` is associated with the electronic hopping, :math:`g` with
the electron-phonon coupling strength, and :math:``w_0` with the phonon
frequency.

Parameters
----------
g :
Electron-phonon coupling strength
t :
Electron hopping parameter
w0 :
Phonon frequency
nsites :
Length of the 1D Holstein chain
pbc :
Boolean specifying whether periodic boundary conditions should be
employed.
"""

def __init__(self, g: float, t: float, w0: float, nsites: int, pbc: bool):
self.g = g
self.t = t
self.w0 = w0
self.m = 1/self.w0
self.nsites = nsites
self.pbc = pbc
self.T = None
self.const = -self.g * numpy.sqrt(2. * self.m * self.w0)

def build(self):
"""Constructs electronic hopping matrix."""
self.T = numpy.diag(numpy.ones(self.nsites-1), 1)
self.T += numpy.diag(numpy.ones(self.nsites-1), -1)

if self.pbc:
self.T[0,-1] = self.T[-1,0] = 1.

self.T *= -self.t

self.T = [self.T.copy(), self.T.copy()]


17 changes: 17 additions & 0 deletions ipie/addons/eph/propagation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2022 The ipie Developers. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Directory for additions to ipie which depend on the core ipie library.
Copy link
Collaborator

Choose a reason for hiding this comment

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

delete

# New features should mirror the ipie layout e.g.
# ipie/addons/finite_temperature/qmc/afqmc.py etc.
Loading