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

[BUG] KeyError when running QNode(qfun, dev)(graph) with graph an instance of networkx Graph object #6585

Closed
1 task done
simidzija opened this issue Nov 14, 2024 · 0 comments
Labels
bug 🐛 Something isn't working

Comments

@simidzija
Copy link

Expected behavior

Let qfun take a networkx Graph as an argument. When I call QNode(qfun, dev)(graph) I expect not to get an error because no error occurs for arguments of different types (including various non numeric types).

Actual behavior

Instead when I run QNode(qfun, dev)(graph) I get KeyError: 'networkx'.

Additional information

No response

Source code

import pennylane as qml
import networkx as nx

def circuit(graph):
    return qml.state()

graph = nx.Graph()
qml.QNode(circuit, dev)(graph)

Tracebacks

Traceback (most recent call last):
  File "/Users/petar/Documents/graph_states/new.py", line 11, in <module>
    circuit(x)
  File "/opt/miniconda3/envs/qml/lib/python3.11/site-packages/pennylane/workflow/qnode.py", line 987, in __call__
    return self._impl_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/qml/lib/python3.11/site-packages/pennylane/workflow/qnode.py", line 973, in _impl_call
    interface = INTERFACE_MAP[interface]
                ~~~~~~~~~~~~~^^^^^^^^^^^
KeyError: 'networkx'

System information

Name: PennyLane
Version: 0.39.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /opt/miniconda3/envs/qml/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, toml, typing-extensions
Required-by: PennyLane_Lightning

Platform info:           macOS-15.1-arm64-arm-64bit
Python version:          3.11.10
Numpy version:           1.26.4
Scipy version:           1.14.1
Installed devices:
- default.clifford (PennyLane-0.39.0)
- default.gaussian (PennyLane-0.39.0)
- default.mixed (PennyLane-0.39.0)
- default.qubit (PennyLane-0.39.0)
- default.qutrit (PennyLane-0.39.0)
- default.qutrit.mixed (PennyLane-0.39.0)
- default.tensor (PennyLane-0.39.0)
- null.qubit (PennyLane-0.39.0)
- reference.qubit (PennyLane-0.39.0)
- lightning.qubit (PennyLane_Lightning-0.39.0)

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.
@simidzija simidzija added the bug 🐛 Something isn't working label Nov 14, 2024
PietropaoloFrisoni added a commit that referenced this issue 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 issue 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
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants