Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 a simple API for gradient transforms #1589
Add a simple API for gradient transforms #1589
Changes from 51 commits
ed38d78
2213575
424d19e
c45256f
80faa2b
2002f3d
446b640
46ab93f
b588ee8
b99fb3a
a8b031f
1192b7a
8c3e267
4510ed3
1ed0dac
34bc66c
eb0c423
cce9c81
c40679e
f330aa9
1e4ef3c
4e21821
10e91c5
a5045e3
cb0cc93
e69c29c
26acd1f
05a9201
50b4a67
5fa87d8
c1b9a5a
3766c45
54697af
903bc93
5f3aadd
d7c1d9d
bddc1c6
4a283d9
3a904aa
403ae4d
f4028ea
0ccb881
f168130
23a3c63
c73a67f
355af2e
92e29be
f47a7c7
361a8eb
1548b5d
b4e49ac
73cb746
e0282d3
9199679
ad433de
a18a978
011bf78
1a4b48d
6ba19e1
1ebe252
eeeefca
a098654
a47d582
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
What is the classical processing used for in the case where it is present?
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.
This is a good question :)
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.
So!
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.
Recall that gradient transforms are of the form
tape -> processing_fn(execute(gradient_tapes))
. That is, they will return the Jacobian of the quantum circuit output with respect to gate arguments.This is all fine and very well-defined when applying a gradient transform to a tape.
However, when applying a gradient transform to a QNode, there is a subtlety --- the arguments you pass to evlauate the transform are QNode arguments, which are not necessarily gate arguments! In fact, it is very easy to introduce classical processing in-between the QNode arguments and the gate arguments without realizing.
Consider the following:
Here, we have added classical processing inbetween the QNode and gate arguments by permuting the arguments, and multiplying by a scalar. The processing function mapping
qnode args -> gate args
looks like this:If we were to evaluating the gradient transform ignoring this classical processing, we would extract only the quantum Jacobian. This would be a
(2, 2)
matrix (2 gate arguments, 2 output dimensions), and would not be what the user expects at all.Instead, the user would expect a
(3, 2)
matrix output (3 QNode args, 2 output dimensions). So what we need to do is compute the ('classical') Jacobian of theC
function above (which will be(3, 2)
) and multiply it by the quantum Jacobian: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.
@josh146 thanks for the explanation, this helped me understand what is happening in the code below 💯
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.
No worries! Even just permuting the gate arguments wrt qnode arguments is non-trivial classical processing - the classical Jacobian will be a permutation matrix, not an identity matrix!
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.
This is the part of the code I was most worried about, hence all the different classical processing tests 😆
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.
@glassnotes this is also similar to the issue you had with the Fourier module I believe? Except it is a bit harder to solve; simply knowing the Jacobian of the classical processing was insufficient, in that case, you needed the actual gate mapping function
C
.Something maybe we could do with tracing 😄
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.
Thanks for elaborating on this @josh146 , I've definitely used classical processing between the QNode arguments and gate arguments without realizing! I'll be keeping an eye out for this now