Skip to content

Commit

Permalink
Add support for MM charges and positions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Grofe committed Feb 6, 2025
1 parent 0348a87 commit f86d051
Show file tree
Hide file tree
Showing 29 changed files with 310 additions and 25 deletions.
67 changes: 56 additions & 11 deletions azure-quantum/azure/quantum/target/microsoft/elements/dft/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pathlib import Path
import copy
import json
from collections import defaultdict


class MicrosoftElementsDft(Target):
Expand Down Expand Up @@ -185,25 +186,69 @@ def _xyz_to_qcschema_mol(self, file_data: str ) -> Dict[str, Any]:
"""
Convert xyz format to qcschema molecule.
"""

lines = file_data.split("\n")

mm_index = None
for i, line in enumerate(lines):
if '--' in line:
mm_index = i
break

mol = {
"schema_name": "qcschema_molecule",
"schema_version": 2,
}
if mm_index:
qm_xyz = self._xyz_primitive(lines[0:mm_index])
mm_xyz = self._xyz_primitive(lines[mm_index+1:])
mol["geometry"] = qm_xyz["geometry"]
mol["symbols"] = qm_xyz["symbols"]
mol["extras"] = {
"mm_geometry": mm_xyz["geometry"],
"mm_charges": mm_xyz["charges"],
"mm_symbols": mm_xyz["symbols"],
}
else:
qm_xyz = self._xyz_primitive(lines)
mol["geometry"] = qm_xyz["geometry"]
mol["symbols"] = qm_xyz["symbols"]


return mol

@classmethod
def _xyz_primitive(self, lines: list[str]) -> dict:
"""
Read through a list of lines and extract the xyz data in the format of an xyz file.
Returns: result = {
"geometry": [ x1, y1. z1, ...]
"symbols": ['H', 'O', ...]
"charges": [q1, q2, ...] # Optional
}
If there are four columns in the positions section then return that as MM charges.
"""
if len(lines) < 3:
raise ValueError("Invalid xyz format.")
raise ValueError("Invalid xyz format. Not enough lines.")
n_atoms = int(lines.pop(0))
comment = lines.pop(0)
mol = {
"geometry": [],
"symbols": [],
}
mol = defaultdict(list)
bohr_to_angstrom = 0.52917721092
for line in lines:
if line:
elements = line.split()
if len(elements) < 4:
raise ValueError("Invalid xyz format.")
symbol, x, y, z = elements
mol["symbols"].append(symbol)
mol["geometry"] += [float(x)/bohr_to_angstrom, float(y)/bohr_to_angstrom, float(z)/bohr_to_angstrom]
if len(elements) == 4:
symbol, x, y, z = elements
mol["symbols"].append(symbol)
mol["geometry"] += [float(x)/bohr_to_angstrom, float(y)/bohr_to_angstrom, float(z)/bohr_to_angstrom]
elif len(elements) == 5:
symbol, x, y, z, q = elements
mol["symbols"].append(symbol)
mol["geometry"] += [float(x)/bohr_to_angstrom, float(y)/bohr_to_angstrom, float(z)/bohr_to_angstrom]
mol["charges"].append(q)
else:
raise ValueError("Invalid xyz format. Should have: Element x y z")
else:
break

Expand Down
50 changes: 50 additions & 0 deletions azure-quantum/tests/unit/test_microsoft_elements_dft.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,64 @@ def test_create_toc_data(data_regression, inputs):
H 1.00 0.00 0.00
H -1.00 1.00 1.00
""",
"""3
water
O 0.00 0.00 0.00
H 1.00 0.00 0.00
H -1.00 1.00 1.00
--MM--
3
MM water
O 0.00 0.00 2.00 -0.6
H 1.00 0.00 2.00 0.3
H -1.00 1.00 3.00 0.3
""",
"""3
water
O 0.00 0.00 0.00
H 1.00 0.00 0.00
H -1.00 1.00 1.00
-- MM --
3
MM water
O 0.00 0.00 2.00 -0.6
H 1.00 0.00 2.00 0.3
H -1.00 1.00 3.00 0.3
""",
"""3
water
O 0.00 0.00 0.00
H 1.00 0.00 0.00
H -1.00 1.00 1.00
--
3
MM water
O 0.00 0.00 2.00 -0.6
H 1.00 0.00 2.00 0.3
H -1.00 1.00 3.00 0.3
""",
"""3
water
O 0.00 0.00 0.00
H 1.00 0.00 0.00
H -1.00 1.00 1.00
--MM--
3
MM water
O 0.00 0.00 2.00 -0.6
H 1.00 0.00 2.00 0.3
H -1.00 1.00 3.00 0.3
""",
],
ids=[
'minimal',
'minimal+line_at_end',
'mm_label_with_no_spaces',
'mm_label_with_spaces',
'mm_minimal_label',
'mm_with_extra_line_before',
]
)
def test_xyz_parsing_correct_xyz_files(data_regression, xyz_str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down Expand Up @@ -430,6 +432,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down Expand Up @@ -445,6 +447,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down Expand Up @@ -430,6 +432,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down Expand Up @@ -443,6 +445,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down Expand Up @@ -433,6 +435,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down Expand Up @@ -433,6 +435,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down Expand Up @@ -433,6 +435,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down Expand Up @@ -449,6 +451,8 @@
- 187.49201203790946
- 195.49783676462934
- 192.17191878539484
schema_name: qcschema_molecule
schema_version: 2
symbols:
- O
- H
Expand Down
Loading

0 comments on commit f86d051

Please sign in to comment.