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

[Capture] Add finite differences jvps #6853

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Jan 17, 2025

Context:

Currently, program capture only supports differentiation with backprop. This is only supported on default.qubit

Description of the Change:

Adds the capability to take finite difference derivatives.

Benefits:

We can now differentiate anything, and we can differentiate with the lightnings.

Possible Drawbacks:

finite diff tends to be noisy to the point of uselessness.

Related GitHub Issues:

[sc-82167]

Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Base automatically changed from setup-jvp-structure to master January 21, 2025 21:21
Copy link

codecov bot commented Jan 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.55%. Comparing base (afec979) to head (c53f10d).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6853   +/-   ##
=======================================
  Coverage   99.54%   99.55%           
=======================================
  Files         477      477           
  Lines       45246    45273   +27     
=======================================
+ Hits        45042    45070   +28     
+ Misses        204      203    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@albi3ro albi3ro requested a review from mudit2812 January 23, 2025 16:55
@albi3ro albi3ro requested a review from andrijapau January 23, 2025 22:45
pennylane/gradients/finite_difference.py Outdated Show resolved Hide resolved
pennylane/gradients/finite_difference.py Outdated Show resolved Hide resolved
pennylane/gradients/finite_difference.py Outdated Show resolved Hide resolved
pennylane/gradients/finite_difference.py Outdated Show resolved Hide resolved
pennylane/gradients/finite_difference.py Outdated Show resolved Hide resolved
tests/capture/workflow/test_capture_finite_diff.py Outdated Show resolved Hide resolved
return qml.expval(qml.Z(0))

with pytest.warns(UserWarning, match="Detected float32 parameter with finite differences."):
jax.grad(circuit)(jnp.array(0.5, dtype=jnp.float32))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be using qml.grad in the tests?

Copy link
Contributor

Choose a reason for hiding this comment

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

Potential over-testing suggestion: or maybe parametrizing over qml and jax diff functions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a jax specific test.

jax.grad(circuit)(jnp.array(0.5, dtype=jnp.float32))


class TestGradients:
Copy link
Contributor

Choose a reason for hiding this comment

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

Non blocking questions:
Do we have tests to validate that the correct diff method is actually being used? Does it matter if we do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have a test at the bottom where I double check the captured jaxpr.

Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Forgot to say in the last review, but could you also add a test for differentiating for complex parameters? Other than that, I'm ready to approve.

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