-
Notifications
You must be signed in to change notification settings - Fork 586
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
Refine HF solver to compute correct gradients #1780
Conversation
Hello. You may have forgotten to update the changelog!
|
Codecov Report
@@ Coverage Diff @@
## qchem_hf #1780 +/- ##
=========================================
Coverage 98.95% 98.95%
=========================================
Files 215 215
Lines 16175 16181 +6
=========================================
+ Hits 16006 16012 +6
Misses 169 169
Continue to review full report at Codecov.
|
@soranjh is there a deeper technical issue with computing gradients with degenerate eigenvalues? Or is it simply a matter of Autograd not supporting it (but other frameworks do?) |
Oh, that's good to know! This means there is the option for us to simply patch/fix the |
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.
It looks like a good temporary fix for making it into the release, but we should as @josh146 mentioned probably rewrite the autograd function in qml.math
afterwards :)
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.
Thanks @soranjh, looks good from my end!
pennylane/hf/hamiltonian.py
Outdated
for n, t in enumerate(h_ferm[1]): | ||
|
||
if len(t) == 0: | ||
h = qml.Hamiltonian([h_ferm[0][n]], [qml.Identity(0)]) | ||
h = h + qml.Hamiltonian([h_ferm[0][n]], [qml.Identity(0)]) |
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.
minor question: does adding in a constant * I
term cause any redundant quantum evaluations 🤔 E.g., is PL evaluating <psi|I|psi>
even though it doesn't need to?
Not an issue for this PR, but if this is the case, this is a bug we should fix in a separate PR!
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.
I have modified computation of the Hamiltonian such that all coefficients and ops are computed first and the PL Hamiltonian is computed once at the end using h = qml.Hamiltonian(coeffs, ops, simplify=True)
. This avoids multiple PL Hamiltonian additions and makes the process much faster and also fixes the issue of having redundant terms.
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.
Looks great!
Indeed, let's revisit this in the future for a cleaner fix
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.
Looks good.
Context:
The HF solver returns
nan
values when gradients are computed for any molecule larger than H2. The issue is related to the autograd failure in computing gradients of functions that perform matrix diagonalization for matrices with degenerate eigenvalues. See for example:which returns
The issue can be fixed by lifting the degeneracy through adding a small random value (in the order of 1.0e-12) to each of the diagonal elements of the matrix given above with
s = s + anp.diag(anp.random.rand(len(s)) * 1.0e-12)
This returns the gradients as
Description of the Change:
In the
generate_scf
, the overlap matrix is modified such that a small random perturbation in the order of 1.0e-12, is added to its diagonal elements.Benefits:
This change allows computing gradient values for any given molecule.
Possible Drawbacks:
The diagonal elements of the overlap matrix are all 1 since the atomic orbitals are normalised. We have assumed that adding a small random value to these elements does not affect the accuracy of the results returned by the HF solver. However, this is not a fundamental fix to the problem of autograd failing to compute the gradients when
eigh
is applied to a matrix with degenerate eigenvalues.Related GitHub Issues:
No related issues.