Skip to content

Enable NanoPET for atomic-basis spherical targets #527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
66b214d
Allow reading of traget type `'atomic_basis_spherical'`
jwa7 Mar 21, 2025
580036c
Missing `TargetInfo` attribute
jwa7 Mar 21, 2025
e635b48
Allow rotational augmentation of targets on atomic spherical basis (s…
jwa7 Mar 21, 2025
826263b
use `{}_to_device` and `{}_to_dtype` functions in validation batching
jwa7 Mar 21, 2025
c9d14cb
Modify loss to allow for empty blocks
jwa7 Mar 24, 2025
cf17a19
Allow multi-output training for atomic basis targets
jwa7 Mar 25, 2025
7a1179b
Stubs for documentation
jwa7 Mar 25, 2025
4c2942b
Use DiskDataset.
jwa7 Apr 3, 2025
130b110
Linter on docs.
jwa7 Apr 3, 2025
014cdf1
Merge branch 'main' into pet_atomic_basis
jwa7 Apr 8, 2025
716bde1
Predict whole matrix, part 1
jwa7 Apr 10, 2025
3779229
Treat matrices as a single target
jwa7 Apr 16, 2025
4c52ce8
Update docs
jwa7 Apr 16, 2025
06bf314
Format, lint
jwa7 Apr 16, 2025
4f3d879
Remove uncoupled basis targets for now
jwa7 Apr 16, 2025
fe6f26d
Modify per_atom.py
jwa7 Apr 16, 2025
696a62d
Merge branch 'main' into pet_atomic_basis
jwa7 Apr 16, 2025
a1e06d5
Paolo review comments
jwa7 Apr 17, 2025
a857b87
Address Guillaume review comments
jwa7 Apr 23, 2025
73c0919
Merge branch 'main' into pet_atomic_basis
jwa7 Apr 23, 2025
25822a5
Lint
jwa7 Apr 23, 2025
3a183d4
Use a str as `TargetInfo.target_type` instead of `is_scalar`, `is_car…
jwa7 Apr 25, 2025
9471b85
Use a layout TensorMap, not the incredibly slow `join`
jwa7 May 6, 2025
aaff647
incomplete fixing of the merge
jwa7 Jun 17, 2025
e7dc511
Fix bug
ppegolo Jun 18, 2025
647b1bd
Remove prints
ppegolo Jun 18, 2025
f793a21
Remove other prints
ppegolo Jun 18, 2025
eb13745
Fix device mismatch
ppegolo Jun 18, 2025
ff005fc
Linting
ppegolo Jun 18, 2025
e8ac068
Merge branch 'main' into pet_atomic_basis
ppegolo Jun 18, 2025
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
170 changes: 170 additions & 0 deletions docs/src/advanced-concepts/fitting-atomic-basis-spherical-targets.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
Fitting spherical targets on an atomic basis
============================================

.. note:: This tutorial is currently only applicable for model training with NanoPET.

.. note:: This section is a work in progress. It will be updated in the future to
include more details and examples.

Targeting electronic structure quantities such as the electron density, Hamiltonian
matrix, and density matrix is also possible with NanoPET in metatrain. These targets are
ususally expressed on an atom-centered quantum chemical basis set, and differ from
normal spherical targets in that they are per-atom or per-pair quantities, and crucially
with a size that is dependent on the atomic type(s) of the atom center(s).


Defining spherical targets on an atomic basis
---------------------------------------------

We will briefly outline a couple of examples of spherical targets on an atomic basis.
This section intends to describe the metadata structure of such targets, but the details
on how to generate these is outside the scope of this tutorial.

Electron density on a basis
###########################

First let's consider the electron density (though applicable also to any scalar field),
decomposed onto an atom-centered basis set. In this case, the model targets a set of
equivariant expansion coefficients.

These are stored in block-sparse TensorMap format according to the basis set definition.

For a methane (CH4) molecule, for instance, the keys may look like:

.. code-block::

TensorMap with 8 blocks
keys: o3_lambda o3_sigma center_type
0 1 6
1 1 6
2 1 6
3 1 6
4 1 6
0 1 1
1 1 1
2 1 1

where "o3_lambda" and "o3_sigma" are the angular order and inversion symmetry of the
irreducible components of the density decomposition (which is positive for a scalar
field, a real tensor), and "center_type" tracks the type of atom on which the
corresponding basis functions are centered.

For example for the block indexed by key:

.. code-block::

LabelsEntry(o3_lambda=2, o3_sigma=-1, center_type=6)


.. code-block::

TensorBlock
samples (1): ['system', 'atom']
components (5): ['o3_mu']
properties (5): ['n']
gradients: None

As this is a per-atom (single-center) quantity, the samples contain the "system" and
"atom" dimensions. As this is a Carbon block (type 6), there is one sample.

A single "o3_mu" dimension in the components tracks the components of the spherical
tensor of order "o3_lambda" (as indexed in the keys), and the radial basis functions are
enumerated in the properties with dimension "n".

Hamiltonian (or density matrix) on a coupled basis
##################################################

The elements of the Hamiltonian matrix (or analogously the density matrix) on the atomic
orbital basis can also be targeted. For now, this must be expressed in the coupled
angular momenta basis, and symmetrized with respect to permutations. Details on how to
represent Hamiltonian matrices in such a way are outside the scope of this tutorial but
will be covered elsewhere.

For methane, the keys of the Hamiltonian (coupled and symmetrized) are as follows:

.. code-block::

TensorMap with 56 blocks
keys: o3_lambda o3_sigma s2_pi first_atom_type second_atom_type
0 1 0 6 6
0 1 1 6 6
...
2 1 1 1 1
2 1 -1 1 1

where "o3_lambda" and "o3_sigma" are as before, and "s2_pi" tracks the permutational
symmetry of each block. A value of zero means unsymmetrized - in the case of on-site
terms or off-site terms for atoms of different types. Values of +/- 1 refer to plus- or
minus-permutationally symmetrized blocks, and only exist for off-site atom pairs of the
same atomic type. "first_atom_type" and "second_atom_type" refer to the types of the
pair of atoms the matrix element corresponds to.

For example for the block indexed by key:

.. code-block::

LabelsEntry(o3_lambda=1, o3_sigma=-1, s2_pi=0, first_atom_type=1, second_atom_type=6)


the block metadata is as follows:

.. code-block::

TensorBlock
samples (0): ['system', 'first_atom', 'second_atom', 'cell_shift_a', 'cell_shift_b', 'cell_shift_c']
components (1): ['o3_mu']
properties (39): ['l_1', 'l_2', 'n_1', 'n_2']
gradients: None

As this is a per-pair (two-center) quantity, the samples dimensions are the standard
dimensions found in a neighbor list, tracking the atomic indices of the atoms in the
pair and the cell translation vector that separates them (which can be non-zero in
periodic systems).

The components axis is the same as for the density coefficients above, as the
Hamiltonian is expressed on the coupled angular momenta basis.

Finally the properties tracks the indices of the original orbitals in the uncoupled
basis.

Preparing atomic basis spherical targets for metatrain
------------------------------------------------------

Atomic basis spherical targets must be stored in TensorMap format and written to a
DiskDataset prior to calling metatrain. With targets stored with the metadata structure
as outlined above, one can create a DiskDataset by following the example in
"examples/programmatic/disk_dataset".

Then, the ``systems`` and ``targets`` section of the input file should be written as
follows:

Input file
##########


.. code-block:: yaml

systems: disk_dataset.zip

targets:

mtt::electron_density_basis:
read_from: disk_dataset.zip
type: spherical_atomic_basis

mtt::hamiltonian:
read_from: disk_dataset.zip
type: spherical_atomic_basis
unit: Ha

Unlike normal spherical targets, the ``irreps`` do not need to be specified in the input
file and are instead inferred by reading the targets in the dataset. Whether the targets
are per-atom or per-pair is also inferred from the samples metadata of the targets, so
only the name (i.e. ``mtt::electron_density_basis``) and ``unit`` of the quantity needs to
be specified.

It is important to note that for per-pair targets suhc as the Hamiltonian and Density
Matrix, the atom-pair samples present for the target of a given system must reflect its
full neighbor list (including self terms). In other words, the targets must not contain
samples for pairs of atoms outside the specified model cutoff, and must contain all
samples within the cutoff.
10 changes: 9 additions & 1 deletion docs/src/advanced-concepts/fitting-generic-targets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,31 @@ capabilities of the architectures in metatrain.
- Scalars
- Spherical tensors
- Cartesian tensors
- Atomic basis spherical targets
* - SOAP-BPNN
- Energy, forces, stress/virial
- Yes
- Yes
- No
- No
* - GAP
- Energy, forces
- No
- No
- No
- No
* - PET
- Energy, forces, stress/virial
- Yes
- Yes
- Only with ``rank=1`` (vectors) and ``rank=2`` (2D tensors)
- No
* - NanoPET
- Energy, forces, stress/virial
- Yes
- Yes
- Only with ``rank=1`` (vectors) and ``rank=2`` (2D tensors)
- Yes (*)


Preparing generic targets for reading by metatrain
Expand All @@ -53,7 +58,7 @@ Input file
In order to read a generic target, you will have to specify its layout in the input
file. Suppose you want to learn a target named ``mtt::my_target``, which is
represented as a set of 10 independent per-atom 3D Cartesian vector (we need to
learn 3x10 values for each atom). The ``target`` section in the input file
learn 3x10 values for each atom). The ``targets`` section in the input file
should look
like this:

Expand Down Expand Up @@ -111,6 +116,9 @@ the target section should would look like this:
where ``o3_lambda`` specifies the L value of the spherical tensor and ``o3_sigma`` its
parity with respect to inversion (1 for proper tensors, -1 for pseudo-tensors).

(*) More information on how to learn targets expressed on an atomic basis of spherical
harmonics is given in the section on :ref:`learning-atomic-basis-spherical-targets`.

Preparing your targets -- ASE
#############################

Expand Down
Loading
Loading