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
3 changes: 2 additions & 1 deletion qmb/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions qmb/fcidump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions qmb/hubbard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
35 changes: 17 additions & 18 deletions qmb/ising.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Comment on lines +75 to +91

Copilot AI Jun 16, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] The string-building logic for the group name is repetitive; consider extracting a helper function for creating these formatted segments to improve maintainability.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

纠结,我感觉现在这个写法确实不太好而且容易出错,但是抽成函数之后大概率需要 getattr 之类更不安全的操作,可读性可能也未必特别好。

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]:
Expand Down
8 changes: 4 additions & 4 deletions qmb/model_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,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
-------
Expand Down
5 changes: 2 additions & 3 deletions qmb/openfermion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down