-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
run_autograph
is now idempotent (#7001)
**Context:** When support for control flow with `autograph` (through the `QNode` execution pipeline) was introduced in #6837 a minor bug was discovered where if you try to apply `autograph` to an already `autograph`'d function, an _ambiguous_ error would show up, ```python qml.capture.enable() from pennylane.capture import run_autograph dev = qml.device("default.qubit", wires=1) @qml.qnode(qml.device("default.qubit", wires=1), autograph=True) def circ(x: float): qml.RY(x, wires=0) return qml.expval(qml.PauliZ(0)) >>> ag_circ = run_autograph(circ) >>> ag_ag_circ = run_autograph(ag_circ) ValueError: closure mismatch, requested ('ag__',), but source function had () ``` There are a few routes to solve this issue, 1. Raise a more helpful error 2. Throw a warning and proceed with converted function (Catalyst does this) 3. *Silently* proceed with converted function **Description of the Change:** Option (2) was preferred and so if an already converted function is detected (by the presence of the `ag_uncoverted` attribute), we throw an `AutoGraphWarning` and just return early. Now we have the behaviour, ```python qml.capture.enable() from pennylane.capture import run_autograph dev = qml.device("default.qubit", wires=1) @qml.qnode(qml.device("default.qubit", wires=1), autograph=True) def circ(x: float): qml.RY(x, wires=0) return qml.expval(qml.PauliZ(0)) ag_circ = run_autograph(circ) >>> ag_ag_circ = run_autograph(ag_circ) AutoGraphWarning: AutoGraph will not transform the function <function ...> as it has already been transformed. >>> ag_ag_circ.func is ag_circ.func True >>> ag_ag_circ(-1) Array(0.5403023, dtype=float32) ``` **Benefits:** Enables `autograph` specific tests to run with the default `autograph=True` argument through the `QNode`. **Possible Drawbacks:** Suppressed the warnings in the `autograph` test folder. This means that a future test that emits this warning could be missed if we don't pay attention to the WAE action that is run weekly. Fixes [sc-83366]
- Loading branch information
1 parent
8e30920
commit 2c3a11f
Showing
11 changed files
with
145 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright 2018-2020 Xanadu Quantum Technologies Inc. | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
Pytest configuration file for AutoGraph test folder. | ||
""" | ||
import warnings | ||
|
||
import pytest | ||
|
||
from pennylane.capture.autograph import AutoGraphWarning | ||
|
||
|
||
# pylint: disable=unused-import | ||
# This is intended to suppress the *expected* warnings that arise when | ||
# testing AutoGraph transformation functions with a `QNode` (which by default | ||
# has AutoGraph transformations applied to it due to the `autograph` argument). | ||
@pytest.fixture(autouse=True) | ||
def filter_expected_warnings(): | ||
with warnings.catch_warnings(): | ||
warnings.filterwarnings( | ||
"ignore", | ||
category=AutoGraphWarning, | ||
message=r"AutoGraph will not transform the function .* as it has already been transformed\.", | ||
) | ||
yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.