-
Notifications
You must be signed in to change notification settings - Fork 57
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
Fix pytorch gradients #1450
base: master
Are you sure you want to change the base?
Fix pytorch gradients #1450
Conversation
I found a potential problem when you cast the parameters of parametrized gates: qibo/src/qibo/backends/pytorch.py Line 32 in 1b6eb8a
here you cast to self.dtype which is usually torch.complex128 but it should actually be torch.float32/64 since this is a parameter. I don't know whether this is the origin of the problem with the gradients though.
EDIT: nevermind, you necessarily have to cast to complex otherwise when you build the matrix elements pytorch complains about mismatching dtypes... |
Actually this may be part of the problem, i am rewriting in a better way the whole casting function differentiating the matrix_dtype from the parameters_dtype |
I think that now everything has been fixed. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1450 +/- ##
==========================================
- Coverage 99.94% 99.94% -0.01%
==========================================
Files 81 81
Lines 11740 11715 -25
==========================================
- Hits 11733 11708 -25
Misses 7 7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Thanks @Simone-Bordoni, #1449 is still failing even with this patch, but I think that's more related to a problem with the |
In the new version the backend.cast has requires_grad=False by default, you need to modify #1449 and activate the gradients when casting. |
Like this? self.circuit_parameters = torch.nn.Parameter(BACKEND.cast(circuit.get_parameters(), dtype=torch.float64, requires_grad=True)) because this fails anyway. EDIT: sorry maybe I should have specified better, but it doesn't fail in the sense that the gradients don't pass, it actually crashes with an error:
|
OK, I thought that the problem was the gradient not passing. Is it a different error with respect to what you were getting before this update? |
Yes, if I remember correctly before it used to run without crashing but had the gradients flow problem. |
Fix #1449
Checklist: