Skip to content

Commit

Permalink
Set default charge/spin if not given in .xyz file
Browse files Browse the repository at this point in the history
  • Loading branch information
chmwzc committed Dec 5, 2023
1 parent a0e45f8 commit 3c79b6d
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/qibochem/driver/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, geometry=None, charge=0, multiplicity=1, basis=None, xyz_file
else:
# Check if xyz_file exists, then fill in the Molecule attributes
assert Path(f"{xyz_file}").exists(), f"{xyz_file} not found!"
self._process_xyz_file(xyz_file)
self._process_xyz_file(xyz_file, charge, multiplicity)

Check warning on line 45 in src/qibochem/driver/molecule.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/driver/molecule.py#L45

Added line #L45 was not covered by tests
if basis is None:
# Default bais is STO-3G
self.basis = "sto-3g"
Expand Down Expand Up @@ -80,7 +80,7 @@ def __init__(self, geometry=None, charge=0, multiplicity=1, basis=None, xyz_file
self.n_active_e = None
self.n_active_orbs = None # Number of spin-orbitals in the active space

def _process_xyz_file(self, xyz_file):
def _process_xyz_file(self, xyz_file, charge, multiplicity):
"""
Reads a .xyz file to obtain and set the molecular coordinates (in OpenFermion format),
charge, and multiplicity
Expand All @@ -91,7 +91,15 @@ def _process_xyz_file(self, xyz_file):
with open(xyz_file, encoding="utf-8") as file_handler:
# First two lines: # atoms and comment line (charge, multiplicity)
_n_atoms = int(file_handler.readline()) # Not needed/used
_charge, _multiplicity = (int(_num) for _num in file_handler.readline().split())

# Try to read charge and multiplicity from comment line
split_line = [int(_num) for _num in file_handler.readline().split()]
if len(split_line) == 2:

Check warning on line 97 in src/qibochem/driver/molecule.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/driver/molecule.py#L96-L97

Added lines #L96 - L97 were not covered by tests
# Format of comment line matches (charge, multiplicity):
_charge, _multiplicity = split_line

Check warning on line 99 in src/qibochem/driver/molecule.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/driver/molecule.py#L99

Added line #L99 was not covered by tests
else:
# Otherwise, use the default (from __init__) values of 0 and 1
_charge, _multiplicity = charge, multiplicity

Check warning on line 102 in src/qibochem/driver/molecule.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/driver/molecule.py#L102

Added line #L102 was not covered by tests

# Start reading xyz coordinates from the 3rd line onwards
_geometry = []
Expand Down

0 comments on commit 3c79b6d

Please sign in to comment.