Skip to content

Commit

Permalink
Fix logging trace statement with empty gradient_fn (#4669)
Browse files Browse the repository at this point in the history
### Before submitting

Please complete the following checklist when submitting a PR:

- [x] All new features must include a unit test.
If you've fixed a bug or added code that should be tested, add a test to
the
      test directory!

- [x] All new functions and code must be clearly commented and
documented.
If you do make documentation changes, make sure that the docs build and
      render correctly by running `make docs`.

- [x] Ensure that the test suite passes, by running `make test`.

- [x] Add a new entry to the `doc/releases/changelog-dev.md` file,
summarizing the
      change, and including a link back to the PR.

- [x] The PennyLane source code conforms to
      [PEP8 standards](https://www.python.org/dev/peps/pep-0008/).
We check all of our code against [Pylint](https://www.pylint.org/).
      To lint modified files, simply `pip install pylint`, and then
      run `pylint pennylane/path/to/file.py`.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


------------------------------------------------------------------------------------------------------------

**Context:** The logging implementation for the TRACE level assumes the
existence of several functions to report their source when enabled. If
the gradient_fn is None, the logger attempts to retrieve the source and
fails.

**Description of the Change:** This adds an existence check for all
execute-enabled TRACE level log statements, and reports only if the
associated functions exist.

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: Mudit Pandey <[email protected]>
  • Loading branch information
mlxd and mudit2812 authored Oct 20, 2023
1 parent 5cc309b commit 89c2759
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 16 deletions.
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

0 comments on commit 89c2759

Please sign in to comment.