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

Clean up how interface is handled in QNode and qml.execute #6225

Merged
merged 38 commits into from
Sep 16, 2024

Conversation

astralcai
Copy link
Contributor

@astralcai astralcai commented Sep 5, 2024

Regarding numpy and autograd:

  • When the parameters are of the numpy interface, internally treat it as interface=None.
  • Does not change the behaviour of treating user specified interface="numpy" as using autograd.

Regarding interfaces in general:

  • The set of canonical interface names in INTERFACE_MAP is expanded to include more specific names such as jax-jit, and tf-autograph. _convert_to_interfaces in qnode.py uses a separate interface_conversion_map to further map the specific interfaces to their corresponding general interface names that can be passed to the like argument of qml.math.asarray (e.g. "tf" to "tensorflow", "jax-jit" to "jax").
  • In QNode and qml.execute, every time we get an interface from user input or qml.math.get_interface, we map it to a canonical interface name using INTERFACE_MAP. Aside from these two scenarios, we assume that the interface name is one of the canonical interface names everywhere else. QNode.interface is now assumed to be one of the canonical interface names.
  • User input of interface=None gets mapped to numpy immediately. Internally, QNode.interface will never be None. It'll be numpy for having no interface.
  • If qml.math.get_interface returns numpy, we do not map it to anything. We keep numpy.

Collateral bug fix included as well:

  • Fixes a bug where a circuit of the autograd interfaces sometimes returns results that are not autograd.
  • Adds compute_sparse_matrix to Hermitian

[sc-73144]

Copy link
Contributor

github-actions bot commented Sep 5, 2024

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.

@astralcai astralcai changed the title Internally stop treating all numpy as autograd Clean up how interface is handled in QNode and qml.execute Sep 10, 2024
Copy link

codecov bot commented Sep 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.58%. Comparing base (d0344b0) to head (99d393c).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6225      +/-   ##
==========================================
- Coverage   99.59%   99.58%   -0.01%     
==========================================
  Files         443      443              
  Lines       42255    42273      +18     
==========================================
+ Hits        42082    42096      +14     
- Misses        173      177       +4     

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

astralcai and others added 2 commits September 11, 2024 09:55
tests/test_qnode.py Outdated Show resolved Hide resolved
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.

🎉 Very happy to finally be getting this done 🚀 🎉

Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

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

LGTM! Just a curiosity and a non-blocking observation

doc/releases/changelog-dev.md Show resolved Hide resolved
pennylane/workflow/execution.py Show resolved Hide resolved
@astralcai astralcai merged commit 228fdaf into master Sep 16, 2024
37 checks passed
@astralcai astralcai deleted the autograd-bug branch September 16, 2024 13:57
mudit2812 pushed a commit that referenced this pull request Sep 16, 2024
Regarding `numpy` and `autograd`:
- When the parameters are of the `numpy` interface, internally treat it
as `interface=None`.
- Does not change the behaviour of treating user specified
`interface="numpy"` as using autograd.

Regarding interfaces in general:
- The set of canonical interface names in `INTERFACE_MAP` is expanded to
include more specific names such as `jax-jit`, and `tf-autograph`.
`_convert_to_interfaces` in `qnode.py` uses a separate
`interface_conversion_map` to further map the specific interfaces to
their corresponding general interface names that can be passed to the
`like` argument of `qml.math.asarray` (e.g. "tf" to "tensorflow",
"jax-jit" to "jax").
- In `QNode` and `qml.execute`, every time we get an interface from user
input or `qml.math.get_interface`, we map it to a canonical interface
name using `INTERFACE_MAP`. Aside from these two scenarios, we assume
that the interface name is one of the canonical interface names
everywhere else. `QNode.interface` is now assumed to be one of the
canonical interface names.
- User input of `interface=None` gets mapped to `numpy` immediately.
Internally, `QNode.interface` will never be `None`. It'll be `numpy` for
having no interface.
- If `qml.math.get_interface` returns `numpy`, we do not map it to
anything. We keep `numpy`.

Collateral bug fix included as well:
- Fixes a bug where a circuit of the `autograd` interfaces sometimes
returns results that are not `autograd`.
- Adds `compute_sparse_matrix` to `Hermitian`

[sc-73144]

---------

Co-authored-by: Christina Lee <christina@xanadu.ai>
mudit2812 pushed a commit that referenced this pull request Sep 16, 2024
Regarding `numpy` and `autograd`:
- When the parameters are of the `numpy` interface, internally treat it
as `interface=None`.
- Does not change the behaviour of treating user specified
`interface="numpy"` as using autograd.

Regarding interfaces in general:
- The set of canonical interface names in `INTERFACE_MAP` is expanded to
include more specific names such as `jax-jit`, and `tf-autograph`.
`_convert_to_interfaces` in `qnode.py` uses a separate
`interface_conversion_map` to further map the specific interfaces to
their corresponding general interface names that can be passed to the
`like` argument of `qml.math.asarray` (e.g. "tf" to "tensorflow",
"jax-jit" to "jax").
- In `QNode` and `qml.execute`, every time we get an interface from user
input or `qml.math.get_interface`, we map it to a canonical interface
name using `INTERFACE_MAP`. Aside from these two scenarios, we assume
that the interface name is one of the canonical interface names
everywhere else. `QNode.interface` is now assumed to be one of the
canonical interface names.
- User input of `interface=None` gets mapped to `numpy` immediately.
Internally, `QNode.interface` will never be `None`. It'll be `numpy` for
having no interface.
- If `qml.math.get_interface` returns `numpy`, we do not map it to
anything. We keep `numpy`.

Collateral bug fix included as well:
- Fixes a bug where a circuit of the `autograd` interfaces sometimes
returns results that are not `autograd`.
- Adds `compute_sparse_matrix` to `Hermitian`

[sc-73144]

---------

Co-authored-by: Christina Lee <christina@xanadu.ai>
mudit2812 pushed a commit that referenced this pull request Sep 18, 2024
Regarding `numpy` and `autograd`:
- When the parameters are of the `numpy` interface, internally treat it
as `interface=None`.
- Does not change the behaviour of treating user specified
`interface="numpy"` as using autograd.

Regarding interfaces in general:
- The set of canonical interface names in `INTERFACE_MAP` is expanded to
include more specific names such as `jax-jit`, and `tf-autograph`.
`_convert_to_interfaces` in `qnode.py` uses a separate
`interface_conversion_map` to further map the specific interfaces to
their corresponding general interface names that can be passed to the
`like` argument of `qml.math.asarray` (e.g. "tf" to "tensorflow",
"jax-jit" to "jax").
- In `QNode` and `qml.execute`, every time we get an interface from user
input or `qml.math.get_interface`, we map it to a canonical interface
name using `INTERFACE_MAP`. Aside from these two scenarios, we assume
that the interface name is one of the canonical interface names
everywhere else. `QNode.interface` is now assumed to be one of the
canonical interface names.
- User input of `interface=None` gets mapped to `numpy` immediately.
Internally, `QNode.interface` will never be `None`. It'll be `numpy` for
having no interface.
- If `qml.math.get_interface` returns `numpy`, we do not map it to
anything. We keep `numpy`.

Collateral bug fix included as well:
- Fixes a bug where a circuit of the `autograd` interfaces sometimes
returns results that are not `autograd`.
- Adds `compute_sparse_matrix` to `Hermitian`

[sc-73144]

---------

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