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

Standardize batch transform UI #1810

Merged
merged 73 commits into from
Oct 29, 2021
Merged

Standardize batch transform UI #1810

merged 73 commits into from
Oct 29, 2021

Conversation

josh146
Copy link
Member

@josh146 josh146 commented Oct 27, 2021

Context: #1809 standardizes @qfunc_transforms to have the same consistent UI regardless of whether it is applied to qfuncs, tapes, or devices. This PR makes the equivalent change to the @batch_transform decorator.

Description of the Change:

  • The UI is now consistent with qfunc transforms

  • Batch transforms can be bound to devices

  • Fixes a bug found in the device class when creating a backprop device

Example:

import pennylane as qml

dev = qml.device("default.mixed", wires=2)
dev = qml.metric_tensor("diag")(dev)

@qml.beta.qnode(dev)
def f(w, x, y, z):
    qml.RX(w, wires=0)
    qml.RY(x, wires=1)
    qml.CNOT(wires=[0, 1])
    qml.RY(y, wires=0)
    qml.RX(z, wires=1)
    return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))

>>> print(f(0.9, 0.4, 0.5, 0.6))
[[0.25       0.         0.         0.        ]
 [0.         0.25       0.         0.        ]
 [0.         0.         0.22673736 0.        ]
 [0.         0.         0.         0.21208834]]

Benefits: Makes the batch transform UI consistent with the qfunc transform UI, and fixes various UI bugs and edge cases.

Possible Drawbacks: n/a

Related GitHub Issues: n/a

@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 Oct 27, 2021

Codecov Report

Merging #1810 (c49fdb5) into master (ec197be) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1810   +/-   ##
=======================================
  Coverage   98.92%   98.92%           
=======================================
  Files         209      209           
  Lines       15756    15783   +27     
=======================================
+ Hits        15587    15614   +27     
  Misses        169      169           
Impacted Files Coverage Δ
pennylane/beta/qnode.py 100.00% <100.00%> (ø)
pennylane/transforms/batch_transform.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 ec197be...c49fdb5. Read the comment docs.

@josh146 josh146 added the review-ready 👌 PRs which are ready for review by someone from the core team. label Oct 27, 2021
Base automatically changed from device-transform to add_noisy_tape_transform October 27, 2021 12:36
Base automatically changed from add_noisy_tape_transform to master October 27, 2021 14:35
Copy link
Contributor

@glassnotes glassnotes left a comment

Choose a reason for hiding this comment

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

@josh146 I've just done a quick pass and left some initial questions. Very glad to have the new feature!

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
@@ -414,11 +414,16 @@ def _validate_backprop_method(device, interface):
if interface in backprop_devices:
# TODO: need a better way of passing existing device init options
# to a new device?
expand_fn = device.expand_fn
Copy link
Contributor

Choose a reason for hiding this comment

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

Regarding the TODO above, I guess we need something like device.clone()?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a todo we have kinda put on the backlog, just because the need for it disappears if we simply merge all the default qubit subclasses. Which should hopefully be doable now with qml.math!

Copy link
Contributor

Choose a reason for hiding this comment

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

🤞 🤞


def wrapper(qnode):
if isinstance(qnode, qml.Device):
Copy link
Contributor

Choose a reason for hiding this comment

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

I've gotten a bit tangled here; earlier we checked if the input was a device. But here, we're in the case where the input is a QNode, and with is a wrapper that can consider an argument that is a device?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is because the @batch_transform decorator is trying to be too clever 😬 I originally coded it up so that all of the following UIs work:

# inline syntax
qnode = qml.batch_transform(qnode, *transform_args)
tapes, fn = qml.batch_transform(tape, *transform_args)
dev = qml.batch_transform(dev, *transform_args)

# decorator syntax
qnode = qml.batch_transform(*transform_args)(qnode)
tapes, fn = qml.batch_transform(*transform_args)(tape)
dev = qml.batch_transform(*transform_args)(dev)

The first three if statements cover the inline cases, this if statement covers the decorator syntax (hence the additional nested wrapper).

The reason for this complication is that

  • I wanted to preserve the inline syntax, since qml.metric_tensor(qnode, *targs) has worked like this for a very long time.
  • I also wanted to preserve the decorator syntax used by qfunc transforms and qml.grad

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh okay, thanks for clarifying that!

tests/transforms/test_batch_transform.py Show resolved Hide resolved
Copy link
Contributor

@glassnotes glassnotes left a comment

Choose a reason for hiding this comment

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

Thanks @josh146 for the clarifications! Looks good 👍

@@ -414,11 +414,16 @@ def _validate_backprop_method(device, interface):
if interface in backprop_devices:
# TODO: need a better way of passing existing device init options
# to a new device?
expand_fn = device.expand_fn
Copy link
Contributor

Choose a reason for hiding this comment

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

🤞 🤞

pennylane/transforms/batch_transform.py Show resolved Hide resolved

def wrapper(qnode):
if isinstance(qnode, qml.Device):
Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh okay, thanks for clarifying that!

tests/transforms/test_batch_transform.py Show resolved Hide resolved
@josh146 josh146 merged commit 81fd197 into master Oct 29, 2021
@josh146 josh146 deleted the batch-transform-ui branch October 29, 2021 15:18
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

3 participants