-
Notifications
You must be signed in to change notification settings - Fork 2
wip: Differentiable Hamiltonian evaluation #52
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import numpy as np | ||
| import pytest | ||
| import equinox as eqx | ||
| import jax | ||
| from jax.experimental import enable_x64 | ||
| from numpy.testing import assert_allclose | ||
| from pyscf import dft | ||
|
|
@@ -40,3 +42,29 @@ def test_energy(inputs, basis_name, mol): | |
| actual = H(P) + nuclear_energy(mol) | ||
| expect = s.energy_tot() | ||
| assert_allclose(actual, expect, atol=1e-6) | ||
|
|
||
|
|
||
| def test_autograd_wrt_positions(): | ||
| mol = molecule("h2") | ||
| scfmol = to_pyscf(mol, basis_name="def2-SVP") | ||
| s = dft.RKS(scfmol, xc=cases["lda"]) | ||
| s.kernel() | ||
| P = np.asarray(s.make_rdm1()) | ||
| g = s.Gradients() | ||
| scf_grad = g.kernel() | ||
|
|
||
| @jax.jit | ||
| def f(pos, rest, basis): | ||
| structure = eqx.combine(pos, rest) | ||
| basis = eqx.tree_at(lambda x: x.structure, basis, structure) | ||
| pcenter = structure.position[basis.primitives.atom_index] | ||
| basis = eqx.tree_at(lambda x: x.primitives.center, basis, pcenter) | ||
| H = Hamiltonian(basis=basis, xc_method="lda", backend="mess") | ||
|
|
||
| return H(P) + nuclear_energy(structure) | ||
|
|
||
| mol = jax.device_put(mol) | ||
| basis = basisset(mol, "def2-SVP") | ||
| pos, rest = eqx.partition(mol, lambda x: id(x) == id(mol.position)) | ||
| grad_E = jax.grad(f)(pos, rest, basis) | ||
| assert_allclose(-grad_E.position, scf_grad, atol=1e-1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have an intuition regarding these accuracies? And what is the current relative error? :D
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current problem is the XC evaluation isn't exactly like-for-like but it should be possible to get them to match much closer. Checkout test/test_autograd_integrals.py which shows that autodiff can match the analytic gradients of the one-electron components. I'm optimistic to have the absolute error at 1e-5 once mess can match the XC mesh generation exactly the same as pyscf's numerical quadrature. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super interesting! 🚀