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

Add DeviceDerivatives jacobian product calculator #4527

Merged
merged 67 commits into from
Oct 19, 2023
Merged

Add DeviceDerivatives jacobian product calculator #4527

merged 67 commits into from
Oct 19, 2023

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Aug 25, 2023

This PR follows on from #4435 , and adds a child class that can handle device-provided derivatives.

It has two private caches, _results_cache and _jacs_cache. This handles storing the jacobian between the forward pass and the backward pass. The caches will also store results and jacobians between successive calls to the same instance.

The class can handle both device interfaces, qml.Device and qml.devices.Device.

Name for the class is still up in the air. This class takes the full jacobian from the device and then performs the dot product. The class in #4637 takes the jacobian product itself from the device.

albi3ro and others added 30 commits August 2, 2023 16:11
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just found a print statement, otherwise looks good to me, thanks @albi3ro ! 🎉

tests/interfaces/test_jacobian_products.py Outdated Show resolved Hide resolved
@mudit2812 mudit2812 added this to the v0.33 milestone Oct 17, 2023
@albi3ro albi3ro requested review from rmoyard and removed request for mudit2812 and timmysilv October 18, 2023 15:37
Copy link
Contributor

@rmoyard rmoyard 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 this PR! Two high level questions, how do we integrate adjoint_vjp and adjoint_jvp that do not use the multiplication? Second question: how do we choose between the tmethods of this class in the interfaces when they are multiple choise like (adjoint_vjp and vjp with multiplication)?

pennylane/interfaces/jacobian_products.py Outdated Show resolved Hide resolved
pennylane/interfaces/jacobian_products.py Outdated Show resolved Hide resolved
pennylane/interfaces/jacobian_products.py Show resolved Hide resolved
pennylane/interfaces/jacobian_products.py Show resolved Hide resolved
pennylane/interfaces/jacobian_products.py Show resolved Hide resolved
pennylane/interfaces/jacobian_products.py Show resolved Hide resolved
@albi3ro
Copy link
Contributor Author

albi3ro commented Oct 18, 2023

Thanks for this PR! Two high level questions, how do we integrate adjoint_vjp and adjoint_jvp that do not use the multiplication? Second question: how do we choose between the tmethods of this class in the interfaces when they are multiple choise like (adjoint_vjp and vjp with multiplication)?

Thanks for the review :)

DefaultQubit.compute_vjp and DefaultQubit.execute_and_compute_jvp will be bound through the next class to be added: #4637

Each interface will prefer a different method of the class. Autograd and torch will just use compute_vjp. Jax jit will use compute_jacobian, and tensorflow will either use compute_vjp or compute_jacobian based on whether or not it needs to trace the vjp calculation.

Jax will use execute_and_compute_jvp for all cases except for when its a DeviceJacobianProducts class (yes that's patchy but it works).

An instance of the relevant class will be created in the qml.execute code.

@rmoyard rmoyard self-requested a review October 19, 2023 16:28
Copy link
Contributor

@rmoyard rmoyard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me! 👍 Thanks for giving more details

@albi3ro albi3ro enabled auto-merge (squash) October 19, 2023 16:38
@albi3ro albi3ro merged commit fad9939 into master Oct 19, 2023
39 checks passed
@albi3ro albi3ro deleted the jpc-devices branch October 19, 2023 17:47
albi3ro added a commit that referenced this pull request Oct 19, 2023
This PR is a follow on to #4527 . [sc-44325]

It adds `DeviceJacobianProducts` that can bind device provided jvps and
vjps to the execution pipeline.

---------

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
mudit2812 added a commit that referenced this pull request Oct 20, 2023
This PR follows on from #4435 , and adds a child class that can handle
device-provided derivatives.

It has two private caches, `_results_cache` and `_jacs_cache`. This
handles storing the jacobian between the forward pass and the backward
pass. The caches will also store results and jacobians between
successive calls to the same instance.

The class can handle both device interfaces, `qml.Device` and
`qml.devices.Device`.

Name for the class is still up in the air. This class takes the full
jacobian from the device and then performs the dot product. The class in
#4637 takes the jacobian product itself from the device.

---------

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
mudit2812 pushed a commit that referenced this pull request Oct 20, 2023
This PR is a follow on to #4527 . [sc-44325]

It adds `DeviceJacobianProducts` that can bind device provided jvps and
vjps to the execution pipeline.

---------

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
albi3ro added a commit that referenced this pull request Nov 7, 2023
This PR follows on from #4527 and #4637 .

Since we still need to make performance improvements to
`DefaultQubit.compute_vjp`, I added an opt-in keyword argument
`use_device_jacobian_product` to `qml.execute`. This allows us to easily
compare between using the device jacobian product and not.

Fixes #3217

---------

Co-authored-by: David Wierichs <david.wierichs@xanadu.ai>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review-ready 👌 PRs which are ready for review by someone from the core team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants