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 VJP support to PL-Lightning #181

Merged
merged 33 commits into from
Dec 6, 2021
Merged

Add VJP support to PL-Lightning #181

merged 33 commits into from
Dec 6, 2021

Conversation

maliasadi
Copy link
Member

@maliasadi maliasadi commented Nov 25, 2021

Context:
Implement and Integrate Vector Jacobian Product (VJP) methods in PL-Lightning along with adding tests following PL implementation here and using pybind11.

Description of the Change:
Add vecMatrixProd to Util.hpp and class VectorJacobianProduct to AdjointDiff.cpp with two public methods:

  • tensorDot: Calculates tensor-dot of a given vector of gradient outputs and a Jacobian.
  • vectorJacobianProduct: Calculates the vjp for the selected set of parametric gates utilizing the AdjointJacobian method.

These methods are fully tested at Test_VectorJacobianProduct.cpp.

Python bindings are added to Bindings.cpp and lightning.qubit is now offering two methods to compute vjps:

  • vector_jacobian_product: Generate the the vector-Jacobian products of a tape.
  • batch_vjp: Generate the the vector-Jacobian products of a batch of tapes.

These methods are fully tested at test_vector_jacobian_product.

@codecov
Copy link

codecov bot commented Nov 25, 2021

Codecov Report

Merging #181 (fb80f55) into master (82c197b) will increase coverage by 1.26%.
The diff coverage is 98.41%.

❗ Current head fb80f55 differs from pull request most recent head 9df0c3a. Consider uploading reports for the commit 9df0c3a to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #181      +/-   ##
==========================================
+ Coverage   92.97%   94.23%   +1.26%     
==========================================
  Files           4        4              
  Lines         185      243      +58     
==========================================
+ Hits          172      229      +57     
- Misses         13       14       +1     
Impacted Files Coverage Δ
pennylane_lightning/lightning_qubit.py 92.16% <98.41%> (+3.27%) ⬆️

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 82c197b...9df0c3a. Read the comment docs.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 25, 2021

Test Report (C++) on Ubuntu

       1 files  ±    0         1 suites  ±0   0s ⏱️ ±0s
   368 tests +  23     368 ✔️ +  23  0 💤 ±0  0 ±0 
2 205 runs  +286  2 205 ✔️ +286  0 💤 ±0  0 ±0 

Results for commit 9df0c3a. ± Comparison against base commit 82c197b.

♻️ This comment has been updated with latest results.

@maliasadi maliasadi changed the title [WIP] Add VJP support to PL-Lightning Add VJP support to PL-Lightning Nov 26, 2021
@maliasadi maliasadi requested review from mlxd and josh146 November 26, 2021 19:54
@maliasadi maliasadi marked this pull request as ready for review November 26, 2021 20:49
Copy link
Member

@mlxd mlxd left a comment

Choose a reason for hiding this comment

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

Phenomenal work @maliasadi !
I had a few small comments. Also, just wondering if we can add tests that trigger the conditions CodeFactor/CodeCov complain about?

Comment on lines 21 to 22
template class Pennylane::Algorithms::VectorJacobianProduct<float>;
template class Pennylane::Algorithms::VectorJacobianProduct<double>;
Copy link
Member

Choose a reason for hiding this comment

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

Do you think this should be included in the Adjoint file, or separated into its own file in the algorithms directory?

Copy link
Member Author

Choose a reason for hiding this comment

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

it's definitely better to be added into its own file namely JacobianProd.cpp in the same directory.

Test_StateVector_Nonparam.cpp
Test_StateVector_Param.cpp
Test_StateVectorManaged_Nonparam.cpp
Test_StateVectorManaged_Param.cpp
Test_Util.cpp
Test_Bindings.cpp
Copy link
Member

Choose a reason for hiding this comment

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

Just curious about the reordering of these? If ordering doesn't matter, I think keeping alphabetical may be a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch! It wasn't intentional 🙂

pennylane_lightning/src/util/Util.hpp Show resolved Hide resolved
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

Thanks @maliasadi! This is great, I've left some comments throughout.

Correct me if I am wrong, but currently the built-in Jacobian method only supports gradients technically, since only tapes with a single, scalar, output is supported?

As we iterate the gradient support in PL and lightning, I think it makes sense to 'decouple' the VJP and Jacobian methods at some point, so that a full Jacobian is not reconstructed when the VJP is requested

pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_qubit.py Show resolved Hide resolved
pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved

vjps = dev.batch_vjp(tapes, dys, vjp_pybind=vjp_pybind)

assert sum(len(t) for t in vjps) == sum(len(t.trainable_params) for t in tapes)
Copy link
Member

Choose a reason for hiding this comment

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

really nice testing!!

@maliasadi maliasadi closed this Nov 30, 2021
@maliasadi maliasadi reopened this Nov 30, 2021
@maliasadi
Copy link
Member Author

maliasadi commented Nov 30, 2021

just wondering if we can add tests that trigger the conditions CodeFactor/CodeCov complain about?

Thank you @mlxd! I added adjoint_diff_support_check method that raises QuantumFunctionError in case of finding not supported measurements, observables, or operations by the Lightning adjoint differentiation method for a given tape, and used this method in both adjoint_jacobian and vector_jacobian_product to fix the CodeFactor complain. Please, Let me know if you think this is not the best approach to tackle this issue.

@maliasadi maliasadi requested a review from mlxd November 30, 2021 23:19
@maliasadi
Copy link
Member Author

Thank you @josh146! 🙂

As we iterate the gradient support in PL and lightning, I think it makes sense to 'decouple' the VJP and Jacobian methods at some point, so that a full Jacobian is not reconstructed when the VJP is requested

You're right! However, one can use compute_vjp to compute VJP from a pre-calculated Jacobian. I hope this addresses the issue with reconstructing Jacobian in the vector_jacobian_product method. Moreover, I updated vector_jacobian_product and batch_vjp to also return the adjoint_jacobian results. Please let me know what you think about these new changes.

Copy link
Member

@mlxd mlxd left a comment

Choose a reason for hiding this comment

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

Great job putting this together @maliasadi

As discussed, I think we can safely merge this and build upon it later for the optimal implementation.

@maliasadi maliasadi requested review from AmintorDusko and removed request for AmintorDusko December 3, 2021 17:58
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

Agree with Lee, great work @maliasadi!

@mlxd mlxd merged commit be1599f into master Dec 6, 2021
@mlxd mlxd deleted the lightning-add-vjp branch December 6, 2021 11:39
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.

3 participants