Skip to content

Commit

Permalink
Change DeprecationWarning and PendingDeprecationWarning to UserWarning (
Browse files Browse the repository at this point in the history
#1211)

* increase stacklevel

* black

* Use UserWarning

* pytest warns

* adjust msg

* Update pennylane/_qubit_device.py

Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: Josh Izaac <josh146@gmail.com>
  • Loading branch information
3 people committed Apr 30, 2021
1 parent beaf0a6 commit 05e50a9
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ fully differentiable.
of shots is set explicitly.

* If creating a QNode from a quantum function with an argument named `shots`,
a `DeprecationWarning` is raised, warning the user that this is a reserved
a `UserWarning` is raised, warning the user that this is a reserved
argument to change the number of shots on a per-call basis.
[(#1075)](https://github.com/PennyLaneAI/pennylane/pull/1075)

Expand Down
2 changes: 1 addition & 1 deletion pennylane/_qubit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def sample_basis_states(self, number_of_states, state_probability):
"The number of shots has to be explicitly set on the device "
"when using sample-based measurements. Since no shots are specified, "
"a default of 1000 shots is used.",
DeprecationWarning,
UserWarning,
)

shots = self.shots or 1000
Expand Down
4 changes: 2 additions & 2 deletions pennylane/devices/default_qubit_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ def sample_basis_states(self, number_of_states, state_probability):
"The number of shots has to be explicitly set on the jax device "
"when using sample-based measurements. Since no shots are specified, "
"a default of 1000 shots is used.\n"
"This warning will replaced with an error in a future release.",
DeprecationWarning,
"This warning will be replaced with an error in a future release.",
UserWarning,
)

shots = self.shots or 1000
Expand Down
2 changes: 1 addition & 1 deletion pennylane/qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def __init__(
"Detected 'shots' as an argument to the given quantum function. "
"The 'shots' argument name is reserved for overriding the number of shots "
"taken by the device. Its use outside of this context should be avoided.",
DeprecationWarning,
UserWarning,
)
self._qfunc_uses_shots_arg = True
else:
Expand Down
2 changes: 1 addition & 1 deletion pennylane/templates/embeddings/amplitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def __init__(self, features, wires, pad_with=None, normalize=False, pad=None, do
if pad is not None:
warnings.warn(
"The pad argument will be replaced by the pad_with option in future versions of PennyLane.",
PendingDeprecationWarning,
UserWarning,
)
if pad_with is None:
pad_with = pad
Expand Down
2 changes: 1 addition & 1 deletion pennylane/vqe/vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class VQECost(ExpvalCost):
def __init__(self, *args, **kwargs):
warnings.warn(
"Use of VQECost is deprecated and should be replaced with ExpvalCost",
DeprecationWarning,
UserWarning,
2,
)
super().__init__(*args, **kwargs)
4 changes: 3 additions & 1 deletion tests/devices/test_default_qubit_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ def test_sampling_analytic_mode(self):
def circuit():
return qml.sample(qml.PauliZ(wires=0))

with pytest.deprecated_call():
with pytest.warns(
UserWarning, match="The number of shots has to be explicitly set on the jax device"
):
res = circuit()

assert len(res) == 1000
Expand Down
4 changes: 2 additions & 2 deletions tests/tape/test_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ def circuit(a, shots=0):
return qml.sample(qml.PauliZ(wires=0))

with pytest.warns(
DeprecationWarning, match="The 'shots' argument name is reserved for overriding"
UserWarning, match="The 'shots' argument name is reserved for overriding"
):
circuit = qml.QNode(circuit, dev)

Expand All @@ -909,7 +909,7 @@ def circuit(a, shots):

# assert that warning is still raised
with pytest.warns(
DeprecationWarning, match="The 'shots' argument name is reserved for overriding"
UserWarning, match="The 'shots' argument name is reserved for overriding"
):
circuit = qml.QNode(circuit, dev)

Expand Down
2 changes: 1 addition & 1 deletion tests/templates/test_embeddings/test_amplitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def circuit(x=None):
return qml.expval(qml.PauliZ(0))

with pytest.warns(
PendingDeprecationWarning,
UserWarning,
match="will be replaced by the pad_with option in future versions",
):
circuit(x=inputs)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_qubit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def test_raises_deprecation_warning(self, mock_qubit_device, monkeypatch):
state_probs = [0.1, 0.2, 0.3, 0.4]

with pytest.warns(
DeprecationWarning, match="The number of shots has to be explicitly set on the device"
UserWarning, match="The number of shots has to be explicitly set on the device"
):
dev.sample_basis_states(number_of_states, state_probs)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,13 +1216,13 @@ def test_all_interfaces_gradient_agree(self, tol):


def test_vqe_cost():
"""Tests that VQECost raises a DeprecationWarning but otherwise behaves as ExpvalCost"""
"""Tests that VQECost raises a UserWarning but otherwise behaves as ExpvalCost"""

h = qml.Hamiltonian([1], [qml.PauliZ(0)])
dev = qml.device("default.qubit", wires=1)
ansatz = qml.templates.StronglyEntanglingLayers

with pytest.warns(DeprecationWarning, match="Use of VQECost is deprecated"):
with pytest.warns(UserWarning, match="Use of VQECost is deprecated"):
cost = qml.VQECost(ansatz, h, dev)

assert isinstance(cost, qml.ExpvalCost)

0 comments on commit 05e50a9

Please sign in to comment.