-
Notifications
You must be signed in to change notification settings - Fork 66
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
Support differentiability through Torch tensors #703
Conversation
Typing is still failing
* works with basic features of pulser-diff * Fixed phase attribute setting; removed debugging code; reverted unnecessary changes * Modified register creation code to work with AbstractArray; register coordinates are differentiable with pulser-diff * Fixed type hints * Minor fixes and refactoring * Modified ParamObj code to work with quantum model training in pulser-diff * Minor refactoring; add possibility to ensure 0D AbstractArray is reshaped into 1D * Force array only for scalars
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 for tackling this ! Here are some comments about the integration of this new feature in github workflow, as well as some comments on the new pulser.math module :)
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.
Some comments about waveform, Pulse, Channels and Devices
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.
This is regarding the register :)
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 agree with the handling of torch tensors in the more complex structures. I have only couple questions on the pulser-pasqal side - but it's more future work that work that should be done at the moment.
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 a lot for the review @a-corni !
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 to me ! 👍
**Main changes:** - Reworking the NoiseModel interface (#710) - Allow modification of the EOM setpoint without disabling EOM mode (#708) - Enable definition of effective noise operators in all basis (#716) - Add leakage (#720) - Support differentiability through Torch tensors (#703) - Add from_abstract_repr to Device and VirtualDevice (#727) - [FEAT] Handle batches with partial results (#707) - Add open batches to pulser-pasqal (#701)
This PR adds native support for
torch.Tensor
on select quantities, so that the final Hamiltonian can be differentiated wrt to these parameters. A few important design considerations were taken here:torch
will be an optional dependency: Users that don't require this feature should be able to keep installing and using pulser as before. For those who do want thetorch
support, they can have it by adding[torch]
to theirpip install
commands:# Both options work pip install pulser[torch] pip install pulser-core[torch]
np.ndarray
s andtorch.Tensor
s indiscriminately, the wrapper classAbstractArray
is created. This class contains an array-like object, which is stored either astorch.Tensor
or anp.ndarray
. To the largest extent possible, instances of this class can be handled like array, allowing operations, indexing, etc... However, their use should be only internal, so they contain only the necessary functionality for the existent codebase - whenever extra functionality is needed, it can always be added.AbstractArray
must use custom functions to preserve theAbstractArray
type in the output. As such, analgous definitions of the necessarynumpy/torch
functions were defined in the newpulser.math
module. This new module is, by convention, imported withimport pulser.math as pm
and can often be used as a drop-in replacement fornp
.differentiabilitytensor-like behaviour are marked with the newpm.Differentiable
pm.TensorLike
type-hint. When this is present, atorch.Tensor
(withrequires_grad=True
or not) can be provided. Whenever a parameter is not marked aspm.TensorLike
, providing atorch.tensor
is not guaranteed to work as intended.