From e125fb6c08b1b9d08eb309dae770d114af0678b7 Mon Sep 17 00:00:00 2001 From: "Lee J. O'Riordan" Date: Thu, 12 Oct 2023 10:46:42 -0400 Subject: [PATCH 1/5] Fix logging trace statement with empty gradient_fn --- pennylane/interfaces/execution.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pennylane/interfaces/execution.py b/pennylane/interfaces/execution.py index a5d07e979ae..17b231fe530 100644 --- a/pennylane/interfaces/execution.py +++ b/pennylane/interfaces/execution.py @@ -292,13 +292,13 @@ def cache_execute(fn: Callable, cache, pass_kwargs=False, return_tuple=True, exp logger.debug( "Entry with args=(fn=%s, cache=%s, pass_kwargs=%s, return_tuple=%s, expand_fn=%s) called by=%s", fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and fn and callable(fn)) else "\n" + inspect.getsource(fn), cache, pass_kwargs, return_tuple, expand_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(expand_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and expand_fn and callable(expand_fn)) else "\n" + inspect.getsource(expand_fn) + "\n", "::L".join(str(i) for i in inspect.getouterframes(inspect.currentframe(), 2)[1][1:3]), ) @@ -516,7 +516,9 @@ def cost_fn(params, x): tapes, repr(device), gradient_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(gradient_fn)) + if not ( + logger.isEnabledFor(qml.logging.TRACE) and gradient_fn and callable(gradient_fn) + ) else "\n" + inspect.getsource(gradient_fn) + "\n", interface, grad_on_execution, @@ -526,7 +528,7 @@ def cost_fn(params, x): max_diff, override_shots, expand_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(gradient_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and expand_fn and callable(expand_fn)) else "\n" + inspect.getsource(expand_fn) + "\n", max_expansion, device_batch_transform, From c889e6c72e83748aa446f990cb8ef90cdba46744 Mon Sep 17 00:00:00 2001 From: "Lee J. O'Riordan" Date: Thu, 12 Oct 2023 10:55:15 -0400 Subject: [PATCH 2/5] Ensure other TRACE users are also updated --- pennylane/interfaces/autograd.py | 12 ++++++++---- pennylane/interfaces/jax.py | 6 ++++-- pennylane/interfaces/torch.py | 6 ++++-- pennylane/qnode.py | 2 +- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pennylane/interfaces/autograd.py b/pennylane/interfaces/autograd.py index 62d7b384221..ab8bb3c675a 100644 --- a/pennylane/interfaces/autograd.py +++ b/pennylane/interfaces/autograd.py @@ -116,10 +116,12 @@ def _execute( tapes, repr(device), execute_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(execute_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and execute_fn and callable(execute_fn)) else "\n" + inspect.getsource(execute_fn) + "\n", gradient_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(gradient_fn)) + if not ( + logger.isEnabledFor(qml.logging.TRACE) and gradient_fn and callable(gradient_fn) + ) else "\n" + inspect.getsource(gradient_fn) + "\n", gradient_kwargs, _n, @@ -179,10 +181,12 @@ def vjp( tapes, repr(device), execute_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(execute_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and execute_fn and callable(execute_fn)) else "\n" + inspect.getsource(execute_fn) + "\n", gradient_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(gradient_fn)) + if not ( + logger.isEnabledFor(qml.logging.TRACE) and gradient_fn and callable(gradient_fn) + ) else "\n" + inspect.getsource(gradient_fn) + "\n", gradient_kwargs, _n, diff --git a/pennylane/interfaces/jax.py b/pennylane/interfaces/jax.py index 632be8a51a7..99d293bf80a 100644 --- a/pennylane/interfaces/jax.py +++ b/pennylane/interfaces/jax.py @@ -106,10 +106,12 @@ def execute(tapes, device, execute_fn, gradient_fn, gradient_kwargs, _n=1, max_d tapes, repr(device), execute_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(execute_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and execute_fn and callable(execute_fn)) else "\n" + inspect.getsource(execute_fn) + "\n", gradient_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(gradient_fn)) + if not ( + logger.isEnabledFor(qml.logging.TRACE) and gradient_fn and callable(gradient_fn) + ) else "\n" + inspect.getsource(gradient_fn) + "\n", gradient_kwargs, _n, diff --git a/pennylane/interfaces/torch.py b/pennylane/interfaces/torch.py index 0ba68c71de1..f69063db336 100644 --- a/pennylane/interfaces/torch.py +++ b/pennylane/interfaces/torch.py @@ -282,10 +282,12 @@ def execute(tapes, device, execute_fn, gradient_fn, gradient_kwargs, _n=1, max_d tapes, repr(device), execute_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(execute_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and execute_fn and callable(execute_fn)) else "\n" + inspect.getsource(execute_fn) + "\n", gradient_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(gradient_fn)) + if not ( + logger.isEnabledFor(qml.logging.TRACE) and gradient_fn and callable(gradient_fn) + ) else "\n" + inspect.getsource(gradient_fn) + "\n", gradient_kwargs, _n, diff --git a/pennylane/qnode.py b/pennylane/qnode.py index 3b79f2cce01..626bd8c9427 100644 --- a/pennylane/qnode.py +++ b/pennylane/qnode.py @@ -400,7 +400,7 @@ def __init__( logger.debug( """Creating QNode(func=%s, device=%s, interface=%s, diff_method=%s, expansion_strategy=%s, max_expansion=%s, grad_on_execution=%s, cache=%s, cachesize=%s, max_diff=%s, gradient_kwargs=%s""", func - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(func)) + if not (logger.isEnabledFor(qml.logging.TRACE) and func and callable(func)) else "\n" + inspect.getsource(func), repr(device), interface, From 58da05bba7f30fae72effaacec80eaf9aa9c6c9f Mon Sep 17 00:00:00 2001 From: "Lee J. O'Riordan" Date: Thu, 12 Oct 2023 10:56:31 -0400 Subject: [PATCH 3/5] Update changelog --- doc/releases/changelog-dev.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index e0ce840e73f..c3ce858eeed 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -363,6 +363,9 @@ the last axis (`axis=-1`). Without the fix, it would wrongly return `tensor[indices]`. [(#4605)](https://github.com/PennyLaneAI/pennylane/pull/4605) +* Ensure the logging `TRACE` level works with gradient-free execution. + [(#4669)](https://github.com/PennyLaneAI/pennylane/pull/4669) +

Contributors ✍️

This release contains contributions from (in alphabetical order): From b1d1615703bd7cad971ec624a48719fc351ddac9 Mon Sep 17 00:00:00 2001 From: "Lee J. O'Riordan" Date: Thu, 12 Oct 2023 10:56:53 -0400 Subject: [PATCH 4/5] Update changelog again --- doc/releases/changelog-dev.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index c3ce858eeed..ff9853b0617 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -381,6 +381,7 @@ Lillian M. A. Frederiksen, Vincent Michaud-Rioux, Romain Moyard, Daniel F. Nino, +Lee James O'Riordan, Mudit Pandey, Matthew Silverman, Jay Soni, From bef68ededff03fc80e96e369662dd51665191db1 Mon Sep 17 00:00:00 2001 From: "Lee J. O'Riordan" Date: Thu, 12 Oct 2023 11:14:58 -0400 Subject: [PATCH 5/5] Use inspect utils to avoid partials --- pennylane/interfaces/autograd.py | 12 ++++-------- pennylane/interfaces/execution.py | 10 ++++------ pennylane/interfaces/jacobian_products.py | 2 +- pennylane/interfaces/jax.py | 6 ++---- pennylane/interfaces/tensorflow.py | 4 ++-- pennylane/interfaces/torch.py | 6 ++---- pennylane/qnode.py | 2 +- 7 files changed, 16 insertions(+), 26 deletions(-) diff --git a/pennylane/interfaces/autograd.py b/pennylane/interfaces/autograd.py index ab8bb3c675a..70b0011a94b 100644 --- a/pennylane/interfaces/autograd.py +++ b/pennylane/interfaces/autograd.py @@ -116,12 +116,10 @@ def _execute( tapes, repr(device), execute_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and execute_fn and callable(execute_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(execute_fn)) else "\n" + inspect.getsource(execute_fn) + "\n", gradient_fn - if not ( - logger.isEnabledFor(qml.logging.TRACE) and gradient_fn and callable(gradient_fn) - ) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(gradient_fn)) else "\n" + inspect.getsource(gradient_fn) + "\n", gradient_kwargs, _n, @@ -181,12 +179,10 @@ def vjp( tapes, repr(device), execute_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and execute_fn and callable(execute_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(execute_fn)) else "\n" + inspect.getsource(execute_fn) + "\n", gradient_fn - if not ( - logger.isEnabledFor(qml.logging.TRACE) and gradient_fn and callable(gradient_fn) - ) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(gradient_fn)) else "\n" + inspect.getsource(gradient_fn) + "\n", gradient_kwargs, _n, diff --git a/pennylane/interfaces/execution.py b/pennylane/interfaces/execution.py index 17b231fe530..9fcc3830d62 100644 --- a/pennylane/interfaces/execution.py +++ b/pennylane/interfaces/execution.py @@ -292,13 +292,13 @@ def cache_execute(fn: Callable, cache, pass_kwargs=False, return_tuple=True, exp logger.debug( "Entry with args=(fn=%s, cache=%s, pass_kwargs=%s, return_tuple=%s, expand_fn=%s) called by=%s", fn - if not (logger.isEnabledFor(qml.logging.TRACE) and fn and callable(fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(fn)) else "\n" + inspect.getsource(fn), cache, pass_kwargs, return_tuple, expand_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and expand_fn and callable(expand_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(expand_fn)) else "\n" + inspect.getsource(expand_fn) + "\n", "::L".join(str(i) for i in inspect.getouterframes(inspect.currentframe(), 2)[1][1:3]), ) @@ -516,9 +516,7 @@ def cost_fn(params, x): tapes, repr(device), gradient_fn - if not ( - logger.isEnabledFor(qml.logging.TRACE) and gradient_fn and callable(gradient_fn) - ) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(gradient_fn)) else "\n" + inspect.getsource(gradient_fn) + "\n", interface, grad_on_execution, @@ -528,7 +526,7 @@ def cost_fn(params, x): max_diff, override_shots, expand_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and expand_fn and callable(expand_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(expand_fn)) else "\n" + inspect.getsource(expand_fn) + "\n", max_expansion, device_batch_transform, diff --git a/pennylane/interfaces/jacobian_products.py b/pennylane/interfaces/jacobian_products.py index 022c113f560..32b7c6945b8 100644 --- a/pennylane/interfaces/jacobian_products.py +++ b/pennylane/interfaces/jacobian_products.py @@ -169,7 +169,7 @@ def __init__( logger.debug( "TransformJacobianProduct being created with (%s, %s, %s)", inspect.getsource(inner_execute) - if logger.isEnabledFor(qml.logging.TRACE) + if (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(inner_execute)) else inner_execute, gradient_transform, gradient_kwargs, diff --git a/pennylane/interfaces/jax.py b/pennylane/interfaces/jax.py index 99d293bf80a..82fe2dd01d8 100644 --- a/pennylane/interfaces/jax.py +++ b/pennylane/interfaces/jax.py @@ -106,12 +106,10 @@ def execute(tapes, device, execute_fn, gradient_fn, gradient_kwargs, _n=1, max_d tapes, repr(device), execute_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and execute_fn and callable(execute_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(execute_fn)) else "\n" + inspect.getsource(execute_fn) + "\n", gradient_fn - if not ( - logger.isEnabledFor(qml.logging.TRACE) and gradient_fn and callable(gradient_fn) - ) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(gradient_fn)) else "\n" + inspect.getsource(gradient_fn) + "\n", gradient_kwargs, _n, diff --git a/pennylane/interfaces/tensorflow.py b/pennylane/interfaces/tensorflow.py index 8947d84084b..f005b60844b 100644 --- a/pennylane/interfaces/tensorflow.py +++ b/pennylane/interfaces/tensorflow.py @@ -172,10 +172,10 @@ def execute(tapes, device, execute_fn, gradient_fn, gradient_kwargs, _n=1, max_d tapes, repr(device), execute_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(execute_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(execute_fn)) else "\n" + inspect.getsource(execute_fn) + "\n", gradient_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and callable(gradient_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(gradient_fn)) else "\n" + inspect.getsource(gradient_fn) + "\n", gradient_kwargs, _n, diff --git a/pennylane/interfaces/torch.py b/pennylane/interfaces/torch.py index f69063db336..ceae05a4249 100644 --- a/pennylane/interfaces/torch.py +++ b/pennylane/interfaces/torch.py @@ -282,12 +282,10 @@ def execute(tapes, device, execute_fn, gradient_fn, gradient_kwargs, _n=1, max_d tapes, repr(device), execute_fn - if not (logger.isEnabledFor(qml.logging.TRACE) and execute_fn and callable(execute_fn)) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(execute_fn)) else "\n" + inspect.getsource(execute_fn) + "\n", gradient_fn - if not ( - logger.isEnabledFor(qml.logging.TRACE) and gradient_fn and callable(gradient_fn) - ) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(gradient_fn)) else "\n" + inspect.getsource(gradient_fn) + "\n", gradient_kwargs, _n, diff --git a/pennylane/qnode.py b/pennylane/qnode.py index 626bd8c9427..db01aed468d 100644 --- a/pennylane/qnode.py +++ b/pennylane/qnode.py @@ -400,7 +400,7 @@ def __init__( logger.debug( """Creating QNode(func=%s, device=%s, interface=%s, diff_method=%s, expansion_strategy=%s, max_expansion=%s, grad_on_execution=%s, cache=%s, cachesize=%s, max_diff=%s, gradient_kwargs=%s""", func - if not (logger.isEnabledFor(qml.logging.TRACE) and func and callable(func)) + if not (logger.isEnabledFor(qml.logging.TRACE) and inspect.isfunction(func)) else "\n" + inspect.getsource(func), repr(device), interface,