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 batch_partial implementation #2585

Merged
merged 29 commits into from May 31, 2022

Conversation

eddddddy
Copy link
Collaborator

@eddddddy eddddddy commented May 17, 2022

Context:
There are scenarios where we want to execute a circuit multiple times, with some parameters identical and other parameters differing between the executions. It makes sense to be able to batch these executions together.

Description of the Change:
Add a new transform qml.batch_partial that behaves similarly to functools.partial, but supports an additional batch dimension in the unevaluated parameters. Now we would be able to achieve the desired behaviour by doing something like:

dev = qml.device("default.qubit", wires=1)

@qml.qnode(dev)
def circuit(x, y):
   qml.RX(x, wires=0)
   qml.RY(y, wires=0)
   return qml.expval(qml.PauliZ(wires=0))
>>> batched_partial_circuit = qml.batch_partial(circuit, x=np.array(np.pi / 2))
>>> y = np.array([0.2, 0.3, 0.4])
>>> batched_partial_circuit(y=y)
tensor([0.69301172, 0.67552491, 0.65128847], requires_grad=True)

More complex scenarios involving callable arguments are also possible. For example,

>>> batched_lambda_circuit = qml.batch_partial(circuit, x=lambda z: z + 0.2, y=lambda z: z ** 2)
>>> z = np.array([0.2, 0.3, 0.4])
>>> batched_lambda_circuit(z)
tensor([0.92032424, 0.87403075, 0.81479384], requires_grad=True)

Benefits:
The user can now add a batch dimension to arguments passed to a partially-evaluated QNode.

Possible Drawbacks:
None

Related GitHub Issues:
Closes #1881

@eddddddy eddddddy added the WIP 🚧 Work-in-progress label May 17, 2022
@codecov
Copy link

codecov bot commented May 17, 2022

Codecov Report

Merging #2585 (37c2b5b) into master (8265d21) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #2585   +/-   ##
=======================================
  Coverage   99.58%   99.58%           
=======================================
  Files         244      245    +1     
  Lines       19673    19724   +51     
=======================================
+ Hits        19591    19642   +51     
  Misses         82       82           
Impacted Files Coverage Δ
pennylane/__init__.py 100.00% <ø> (ø)
pennylane/transforms/__init__.py 100.00% <100.00%> (ø)
pennylane/transforms/batch_partial.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 8265d21...37c2b5b. Read the comment docs.

@eddddddy eddddddy marked this pull request as ready for review May 19, 2022 14:27
@eddddddy eddddddy requested a review from antalszava May 19, 2022 14:27
@eddddddy eddddddy removed the WIP 🚧 Work-in-progress label May 19, 2022
@eddddddy eddddddy changed the title [WIP] Add batch_partial implementation Add batch_partial implementation May 19, 2022
@antalszava
Copy link
Contributor

Thank you @eddddddy! 🎉 Will be leaving a more detailed review during the first half of tomorrow.

Hi @dwierichs, just a ping to see if the main UI here is looking good. 👍

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.

Looking really nice, thanks @eddddddy! Cool addition to support callables as preprocessing :)
Only have some minor confusions/questions regarding the doc string.

doc/releases/changelog-dev.md Outdated Show resolved Hide resolved
doc/releases/changelog-dev.md Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
eddddddy and others added 6 commits May 20, 2022 09:33
Co-authored-by: David Wierichs <davidwierichs@gmail.com>
Co-authored-by: David Wierichs <davidwierichs@gmail.com>
Co-authored-by: David Wierichs <davidwierichs@gmail.com>
Co-authored-by: David Wierichs <davidwierichs@gmail.com>
@eddddddy
Copy link
Collaborator Author

Hi @dwierichs, thanks for the helpful comments. There are just a couple outstanding that need another look, otherwise I've mostly addressed them.

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.

Hi @eddddddy, this is looking great overall 🥳 Have a couple of suggestions for refining things, but the overall implementation seems to check out nicely.

pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
tests/transforms/test_batch_partial.py Outdated Show resolved Hide resolved
tests/transforms/test_batch_partial.py Outdated Show resolved Hide resolved
tests/transforms/test_batch_partial.py Outdated Show resolved Hide resolved
tests/transforms/test_batch_partial.py Outdated Show resolved Hide resolved
tests/transforms/test_batch_partial.py Show resolved Hide resolved
eddddddy and others added 4 commits May 20, 2022 16:16
Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: antalszava <antalszava@gmail.com>
eddddddy and others added 5 commits May 20, 2022 16:17
Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: antalszava <antalszava@gmail.com>
Co-authored-by: antalszava <antalszava@gmail.com>
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Outdated Show resolved Hide resolved
pennylane/transforms/batch_partial.py Show resolved Hide resolved
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.

Looks good @eddddddy! 🙂 🎉 A couple of final smaller suggestions and a question.

pennylane/transforms/batch_partial.py Show resolved Hide resolved
@eddddddy eddddddy merged commit 3ca1d02 into PennyLaneAI:master May 31, 2022
@eddddddy eddddddy deleted the batch_partial_qnode branch May 31, 2022 15:28
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.

Make the "partial" of a QNode batch-executable
3 participants