-
Notifications
You must be signed in to change notification settings - Fork 39
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 C++ adjoint diff methods #136
Conversation
Hello. You may have forgotten to update the changelog!
|
Codecov Report
@@ Coverage Diff @@
## master #136 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 3 4 +1
Lines 71 182 +111
==========================================
+ Hits 71 182 +111
Continue to review full report at Codecov.
|
* Update tests * Add problematic statements * Fix tests * Fix type hinting * Make function as expected * Add quick return for no jacobian * Move position * Add raise for non supported ops * Ensure jacobian shape formatted as expected * Return jacobian directly as numpy array * Ensure correct return type of jacobian * Ensure param number tracking is correct for adjjac * Fix CRY gate index * All tests passing * Calculate parametric ops when creating opsdata object * Apply formatting * Ensure MacOS wheel builder uses a more modern compiler toolchain * Fix mac wheel builder Co-authored-by: Lee J. O'Riordan <loriordan@gmail.com>
Just a note, CodeFactor seems to be caching old runs and using those --- please ignore its Action failure. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CC: /usr/local/opt/llvm/bin/clang | ||
CXX: /usr/local/opt/llvm/bin/clang++ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the CC option for C (i.e., the language)? Do we need it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, and you are right. Removing CC should be fine (though I'll test it out). There may be a way to avoid use of this too, though I must check this out first.
wires_map (dict): a dictionary mapping input wires to the device's backend wires | ||
|
||
Returns: | ||
Tuple[list, list, list, list, list]: A serialization of the operations, containing a list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tuple[list, list, list, list, list]: A serialization of the operations, containing a list | |
Tuple[Tuple[list, list, list, list, list], bool]: A serialization of the operations as a | |
tuple of lists and a flag indicating whether non-default state preparation is used in the | |
tape. |
|
||
def _serialize_ops( | ||
tape: QuantumTape, wires_map: dict | ||
) -> Tuple[List[List[str]], List[np.ndarray], List[List[int]], List[bool], List[np.ndarray]]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
) -> Tuple[List[List[str]], List[np.ndarray], List[List[int]], List[bool], List[np.ndarray]]: | |
) -> Tuple[Tuple[List[List[str]], List[np.ndarray], List[List[int]], List[bool], List[np.ndarray]], bool]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this should be fine, I wonder will it mess with the conversion to C++ (though I'd imagine pybind is clever enough that we do need to be explicit).
if not CPP_BINARY_AVAILABLE: | ||
return super().adjoint_jacobian(tape, starting_state, use_device_state) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this? Since LightningQubit
is reset to DefaultQubit
if CPP_BINARY_AVAILABLE
(see lines 205 and after)
* Add test for dev pl * Add test * Move to original tests * Add additional ops creation method * Fix QFT errors * Reformat * Revert "Fix QFT errors" This reverts commit f62a763. * Preclude certain gates * Update test * Add double comments * Update test * Add tests for standard and custom wire labels * Remove print * Extend tests for RX,RY * Prevent trainable params from going negative * Ensure correct coef applied when using generator * Update Ops repr * Reenable tests * Update CI for non compiled builds * Add exception for Projector observable * Add Hermitian in tensor product * Ensure data is passed as complex128 to cpp * Run black * Ensure serialisation of numpy data is cast correctly * Remove redundant methods * Add support to get obs data * Fix serialize test array structures * Add hermitian checks * Ensure testing complete for current requirements * Reformat bindings * Skip test for Hermitian on default * Ensure bin available passed as class param * Skip for non binary tests Co-authored-by: Lee J. O'Riordan <loriordan@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work Lee, just two minor comments.
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Thanks for the reviews all. As per our chat on Friday I'm ignoring the codefactor report and merging this, as it still fails to remove the cached version. |
* PennyLane-Lightning now has support for the adjoint Jacobian method of http://arxiv.org/abs/2009.02823. | ||
[(136)](https://github.com/PennyLaneAI/pennylane-lightning/pull/136) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me this is a major new feature 🚀 🎉 We can probably move it up during release 😁
Context: This PR will add C++ support for adjoint diff, based on arXiV: arXiV:2009.02823.
Description of the Change:
StateVectorManaged
class added as a subclass ofStateVector
, where memory management is handled internally by the class.AdjointDiff.hpp
file added, withAdjointJacobian
and helper classes to prepare and calculate the Jacobian for a given set of parameters and observables. For multiple observables, the calculations are handled independently by OpenMP threads and populate the Jacobian.std::variant
based functionality used to distinguish between parametric gates, non parametric gates, and arbitrary gates defined by a list of complex parameters.std::variant
.algorithms
for algorithms using and to be used by the simulator;bindings
for source files directly related to binding the C++ code with Python;simulator
for source-files directly related to the simulator;util
for utility functions to be used across the package;CRY
gate, which had an incorrect index.Benefits: C++ backed adjoint method is much faster (4x-6x) than Python.
Possible Drawbacks: Requires modern C++17 knowledge to maintain. Design may require some later refactor.
Related GitHub Issues: