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

Add support for easily creating expansion functions given a device #1760

Merged
merged 107 commits into from
Oct 25, 2021

Conversation

josh146
Copy link
Member

@josh146 josh146 commented Oct 16, 2021

Context: Comments on a recent blog post made me realize that there is no intuitive way in PennyLane to, given a device, construct an expansion function to expand circuits down to the native gate set of that device.

Description of the Change: The recently added create_expand_fn is modified, to accept a stopping criterion and/or a device.

  • If provided with just a stopping condition, the function acts as before.
  • If provided with a stopping condition and a device, the function generates an expansion that first expands given the stopping condition, before expanding so that the circuit is supported on the device.
  • Finally, if neither a stopping condition nor device is provided, the tape is expanded simply to the given depth.

Benefits: East to create an expansion function using a device as a criterion.

Possible Drawbacks: n/a

Related GitHub Issues: n/a

@josh146 josh146 added the review-ready 👌 PRs which are ready for review by someone from the core team. label Oct 16, 2021
@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.

@josh146
Copy link
Member Author

josh146 commented Oct 16, 2021

@dwierichs this is an idea I had while reviewing #1743 🙂

@codecov
Copy link

codecov bot commented Oct 16, 2021

Codecov Report

Merging #1760 (17be48d) into master (189b196) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1760   +/-   ##
=======================================
  Coverage   98.91%   98.91%           
=======================================
  Files         207      207           
  Lines       15617    15629   +12     
=======================================
+ Hits        15448    15460   +12     
  Misses        169      169           
Impacted Files Coverage Δ
pennylane/_device.py 98.24% <100.00%> (+0.01%) ⬆️
pennylane/transforms/tape_expand.py 100.00% <100.00%> (ø)

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 189b196...17be48d. Read the comment docs.

Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Nice addition! This should make using create_expand_fn much nicer for users.

tests/transforms/test_tape_expand.py Outdated Show resolved Hide resolved
pennylane/transforms/tape_expand.py Show resolved Hide resolved
pennylane/transforms/tape_expand.py Outdated Show resolved Hide resolved
@@ -36,6 +41,8 @@ def create_expand_fn(depth, stop_at, docstring=None):
``stop_at(obj)``, where ``obj`` is a *queueable* PennyLane object such as
:class:`~.Operation` or :class:`~.MeasurementProcess`. It must return a
boolean, indicating if the expansion should stop at this object.
device (.Device): Ensure that the expanded tape only uses native gates of the
given device.
docstring (str): docstring for the generated expansion function

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe the docstring should mention in which order the expansions via stop_at and device are carried out :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually spent a long time thinking about this! Is the order chosen in this PR the correct order?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure. On one hand, the device decomposition can create gates that are not in accordance with the stop_at criterion (although I am not sure what a realistic scenario for this would be), on the other hand, it would be annoying to provide a device to create_expand_fn and receive a tape that does not run on that device (which could happen if we switch the order).
Maybe the latter point is the weaker one and we should actually switch the order, because in PL in general users leave decomposition to device-compatible operations to the backend.

Finally, one could consider forcing both criteria to be satisfied at the same time by transforming the stopping criterion of the device into a BooleanFn and merging it with stop_at via &.

Co-authored-by: David Wierichs <davidwierichs@gmail.com>
Base automatically changed from expand-qfunc-transform to master October 17, 2021 13:34

if device is not None:
tape = device.expand_fn(tape, max_expansion=_depth)
# _update_trainable_params(tape)
Copy link
Member Author

Choose a reason for hiding this comment

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

@dwierichs your solution above re: only updating trainable parameters only once, after the with statement, seems to cause issues; I get > 200 test failures 😆

It seems the only combinations that allow the tests to pass are:

  • Not updating
  • Updating after every expand. I think this makes sense, since if you are doing multiple tape expansions, and later ones have a stopping criterion that depends on trainable parameters, this will need to be updated inbetween each expansion!

For now, I've added it back after each expansion but commented it out.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, haha, yes that makes sense :) Did not think about the interaction of expands and setting the trainable params!

Copy link
Contributor

@dwierichs dwierichs left a comment

Choose a reason for hiding this comment

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

Can't find anything to criticize, looks good to me :)
The only pending decision would be regarding the order/merging of device expand and stop_at expansion, I think.

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
@josh146 josh146 merged commit f575490 into master Oct 25, 2021
@josh146 josh146 deleted the support-device-expand branch October 25, 2021 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review-ready 👌 PRs which are ready for review by someone from the core team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants