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

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

Merged
merged 8 commits into from
Jan 31, 2022

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Jan 28, 2022

Currently, if "diff_method"=None, then all parameters are converted to numpy parameters prior to execution. This assumes that execution is happening on a numpy device, and excludes choosing "diff_method"=None on an interface device like "default.qubit.jax".

This PR fixes that issue by only unwrapping parameters if "diff_method"=None AND it's not on an interface device.

The proxy for determining whether or not its an interface device is checking whether "passthru_interface" is in device.capabilities().

@github-actions
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@codecov
Copy link

codecov bot commented Jan 28, 2022

Codecov Report

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

Impacted file tree graph

@@              Coverage Diff               @@
##             v0.21.0-rc0    #2136   +/-   ##
==============================================
  Coverage               ?   99.20%           
==============================================
  Files                  ?      230           
  Lines                  ?    17720           
  Branches               ?        0           
==============================================
  Hits                   ?    17579           
  Misses                 ?      141           
  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 2b7e930...2fb5412. Read the comment docs.

@albi3ro albi3ro changed the base branch from master to v0.21.0-rc0 January 31, 2022 16:58
Copy link
Contributor

@antalszava antalszava left a comment

Choose a reason for hiding this comment

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

Great fix, thank you @albi3ro! 🎉 Had two minor comments, once those are addressed, this is good to be merged from my side.

@@ -324,15 +324,16 @@ def cost_fn(params, x):
if expand_fn == "device":
expand_fn = lambda tape: device.expand_fn(tape, max_expansion=max_expansion)

if gradient_fn is None:
# don't unwrap if its an interface device
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# don't unwrap if its an interface device
# don't unwrap if it's an interface device

with qml.tape.Unwrap(*tapes):
res = cache_execute(batch_execute, cache, return_tuple=False, expand_fn=expand_fn)(
tapes
)

return batch_fn(res)

if gradient_fn == "backprop" or interface is None:
if gradient_fn == "backprop" or interface is None or gradient_fn is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

For readability: adding gradient_fn is None to the condition can be challenging to understand at first if the reader only looks at this specific line. Due to the previous if statement on line 328, in practice (gradient_fn is None) === (gradient_fn is None) and ("passthru_interface" in device.capabilities()).

Could this info be either commented or somehow conveyed by creating well-named objects that are then used in the condition?

@antalszava antalszava merged commit de60e9e into v0.21.0-rc0 Jan 31, 2022
@antalszava antalszava deleted the interface-device-diff_method-None branch January 31, 2022 23:17
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>
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