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

Fix logging trace statement with empty gradient_fn #4669

Merged
merged 8 commits into from
Oct 20, 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
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,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)

<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):
Expand All @@ -472,6 +475,7 @@ Lillian M. A. Frederiksen,
Vincent Michaud-Rioux,
Romain Moyard,
Daniel F. Nino,
Lee James O'Riordan,
Mudit Pandey,
Matthew Silverman,
Jay Soni,
8 changes: 4 additions & 4 deletions pennylane/interfaces/autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ 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 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,
Expand Down Expand Up @@ -179,10 +179,10 @@ 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 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,
Expand Down
8 changes: 4 additions & 4 deletions pennylane/interfaces/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 inspect.isfunction(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 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]),
)
Expand Down Expand Up @@ -519,7 +519,7 @@ 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 inspect.isfunction(gradient_fn))
else "\n" + inspect.getsource(gradient_fn) + "\n",
interface,
grad_on_execution,
Expand All @@ -529,7 +529,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 inspect.isfunction(expand_fn))
else "\n" + inspect.getsource(expand_fn) + "\n",
max_expansion,
device_batch_transform,
Expand Down
2 changes: 1 addition & 1 deletion pennylane/interfaces/jacobian_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,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,
Expand Down
4 changes: 2 additions & 2 deletions pennylane/interfaces/jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +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 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,
Expand Down
4 changes: 2 additions & 2 deletions pennylane/interfaces/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions pennylane/interfaces/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +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 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,
Expand Down
2 changes: 1 addition & 1 deletion pennylane/qnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 inspect.isfunction(func))
else "\n" + inspect.getsource(func),
repr(device),
interface,
Expand Down
Loading