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
tests/test_qnode.py Outdated Show resolved Hide resolved
@astralcai astralcai requested a review from albi3ro September 13, 2024 14:00
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
mudit2812 pushed a commit that referenced this pull request Sep 23, 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 <[email protected]>
PietropaoloFrisoni added a commit that referenced this pull request Nov 20, 2024
… an instance of `networkx` Graph object (#6600)

**Context:** Issue #6585 indicates that we don't currently allow
arguments with types defined in libraries not listed in the supported
interfaces.

In #6225, the convention of treating internally as `interface=None`
parameters of the `numpy` interface has been introduced.
The idea of this PR is to allow non-trainable `qnode` inputs to "just
work" as arguments, without any special consideration or treatment. We
want to rely on the default `numpy` interface for libraries we know
nothing about.

**Description of the Change:** By default, we set `None` as the value of
the interface if the latter is not found in the list of supported
interfaces. According to the convention introduced in #6225, this should
automatically map the interface to `numpy`.

**Benefits:** Now the `qnode` accepts arguments with types defined in
libraries that are not necessarily in the list of supported interfaces,
such as the `Graph` class defined in `networkx`.

**Possible Drawbacks:** None that I can think of.

**Related GitHub Issues:** #6585 

**Related Shortcut Stories:** [sc-78344]

---------

Co-authored-by: Christina Lee <[email protected]>
astralcai pushed a commit that referenced this pull request Nov 20, 2024
… an instance of `networkx` Graph object (#6600)

**Context:** Issue #6585 indicates that we don't currently allow
arguments with types defined in libraries not listed in the supported
interfaces.

In #6225, the convention of treating internally as `interface=None`
parameters of the `numpy` interface has been introduced.
The idea of this PR is to allow non-trainable `qnode` inputs to "just
work" as arguments, without any special consideration or treatment. We
want to rely on the default `numpy` interface for libraries we know
nothing about.

**Description of the Change:** By default, we set `None` as the value of
the interface if the latter is not found in the list of supported
interfaces. According to the convention introduced in #6225, this should
automatically map the interface to `numpy`.

**Benefits:** Now the `qnode` accepts arguments with types defined in
libraries that are not necessarily in the list of supported interfaces,
such as the `Graph` class defined in `networkx`.

**Possible Drawbacks:** None that I can think of.

**Related GitHub Issues:** #6585 

**Related Shortcut Stories:** [sc-78344]

---------

Co-authored-by: Christina Lee <[email protected]>
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