-
Notifications
You must be signed in to change notification settings - Fork 575
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
Changes from all commits
6c568ae
dc19d6e
2a4c649
56cf430
70076ea
ed13f46
a09256f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
""" | ||
import itertools | ||
from functools import reduce | ||
import warnings | ||
|
||
import pytest | ||
import numpy as np | ||
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we just need another case with no params here... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh oops, I didn't even spot this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!