Closed cvjjm closed 3 years ago
Hi @cvjjm -- good catch.
Maybe this has to do with the non standard grad recipe of CRY?
Yes, this is most likely it; the parameter-shift rule for Hessians was adapted from Estimating the gradient and higher-order derivatives on quantum hardware, which predates the 4-term rule!
We are soon to release a bugfix 0.14.1 version, including fixes for #1242. Perhaps the best plan is therefore:
Replacing qml.CRY
with qml.CRY.decomposition
in the above code works around the problem, which is corroborating the suspicion that the non-standard shift rules are the culprit. It would be good to add a test that covers this case.
Also please have a look at the other two issues I pointed out above.
It would be good to add a test that covers this case.
Definitely, will do this ASAP.
Also please have a look at the other two issues I pointed out above.
Will investigate this further!
Hi @cvjjm! I've had some time to explore this more deeply, here are my findings:
In the specific case of the controlled rotation gates, the implementation of the parameter-shift Hessian is hardcoded to follow the results from https://arxiv.org/abs/2008.06517. One short-term solution could be to expand the tape around the controlled rotation gates inside the parameter-shift Hessian logic. Unfortunately, the controlled rotation decomposition are not 1-1 and involve classical processing. I attempted to implement this, but the complexity spiraled significantly. Likely the best solution is to:
For now, raise an error if controlled rotation gates occur
Suggest to the user they decompose these gates
For the next release, re-write the Hessian logic to be more general.
I've implemented the better error handling in #1260, ready for a small bugfix release.
# commenting this produces: "IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed
This was a great edge case find. It turns out that the vector-Hessian product that was being returned to Autograd had the wrong number of dimensions in the edge case where there were unused QNode parameters. I've fixed this in #1260.
#print(circ.draw()) # produces "TypeError: unsupported format string passed to ArrayBox.__format__" Maybe another bug?
I investigated this as well, but it turns out that the issue here is due to qnode.draw()
being deprecated. If you use the new drawing function,
qml.draw(circ)(params)
this works as expected. Recently, Python changed how deprecation warnings are displayed for end-users, so we're working to make sure our deprecation warnings are fully accessible again in #1211!
The parameter shift Hessian does not seem to work reliably in all cases. It is difficult to come up with a very minimal example demonstrating the problem as I do not yet fully understand what what is going wrong. But I managed to condense it down to the following "minimal" example that demonstrates the main bug and shows a few minor problems. Hopefully, e.g., @josh146 can take it from there and get to the bottom of this.
The main problem is that the Autograd Hessian of a function involving a parameter-shift QNode does not agree with a finite difference approximation of the Hessian or the "best" method Hessian (the later two do agree to the expected accuracy).
There are a few additional minor issues that can be triggered by modifying the code according to the comments: The first being an IndexError when a gate is commented from the ansatz, making the result independent of one of the paramters. The second being a error that occurs when trying to draw a circuit during Hessian computation.
Here is the example:
The expected output is
instead I am getting
Maybe this has to do with the non standard grad recipe of CRY?