From 79e3c9998a2a24d9697990e37125a0a8527b6740 Mon Sep 17 00:00:00 2001 From: Hao Zhang Date: Mon, 16 Jun 2025 19:15:37 +0800 Subject: [PATCH 1/2] Replace the function `preparse` to `default_group_name`. --- qmb/common.py | 3 ++- qmb/fcidump.py | 5 ++--- qmb/ising.py | 35 +++++++++++++++++------------------ qmb/model_dict.py | 8 ++++---- qmb/openfermion.py | 5 ++--- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/qmb/common.py b/qmb/common.py index ee19c00..ee08d35 100644 --- a/qmb/common.py +++ b/qmb/common.py @@ -107,7 +107,8 @@ def main(self, *, model_param: typing.Any = None, network_param: typing.Any = No if "-h" in self.network_args or "--help" in self.network_args: tyro.cli(network_config_t, args=self.network_args) if self.group_name is None: - self.group_name = model_t.preparse(self.physics_args) + model_param_for_group_name = tyro.cli(model_config_t, args=self.physics_args) + self.group_name = model_t.default_group_name(model_param_for_group_name) self.folder().mkdir(parents=True, exist_ok=True) diff --git a/qmb/fcidump.py b/qmb/fcidump.py index a3538ea..5ffd75f 100644 --- a/qmb/fcidump.py +++ b/qmb/fcidump.py @@ -124,9 +124,8 @@ class Model(ModelProto[ModelConfig]): config_t = ModelConfig @classmethod - def preparse(cls, input_args: tuple[str, ...]) -> str: - args: ModelConfig = tyro.cli(ModelConfig, args=input_args) - return args.model_name + def default_group_name(cls, config: ModelConfig) -> str: + return config.model_name def __init__(self, args: ModelConfig) -> None: # pylint: disable=too-many-locals diff --git a/qmb/ising.py b/qmb/ising.py index d625015..005b585 100644 --- a/qmb/ising.py +++ b/qmb/ising.py @@ -72,26 +72,25 @@ class Model(ModelProto[ModelConfig]): config_t = ModelConfig @classmethod - def preparse(cls, input_args: tuple[str, ...]) -> str: + def default_group_name(cls, config: ModelConfig) -> str: # pylint: disable=too-many-locals - args = tyro.cli(ModelConfig, args=input_args) - x = f"_x{args.x}" if args.x != 0 else "" - y = f"_y{args.y}" if args.y != 0 else "" - z = f"_z{args.z}" if args.z != 0 else "" - xh = f"_xh{args.xh}" if args.xh != 0 else "" - yh = f"_yh{args.yh}" if args.yh != 0 else "" - zh = f"_zh{args.zh}" if args.zh != 0 else "" - xv = f"_xv{args.xv}" if args.xv != 0 else "" - yv = f"_yv{args.yv}" if args.yv != 0 else "" - zv = f"_zv{args.zv}" if args.zv != 0 else "" - xd = f"_xd{args.xd}" if args.xd != 0 else "" - yd = f"_yd{args.yd}" if args.yd != 0 else "" - zd = f"_zd{args.zd}" if args.zd != 0 else "" - xa = f"_xa{args.xa}" if args.xa != 0 else "" - ya = f"_ya{args.ya}" if args.ya != 0 else "" - za = f"_za{args.za}" if args.za != 0 else "" + x = f"_x{config.x}" if config.x != 0 else "" + y = f"_y{config.y}" if config.y != 0 else "" + z = f"_z{config.z}" if config.z != 0 else "" + xh = f"_xh{config.xh}" if config.xh != 0 else "" + yh = f"_yh{config.yh}" if config.yh != 0 else "" + zh = f"_zh{config.zh}" if config.zh != 0 else "" + xv = f"_xv{config.xv}" if config.xv != 0 else "" + yv = f"_yv{config.yv}" if config.yv != 0 else "" + zv = f"_zv{config.zv}" if config.zv != 0 else "" + xd = f"_xd{config.xd}" if config.xd != 0 else "" + yd = f"_yd{config.yd}" if config.yd != 0 else "" + zd = f"_zd{config.zd}" if config.zd != 0 else "" + xa = f"_xa{config.xa}" if config.xa != 0 else "" + ya = f"_ya{config.ya}" if config.ya != 0 else "" + za = f"_za{config.za}" if config.za != 0 else "" desc = x + y + z + xh + yh + zh + xv + yv + zv + xd + yd + zd + xa + ya + za - return f"Ising_{args.m}_{args.n}" + desc + return f"Ising_{config.m}_{config.n}" + desc @classmethod def _prepare_hamiltonian(cls, args: ModelConfig) -> dict[tuple[tuple[int, int], ...], complex]: diff --git a/qmb/model_dict.py b/qmb/model_dict.py index 3872cbb..30c372f 100644 --- a/qmb/model_dict.py +++ b/qmb/model_dict.py @@ -79,14 +79,14 @@ class ModelProto(typing.Protocol[ModelConfig]): config_t: type[ModelConfig] @classmethod - def preparse(cls, input_args: tuple[str, ...]) -> str: + def default_group_name(cls, config: ModelConfig) -> str: """ - Preparse the arguments to obtain the group name for logging perposes + Get the default group name for logging purposes. Parameters ---------- - input_args : tuple[str, ...] - The input arguments to the model. + config : ModelConfig + The config of model. Returns ------- diff --git a/qmb/openfermion.py b/qmb/openfermion.py index 2882556..088e40e 100644 --- a/qmb/openfermion.py +++ b/qmb/openfermion.py @@ -50,9 +50,8 @@ class Model(ModelProto[ModelConfig]): config_t = ModelConfig @classmethod - def preparse(cls, input_args: tuple[str, ...]) -> str: - args: ModelConfig = tyro.cli(ModelConfig, args=input_args) - return args.model_name + def default_group_name(cls, config: ModelConfig) -> str: + return config.model_name def __init__(self, args: ModelConfig) -> None: logging.info("Input arguments successfully parsed") From c220c6406e7aad44c98ac344e7c9663506ea7437 Mon Sep 17 00:00:00 2001 From: Hao Zhang Date: Mon, 16 Jun 2025 23:15:53 +0800 Subject: [PATCH 2/2] Update `qmb/hubbard.py`. --- qmb/hubbard.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/qmb/hubbard.py b/qmb/hubbard.py index 668d4b0..5b278dc 100644 --- a/qmb/hubbard.py +++ b/qmb/hubbard.py @@ -57,9 +57,8 @@ class Model(ModelProto[ModelConfig]): config_t = ModelConfig @classmethod - def preparse(cls, input_args: tuple[str, ...]) -> str: - args = tyro.cli(ModelConfig, args=input_args) - return f"Hubbard_{args.m}x{args.n}_t{args.t}_u{args.u}" + def default_group_name(cls, config: ModelConfig) -> str: + return f"Hubbard_{config.m}x{config.n}_t{config.t}_u{config.u}" @classmethod def _prepare_hamiltonian(cls, args: ModelConfig) -> dict[tuple[tuple[int, int], ...], complex]: