Skip to content
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

Merged
merged 53 commits into from
Sep 17, 2024
Merged

Conversation

HGSilveri
Copy link
Collaborator

@HGSilveri HGSilveri commented Jul 1, 2024

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:

  1. 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 the torch support, they can have it by adding [torch] to their pip install commands:
# Both options work
pip install pulser[torch]
pip install pulser-core[torch]
  1. To handle both np.ndarrays and torch.Tensors indiscriminately, the wrapper class AbstractArray is created. This class contains an array-like object, which is stored either as torch.Tensor or a np.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.
  2. Operations on AbstractArray must use custom functions to preserve the AbstractArray type in the output. As such, analgous definitions of the necessary numpy/torch functions were defined in the new pulser.math module. This new module is, by convention, imported with import pulser.math as pm and can often be used as a drop-in replacement for np.
  3. Parameters supporting differentiabilitytensor-like behaviour are marked with the new pm.Differentiablepm.TensorLike type-hint. When this is present, a torch.Tensor (with requires_grad=True or not) can be provided. Whenever a parameter is not marked as pm.TensorLike, providing a torch.tensor is not guaranteed to work as intended.

HGSilveri and others added 30 commits June 24, 2024 10:53
* 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
@HGSilveri HGSilveri added this to the Pulser v1 milestone Jul 11, 2024
@HGSilveri HGSilveri self-assigned this Jul 17, 2024
Copy link
Collaborator

@a-corni a-corni left a 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 :)

.github/workflows/ci.yml Show resolved Hide resolved
.github/workflows/ci.yml Show resolved Hide resolved
pulser-core/pulser/math/abstract_array.py Show resolved Hide resolved
pulser-core/pulser/math/abstract_array.py Show resolved Hide resolved
pulser-core/pulser/math/abstract_array.py Show resolved Hide resolved
pulser-core/pulser/math/abstract_array.py Show resolved Hide resolved
pulser-core/pulser/math/abstract_array.py Show resolved Hide resolved
pulser-core/pulser/math/abstract_array.py Show resolved Hide resolved
pulser-core/pulser/math/__init__.py Outdated Show resolved Hide resolved
pulser-core/pulser/math/__init__.py Show resolved Hide resolved
Copy link
Collaborator

@a-corni a-corni left a 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

pulser-core/pulser/channels/base_channel.py Outdated Show resolved Hide resolved
pulser-core/pulser/channels/base_channel.py Show resolved Hide resolved
pulser-core/pulser/channels/base_channel.py Show resolved Hide resolved
pulser-core/pulser/channels/base_channel.py Show resolved Hide resolved
pulser-core/pulser/channels/base_channel.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
Copy link
Collaborator

@a-corni a-corni left a 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 :)

.github/workflows/ci.yml Show resolved Hide resolved
pulser-core/pulser/register/base_register.py Show resolved Hide resolved
pulser-core/pulser/register/base_register.py Show resolved Hide resolved
pulser-core/pulser/register/register.py Show resolved Hide resolved
pulser-core/pulser/register/_coordinates.py Show resolved Hide resolved
pulser-core/pulser/register/weight_maps.py Show resolved Hide resolved
pulser-core/pulser/register/weight_maps.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@a-corni a-corni left a 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.

pulser-core/pulser/sampler/samples.py Show resolved Hide resolved
pulser-core/pulser/sampler/samples.py Outdated Show resolved Hide resolved
pulser-core/pulser/sampler/samples.py Outdated Show resolved Hide resolved
pulser-pasqal/pulser_pasqal/pasqal_cloud.py Show resolved Hide resolved
Copy link
Collaborator Author

@HGSilveri HGSilveri left a 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 !

pulser-core/pulser/channels/base_channel.py Show resolved Hide resolved
pulser-core/pulser/channels/base_channel.py Show resolved Hide resolved
pulser-core/pulser/devices/_device_datacls.py Show resolved Hide resolved
pulser-core/pulser/register/register.py Show resolved Hide resolved
pulser-core/pulser/register/weight_maps.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
@HGSilveri HGSilveri modified the milestones: Pulser v1, v0.20 Release Sep 11, 2024
tests/test_abstract_repr.py Show resolved Hide resolved
tests/test_channels.py Show resolved Hide resolved
.github/workflows/ci.yml Show resolved Hide resolved
pulser-core/pulser/math/abstract_array.py Show resolved Hide resolved
pulser-core/pulser/math/abstract_array.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
pulser-core/pulser/waveforms.py Show resolved Hide resolved
Copy link
Collaborator

@a-corni a-corni left a 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 ! 👍

@HGSilveri HGSilveri merged commit e21d3a8 into develop Sep 17, 2024
9 checks passed
@HGSilveri HGSilveri deleted the abstract-array branch September 17, 2024 15:17
@HGSilveri HGSilveri mentioned this pull request Sep 20, 2024
HGSilveri added a commit that referenced this pull request Sep 20, 2024
**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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants