-
Notifications
You must be signed in to change notification settings - Fork 641
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
HT gradient - additional modes #7046
base: master
Are you sure you want to change the base?
Conversation
…and finished reversed method for single measurement
So for the failing test, we keep a hardcoded list of potential gradient keyword arguments here:
This was done as a patch to add validation to the |
# Get a generator and coefficients | ||
sub_coeffs, generators = _get_pauli_generators(trainable_op) | ||
|
||
# Create measurement with gate generators | ||
# With type pennylane.measurements.expval.ExpectationMP | ||
mp = qml.expval( | ||
qml.Hamiltonian( | ||
coeffs=sub_coeffs, | ||
observables=generators, | ||
# grouping_type="commuting", | ||
) | ||
) |
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.
When we are using the generator as a meausrement, do we still need to represent the observable as a linear combination of unitaries (paulis)?
Or could we technically just do:
mp = qml.expval(trainable_op.generator() @ qml.Y(aux_wire))
This might be a bit more general, and leaves interpreting the new observable up to the device.
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, I changed the how the measurements are constructed, everything works well so far.
I also added the keyword "mode" into that hardcoded set.
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.
BTW, with this implementation, if a user wants to minimize the number of distinct circuits, would the device be able to apply measurement grouping with respect to this expval
?
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.
Yep. Measuremet grouping is on by default now with split_non_commuting
.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7046 +/- ##
==========================================
- Coverage 99.60% 99.48% -0.12%
==========================================
Files 490 496 +6
Lines 47256 47393 +137
==========================================
+ Hits 47070 47151 +81
- Misses 186 242 +56 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
coding style Co-authored-by: Isaac De Vlugt <[email protected]>
coding style
return new_batch, new_coeffs | ||
|
||
def _reversed_hadamard_test(tape, trainable_param_idx, aux_wire) -> tuple[list, 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.
For reversed and direct reversed, can we add a check making sure there is only one meausrement, and raising a ValueError
if not?
We could also add this validation up near the other validation checks, like the assert_no_state
.
The benefit of having it here is that we've already dispatched by mode, but the benefit of having it by the other assertions is that we would only have to run it once.
all supported quantum operation arguments. More info is in the documentation | ||
for :func:`qml.gradients.hadamard_grad <.gradients.hadamard_grad>`. | ||
|
||
* ``"reversed-hadamard"``: A variant of ``"hadamard"``, where the role of the |
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.
At this point, the different modes are accessible via:
qml.QNode(f, dev, diff_method="hadamard", gradient_kwargs={"mode": "reversed"})
To automatically choose the mode from a string diff_method
, we will need to update this workflow function:
pennylane/pennylane/workflow/resolution.py
Line 163 in db404c0
def _resolve_diff_method( |
to be able to interpret the various hadamard options. We can do so in this PR, but I'd prefer to do that in a follow on.
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.
Given direct and reversed direct don't need an aux wire, bypass the the extraction of the aux_wire
if the mode is direct or revesed direct:
aux_wire = _get_aux_wire(aux_wire, tape, device_wires)
Potentially just leaving it None
if when we don't need it. This will allow us to direct and reversed direct when we don't have any bonus wires.
Context: Hadamard gradients method variants
Description of the Change: added support for different modes in hadamard gradient and implemented direct HT, reversed HT and reversed_direct HT
Benefits: more variants of HT + measurement grouping generates better efficiency for gradient estimation
Possible Drawbacks: for reversed methods, that is reversed HT and reversed_direct HT, the current version does not support circuits with multiple measurements. One will need to modify the processing_fn in how it aggregates the results so support this. For non-reversed methods, we aggregate for each measurement and the code work because the shape is N_G*M where N_G is the number of terms in the generator and M is the number of measurements. However, in reversed method, the shape is [N_1, ..., N_M], where for the i-th measurement, there are N_i gradient tapes. Different measurement has different number of tapes, and all the tape has only one measurement, so one does not need to aggregate across dimensions, just group neighboring ones would do.
Related GitHub Issues: