Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions qmp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import omegaconf

from .utility.context import RuntimeContext, DACITE_CAST
from .utility.subcommand_dict import subcommand_dict
from .utility.action_dict import action_dict


@hydra.main(version_base=None, config_path=str(pathlib.Path().resolve()), config_name="config")
Expand All @@ -30,7 +30,7 @@ def main(runtime_config: omegaconf.DictConfig) -> None:

# 2. Instantiate Algorithm
run = dacite.from_dict(
data_class=subcommand_dict[runtime_config.action.name], # type: ignore[arg-type]
data_class=action_dict[runtime_config.action.name], # type: ignore[arg-type]
data=omegaconf.OmegaConf.to_container(runtime_config.action.params, resolve=True), # type: ignore[arg-type]
config=dacite.Config(cast=DACITE_CAST),
)
Expand Down
4 changes: 2 additions & 2 deletions qmp/algorithms/chop_imag.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import omegaconf
import torch.utils.tensorboard
from ..utility.context import RuntimeContext
from ..utility.subcommand_dict import subcommand_dict
from ..utility.action_dict import action_dict


@dataclasses.dataclass
Expand Down Expand Up @@ -98,4 +98,4 @@ def main(
context.save(data, 0)


subcommand_dict["chop_imag"] = ChopImagConfig
action_dict["chop_imag"] = ChopImagConfig
4 changes: 2 additions & 2 deletions qmp/algorithms/guide.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import torch
import torch.utils.tensorboard
from ..utility.context import RuntimeContext
from ..utility.subcommand_dict import subcommand_dict
from ..utility.action_dict import action_dict


@dataclasses.dataclass
Expand Down Expand Up @@ -188,4 +188,4 @@ def distribution() -> torch.Tensor:
logging.info("Current optimization cycle completed")


subcommand_dict["guide"] = GuideConfig
action_dict["guide"] = GuideConfig
8 changes: 4 additions & 4 deletions qmp/algorithms/haar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import torch.utils.tensorboard
from ..utility import losses
from ..utility.context import RuntimeContext
from ..utility.subcommand_dict import subcommand_dict
from ..utility.action_dict import action_dict
from ..utility.model_dict import ModelProto
from ..utility.optimizer import scale_learning_rate

Expand Down Expand Up @@ -392,7 +392,7 @@ def main(
)

if "haar" not in data and "imag" in data:
logging.warning("The 'imag' subcommand is deprecated, please use 'haar' instead.")
logging.warning("The 'imag' action is deprecated, please use 'haar' instead.")
data["haar"] = data["imag"]
del data["imag"]
if "haar" not in data:
Expand Down Expand Up @@ -587,5 +587,5 @@ def closure() -> torch.Tensor:
logging.info("Current optimization cycle completed")


subcommand_dict["haar"] = HaarConfig
subcommand_dict["imag"] = HaarConfig
action_dict["haar"] = HaarConfig
action_dict["imag"] = HaarConfig
6 changes: 3 additions & 3 deletions qmp/algorithms/haar_with_orbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from scipy.optimize import linear_sum_assignment
from ..utility import losses
from ..utility.context import RuntimeContext
from ..utility.subcommand_dict import subcommand_dict
from ..utility.action_dict import action_dict
from ..utility.model_dict import ModelProto
from ..utility.optimizer import scale_learning_rate
from ..hamiltonian import Hamiltonian
Expand Down Expand Up @@ -211,7 +211,7 @@ def main(
)

if "haar" not in data and "imag" in data:
logging.warning("The 'imag' subcommand is deprecated, please use 'haar' instead.")
logging.warning("The 'imag' action is deprecated, please use 'haar' instead.")
data["haar"] = data["imag"]
del data["imag"]
if "haar" not in data:
Expand Down Expand Up @@ -412,4 +412,4 @@ def closure() -> torch.Tensor:
logging.info("Checkpoint successfully saved")


subcommand_dict["haar_with_orbit"] = HaarWithOrbitConfig
action_dict["haar_with_orbit"] = HaarWithOrbitConfig
4 changes: 2 additions & 2 deletions qmp/algorithms/orbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import omegaconf
from ..hamiltonian import Hamiltonian
from ..utility.context import RuntimeContext
from ..utility.subcommand_dict import subcommand_dict
from ..utility.action_dict import action_dict


@dataclasses.dataclass
Expand Down Expand Up @@ -202,4 +202,4 @@ def calculate_rdm(self, configs: torch.Tensor, psi: torch.Tensor) -> torch.Tenso
return rdm


subcommand_dict["orbit"] = OrbitConfig
action_dict["orbit"] = OrbitConfig
4 changes: 2 additions & 2 deletions qmp/algorithms/pert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import dataclasses
import omegaconf
from ..utility.context import RuntimeContext
from ..utility.subcommand_dict import subcommand_dict
from ..utility.action_dict import action_dict


@dataclasses.dataclass
Expand Down Expand Up @@ -64,4 +64,4 @@ def main(
logging.info("Error is reduced from %.8f to %.8f", energy0 - model.ref_energy, energy2 - model.ref_energy)


subcommand_dict["pert"] = PerturbationConfig
action_dict["pert"] = PerturbationConfig
4 changes: 2 additions & 2 deletions qmp/algorithms/pretrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import torch
from ..utility import losses
from ..utility.context import RuntimeContext
from ..utility.subcommand_dict import subcommand_dict
from ..utility.action_dict import action_dict


@dataclasses.dataclass
Expand Down Expand Up @@ -74,4 +74,4 @@ def closure() -> torch.Tensor:
logging.info("Current optimization cycle completed")


subcommand_dict["pretrain"] = PretrainConfig
action_dict["pretrain"] = PretrainConfig
4 changes: 2 additions & 2 deletions qmp/algorithms/vmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import torch
import torch.utils.tensorboard
from ..utility.context import RuntimeContext
from ..utility.subcommand_dict import subcommand_dict
from ..utility.action_dict import action_dict


@dataclasses.dataclass
Expand Down Expand Up @@ -117,4 +117,4 @@ def closure() -> torch.Tensor:
logging.info("Current optimization cycle completed")


subcommand_dict["vmc"] = VmcConfig
action_dict["vmc"] = VmcConfig
32 changes: 32 additions & 0 deletions qmp/utility/action_dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
This module is used to store a dictionary that maps action names to their corresponding dataclass types.

Other packages or subpackages can register their actions by adding entries to this dictionary, such as
```
from qmp.utility.action_dict import action_dict
action_dict["my_action"] = MyAction
```
"""

import typing
import omegaconf
from .context import RuntimeContext


class ActionProto(typing.Protocol):
"""
This protocol defines a dataclass with a `main` method, which will be called when the action is executed.
"""

def main(
self,
context: RuntimeContext,
runtime_config: omegaconf.DictConfig,
checkpoint_data: dict[str, typing.Any],
) -> None:
"""
The main method to be called when the action is executed.
"""


action_dict: dict[str, type[ActionProto]] = {}
32 changes: 0 additions & 32 deletions qmp/utility/subcommand_dict.py

This file was deleted.