Conversation
The `update` subfunction in `adagrad()` will produce division-by-zero
errors. These occur when running `check/pytest` in Python 3.12 with
NumPy 2.x:
```
src/openfermion/resource_estimates/pbc/thc/factorizations/thc_jax_test.py::test_kpoint_thc_helper
src/openfermion/resource_estimates/pbc/thc/generate_costing_table_thc_test.py::test_generate_costing_table_thc
/usr/local/google/home/mhucka/project-files/google/github/openfermion/src/openfermion/resource_estimates/thc/utils/adagrad.py:43:
RuntimeWarning: divide by zero encountered in divide
g_sq_inv_sqrt = np.where(g_sq > 0, 1.0 / np.sqrt(g_sq), 0.0)
```
A common method to avoid this situation is to add a small positive
number to `g_sq` before taking the square root. This ensures that the
value is never exactly zero, avoiding the division-by-zero problem.
This approach is a common and robust method for avoiding zero
denominators in gradient-based optimization. It also acts as a
smoothing factor, preventing the update from becoming unstable when
`g_sq` gets very small.
dstrain115
reviewed
Jun 4, 2025
| g_sq += np.square(g) | ||
| g_sq_inv_sqrt = np.where(g_sq > 0, 1.0 / np.sqrt(g_sq), 0.0) | ||
| # Add a small number to avoid division by zero | ||
| g_sq_safe = g_sq + eps |
Contributor
There was a problem hiding this comment.
What do you think about doing this only if abs(g_sq<eps)?
Contributor
Author
There was a problem hiding this comment.
It may impact performance, but probably not too badly. I've rewritten it like this:
if np.abs(g_sq < eps):
# Add a small number to avoid division by zero.
g_sq_inv_sqrt = 1.0 / np.sqrt(g_sq + eps)
else:
g_sq_inv_sqrt = 1.0 / np.sqrt(g_sq)Does that seem okay?
I forgot that g_sq is an array, so a test like x < y means testing every value, and that's going to be more time-consuming.
My reading of to handle cases like this is that the original is not a bad approach in a case where gradients are being done, because ultimately the values are not used in the results. @dstrain115 do you think it's going to lead to errors if left in the original form?
dstrain115
approved these changes
Jun 4, 2025
mhucka
added a commit
to mhucka/OpenFermion
that referenced
this pull request
Sep 25, 2025
The `update` subfunction in `adagrad()` will produce division-by-zero
errors. These occur when running `check/pytest` in Python 3.12 with
NumPy 2.x:
```
src/openfermion/resource_estimates/pbc/thc/factorizations/thc_jax_test.py::test_kpoint_thc_helper
src/openfermion/resource_estimates/pbc/thc/generate_costing_table_thc_test.py::test_generate_costing_table_thc
/usr/local/google/home/mhucka/project-files/google/github/openfermion/src/openfermion/resource_estimates/thc/utils/adagrad.py:43:
RuntimeWarning: divide by zero encountered in divide
g_sq_inv_sqrt = np.where(g_sq > 0, 1.0 / np.sqrt(g_sq), 0.0)
```
A common method to avoid this situation is to add a small positive
number to `g_sq` before taking the square root. This ensures that the
value is never exactly zero, avoiding the division-by-zero problem. This
approach is a common and robust method for avoiding zero denominators in
gradient-based optimization. It also acts as a smoothing factor,
preventing the update from becoming unstable when `g_sq` gets very
small.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
updatesubfunction inadagrad()will produce division-by-zero errors. These occur when runningcheck/pytestin Python 3.12 with NumPy 2.x:A common method to avoid this situation is to add a small positive number to
g_sqbefore taking the square root. This ensures that the value is never exactly zero, avoiding the division-by-zero problem. This approach is a common and robust method for avoiding zero denominators in gradient-based optimization. It also acts as a smoothing factor, preventing the update from becoming unstable wheng_sqgets very small.