-
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
[OpRefactor] Remove par_domain attribute #1907
Conversation
…neAI/pennylane into rewrite-tests-that-loop-over-ops
…m_params_dynamically
…ennylane into set_num_params_dynamically
Codecov Report
@@ Coverage Diff @@
## master #1907 +/- ##
==========================================
- Coverage 98.85% 98.84% -0.02%
==========================================
Files 220 220
Lines 16972 16843 -129
==========================================
- Hits 16778 16648 -130
- Misses 194 195 +1
Continue to review full report at Codecov.
|
@@ -583,9 +570,7 @@ class Operation(Operator): | |||
As with :class:`~.Operator`, the following class attributes must be | |||
defined for all operations: | |||
|
|||
* :attr:`~.Operator.num_params` |
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.
Sorry, I forgot to remove these in an earlier PR
None, | ||
"F", | ||
), "Operations that depend on arrays containing free variables may only be differentiated using the F method." | ||
|
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.
This is deleted for good, since we cannot do this verification any more - we have to rely on the developers to do things right!
"Convert par into a list of parameters that op expects." | ||
if op.par_domain == "A": | ||
return [np.diag([x, 1]) for x in par] | ||
return par |
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.
This and the same below was made redundant by a previous PR anyways!
@@ -115,57 +110,13 @@ class DummyOp(qml.operation.CVOperation): | |||
): | |||
DummyOp(0.5, wires=[0, 1]) | |||
|
|||
def test_grad_method_with_integer_params(self): |
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.
Since the check above is removed, these don't make sense any more
|
||
class TestObservableInstantiation: | ||
"""Test that wires are specified when a qml.operation.Observable is instantiated""" | ||
|
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.
Another thing I snuck in: removing this merges the single test function with the above test class TestObservableConstruction
which is kind of the same
@@ -279,48 +279,36 @@ def circuit(x, y): | |||
assert grad_A == pytest.approx(grad_F, abs=tol) | |||
assert grad_A2 == pytest.approx(grad_F, abs=tol) | |||
|
|||
@pytest.mark.parametrize("name", qml.ops._cv__ops__) | |||
def test_CVOperation_with_heisenberg_and_no_params(self, name, gaussian_dev, tol): | |||
"""An integration test for CV gates that support analytic differentiation |
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.
I realised that the if statement in the test reduces all CV ops loaded to InterfermoterUnitary
and I make the test more explicit.
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.
This is part of a pattern we enforce in a previous PR: do not automatically import and instantiate gates!
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.
Useful to know, thanks!
codecov complains about a new untested |
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.
Thanks @mariaschuld, liberating to see code being removed! Main question is whether num_params
should be removed in a few places? It seems it's no longer needed for the Operator
class and it's child classes. But otherwise good to go!
@@ -146,7 +144,6 @@ def circuit(): | |||
""" | |||
num_params = 1 |
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.
num_params
was removed from operation.py
, should it be removed here too?
tests/beta/test_beta_qnode.py
Outdated
@@ -932,7 +932,6 @@ def test_device_expansion(self, diff_method, mode, mocker): | |||
class UnsupportedOp(qml.operation.Operation): | |||
num_wires = 1 | |||
num_params = 1 |
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.
And here
@@ -87,7 +87,6 @@ class ControlledOperation(Operation): | |||
control_wires: A wire or set of wires. | |||
""" | |||
|
|||
par_domain = "A" | |||
num_wires = AnyWires | |||
num_params = property(lambda self: self.subtape.num_params) |
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.
Again, should num_params
be removed?
tests/beta/test_beta_qnode.py
Outdated
@@ -965,7 +964,6 @@ def test_no_gradient_expansion(self, mocker): | |||
class UnsupportedOp(qml.operation.Operation): | |||
num_wires = 1 | |||
num_params = 1 |
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.
And here
@@ -2283,7 +2276,6 @@ class TestSGate(qml.operation.Operation): | |||
matrix = np.array([[0, 1], [1, 0]]) | |||
num_params = 0 |
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.
There are also a few num_param
s still existing in the tests.
@@ -279,48 +279,36 @@ def circuit(x, y): | |||
assert grad_A == pytest.approx(grad_F, abs=tol) | |||
assert grad_A2 == pytest.approx(grad_F, abs=tol) | |||
|
|||
@pytest.mark.parametrize("name", qml.ops._cv__ops__) | |||
def test_CVOperation_with_heisenberg_and_no_params(self, name, gaussian_dev, tol): | |||
"""An integration test for CV gates that support analytic differentiation |
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.
Useful to know, thanks!
@anthayes92 you are so right! Since this is kind of a different topic, let me open another PR for the |
The
par_domain
attribute of the operator class is an anachronism that is removed everywhere.Luckily, this only affected tests. The only exception is a sanity check at the creation of operations, comparing the parameter domain to the grad method. However, one could argue that such a check is an unnecessary slowdown anyways, and should rather be covered by tests.