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 native tools for flattening and unflattening pytrees #5701

Merged
merged 13 commits into from
May 24, 2024

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented May 16, 2024

Context:

Pytrees are nested data structures. Pytree tools can make it easier to handle said data structure.

Jax, optree, and other packages provide tools for handling pytrees already. To take advantage of those tools in core pennylane, we would need to make one of those packages a dependency. Instead of adding an extra dependency, we can have our own stripped-down version of pytree tools.

Description of the Change:

This PR adds tree_flatten, tree_unflatten, Structure, and Leaf to the pytrees module. It also updates qml.pytrees.register_pytree to register the type with pennylane's pytree setup as well.

>>> op = qml.adjoint(qml.Rot(1.2, 2.3, 3.4, wires=0))
>>> data, structure = flatten(op)
>>> data
[1.2, 2.3, 3.4]
>>> structure
<Tree(AdjointOperation, (), (<Tree(Rot, (<Wires = [0]>, ()), (Leaf, Leaf, Leaf))>,))>
>>> unflatten([-2, -3, -4], structure)
Adjoint(Rot(-2, -3, -4, wires=[0]))

Benefits:

Use in datasets module to serialize and de-serialize pennylane objects.

Potential future use in bind_new_parameters.

Easy extraction and reset of parameters.

Possible Drawbacks:

Not as performant as the C++ bound versions used by jax and optree.

Related GitHub Issues:

[sc-46349]

@albi3ro albi3ro marked this pull request as ready for review May 16, 2024 19:44
Copy link

codecov bot commented May 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (58668d7) to head (6fa9778).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5701      +/-   ##
==========================================
- Coverage   99.68%   99.67%   -0.01%     
==========================================
  Files         416      416              
  Lines       39049    38814     -235     
==========================================
- Hits        38925    38689     -236     
- Misses        124      125       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@albi3ro albi3ro requested a review from mudit2812 May 17, 2024 15:52
Copy link
Contributor

@brownj85 brownj85 left a comment

Choose a reason for hiding this comment

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

Overall looks great! this will be much easier to integrate than the Jax library

pennylane/pytrees.py Outdated Show resolved Hide resolved
pennylane/pytrees.py Outdated Show resolved Hide resolved
pennylane/pytrees.py Outdated Show resolved Hide resolved
pennylane/pytrees.py Outdated Show resolved Hide resolved
pennylane/pytrees.py Outdated Show resolved Hide resolved
Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Looks great! Couple of suggestions.

pennylane/pytrees.py Show resolved Hide resolved
pennylane/pytrees.py Outdated Show resolved Hide resolved
pennylane/pytrees.py Outdated Show resolved Hide resolved
albi3ro and others added 3 commits May 21, 2024 08:52
Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Jack Brown <jack@xanadu.ai>
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
pennylane/pytrees.py Outdated Show resolved Hide resolved
pennylane/pytrees.py Outdated Show resolved Hide resolved
pennylane/pytrees.py Outdated Show resolved Hide resolved
pennylane/pytrees.py Show resolved Hide resolved
pennylane/pytrees.py Outdated Show resolved Hide resolved
tests/test_pytrees.py Outdated Show resolved Hide resolved
albi3ro and others added 5 commits May 23, 2024 08:31
Co-authored-by: Jack Brown <jack@xanadu.ai>
Co-authored-by: Jack Brown <jack@xanadu.ai>
Co-authored-by: Jack Brown <jack@xanadu.ai>
pennylane/pytrees.py Show resolved Hide resolved
pennylane/pytrees.py Show resolved Hide resolved
pennylane/pytrees.py Show resolved Hide resolved
@brownj85 brownj85 self-requested a review May 23, 2024 21:19
@albi3ro albi3ro merged commit 960204f into master May 24, 2024
40 checks passed
@albi3ro albi3ro deleted the pytree-flatten-unflatten branch May 24, 2024 14: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.

None yet

4 participants