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

[OpRefactor] Remove par_domain attribute #1907

Merged
merged 42 commits into from
Nov 18, 2021
Merged

Conversation

mariaschuld
Copy link
Contributor

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.

@josh146 josh146 changed the base branch from master to set_num_params_dynamically November 17, 2021 07:25
@codecov
Copy link

codecov bot commented Nov 17, 2021

Codecov Report

Merging #1907 (5d56bc0) into master (c3f4d04) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
pennylane/ops/__init__.py 100.00% <ø> (ø)
pennylane/ops/channel.py 100.00% <ø> (ø)
pennylane/ops/cv.py 100.00% <ø> (ø)
pennylane/ops/qubit/arithmetic_ops.py 100.00% <ø> (ø)
pennylane/ops/qubit/hamiltonian.py 99.04% <ø> (-0.01%) ⬇️
pennylane/ops/qubit/matrix_ops.py 99.16% <ø> (-0.03%) ⬇️
pennylane/ops/qubit/non_parametric_ops.py 97.30% <ø> (-0.10%) ⬇️
pennylane/ops/qubit/observables.py 100.00% <ø> (ø)
pennylane/ops/qubit/parametric_ops.py 99.82% <ø> (-0.01%) ⬇️
pennylane/ops/qubit/qchem_ops.py 100.00% <ø> (ø)
... and 34 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c3f4d04...5d56bc0. Read the comment docs.

Base automatically changed from set_num_params_dynamically to master November 17, 2021 09:08
@@ -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`
Copy link
Contributor Author

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."

Copy link
Contributor Author

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
Copy link
Contributor Author

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):
Copy link
Contributor Author

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"""

Copy link
Contributor Author

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
Copy link
Contributor Author

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.

Copy link
Contributor Author

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!

Copy link
Contributor

Choose a reason for hiding this comment

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

Useful to know, thanks!

@mariaschuld
Copy link
Contributor Author

codecov complains about a new untested NotImplementedError which we generally don't test - I'll overwrite

Copy link
Contributor

@anthayes92 anthayes92 left a 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
Copy link
Contributor

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?

@@ -932,7 +932,6 @@ def test_device_expansion(self, diff_method, mode, mocker):
class UnsupportedOp(qml.operation.Operation):
num_wires = 1
num_params = 1
Copy link
Contributor

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)
Copy link
Contributor

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?

@@ -965,7 +964,6 @@ def test_no_gradient_expansion(self, mocker):
class UnsupportedOp(qml.operation.Operation):
num_wires = 1
num_params = 1
Copy link
Contributor

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
Copy link
Contributor

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_params 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Useful to know, thanks!

@mariaschuld
Copy link
Contributor Author

@anthayes92 you are so right! Since this is kind of a different topic, let me open another PR for the num_params. I don't know how I missed them in the previous one.

@mariaschuld mariaschuld merged commit 4078456 into master Nov 18, 2021
@mariaschuld mariaschuld deleted the remove-par-domain branch November 18, 2021 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants