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 dev.num_executions bug with QNode caching #2171

Merged
merged 9 commits into from
Feb 7, 2022

Conversation

antalszava
Copy link
Contributor

@antalszava antalszava commented Feb 4, 2022

Context:
See linked issue #2170. The issue arises when swapping the underlying device in the QNode (e.g., when having diff_method="backprop").

Description of the Change:

Instead of incrementing the number of executions, we set the number of executions using the number of executions from the device used.

Benefits:

dev.num_executions is recorded correctly.

Possible Drawbacks:
N/A

Related GitHub Issues:
Closes #2170

@codecov
Copy link

codecov bot commented Feb 4, 2022

Codecov Report

❗ No coverage uploaded for pull request base (v0.21.0-rc0@2a419f4). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@              Coverage Diff               @@
##             v0.21.0-rc0    #2171   +/-   ##
==============================================
  Coverage               ?   99.20%           
==============================================
  Files                  ?      230           
  Lines                  ?    17670           
  Branches               ?        0           
==============================================
  Hits                   ?    17530           
  Misses                 ?      140           
  Partials               ?        0           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2a419f4...4125dd7. Read the comment docs.

Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me @antalszava :)

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
Comment on lines +552 to +558
cache = self.execute_kwargs.get("cache", False)
using_custom_cache = (
hasattr(cache, "__getitem__")
and hasattr(cache, "__setitem__")
and hasattr(cache, "__delitem__")
)
self._tape_cached = using_custom_cache and self.tape.hash in cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antalszava I wonder if it would be a better pattern to do?

Suggested change
cache = self.execute_kwargs.get("cache", False)
using_custom_cache = (
hasattr(cache, "__getitem__")
and hasattr(cache, "__setitem__")
and hasattr(cache, "__delitem__")
)
self._tape_cached = using_custom_cache and self.tape.hash in cache
cache = self.execute_kwargs.get("cache", False)
try:
self._tape_cached = self.tape.hash in cache
except TypeError:
self._tape_cached = False

This is minor though! Your call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep it, as the docstring mentions that __getitem__, __setitem__ and __delitem__ are required to be implemented for a cache.

Co-authored-by: Josh Izaac <josh146@gmail.com>
@antalszava antalszava merged commit 5d975eb into v0.21.0-rc0 Feb 7, 2022
@antalszava antalszava deleted the fix_backprop_qnode_cache_num_exec branch February 7, 2022 04:03
josh146 added a commit that referenced this pull request Feb 8, 2022
* Run updated version of black on repo (#2140)

* Run updated black on repo

* Also reformat tests

* Dont convert variables to numpy if on interface-specific device (#2136)

* dont unwrap interface devices if diffmethod None

* tests and black

* dont expect warning on test, update changelog

* readability update and test fix

* lint

* Update doc/releases/changelog-dev.md

Co-authored-by: antalszava <antalszava@gmail.com>

* Modify changelog for psi4 bug fix (#2144)

* modify changelog for psi4 bug fix

* move the entry to the QChem changelog

Co-authored-by: Antal Szava <antalszava@gmail.com>

* Add useful error message on empty batch transform (#2139)

* Add error on empty batch transform

When a batch transform collects an empty `params` list, it currently
raises an IndexError. This fix adds a check to raise a useful error
message in cases were there are no trainable parameters and
`all_operations` was not set to True.

Also fix template argument types in docstrings.

* Add test case

* Update changelog-dev.md

Co-authored-by: Romain <rmoyard@gmail.com>
Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: Josh Izaac <josh146@gmail.com>

* Add warning when using `qml.grad`/`qml.jacobian` without any trainable parameters (#2148)

* Add warning for gradients with no trainable params

* Fix tests expecting different warning

* Fix many tests with no trainable params

Catch expected warnings

* Formatting

* Add changelog

* Update doc/releases/changelog-dev.md

* Use quotes instead of backticks

Co-authored-by: antalszava <antalszava@gmail.com>

* changelog typos

* Add `qml.hf.hamiltonian.simplify` to docs (#2162)

* Allow qml.hf.simplify

* dummy change to trigger CI

* PL version bump

* add 0.21.0 file

* Update (#2167)

* create sections

* org

* update

* correct examples

* Num params fix2 (#2135)

* made num_params a class property in all remaining Operations as it indeed is independen of the instance in all currently defined Operations
Changed Operation class to allow sub-classes to define num_params as instance or class property as desired

* made num_params of Identity static

* added a simple test to check that for some operations whose num_params can be a static class property this is actually the case

* updated changelog

* linting

* linting

* take both static and instance num_params property into account when checking number of provided parameters

* black

* Update pennylane/operation.py

Co-authored-by: Maria Schuld <maria@xanadu.ai>

* Update pennylane/operation.py

Co-authored-by: Maria Schuld <maria@xanadu.ai>

* communting evolution cannot (and thus should not) declare num_params on the class level

* moved num_params test to later in Operation.__init__()
added comments to provide more context
fixed typo

* reverting change in operation.py becaseu they are not needed

* test all ways of declaring num_params

* Update doc/releases/changelog-dev.md

Co-authored-by: Maria Schuld <maria@xanadu.ai>

* removed optional test

Co-authored-by: Maria Schuld <maria@xanadu.ai>

* black

* Update doc/releases/changelog-dev.md

Co-authored-by: Josh Izaac <josh146@gmail.com>
Co-authored-by: Maria Schuld <maria@xanadu.ai>
Co-authored-by: Maria Schuld <mariaschuld@gmail.com>
Co-authored-by: antalszava <antalszava@gmail.com>

* Extended list of contributors

* QChem

* minor

* section names

* Updated non_parametric ops adjoint method and added test (#2133)

* Updated non_parametric ops adjoint method and added test

* changelog

* lint

* fixed testing bug

* lint

* moving updated adjoint logic into the Operation class and override this method in Observable class

* updated black (#2141)

* Add tape to graph conversion for circuit cutting (#2107)

* add WireCut operator, add qcut package, update docs

* add unit test

* update changelog

* updates

* add tape to graph conversion and unit test

* add unit tests

* add obs to node tests

* restructure qcut module

* add tests for observable and measurement conversion to nodes, update stateprep conversion logic

* fix pylint

* Apply suggestions from code review

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

* add code review suggestions

* add unit tests

* format

* Apply suggestions from code review

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

* add review suggestions

* Update pennylane/transforms/qcut.py

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

* add fix and tests (#2145)

* Incrementing the version number to `0.22.0-dev` (#2142)

* clean changelog-dev

* version bump

* mark current and dev release in the changelog

* Add Qchem v0.21.0 release notes

* Over wrote adjoint method for these templates as they inherit from Operations

* implemented adjoint method for a few more templates

* `WireCut` nodes can be replaced with `MeasureNode` and `PrepareNode` (#2124)

* add WireCut operator, add qcut package, update docs

* add unit test

* update changelog

* updates

* add tape to graph conversion and unit test

* add unit tests

* add obs to node tests

* restructure qcut module

* add tests for observable and measurement conversion to nodes, update stateprep conversion logic

* fix pylint

* add method to replace Wirecut ops with Measure and Prepare ops

* add unit tests

* add unit tests, update changelog

* format

* set measure and prepare node order to be equal (remove 0.5)

* Apply suggestions from code review

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

* add code review suggestions

* add unit tests

* format

* format

* Apply suggestions from code review

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

* add review suggestions

* Update pennylane/transforms/qcut.py

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

* add review suggestions

* add unit test

* update changelog

* update changelog-0.21.0

* Apply suggestions from code review

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

* apply review suggestions

* Apply suggestions from code review

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>

* Update changelog-0.21.0.md (#2149)

Co-authored-by: antalszava <antalszava@gmail.com>

* reset to previous soln

* clean

* more cleaning

* addressing code-review comments

* lint

* remove qcut feature

* reset changelogs

* reset logs

* updated changelog and qchem version

Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: anthayes92 <34694788+anthayes92@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: Nathan Killoran <co9olguy@users.noreply.github.com>

* section name and remove imports

* add paper ref

* tapering example

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Breaking changes

* Improvements order

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* no well known imports

* apply latest suggestion to the tapering example

* JAX

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* trainable weights

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Roto move up

* PR link move up

* Apply suggested headlines

* Apply Rotosolve rephrasing

* emojis

* Fix `dev.num_executions` bug with QNode caching (#2171)

* fix & test

* format

* changelog

* use _tape_cached private attribute on the QNode

* format

* PR number

* changelog

* remove unnecessary comment

* Update doc/releases/changelog-dev.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

Co-authored-by: Josh Izaac <josh146@gmail.com>

* fixed bug when trying to insert around an observable (#2172)

* fixed bug when trying to insert around an observable (or any operation that also inherits from other classes), added tests

* fixed bug in test

* fixing bug in test again

* quick typo fix

* changelog

Co-authored-by: antalszava <antalszava@gmail.com>

* Roto description

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* black examples

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* remove new lines and suggested sentence

* apply suggestion

* full stop

* Add warnings for the gradient transforms when there are no trainable parameters (#2156)

* Add warnings (tape/qnode) with no trainable params

* Generalize warning + add tests

* Expand warning to all gradient transforms

Also fixes one test warning about shots.

* Add changelog

* code review: warning wording, separate asserts

Co-authored-by: antalszava <antalszava@gmail.com>

* code review: warning wording

* Formatting

* Parametrize qnode tests by interface

* Include finite_difference grad transform

use seperate asserts across tests

* Add import skips for interfaces

Co-authored-by: antalszava <antalszava@gmail.com>

* Deprecate `QubitDevice`'s caching (#2154)

* Add warning and test

* no print

* format

* format

* changelog

* add preferred way of caching

* mark current release

* change an example

* require Lightning v0.21.0 or higher in setup.py

* re-add dev changelog

* `v0.21.0` release notes (#2159)

* changelog typos

* PL version bump

* add 0.21.0 file

* create sections

* org

* update

* correct examples

* Extended list of contributors

* QChem

* minor

* section names

* section name and remove imports

* add paper ref

* tapering example

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Breaking changes

* Improvements order

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* no well known imports

* apply latest suggestion to the tapering example

* JAX

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* trainable weights

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Roto move up

* PR link move up

* Apply suggested headlines

* Apply Rotosolve rephrasing

* emojis

* Roto description

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* black examples

* Update doc/releases/changelog-0.21.0.md

Co-authored-by: Josh Izaac <josh146@gmail.com>

* remove new lines and suggested sentence

* apply suggestion

* full stop

* mark current release

* change an example

Co-authored-by: Josh Izaac <josh146@gmail.com>

* Bump the requirement of PennyLane-Lightning to v0.21.0 in `setup.py` (#2177)

* Bump the requirement of PennyLane-Lightning in setup.py

* Update setup.py

* Update setup.py

Co-authored-by: Josh Izaac <josh146@gmail.com>

* merge

* more

Co-authored-by: David Ittah <dime10@users.noreply.github.com>
Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: soranjh <40344468+soranjh@users.noreply.github.com>
Co-authored-by: Romain <rmoyard@gmail.com>
Co-authored-by: Josh Izaac <josh146@gmail.com>
Co-authored-by: cvjjm <60615188+cvjjm@users.noreply.github.com>
Co-authored-by: Maria Schuld <maria@xanadu.ai>
Co-authored-by: Maria Schuld <mariaschuld@gmail.com>
Co-authored-by: Jay Soni <jbsoni@uwaterloo.ca>
Co-authored-by: anthayes92 <34694788+anthayes92@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Nathan Killoran <co9olguy@users.noreply.github.com>
@antalszava
Copy link
Contributor Author

[sc-14432]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants