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

Deprecate old QNode #440

Merged
merged 33 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d04d57d
first attempt
josh146 Dec 3, 2019
0449b02
fix all imports
josh146 Dec 4, 2019
4353021
fix more tests
josh146 Dec 4, 2019
81c5c0b
fix more tests
josh146 Dec 4, 2019
68d3655
fix optimization
josh146 Dec 4, 2019
914201b
fix QNG
josh146 Dec 4, 2019
6d16e06
fixing
josh146 Dec 4, 2019
2a49dac
fix qg
josh146 Dec 4, 2019
972eaae
Fixing test_utils.py
antalszava Dec 4, 2019
c48407e
Change error type for tests in tensornet
antalszava Dec 4, 2019
1184909
Adding model to the capabilities of in vqe test
antalszava Dec 4, 2019
295d214
Fix AutogradQnode queue tests
antalszava Dec 4, 2019
e0dea40
Refactor VQE for torch and tf (QNode already creates specific qnode o…
antalszava Dec 5, 2019
544545c
Fix numpy array test case for parameter empty check, fix test messages
antalszava Dec 5, 2019
87a46e8
Correct docs
antalszava Dec 5, 2019
14f2014
Remove trailing whitespace
antalszava Dec 5, 2019
1d26bfa
Merge branch 'master' into deprecate_old_qnode
josh146 Dec 5, 2019
0e4764e
Merge branch 'deprecate_old_qnode' of https://github.com/XanaduAI/pen…
antalszava Dec 5, 2019
4367596
Merge branch 'master' into deprecate_old_qnode
josh146 Dec 6, 2019
423737e
attempted docs fixes
josh146 Dec 6, 2019
6d9bf20
fix docs
josh146 Dec 6, 2019
d59ab50
update pip version
josh146 Dec 6, 2019
baac65b
Merge branch 'master' into deprecate_old_qnode
josh146 Dec 6, 2019
a8a3fec
remove redundant theme info
josh146 Dec 6, 2019
ccb9628
Merge branch 'deprecate_old_qnode' of github.com:XanaduAI/pennylane i…
josh146 Dec 6, 2019
f993afe
fix incorrect VQE docstring
josh146 Dec 6, 2019
3f06ca5
Revert to_tf() change
antalszava Dec 6, 2019
11fe957
Update tests/test_quantum_gradients.py
antalszava Dec 6, 2019
32ff57d
Remove recorder return statements in test_templates_layers.py
antalszava Dec 6, 2019
1bd2084
Remove recorder return statements in test_templates_subroutines.py
antalszava Dec 6, 2019
d6e498a
Merge branch 'deprecate_old_qnode' of https://github.com/XanaduAI/pen…
antalszava Dec 6, 2019
c0269ba
Changelog
antalszava Dec 6, 2019
f57ec37
Add comment for numpy.array check
antalszava Dec 6, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions doc/code/qml_beta.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ and features.

.. currentmodule:: pennylane.beta

.. automodapi:: pennylane.beta.interfaces
:include-all-objects:
:no-inheritance-diagram:

.. automodapi:: pennylane.beta.plugins
:include-all-objects:
:no-inheritance-diagram:

.. automodapi:: pennylane.beta.qnodes
:include-all-objects:

.. automodapi:: pennylane.beta.vqe
:include-all-objects:
:no-inheritance-diagram:
4 changes: 4 additions & 0 deletions doc/code/qml_interfaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ qml.interfaces
.. rubric:: Modules

.. automodule:: pennylane.interfaces

.. automodapi:: pennylane.interfaces
:include-all-objects:
:no-inheritance-diagram:
5 changes: 5 additions & 0 deletions doc/code/qml_qnodes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
qml.qnodes
==========

.. automodapi:: pennylane.qnodes
:include-all-objects:
7 changes: 3 additions & 4 deletions pennylane/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
from .measure import expval, var, sample
from .ops import *
from .optimize import *
from .qnode import QNode, QuantumFunctionError
from .qnodes import qnode, QNode, QuantumFunctionError
from ._version import __version__

# NOTE: this has to be imported last,
# otherwise it will clash with the .qnode import.
from .decorator import qnode

_current_context = None


# overwrite module docstrings
Expand Down
2 changes: 1 addition & 1 deletion pennylane/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import numpy as np

from pennylane.operation import Operation, Observable, Sample, Variance, Expectation, Tensor
from .qnode import QuantumFunctionError
from pennylane.qnodes import QuantumFunctionError


class DeviceError(Exception):
Expand Down
2 changes: 0 additions & 2 deletions pennylane/beta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@
"""
This module contains experimental, contributed, and beta code.
"""
import pennylane.beta.interfaces
import pennylane.beta.qnodes
import pennylane.beta.vqe
19 changes: 0 additions & 19 deletions pennylane/beta/interfaces/__init__.py

This file was deleted.

6 changes: 1 addition & 5 deletions pennylane/beta/plugins/expt_tensornet_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,10 @@ class TensorNetworkTF(TensorNetwork):

pip install tensornetwork>=0.2 tensorflow>=2.0

In addition, you will need to use the new-style QNode to enable
calculation of the quantum gradient using TensorFlow.

**Example:**

>>> from pennylane.beta.qnodes import qnode
>>> dev = qml.device("expt.tensornet.tf", wires=1)
>>> @qnode(dev, interface="autograd", diff_method="best")
>>> @qml.qnode(dev, interface="autograd", diff_method="best")
>>> def circuit(x):
... qml.RX(x[1], wires=0)
... qml.Rot(x[0], x[1], x[2], wires=0)
Expand Down
9 changes: 2 additions & 7 deletions pennylane/beta/vqe/vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import numpy as np
from pennylane.ops import Observable
from pennylane.measure import expval
from pennylane.qnode import QNode
from pennylane.qnodes import QNode


class Hamiltonian:
Expand Down Expand Up @@ -122,12 +122,7 @@ def circuit(*params, obs=obs):
ansatz(*params, wires=range(device.num_wires))
return expval(obs)

qnode = QNode(circuit, device)

if interface == "tf":
qnode = qnode.to_tf()
elif interface == "torch":
qnode = qnode.to_torch()
antalszava marked this conversation as resolved.
Show resolved Hide resolved
qnode = QNode(circuit, device, interface=interface)

qnodes.append(qnode)

Expand Down
79 changes: 0 additions & 79 deletions pennylane/decorator.py

This file was deleted.

File renamed without changes.
11 changes: 3 additions & 8 deletions pennylane/interfaces/tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module contains the :func:`TFQNode` function to convert Numpy-interfacing quantum nodes to TensorFlow
This module contains the :func:`to_tf` function to convert Numpy-interfacing quantum nodes to TensorFlow
compatible quantum nodes.
"""
# pylint: disable=redefined-outer-name
Expand All @@ -31,7 +31,7 @@
from tensorflow import Variable # pylint: disable=unused-import,ungrouped-imports


def TFQNode(qnode):
def to_tf(qnode):
"""Function that accepts a :class:`~.QNode`, and returns a TensorFlow eager-execution-compatible QNode.

Args:
Expand Down Expand Up @@ -85,12 +85,7 @@ def grad(grad_output, **tfkwargs):
# evaluate the Jacobian matrix of the QNode
variables = tfkwargs.get('variables', None)

if hasattr(qnode, "to_autograd"):
# new style QNode.jacobian has a different signature
jacobian = qnode.jacobian(args, kwargs)
else:
jacobian = qnode.jacobian(args, **kwargs)

jacobian = qnode.jacobian(args, kwargs)
grad_output_np = grad_output.numpy()

# perform the vector-Jacobian product
Expand Down
10 changes: 3 additions & 7 deletions pennylane/interfaces/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module contains the :func:`TorchQNode` function to convert Numpy-interfacing quantum nodes to PyTorch
This module contains the :func:`to_torch` function to convert Numpy-interfacing quantum nodes to PyTorch
compatible quantum nodes.
"""
# pylint: disable=redefined-outer-name,arguments-differ
Expand Down Expand Up @@ -95,7 +95,7 @@ def kwargs_to_numpy(kwargs):
return res


def TorchQNode(qnode):
def to_torch(qnode):
"""Function that accepts a :class:`~.QNode`, and returns a PyTorch-compatible QNode.

Args:
Expand Down Expand Up @@ -140,11 +140,7 @@ def backward(ctx, grad_output): #pragma: no cover
# the way in which the backward class is created on the fly

# evaluate the Jacobian matrix of the QNode
if hasattr(qnode, "to_autograd"):
# new style QNode.jacobian has a different signature
jacobian = qnode.jacobian(ctx.args, ctx.kwargs)
else:
jacobian = qnode.jacobian(ctx.args, **ctx.kwargs)
jacobian = qnode.jacobian(ctx.args, ctx.kwargs)

if grad_output.is_cuda: # pragma: no cover
grad_output_np = grad_output.cpu().detach().numpy()
Expand Down
33 changes: 17 additions & 16 deletions pennylane/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
outcomes from quantum observables - expectation values, variances of expectations,
and measurement samples.
"""
from .qnode import QNode, QuantumFunctionError
import pennylane as qml
from .operation import Observable, Sample, Variance, Expectation, Tensor
from .qnodes import QuantumFunctionError


def _remove_if_in_queue(op):
r"""Helper function to handle removing ops from the QNode queue"""
if op in QNode._current_context.queue:
QNode._current_context.queue.remove(op)
if op in qml._current_context.queue:
qml._current_context.queue.remove(op)


def expval(op):
Expand All @@ -41,7 +42,7 @@ def expval(op):
"{} is not an observable: cannot be used with expval".format(op.name)
)

if QNode._current_context is not None:
if qml._current_context is not None:
# delete observables from QNode operation queue if needed
if isinstance(op, Tensor):
for o in op.obs:
Expand All @@ -52,9 +53,9 @@ def expval(op):
# set return type to be an expectation value
op.return_type = Expectation

if QNode._current_context is not None:
if qml._current_context is not None:
# add observable to QNode observable queue
QNode._current_context._append_op(op)
qml._current_context._append_op(op)

return op

Expand All @@ -73,20 +74,20 @@ def var(op):
"{} is not an observable: cannot be used with var".format(op.name)
)

if QNode._current_context is not None:
if qml._current_context is not None:
# delete operations from QNode queue
if isinstance(op, Tensor):
for o in op.obs:
QNode._current_context.queue.remove(o)
qml._current_context.queue.remove(o)
else:
QNode._current_context.queue.remove(op)
qml._current_context.queue.remove(op)

# set return type to be a variance
op.return_type = Variance

if QNode._current_context is not None:
if qml._current_context is not None:
# add observable to QNode observable queue
QNode._current_context._append_op(op)
qml._current_context._append_op(op)

return op

Expand All @@ -106,19 +107,19 @@ def sample(op):
"{} is not an observable: cannot be used with sample".format(op.name)
)

if QNode._current_context is not None:
if qml._current_context is not None:
# delete operations from QNode queue
if isinstance(op, Tensor):
for o in op.obs:
QNode._current_context.queue.remove(o)
qml._current_context.queue.remove(o)
else:
QNode._current_context.queue.remove(op)
qml._current_context.queue.remove(op)

# set return type to be a sample
op.return_type = Sample

if QNode._current_context is not None:
if qml._current_context is not None:
# add observable to QNode observable queue
QNode._current_context._append_op(op)
qml._current_context._append_op(op)

return op
Loading