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

Raise a deprecation warning if Operator.decomposition is a staticmethod #2214

Merged
merged 7 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion pennylane/gradients/gradient_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def grad_method_validation(method, tape):

diff_methods = {
idx: info["grad_method"]
for idx, info in tape._par_info.items()
for idx, info in tape._par_info.items() # pylint: disable=protected-access
if idx in tape.trainable_params
}

Expand Down
17 changes: 16 additions & 1 deletion pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,22 @@ def expand(self):
tape = qml.tape.QuantumTape(do_queue=False)

with tape:
self.decomposition()

try:
self.decomposition()

except TypeError:
if self.num_params == 0:
self.decomposition(wires=self.wires)
else:
self.decomposition(*self.parameters, wires=self.wires)

warnings.warn(
"Operator.decomposition() is now an instance method, and no longer accepts parameters. "
"Either define the static method 'compute_decomposition' instead, or use "
"'self.wires' and 'self.parameters'.",
UserWarning,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!


if not self.data:
# original operation has no trainable parameters
Expand Down
89 changes: 89 additions & 0 deletions tests/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""
import itertools
from functools import reduce
import warnings

import pytest
import numpy as np
Expand Down Expand Up @@ -1845,3 +1846,91 @@ def test_eigvals_tensor_deprecation(self):
m1 = op.eigvals

assert np.allclose(m1, op.get_eigvals())

def test_decomposition_deprecation_no_parameters(self):
"""Test that old-style staticmethod decompositions for an operation
with no parameters raises a warning"""
dev = qml.device("default.qubit", wires=1)

class MyOp(Operation):
num_wires = 1
num_params = 0

@staticmethod
def decomposition(wires):
qml.RY(0.5, wires=wires[0])

@qml.qnode(dev)
def qnode():
MyOp(wires=0)
return qml.state()

with pytest.warns(UserWarning, match="is now an instance method"):
result1 = qnode()

# using an instance method will not raise a deprecation warning

class MyOp(Operation):
num_wires = 1
num_params = 0

def decomposition(self):
qml.RY(0.5, wires=self.wires[0])

@qml.qnode(dev)
def qnode():
MyOp(wires=0)
return qml.state()

with warnings.catch_warnings():
# any warnings emitted will be raised as errors
warnings.simplefilter("error")
result2 = qnode()

assert np.allclose(result1, result2)

def test_decomposition_deprecation_parameters(self):
"""Test that old-style staticmethod decompositions for an operation
with parameters raises a warning"""
dev = qml.device("default.qubit", wires=1)

class MyOp(Operation):
num_wires = 1
num_params = 2

@staticmethod
def decomposition(*params, wires):
qml.RY(params[0], wires=wires[0])
qml.PauliZ(wires=wires[0])
qml.RX(params[1], wires=wires[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we just need another case with no params here...

Copy link
Member Author

Choose a reason for hiding this comment

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

oh oops, I didn't even spot this

Copy link
Member Author

Choose a reason for hiding this comment

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

done!


@qml.qnode(dev)
def qnode(*params):
MyOp(*params, wires=0)
return qml.state()

with pytest.warns(UserWarning, match="is now an instance method"):
result1 = qnode(0.1, 0.2)

# using an instance method will not raise a deprecation warning

class MyOp(Operation):
num_wires = 1
num_params = 2

def decomposition(self):
qml.RY(self.parameters[0], wires=self.wires[0])
qml.PauliZ(wires=self.wires[0])
qml.RX(self.parameters[1], wires=self.wires[0])

@qml.qnode(dev)
def qnode(*params):
MyOp(*params, wires=0)
return qml.state()

with warnings.catch_warnings():
# any warnings emitted will be raised as errors
warnings.simplefilter("error")
result2 = qnode(0.1, 0.2)

assert np.allclose(result1, result2)