Skip to content

Commit

Permalink
QCQMC part 3: Add Hamiltonian (#345)
Browse files Browse the repository at this point in the history
Adds hamiltonian module for specifying a second quantized hamiltonian used for optimizing a trial wavefunction.
  • Loading branch information
fdmalone authored Jun 22, 2024
1 parent a087ea8 commit 49f9511
Show file tree
Hide file tree
Showing 5 changed files with 618 additions and 13 deletions.
35 changes: 22 additions & 13 deletions recirq/qcqmc/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,43 @@
# limitations under the License.

import abc
import dataclasses
import hashlib
import pathlib
from dataclasses import dataclass
from pathlib import Path

import attrs
import numpy as np

DEFAULT_BASE_DATA_DIR = (Path(__file__).parent / "data").resolve()


def get_integrals_path(
name: str, *, base_data_dir: str = DEFAULT_BASE_DATA_DIR
name: str, *, base_data_dir: pathlib.Path = DEFAULT_BASE_DATA_DIR
) -> Path:
"""Find integral data file by name.
Args:
name: The molecule name.
base_data_dir: The base directory where the data folder should live.
"""
return Path(base_data_dir) / "integrals" / name / "hamiltonian.chk"
return base_data_dir / "integrals" / name / "hamiltonian.chk"


_MOLECULE_INFO = {"fh_sto3g"}


# should go in Dedicated module
@dataclass(frozen=True, repr=False)
@attrs.frozen
class Params(abc.ABC):
"""Store named parameters which will potentiall be read from a file.
Args:
name: The name of the parameters
"""

name: str

# TODO: Is this necessary?
def __eq__(self, other):
"""A helper method to compare two dataclasses which might contain arrays."""
if self is other:
Expand All @@ -53,9 +60,7 @@ def __eq__(self, other):

return all(
array_compatible_eq(thing1, thing2)
for thing1, thing2 in zip(
dataclasses.astuple(self), dataclasses.astuple(other)
)
for thing1, thing2 in zip(attrs.astuple(self), attrs.astuple(other))
)

@property
Expand All @@ -78,7 +83,7 @@ def __repr__(self):
# This custom repr lets us add fields with default values without changing
# the repr. This, in turn, lets us use the hash_key reliably even when
# we add new fields with a default value.
fields = dataclasses.fields(self)
fields = attrs.fields(self)
# adjusted_fields = [f for f in fields if getattr(self, f.name) != f.default]
adjusted_fields = [
f
Expand All @@ -99,8 +104,14 @@ def _json_namespace_(cls):


# Should go in dedicated module
@dataclass(frozen=True, eq=False)
@attrs.frozen
class Data(abc.ABC):
"""A container for different forms of data used in qcqmc (hamiltonian, trial wavefunction, ...).
Args:
params: The name of the parameters
"""

params: Params

def __eq__(self, other):
Expand All @@ -113,9 +124,7 @@ def __eq__(self, other):

return all(
array_compatible_eq(thing1, thing2)
for thing1, thing2 in zip(
dataclasses.astuple(self), dataclasses.astuple(other)
)
for thing1, thing2 in zip(attrs.astuple(self), attrs.astuple(other))
)

@classmethod
Expand Down
Loading

0 comments on commit 49f9511

Please sign in to comment.