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

Remove auto warning and swtich tracker #3829

Merged
merged 2 commits into from
Feb 27, 2023
Merged
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: 1 addition & 5 deletions pennylane/qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,11 +819,7 @@ def __call__(self, *args, **kwargs): # pylint: disable=too-many-branches, too-m

if old_interface == "auto":
self.interface = qml.math.get_interface(*args, *list(kwargs.values()))
if self.device is not self._original_device:
warnings.warn(
"The device was switched during the call of the QNode, to avoid this behaviour define "
"an interface argument instead of auto."
)
self.device.tracker = self._original_device.tracker
rmoyard marked this conversation as resolved.
Show resolved Hide resolved

if not self._qfunc_uses_shots_arg:
# If shots specified in call but not in qfunc signature,
Expand Down
19 changes: 12 additions & 7 deletions tests/test_qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,22 +560,27 @@ def circuit(params):
assert "Use diff_method instead" in str(w[0].message)
assert "Use diff_method instead" in str(w[1].message)

def test_auto_interface_device_switched_warning(self):
"""Test that checks that a warning is raised if the device is switched during the QNode call due to auto
interface."""
def test_auto_interface_tracker_device_switched(self):
"""Test that checks that the tracker is switched to the new device."""
dev = qml.device("default.qubit", wires=1)

@qml.qnode(dev)
def circuit(params):
qml.RX(params, wires=0)
return qml.expval(qml.PauliZ(0))

with pytest.warns(
UserWarning,
match="The device was switched during the call of the QNode",
):
with qml.Tracker(dev) as tracker:
circuit(qml.numpy.array(0.1, requires_grad=True))

assert tracker.totals == {"executions": 1, "batches": 1, "batch_len": 1}
assert np.allclose(tracker.history.pop("results")[0], 0.99500417)
assert tracker.history == {
"executions": [1],
"shots": [None],
"batches": [1],
"batch_len": [1],
}

def test_autograd_interface_device_switched_no_warnings(self):
"""Test that checks that no warning is raised for device switch when you define an interface."""
dev = qml.device("default.qubit", wires=1)
Expand Down