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

gradient_kwargs is now a positional keyword argument to QNode #6828

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ deprecations are listed below.
Pending deprecations
--------------------

* Specifying gradient keyword arguments is deprecated and will be removed in v0.42.
Instead, please specify all arguments through the ``gradient_kwargs`` argument.

- Deprecated in v0.41
- Will be removed in v0.42

* The ``qsvt_legacy`` function has been deprecated.
Instead, use ``qml.qsvt``. The new functionality takes an input polynomial instead of angles.

Expand Down
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

<h3>Deprecations 👋</h3>

* Specifying gradient keyword arguments is deprecated and will be removed in v0.42.
Instead, please specify all arguments through the ``gradient_kwargs`` argument.
[(#6828)](https://github.com/PennyLaneAI/pennylane/pull/6828)

<h3>Documentation 📝</h3>

* Updated documentation for vibrational Hamiltonians
Expand Down
13 changes: 9 additions & 4 deletions pennylane/workflow/qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,7 @@
as the name suggests. If not provided,
the device will determine the best choice automatically. For usage details, please refer to the
:doc:`dynamic quantum circuits page </introduction/dynamic_quantum_circuits>`.

Keyword Args:
**kwargs: Any additional keyword arguments provided are passed to the differentiation
gradient_kwargs (dict): Any additional keyword arguments provided are passed to the differentiation
method. Please refer to the :mod:`qml.gradients <.gradients>` module for details
on supported options for your chosen gradient transform.

Expand Down Expand Up @@ -492,7 +490,7 @@
"""

# pylint: disable=too-many-arguments
def __init__(

Check notice on line 493 in pennylane/workflow/qnode.py

View check run for this annotation

codefactor.io / CodeFactor

pennylane/workflow/qnode.py#L493

Dangerous default value {} as argument (dangerous-default-value)
self,
func: Callable,
device: SupportedDeviceAPIs,
Expand All @@ -506,8 +504,15 @@
device_vjp: Union[None, bool] = False,
postselect_mode: Literal[None, "hw-like", "fill-shots"] = None,
mcm_method: Literal[None, "deferred", "one-shot", "tree-traversal"] = None,
**gradient_kwargs,
gradient_kwargs: dict = {},
**kwargs,
):
if kwargs:
warnings.warn(
f"Specifying gradient keyword arguments {list(kwargs.keys())} is deprecated and will be removed in v0.42. Instead, please specify all arguments in the gradient_kwargs argument.",
qml.PennyLaneDeprecationWarning,
)
gradient_kwargs = kwargs

if logger.isEnabledFor(logging.DEBUG):
logger.debug(
Expand Down
33 changes: 19 additions & 14 deletions tests/test_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ def test_expansion_strategy_error(self):

with pytest.raises(ValueError, match=r"'expansion_strategy' is no longer"):

@qml.qnode(qml.device("default.qubit"), expansion_strategy="device")
@qml.qnode(
qml.device("default.qubit"), gradient_kwargs={"expansion_strategy": "device"}
)
Comment on lines +165 to +167
Copy link
Contributor

Choose a reason for hiding this comment

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

expansion_strategy was never a gradient keyword arg, but is a removed QNode arg.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah. So do we still want to continue supporting these warnings?

def _():
return qml.state()

Expand All @@ -171,7 +173,7 @@ def test_max_expansion_error(self):

with pytest.raises(ValueError, match="'max_expansion' is no longer a valid"):

@qml.qnode(qml.device("default.qubit"), max_expansion=1)
@qml.qnode(qml.device("default.qubit"), gradient_kwargs={"max_expansion": 1})
def _():
qml.state()

Expand Down Expand Up @@ -425,7 +427,7 @@ def test_unrecognized_kwargs_raise_warning(self):

with warnings.catch_warnings(record=True) as w:

@qml.qnode(dev, random_kwarg=qml.gradients.finite_diff)
@qml.qnode(dev, gradient_kwargs={"random_kwarg": qml.gradients.finite_diff})
def circuit(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliZ(0))
Expand All @@ -440,16 +442,19 @@ def test_incorrect_diff_method_kwargs_raise_warning(self):
dev = qml.device("default.qubit", wires=2)

with warnings.catch_warnings(record=True) as w:
with pytest.warns(
qml.PennyLaneDeprecationWarning, match="Specifying gradient keyword arguments"
):

@qml.qnode(dev, grad_method=qml.gradients.finite_diff)
def circuit0(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliZ(0))
@qml.qnode(dev, grad_method=qml.gradients.finite_diff)
def circuit0(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliZ(0))

@qml.qnode(dev, gradient_fn=qml.gradients.finite_diff)
def circuit2(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliZ(0))
@qml.qnode(dev, gradient_fn=qml.gradients.finite_diff)
def circuit2(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliZ(0))

assert len(w) == 2
assert "Use diff_method instead" in str(w[0].message)
Expand Down Expand Up @@ -858,7 +863,7 @@ def test_single_expectation_value_with_argnum_one(self, diff_method, tol):
y = pnp.array(-0.654, requires_grad=True)

@qnode(
dev, diff_method=diff_method, argnum=[1]
dev, diff_method=diff_method, gradient_kwargs={"argnum": [1]}
) # <--- we only choose one trainable parameter
def circuit(x, y):
qml.RX(x, wires=[0])
Expand Down Expand Up @@ -1307,11 +1312,11 @@ def ansatz0():
return qml.expval(qml.X(0))

with pytest.raises(ValueError, match="'shots' is not a valid gradient_kwarg."):
qml.QNode(ansatz0, dev, shots=100)
qml.QNode(ansatz0, dev, gradient_kwargs={"shots": 100})

with pytest.raises(ValueError, match="'shots' is not a valid gradient_kwarg."):

@qml.qnode(dev, shots=100)
@qml.qnode(dev, gradient_kwargs={"shots": 100})
def _():
return qml.expval(qml.X(0))

Expand Down
23 changes: 13 additions & 10 deletions tests/test_qnode_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def test_unrecognized_kwargs_raise_warning(self):

with warnings.catch_warnings(record=True) as w:

@qml.qnode(dev, random_kwarg=qml.gradients.finite_diff)
@qml.qnode(dev, gradient_kwargs={"random_kwarg": qml.gradients.finite_diff})
def circuit(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliZ(0))
Expand All @@ -387,16 +387,19 @@ def test_incorrect_diff_method_kwargs_raise_warning(self):
dev = DefaultQubitLegacy(wires=2)

with warnings.catch_warnings(record=True) as w:
with pytest.warns(
qml.PennyLaneDeprecationWarning, match="Specifying gradient keyword arguments"
):

@qml.qnode(dev, grad_method=qml.gradients.finite_diff)
def circuit0(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliZ(0))
@qml.qnode(dev, grad_method=qml.gradients.finite_diff)
def circuit0(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliZ(0))

@qml.qnode(dev, gradient_fn=qml.gradients.finite_diff)
def circuit2(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliZ(0))
@qml.qnode(dev, gradient_fn=qml.gradients.finite_diff)
def circuit2(params):
qml.RX(params[0], wires=0)
return qml.expval(qml.PauliZ(0)), qml.var(qml.PauliZ(0))

assert len(w) == 2
assert "Use diff_method instead" in str(w[0].message)
Expand Down Expand Up @@ -798,7 +801,7 @@ def test_single_expectation_value_with_argnum_one(self, diff_method, tol):
y = pnp.array(-0.654, requires_grad=True)

@qnode(
dev, diff_method=diff_method, argnum=[1]
dev, diff_method=diff_method, gradient_kwargs={"argnum": [1]}
) # <--- we only choose one trainable parameter
def circuit(x, y):
qml.RX(x, wires=[0])
Expand Down
Loading