-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Supporting floating-point exponents in Operator.power
#11534
Conversation
One or more of the the following people are requested to review this:
|
I wouldn't be surprised if the slowdown you're seeing is SciPy is doing a bit more polishing of the final result when asked for Alternatively, integer powers might be being done in a totally different way in the first way: you can evaluate an integer power def integer_power(matrix, n):
if n < 0:
n = -n
matrix = matrix.inv()
prev_power, power_of_two = out, matrix.copy()
out = np.eye(matrix.shape, matrix.dtype)
while n:
if n & 1:
out @= power_of_two
n >>= 1
power_of_two @= power_of_two
return out That might be significantly faster than calculating the Schur decomposition. Your results potentially suggest that both of the two things I mentioned are happening. |
Pull Request Test Coverage Report for Build 7539466423Warning: This coverage report may be inaccurate.We've detected an issue with your CI configuration that might affect the accuracy of this pull request's coverage report.
💛 - Coveralls |
Thanks, @jakelishman. I have updated the code to use gate's power method for fractional powers (and removed the release note about the speed-up). |
Excellent work, thank you! :-) |
Summary
Fixes #11454.
Details and comments
In
operator.py
we simply switch fromnp.linalg.matrix_power
toscipy.linalg.fractional_matrix_power
. Thanks to @sadbro for this one-liner. This automatically supports floating-point powers.In
gate.py
we switch to usingOperator.power
method.Benchmarking
Initially I was sure that the new method (
scipy.linalg.fractional_matrix_power
, see https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.fractional_matrix_power.html) is faster than the previously implemented method inGate
(based onscipy.linalg.schur
), but after running a few more tests (on my windows laptop), I am no longer convinced of this, and this seems to depend on the exponents. I may need to remove the sentence in release notes about the increased performance.@jakelishman, I know that you have done quite a bit of performance tuning, and so you can probably explain the results I am seeing.
Here is the code that I am running:
When I run this, I get the time for method1 is
8.36
seconds, while the time for method2 is25.05
seconds, so we have ~3x performance improvement. However, when I replace the exponents to be floating-point, and explicitly bythen I get that method 1 takes
119.71
seconds and method 226.63
seconds, we we get ~4.5 slowdown.I still think that whichever method is faster should be implemented in
operator.py
andgate.py
should call that method, but I don't understand which method is better.