-
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
SparsePauliOp.to_matrix() exhibits numerical instabilities #13413
Comments
The number 0.025 isn't exactly representable in floating-point arithmetic - the closest value to it is actually the huge fraction Assuming the non-determinism is what I suggested, we can look to fix the order of evaluation there. If I'm right, it should be as simple as swapping out the As a feature request, we could add a "tolerance" field to The fact that Qiskit pre-1.0 was giving exact zeros for your matrix before is most likely a bit of chance - the trouble with inexact floating-point numbers and accumulation of round-off error is fundamental to numerical computing. |
Thanks for the quick reply. As you already expected, using It would really nice being able to specify a "tolerance", at which all values are floored to exactly zero. |
Sure, no worries! The reason I mentioned the inexact floating-point number is that it's not really specific to 0.025, that's just representative of the problem. It has a chance of happening with any coefficients that aren't exactly integer multiples of powers of two - the number of digits when written in base 10 doesn't matter. If you tried it with >>> 0.025 + 0.025 + 0.025 - 0.025 - 0.025 - 0.025
6.938893903907228e-18
>>> 0.025 - 0.025 + 0.025 - 0.025 + 0.025 - 0.025
0.0 It so happens that The tolerance argument is a feature, and we can do that in a future release (probably Qiskit 2.0 now, since 1.3 closed for new features two weeks ago). Fixing the stability of the order of operations is something we can do in a patch release, though - that's the |
Thanks again for the wonderful explanation, very insightful. What are the next steps? The fix for the stability of the order of operations can happen in this bugticket, right? |
Environment
What is happening?
When calculating the matrix representation of a
SparsePauliOp
consisting of multiple terms viato_matrix()
, numbers that should be exactly zero are not zero but something arounde-18
.This does not happen with
qiskit<1
.How can we reproduce the issue?
This also appears when using
sparse=False
.What should happen?
In
qiskit<1.0
the output looks like this:with
qiskit>1.0
, e.g. I useqiskit==1.2.4
the output looks somewhat like this:Interestingly the positions of the small numbers changes when I rerun the same script with a new call. If I run the statements in a loop the positions of the small numbers is constant.
Any suggestions?
Just a first guess by looking at the implementation of
SparsePauliOp.to_matrix()
the issue might either be in this functionqiskit/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py
Line 956 in cd26d49
or rather in both of the
to_matrix_sparse()
qiskit/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py
Line 965 in cd26d49
and
to_matrix_dense
accelerated functions which get imported.qiskit/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py
Line 968 in cd26d49
The text was updated successfully, but these errors were encountered: