From d509ff916757c5d9f3d456489ec4a021b866c6f2 Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Sun, 6 Mar 2022 22:41:19 +0800 Subject: [PATCH 1/2] Ensures that `@qfunc_transform` decorated functions display their signatures in the docs --- pennylane/transforms/qfunc_transforms.py | 17 ++++++++++++++ tests/transforms/test_qfunc_transform.py | 29 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/pennylane/transforms/qfunc_transforms.py b/pennylane/transforms/qfunc_transforms.py index 5825aaaf6c7..40e056b968c 100644 --- a/pennylane/transforms/qfunc_transforms.py +++ b/pennylane/transforms/qfunc_transforms.py @@ -16,6 +16,8 @@ from copy import deepcopy import functools import inspect +import os +import warnings import pennylane as qml @@ -374,6 +376,21 @@ def new_qfunc(*args, **kwargs): the queueing logic required under steps (1) and (3), so that it does not need to be repeated and tested for every new qfunc transform. """ + if os.environ.get("SPHINX_BUILD") == "1": + # If called during a Sphinx documentation build, + # simply return the original function rather than + # instantiating the object. This allows the signature to + # be correctly displayed in the documentation. + + warnings.warn( + "qfunc transformations have been disabled, as a Sphinx " + "build has been detected via SPHINX_BUILD='1'. If this is not the " + "case, please set the environment variable SPHINX_BUILD='0'.", + UserWarning, + ) + + return tape_transform + if not callable(tape_transform): raise ValueError( "The qfunc_transform decorator can only be applied " diff --git a/tests/transforms/test_qfunc_transform.py b/tests/transforms/test_qfunc_transform.py index 4879d2fae93..8aaa6a2cb06 100644 --- a/tests/transforms/test_qfunc_transform.py +++ b/tests/transforms/test_qfunc_transform.py @@ -302,6 +302,35 @@ def ansatz(): assert np.allclose(normal_result, transformed_result) assert normal_result.shape == transformed_result.shape + def test_sphinx_build(self, monkeypatch): + """Test that qfunc transforms are not created during Sphinx builds""" + + @qml.qfunc_transform + def my_transform(tape): + for op in tape.operations + tape.measurements: + if op.name == "Hadamard": + qml.RZ(np.pi, wires=op.wires) + qml.RY(np.pi / 2, wires=op.wires) + else: + op.queue() + + assert hasattr(my_transform, "tape_fn") + + monkeypatch.setenv("SPHINX_BUILD", "1") + + with pytest.warns(UserWarning, match="qfunc transformations have been disabled"): + + @qml.qfunc_transform + def my_transform(tape): + for op in tape.operations + tape.measurements: + if op.name == "Hadamard": + qml.RZ(np.pi, wires=op.wires) + qml.RY(np.pi / 2, wires=op.wires) + else: + op.queue() + + assert not hasattr(my_transform, "tape_fn") + ############################################ # Test transform, ansatz, and qfunc function From d9916e07d1a13b32fdb796708c5e387c202d0c8d Mon Sep 17 00:00:00 2001 From: Josh Izaac Date: Sun, 6 Mar 2022 22:46:07 +0800 Subject: [PATCH 2/2] update changelog --- doc/releases/changelog-dev.md | 4 ++++ tests/transforms/test_qfunc_transform.py | 18 +++++------------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index ad9cf829525..25eff427e56 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -405,6 +405,10 @@ usage of the passed `substep_optimizer` and its keyword arguments. [(#2160)](https://github.com/PennyLaneAI/pennylane/pull/2160) +* Ensures that signatures of `@qml.qfunc_transform` decorated functions + display correctly in the docs. + [(#2286)](https://github.com/PennyLaneAI/pennylane/pull/2286) +

Operator class refactor

The Operator class has undergone a major refactor with the following changes: diff --git a/tests/transforms/test_qfunc_transform.py b/tests/transforms/test_qfunc_transform.py index 8aaa6a2cb06..fccdcb81a3a 100644 --- a/tests/transforms/test_qfunc_transform.py +++ b/tests/transforms/test_qfunc_transform.py @@ -305,8 +305,7 @@ def ansatz(): def test_sphinx_build(self, monkeypatch): """Test that qfunc transforms are not created during Sphinx builds""" - @qml.qfunc_transform - def my_transform(tape): + def original_fn(tape): for op in tape.operations + tape.measurements: if op.name == "Hadamard": qml.RZ(np.pi, wires=op.wires) @@ -314,22 +313,15 @@ def my_transform(tape): else: op.queue() - assert hasattr(my_transform, "tape_fn") + decorated_transform = qml.qfunc_transform(original_fn) + assert original_fn is not decorated_transform monkeypatch.setenv("SPHINX_BUILD", "1") with pytest.warns(UserWarning, match="qfunc transformations have been disabled"): + decorated_transform = qml.qfunc_transform(original_fn) - @qml.qfunc_transform - def my_transform(tape): - for op in tape.operations + tape.measurements: - if op.name == "Hadamard": - qml.RZ(np.pi, wires=op.wires) - qml.RY(np.pi / 2, wires=op.wires) - else: - op.queue() - - assert not hasattr(my_transform, "tape_fn") + assert original_fn is decorated_transform ############################################