PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.36k stars 604 forks source link

Add is_leaf argument to pytrees\pytrees.py with test at tests\pytrees\test_pytrees.py, fixes #6083 #6107

Closed tonmoy-b closed 2 months ago

tonmoy-b commented 3 months ago

Context: Add is_leaf argument to pytrees\pytrees.py with test at tests\pytrees\test_pytrees.py fixes #6083

Description of the Change: Added is_leaf argument to the function pytrees\pytrees.py. Should this argument evaluate to True then no fufther flattening will be done. Added test to tests\pytrees\test_pytrees.py, with function test_flatten_is_leaf() having two test cases. Added changelog with file \doc\releases\changelog-0.38.0.md

Benefits: argument added to the function flatten which acts as a control mechanism to designate a node as leaf node even if that node could be further flattened, e.g., the node is a tuple.

Possible Drawbacks:

Related GitHub Issues: Fixes #6083

Related Shortcut Stories: [sc-70903]

tonmoy-b commented 3 months ago

hi @albi3ro and @mudit2812, thanks for the review and suggestions. I have accepted the suggested changes. Do let me know if it would be nice for me to look work on any other contributions, I'd love to get more familiar with the code.

codecov[bot] commented 3 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.66%. Comparing base (1f55c88) to head (baf9ac7). Report is 2 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #6107 +/- ## ========================================== - Coverage 99.67% 99.66% -0.01% ========================================== Files 432 432 Lines 41839 41543 -296 ========================================== - Hits 41702 41405 -297 - Misses 137 138 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Alex-Preciado commented 3 months ago

Thank you so much for this PR @tonmoy-b 🚀 !! We will continue with the review next week. Have a great weekend!!

tonmoy-b commented 3 months ago

Thanks for opening this PR @tonmoy-b ! The implementation looks great. I left some suggestions for the changelog. Do you mind also adding a couple of more tests:

  • A test to verify that the pytree structure returned by flatten is correct when given an is_leaf argument.
  • A test to verify that flattening and then unflattening the same pytree will be equivalent to the original object when flatten is given an is_leaf argument.

Other than the above comments, the PR is in great shape and I'm happy to approve once the above comments are resolved 🎉

Hi @mudit2812, @albi3ro @Alex-Preciado ,

  I have added the mentioned tests into tests\pytrees\test_pytrees.py, as three functions (test_flatten_is_leaf, test_unflatten_is_leaf, test_unflatten_for_structure_is_leaf), kindly take a look. 

Regards

Alex-Preciado commented 2 months ago

Thank you so much for this contribution @tonmoy-b !! 🚀

tonmoy-b commented 2 months ago

Thank you so much for this contribution @tonmoy-b !! 🚀

Thanks Alex, it was my pleasure and I hope to make more contributions to Pennylane going forward.