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 get_best_diff_method and _get_gradient_fn to qml.workflow #6399

Merged
merged 25 commits into from
Oct 25, 2024

Conversation

andrijapau
Copy link
Contributor

@andrijapau andrijapau commented Oct 15, 2024

Context:

Right now, QNode.get_gradient_fn and QNode.get_best_method, and QNode.best_method_str all exist as public or static methods and aren't used internally (aside from inside the QNode class) and they don't make much sense externally. To address this, a new user-friendly function should be added.

Description of the Change:

Implemented functions get_best_diff_method and _get_gradient_fn to qml.workflow. They behave the same as the existing methods in QNode but are designed with a simpler interface in mind 😄. The former is intended to be more user-facing and so has a simplifed interface. The latter is more for internal development and will be used to deprecate the internal QNode methods mentioned earlier.

For get_best_diff_method the user can provide the QNode they are trying to execute and it will return a string with the "best" differentiation method.

For _get_gradient_fn, the intention was to extract this from QNode and use it later on once we begin the deprecation and removal of those methods outlined earlier.4

Example

qml.workflow.get_best_diff_method

>>> dev = qml.device("default.qubit")
>>> qn = qml.QNode(lambda: None, dev)
>>> qml.workflow.get_best_diff_method(qn)()
'backprop'
>>> dev_shots = qml.device("default.qubit", shots=45)
>>> qn_shots = qml.QNode(lambda: None, dev_shots)
>>> qml.workflow.get_best_diff_method(qn_shots)()
'parameter-shift'

qml.workflow._get_gradient_fn

>>> qml.workflow._get_gradient_fn(dev_shots, diff_method='parameter-shift')
<transform: param_shift>

Benefits: Improves QNode organization and structure.

Possible Drawbacks: None

[sc-72157]

@andrijapau andrijapau self-assigned this Oct 15, 2024
@andrijapau andrijapau changed the title [WIP] Add get_best_diff_method and get_gradient_fn to qml.workflow Add get_best_diff_method and get_gradient_fn to qml.workflow Oct 18, 2024
@andrijapau andrijapau marked this pull request as ready for review October 18, 2024 19:06
@andrijapau andrijapau requested a review from albi3ro October 18, 2024 19:28
Copy link

codecov bot commented Oct 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.38%. Comparing base (cc80809) to head (35ec3b2).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6399      +/-   ##
==========================================
- Coverage   99.70%   99.38%   -0.32%     
==========================================
  Files         448      450       +2     
  Lines       42566    42619      +53     
==========================================
- Hits        42441    42359      -82     
- Misses        125      260     +135     

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

Copy link
Contributor

@albi3ro albi3ro 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 putting this together.

My thoughts now are:

Do users care at all about the transform version of a diff method specification? Or would they just care about the string specification?

I'm leaning to strictly returning the string variant for now. get_best_diff_method is not intended to be used as part of the core workflow, but a way for users to inspect the workflow.

As for converting the string spec to a gradient transform, I think that could probably happen during a new resolve_execution_config developer function.

@andrijapau andrijapau requested a review from albi3ro October 22, 2024 20:26
@andrijapau andrijapau requested a review from astralcai October 23, 2024 15:23
pennylane/workflow/get_best_diff_method.py Show resolved Hide resolved
pennylane/workflow/get_gradient_fn.py Outdated Show resolved Hide resolved
pennylane/workflow/get_best_diff_method.py Outdated Show resolved Hide resolved
@andrijapau andrijapau requested a review from astralcai October 23, 2024 19:58
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Sorry to update this at this point, but I'm not entirely sure if we need a public get_gradient_fn function.

Who would be the main consumer for this variant? If users, we would probably want the interface to be more user friendly. If developers, we can make it private _get_gradient_fn, and thus be able to change any of the code as need be.

Yes, we end up in a situation where we are deprecating QNode.get_gradient_fn without a true alternative, but I think QNode.diff_method combined with get_best_diff_method are sufficient replacements.

@andrijapau andrijapau requested a review from albi3ro October 24, 2024 18:49
@andrijapau andrijapau changed the title Add get_best_diff_method and get_gradient_fn to qml.workflow Add get_best_diff_method and _get_gradient_fn to qml.workflow Oct 24, 2024
Co-authored-by: Christina Lee <christina@xanadu.ai>
@andrijapau andrijapau requested a review from albi3ro October 24, 2024 19:47
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

🚀

Copy link
Contributor

@astralcai astralcai left a comment

Choose a reason for hiding this comment

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

🚀

@albi3ro albi3ro merged commit 43cb979 into master Oct 25, 2024
39 of 40 checks passed
@albi3ro albi3ro deleted the add-best-diff-method branch October 25, 2024 16:03
mudit2812 pushed a commit that referenced this pull request Nov 11, 2024
…6399)

**Context:**

Right now, `QNode.get_gradient_fn` and `QNode.get_best_method`, and
`QNode.best_method_str` all exist as public or static methods and aren't
used internally (aside from inside the `QNode` class) and they don't
make much sense externally. To address this, a new user-friendly
function should be added.

**Description of the Change:**

Implemented functions `get_best_diff_method` and `_get_gradient_fn` to
`qml.workflow`. They behave the same as the existing methods in `QNode`
but are designed with a simpler interface in mind 😄. The former is
intended to be more user-facing and so has a simplifed interface. The
latter is more for internal development and will be used to deprecate
the internal `QNode` methods mentioned earlier.

For `get_best_diff_method` the user can provide the `QNode` they are
trying to execute and it will return a string with the "best"
differentiation method.

For `_get_gradient_fn`, the intention was to extract this from `QNode`
and use it later on once we begin the deprecation and removal of those
methods outlined earlier.4

Example 

_`qml.workflow.get_best_diff_method`_

```python
>>> dev = qml.device("default.qubit")
>>> qn = qml.QNode(lambda: None, dev)
>>> qml.workflow.get_best_diff_method(qn)()
'backprop'
>>> dev_shots = qml.device("default.qubit", shots=45)
>>> qn_shots = qml.QNode(lambda: None, dev_shots)
>>> qml.workflow.get_best_diff_method(qn_shots)()
'parameter-shift'
```

_`qml.workflow._get_gradient_fn`_

```python
>>> qml.workflow._get_gradient_fn(dev_shots, diff_method='parameter-shift')
<transform: param_shift>
```

**Benefits:** Improves `QNode` organization and structure. 

**Possible Drawbacks:** None

[sc-72157]

---------

Co-authored-by: Christina Lee <christina@xanadu.ai>
andrijapau added a commit that referenced this pull request Nov 13, 2024
…QNode` (#6418)

**Context:**

#6399 added helper functions `get_best_diff_method` to `qml.workflow`.
This means we are free to deprecate the old static methods out of
`QNode`.

**Description of the Change:** 

Deprecation of the methods.

Tweaked the new `_get_gradient_fn` a bit.

**Benefits:** Cleaning up `QNode` and making it more user-friendly.

**Possible Drawbacks:** None

[sc-76083]

---------

Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
Co-authored-by: Christina Lee <christina@xanadu.ai>
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