From b3184289686b3dcf9fa9c2f48e4be115ea2adefa Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 19 Dec 2025 01:47:44 +0000 Subject: [PATCH 1/8] Initial plan From 462d163541246416428cfd55fcfa2f2c3be431af Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 19 Dec 2025 01:53:05 +0000 Subject: [PATCH 2/8] Refactor model input format: use single model_path parameter - Replace model_name and model_path parameters with single model_path - Remove QMP_MODEL_PATH environment variable handling - Update ModelConfig in both fcidump.py and openfermion.py - Update default_group_name to extract name from path - Remove unused os import - Pass linter and formatter checks Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- qmp/models/fcidump.py | 42 ++++++++++++++++++++------------------- qmp/models/openfermion.py | 37 +++++++++++++++++----------------- 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/qmp/models/fcidump.py b/qmp/models/fcidump.py index 23b8102..26bead2 100644 --- a/qmp/models/fcidump.py +++ b/qmp/models/fcidump.py @@ -2,7 +2,6 @@ This file provides an interface to work with FCIDUMP files. """ -import os import typing import logging import dataclasses @@ -20,8 +19,6 @@ from ..hamiltonian import Hamiltonian from ..utility.model_dict import model_dict, ModelProto, NetworkProto, NetworkConfigProto -QMP_MODEL_PATH = "QMP_MODEL_PATH" - @dataclasses.dataclass class ModelConfig: @@ -29,21 +26,13 @@ class ModelConfig: The configuration of the model. """ - # The openfermion model name - model_name: str - # The path of models folder - model_path: pathlib.Path | None = None + # The complete path to the model file (can be relative or absolute) + model_path: pathlib.Path | str # The ref energy of the model, leave empty to read from FCIDUMP.yaml ref_energy: float | None = None def __post_init__(self) -> None: - if self.model_path is not None: - self.model_path = pathlib.Path(self.model_path) - else: - if QMP_MODEL_PATH in os.environ: - self.model_path = pathlib.Path(os.environ[QMP_MODEL_PATH]) - else: - self.model_path = pathlib.Path("models") + self.model_path = pathlib.Path(self.model_path) def _read_fcidump( @@ -134,20 +123,33 @@ class Model(ModelProto[ModelConfig]): @classmethod def default_group_name(cls, config: ModelConfig) -> str: - return config.model_name + # Extract the stem (filename without extension) from the model_path + path = pathlib.Path(config.model_path) + # Remove common FCIDUMP extensions to get a clean name + name = path.name + if name.endswith(".FCIDUMP.gz"): + name = name[:-11] # Remove ".FCIDUMP.gz" + elif name.endswith(".FCIDUMP"): + name = name[:-8] # Remove ".FCIDUMP" + return name def __init__(self, args: ModelConfig) -> None: # pylint: disable=too-many-locals logging.info("Input arguments successfully parsed") - logging.info("Model name: %s, Model path: %s", args.model_name, args.model_path) + logging.info("Model path: %s", args.model_path) - model_name = args.model_name model_path = args.model_path ref_energy = args.ref_energy - assert model_path is not None - model_file_name = model_path / f"{model_name}.FCIDUMP.gz" - model_file_name = model_file_name if model_file_name.exists() else model_path / model_name + # model_path is now the complete path to the file + model_file_name = pathlib.Path(model_path) + + # Extract model name for logging and reference purposes + model_name = model_file_name.name + if model_name.endswith(".FCIDUMP.gz"): + model_name = model_name[:-11] + elif model_name.endswith(".FCIDUMP"): + model_name = model_name[:-8] checksum = hashlib.sha256(model_file_name.read_bytes()).hexdigest() + "v5" cache_file = platformdirs.user_cache_path("qmp", "kclab") / checksum diff --git a/qmp/models/openfermion.py b/qmp/models/openfermion.py index f15b6f4..690e135 100644 --- a/qmp/models/openfermion.py +++ b/qmp/models/openfermion.py @@ -2,7 +2,6 @@ This file provides an interface to work with openfermion models. """ -import os import typing import logging import dataclasses @@ -15,8 +14,6 @@ from ..hamiltonian import Hamiltonian from ..utility.model_dict import model_dict, ModelProto, NetworkProto, NetworkConfigProto -QMP_MODEL_PATH = "QMP_MODEL_PATH" - @dataclasses.dataclass class ModelConfig: @@ -24,19 +21,11 @@ class ModelConfig: The configuration of the model. """ - # The openfermion model name - model_name: str - # The path of models folder - model_path: pathlib.Path | None = None + # The complete path to the model file (can be relative or absolute) + model_path: pathlib.Path | str def __post_init__(self) -> None: - if self.model_path is not None: - self.model_path = pathlib.Path(self.model_path) - else: - if QMP_MODEL_PATH in os.environ: - self.model_path = pathlib.Path(os.environ[QMP_MODEL_PATH]) - else: - self.model_path = pathlib.Path("models") + self.model_path = pathlib.Path(self.model_path) class Model(ModelProto[ModelConfig]): @@ -50,17 +39,27 @@ class Model(ModelProto[ModelConfig]): @classmethod def default_group_name(cls, config: ModelConfig) -> str: - return config.model_name + # Extract the stem (filename without extension) from the model_path + path = pathlib.Path(config.model_path) + # Remove the .hdf5 extension to get a clean name + name = path.name + if name.endswith(".hdf5"): + name = name[:-5] # Remove ".hdf5" + return name def __init__(self, args: ModelConfig) -> None: logging.info("Input arguments successfully parsed") - logging.info("Model name: %s, Model path: %s", args.model_name, args.model_path) + logging.info("Model path: %s", args.model_path) - model_name = args.model_name model_path = args.model_path - assert model_path is not None - model_file_name = model_path / f"{model_name}.hdf5" + # model_path is now the complete path to the file + model_file_name = pathlib.Path(model_path) + + # Extract model name for logging purposes + model_name = model_file_name.name + if model_name.endswith(".hdf5"): + model_name = model_name[:-5] logging.info("Loading OpenFermion model '%s' from file: %s", model_name, model_file_name) openfermion_model: openfermion.MolecularData = openfermion.MolecularData(filename=str(model_file_name)) # type: ignore[no-untyped-call] logging.info("OpenFermion model '%s' successfully loaded", model_name) From 919ccf1d0fcc0893f1df6f53bb04b4146140d39b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 19 Dec 2025 01:55:08 +0000 Subject: [PATCH 3/8] Refactor: extract helper function to reduce code duplication Add _extract_model_name_from_path helper function in both fcidump.py and openfermion.py to avoid duplicating extension removal logic between default_group_name and __init__ methods. Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- qmp/models/fcidump.py | 28 ++++++++++++++-------------- qmp/models/openfermion.py | 22 ++++++++++++---------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/qmp/models/fcidump.py b/qmp/models/fcidump.py index 26bead2..2ecaa4e 100644 --- a/qmp/models/fcidump.py +++ b/qmp/models/fcidump.py @@ -20,6 +20,18 @@ from ..utility.model_dict import model_dict, ModelProto, NetworkProto, NetworkConfigProto +def _extract_model_name_from_path(path: pathlib.Path) -> str: + """ + Extract the model name from a file path by removing FCIDUMP extensions. + """ + name = path.name + if name.endswith(".FCIDUMP.gz"): + return name[:-11] # Remove ".FCIDUMP.gz" + elif name.endswith(".FCIDUMP"): + return name[:-8] # Remove ".FCIDUMP" + return name + + @dataclasses.dataclass class ModelConfig: """ @@ -123,15 +135,7 @@ class Model(ModelProto[ModelConfig]): @classmethod def default_group_name(cls, config: ModelConfig) -> str: - # Extract the stem (filename without extension) from the model_path - path = pathlib.Path(config.model_path) - # Remove common FCIDUMP extensions to get a clean name - name = path.name - if name.endswith(".FCIDUMP.gz"): - name = name[:-11] # Remove ".FCIDUMP.gz" - elif name.endswith(".FCIDUMP"): - name = name[:-8] # Remove ".FCIDUMP" - return name + return _extract_model_name_from_path(pathlib.Path(config.model_path)) def __init__(self, args: ModelConfig) -> None: # pylint: disable=too-many-locals @@ -145,11 +149,7 @@ def __init__(self, args: ModelConfig) -> None: model_file_name = pathlib.Path(model_path) # Extract model name for logging and reference purposes - model_name = model_file_name.name - if model_name.endswith(".FCIDUMP.gz"): - model_name = model_name[:-11] - elif model_name.endswith(".FCIDUMP"): - model_name = model_name[:-8] + model_name = _extract_model_name_from_path(model_file_name) checksum = hashlib.sha256(model_file_name.read_bytes()).hexdigest() + "v5" cache_file = platformdirs.user_cache_path("qmp", "kclab") / checksum diff --git a/qmp/models/openfermion.py b/qmp/models/openfermion.py index 690e135..57b3f0d 100644 --- a/qmp/models/openfermion.py +++ b/qmp/models/openfermion.py @@ -15,6 +15,16 @@ from ..utility.model_dict import model_dict, ModelProto, NetworkProto, NetworkConfigProto +def _extract_model_name_from_path(path: pathlib.Path) -> str: + """ + Extract the model name from a file path by removing the .hdf5 extension. + """ + name = path.name + if name.endswith(".hdf5"): + return name[:-5] # Remove ".hdf5" + return name + + @dataclasses.dataclass class ModelConfig: """ @@ -39,13 +49,7 @@ class Model(ModelProto[ModelConfig]): @classmethod def default_group_name(cls, config: ModelConfig) -> str: - # Extract the stem (filename without extension) from the model_path - path = pathlib.Path(config.model_path) - # Remove the .hdf5 extension to get a clean name - name = path.name - if name.endswith(".hdf5"): - name = name[:-5] # Remove ".hdf5" - return name + return _extract_model_name_from_path(pathlib.Path(config.model_path)) def __init__(self, args: ModelConfig) -> None: logging.info("Input arguments successfully parsed") @@ -57,9 +61,7 @@ def __init__(self, args: ModelConfig) -> None: model_file_name = pathlib.Path(model_path) # Extract model name for logging purposes - model_name = model_file_name.name - if model_name.endswith(".hdf5"): - model_name = model_name[:-5] + model_name = _extract_model_name_from_path(model_file_name) logging.info("Loading OpenFermion model '%s' from file: %s", model_name, model_file_name) openfermion_model: openfermion.MolecularData = openfermion.MolecularData(filename=str(model_file_name)) # type: ignore[no-untyped-call] logging.info("OpenFermion model '%s' successfully loaded", model_name) From 88d95df2fca8c94fcefe396003848e0bf4b2e13b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 19 Dec 2025 01:56:17 +0000 Subject: [PATCH 4/8] Fix: remove redundant pathlib.Path().name call Remove redundant pathlib.Path(model_name).name since model_name is already the filename without path components. Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- qmp/models/fcidump.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qmp/models/fcidump.py b/qmp/models/fcidump.py index 2ecaa4e..37f8543 100644 --- a/qmp/models/fcidump.py +++ b/qmp/models/fcidump.py @@ -198,7 +198,7 @@ def __init__(self, args: ModelConfig) -> None: if fcidump_ref_energy_file.exists(): with open(fcidump_ref_energy_file, "rt", encoding="utf-8") as file: fcidump_ref_energy_data = yaml.safe_load(file) - self.ref_energy = fcidump_ref_energy_data.get(pathlib.Path(model_name).name, 0) + self.ref_energy = fcidump_ref_energy_data.get(model_name, 0) else: self.ref_energy = 0 logging.info("Reference energy for model '%s' is %.10f", model_name, self.ref_energy) From 90ea800b6353ff43036aac8fb35e5b950a13a7a8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 19 Dec 2025 01:57:33 +0000 Subject: [PATCH 5/8] Refactor: simplify variable assignments in __init__ Remove redundant intermediate variables since model_path is already converted to pathlib.Path in __post_init__. Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- qmp/models/fcidump.py | 6 ++---- qmp/models/openfermion.py | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/qmp/models/fcidump.py b/qmp/models/fcidump.py index 37f8543..ee12f95 100644 --- a/qmp/models/fcidump.py +++ b/qmp/models/fcidump.py @@ -142,12 +142,10 @@ def __init__(self, args: ModelConfig) -> None: logging.info("Input arguments successfully parsed") logging.info("Model path: %s", args.model_path) - model_path = args.model_path + # model_path is now the complete path to the file (already converted to Path in __post_init__) + model_file_name = args.model_path ref_energy = args.ref_energy - # model_path is now the complete path to the file - model_file_name = pathlib.Path(model_path) - # Extract model name for logging and reference purposes model_name = _extract_model_name_from_path(model_file_name) diff --git a/qmp/models/openfermion.py b/qmp/models/openfermion.py index 57b3f0d..40cd691 100644 --- a/qmp/models/openfermion.py +++ b/qmp/models/openfermion.py @@ -55,10 +55,8 @@ def __init__(self, args: ModelConfig) -> None: logging.info("Input arguments successfully parsed") logging.info("Model path: %s", args.model_path) - model_path = args.model_path - - # model_path is now the complete path to the file - model_file_name = pathlib.Path(model_path) + # model_path is now the complete path to the file (already converted to Path in __post_init__) + model_file_name = args.model_path # Extract model name for logging purposes model_name = _extract_model_name_from_path(model_file_name) From 10922a6863ec9ff3c951a7c4fc859357876c277d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 19 Dec 2025 01:58:35 +0000 Subject: [PATCH 6/8] Fix: remove unnecessary pathlib.Path wrappers in default_group_name Remove redundant pathlib.Path() calls since config.model_path is already a Path object after __post_init__. Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- qmp/models/fcidump.py | 2 +- qmp/models/openfermion.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/qmp/models/fcidump.py b/qmp/models/fcidump.py index ee12f95..7e465cf 100644 --- a/qmp/models/fcidump.py +++ b/qmp/models/fcidump.py @@ -135,7 +135,7 @@ class Model(ModelProto[ModelConfig]): @classmethod def default_group_name(cls, config: ModelConfig) -> str: - return _extract_model_name_from_path(pathlib.Path(config.model_path)) + return _extract_model_name_from_path(config.model_path) def __init__(self, args: ModelConfig) -> None: # pylint: disable=too-many-locals diff --git a/qmp/models/openfermion.py b/qmp/models/openfermion.py index 40cd691..c2f047b 100644 --- a/qmp/models/openfermion.py +++ b/qmp/models/openfermion.py @@ -49,7 +49,7 @@ class Model(ModelProto[ModelConfig]): @classmethod def default_group_name(cls, config: ModelConfig) -> str: - return _extract_model_name_from_path(pathlib.Path(config.model_path)) + return _extract_model_name_from_path(config.model_path) def __init__(self, args: ModelConfig) -> None: logging.info("Input arguments successfully parsed") From 5bb239e97e8f70b85f3fa55da1e9e6a5bbd87510 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 19 Dec 2025 02:06:01 +0000 Subject: [PATCH 7/8] Remove model_name extraction and logging - Remove _extract_model_name_from_path helper function - Remove all model_name variables from __init__ methods - Simplify logging to not include model names - Keep extension stripping logic inline in default_group_name - Keep YAML key extraction inline in fcidump.py for ref_energy lookup Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- qmp/models/fcidump.py | 58 ++++++++++++++++++--------------------- qmp/models/openfermion.py | 30 +++++++------------- 2 files changed, 37 insertions(+), 51 deletions(-) diff --git a/qmp/models/fcidump.py b/qmp/models/fcidump.py index 7e465cf..b59e2ee 100644 --- a/qmp/models/fcidump.py +++ b/qmp/models/fcidump.py @@ -20,18 +20,6 @@ from ..utility.model_dict import model_dict, ModelProto, NetworkProto, NetworkConfigProto -def _extract_model_name_from_path(path: pathlib.Path) -> str: - """ - Extract the model name from a file path by removing FCIDUMP extensions. - """ - name = path.name - if name.endswith(".FCIDUMP.gz"): - return name[:-11] # Remove ".FCIDUMP.gz" - elif name.endswith(".FCIDUMP"): - return name[:-8] # Remove ".FCIDUMP" - return name - - @dataclasses.dataclass class ModelConfig: """ @@ -135,7 +123,13 @@ class Model(ModelProto[ModelConfig]): @classmethod def default_group_name(cls, config: ModelConfig) -> str: - return _extract_model_name_from_path(config.model_path) + # Use the filename as the group name + name = config.model_path.name + if name.endswith(".FCIDUMP.gz"): + return name[:-11] # Remove ".FCIDUMP.gz" + elif name.endswith(".FCIDUMP"): + return name[:-8] # Remove ".FCIDUMP" + return name def __init__(self, args: ModelConfig) -> None: # pylint: disable=too-many-locals @@ -146,46 +140,42 @@ def __init__(self, args: ModelConfig) -> None: model_file_name = args.model_path ref_energy = args.ref_energy - # Extract model name for logging and reference purposes - model_name = _extract_model_name_from_path(model_file_name) - checksum = hashlib.sha256(model_file_name.read_bytes()).hexdigest() + "v5" cache_file = platformdirs.user_cache_path("qmp", "kclab") / checksum if cache_file.exists(): - logging.info("Loading FCIDUMP metadata '%s' from file: %s", model_name, model_file_name) + logging.info("Loading FCIDUMP metadata from file: %s", model_file_name) (n_orbit, n_electron, n_spin), _ = _read_fcidump(model_file_name, cached=True) - logging.info("FCIDUMP metadata '%s' successfully loaded", model_name) + logging.info("FCIDUMP metadata successfully loaded") - logging.info("Loading FCIDUMP Hamiltonian '%s' from cache", model_name) + logging.info("Loading FCIDUMP Hamiltonian from cache") openfermion_hamiltonian_data = torch.load(cache_file, map_location="cpu", weights_only=True) - logging.info("FCIDUMP Hamiltonian '%s' successfully loaded", model_name) + logging.info("FCIDUMP Hamiltonian successfully loaded") - logging.info("Recovering internal Hamiltonian representation for model '%s'", model_name) + logging.info("Recovering internal Hamiltonian representation") self.hamiltonian = Hamiltonian(openfermion_hamiltonian_data, kind="fermi") - logging.info("Internal Hamiltonian representation for model '%s' successfully recovered", model_name) + logging.info("Internal Hamiltonian representation successfully recovered") else: - logging.info("Loading FCIDUMP Hamiltonian '%s' from file: %s", model_name, model_file_name) + logging.info("Loading FCIDUMP Hamiltonian from file: %s", model_file_name) (n_orbit, n_electron, n_spin), openfermion_hamiltonian_dict = _read_fcidump(model_file_name) - logging.info("FCIDUMP Hamiltonian '%s' successfully loaded", model_name) + logging.info("FCIDUMP Hamiltonian successfully loaded") logging.info("Converting OpenFermion Hamiltonian to internal Hamiltonian representation") self.hamiltonian = Hamiltonian(openfermion_hamiltonian_dict, kind="fermi") - logging.info("Internal Hamiltonian representation for model '%s' has been successfully created", model_name) + logging.info("Internal Hamiltonian representation has been successfully created") - logging.info("Caching OpenFermion Hamiltonian for model '%s'", model_name) + logging.info("Caching OpenFermion Hamiltonian") cache_file.parent.mkdir(parents=True, exist_ok=True) torch.save((self.hamiltonian.site, self.hamiltonian.kind, self.hamiltonian.coef), cache_file) - logging.info("OpenFermion Hamiltonian for model '%s' successfully cached", model_name) + logging.info("OpenFermion Hamiltonian successfully cached") self.n_qubit: int = n_orbit * 2 self.n_electron: int = n_electron self.n_spin: int = n_spin logging.info( - "Identified %d qubits, %d electrons and %d spin for model '%s'", + "Identified %d qubits, %d electrons and %d spin", self.n_qubit, self.n_electron, self.n_spin, - model_name, ) self.ref_energy: float @@ -196,10 +186,16 @@ def __init__(self, args: ModelConfig) -> None: if fcidump_ref_energy_file.exists(): with open(fcidump_ref_energy_file, "rt", encoding="utf-8") as file: fcidump_ref_energy_data = yaml.safe_load(file) - self.ref_energy = fcidump_ref_energy_data.get(model_name, 0) + # Extract filename without extensions for YAML lookup + yaml_key = model_file_name.name + if yaml_key.endswith(".FCIDUMP.gz"): + yaml_key = yaml_key[:-11] + elif yaml_key.endswith(".FCIDUMP"): + yaml_key = yaml_key[:-8] + self.ref_energy = fcidump_ref_energy_data.get(yaml_key, 0) else: self.ref_energy = 0 - logging.info("Reference energy for model '%s' is %.10f", model_name, self.ref_energy) + logging.info("Reference energy is %.10f", self.ref_energy) def apply_within(self, configs_i: torch.Tensor, psi_i: torch.Tensor, configs_j: torch.Tensor) -> torch.Tensor: return self.hamiltonian.apply_within(configs_i, psi_i, configs_j) diff --git a/qmp/models/openfermion.py b/qmp/models/openfermion.py index c2f047b..d5afbb7 100644 --- a/qmp/models/openfermion.py +++ b/qmp/models/openfermion.py @@ -15,16 +15,6 @@ from ..utility.model_dict import model_dict, ModelProto, NetworkProto, NetworkConfigProto -def _extract_model_name_from_path(path: pathlib.Path) -> str: - """ - Extract the model name from a file path by removing the .hdf5 extension. - """ - name = path.name - if name.endswith(".hdf5"): - return name[:-5] # Remove ".hdf5" - return name - - @dataclasses.dataclass class ModelConfig: """ @@ -49,7 +39,11 @@ class Model(ModelProto[ModelConfig]): @classmethod def default_group_name(cls, config: ModelConfig) -> str: - return _extract_model_name_from_path(config.model_path) + # Use the filename as the group name + name = config.model_path.name + if name.endswith(".hdf5"): + return name[:-5] # Remove ".hdf5" + return name def __init__(self, args: ModelConfig) -> None: logging.info("Input arguments successfully parsed") @@ -58,27 +52,23 @@ def __init__(self, args: ModelConfig) -> None: # model_path is now the complete path to the file (already converted to Path in __post_init__) model_file_name = args.model_path - # Extract model name for logging purposes - model_name = _extract_model_name_from_path(model_file_name) - logging.info("Loading OpenFermion model '%s' from file: %s", model_name, model_file_name) + logging.info("Loading OpenFermion model from file: %s", model_file_name) openfermion_model: openfermion.MolecularData = openfermion.MolecularData(filename=str(model_file_name)) # type: ignore[no-untyped-call] - logging.info("OpenFermion model '%s' successfully loaded", model_name) + logging.info("OpenFermion model successfully loaded") self.n_qubits: int = int(openfermion_model.n_qubits) # type: ignore[arg-type] self.n_electrons: int = int(openfermion_model.n_electrons) # type: ignore[arg-type] - logging.info( - "Identified %d qubits and %d electrons for model '%s'", self.n_qubits, self.n_electrons, model_name - ) + logging.info("Identified %d qubits and %d electrons", self.n_qubits, self.n_electrons) self.ref_energy: float = float(openfermion_model.fci_energy) # type: ignore[arg-type] - logging.info("Reference energy for model '%s' is %.10f", model_name, self.ref_energy) + logging.info("Reference energy is %.10f", self.ref_energy) logging.info("Converting OpenFermion Hamiltonian to internal Hamiltonian representation") self.hamiltonian: Hamiltonian = Hamiltonian( openfermion.transforms.get_fermion_operator(openfermion_model.get_molecular_hamiltonian()).terms, # type: ignore[no-untyped-call] kind="fermi", ) - logging.info("Internal Hamiltonian representation for model '%s' has been successfully created", model_name) + logging.info("Internal Hamiltonian representation has been successfully created") def apply_within(self, configs_i: torch.Tensor, psi_i: torch.Tensor, configs_j: torch.Tensor) -> torch.Tensor: return self.hamiltonian.apply_within(configs_i, psi_i, configs_j) From f454ef738fc01c271f980da975e31b97317b5746 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 19 Dec 2025 02:07:46 +0000 Subject: [PATCH 8/8] Refactor: use removesuffix() for cleaner extension removal Replace string slicing with removesuffix() method for better readability and intent clarity. This is available in Python 3.9+ and the project requires Python 3.12. Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com> --- qmp/models/fcidump.py | 14 +++----------- qmp/models/openfermion.py | 7 ++----- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/qmp/models/fcidump.py b/qmp/models/fcidump.py index b59e2ee..13b59aa 100644 --- a/qmp/models/fcidump.py +++ b/qmp/models/fcidump.py @@ -123,13 +123,9 @@ class Model(ModelProto[ModelConfig]): @classmethod def default_group_name(cls, config: ModelConfig) -> str: - # Use the filename as the group name + # Use the filename as the group name, removing extensions name = config.model_path.name - if name.endswith(".FCIDUMP.gz"): - return name[:-11] # Remove ".FCIDUMP.gz" - elif name.endswith(".FCIDUMP"): - return name[:-8] # Remove ".FCIDUMP" - return name + return name.removesuffix(".FCIDUMP.gz").removesuffix(".FCIDUMP") def __init__(self, args: ModelConfig) -> None: # pylint: disable=too-many-locals @@ -187,11 +183,7 @@ def __init__(self, args: ModelConfig) -> None: with open(fcidump_ref_energy_file, "rt", encoding="utf-8") as file: fcidump_ref_energy_data = yaml.safe_load(file) # Extract filename without extensions for YAML lookup - yaml_key = model_file_name.name - if yaml_key.endswith(".FCIDUMP.gz"): - yaml_key = yaml_key[:-11] - elif yaml_key.endswith(".FCIDUMP"): - yaml_key = yaml_key[:-8] + yaml_key = model_file_name.name.removesuffix(".FCIDUMP.gz").removesuffix(".FCIDUMP") self.ref_energy = fcidump_ref_energy_data.get(yaml_key, 0) else: self.ref_energy = 0 diff --git a/qmp/models/openfermion.py b/qmp/models/openfermion.py index d5afbb7..5565710 100644 --- a/qmp/models/openfermion.py +++ b/qmp/models/openfermion.py @@ -39,11 +39,8 @@ class Model(ModelProto[ModelConfig]): @classmethod def default_group_name(cls, config: ModelConfig) -> str: - # Use the filename as the group name - name = config.model_path.name - if name.endswith(".hdf5"): - return name[:-5] # Remove ".hdf5" - return name + # Use the filename as the group name, removing extension + return config.model_path.name.removesuffix(".hdf5") def __init__(self, args: ModelConfig) -> None: logging.info("Input arguments successfully parsed")