SparsePauliOp.is_unitary() doesn't respect the input tolerance values
Environment
- Qiskit version: the main branch
- Python version: -
- Operating system: -
What is happening?
The comment of the method said the tolerance is used to check if the values are zero, but the implementation actually only used the tolerance to check if the coefficient of the identity operator is one.
The tolerance values should be propagated to SparsePauliOp.simplify() to set the coefficients smaller than the tolerance to zero.
https://github.com/Qiskit/qiskit/blob/6ce77887008c181f74306887ec494fedd2911a56/qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py#L433-L459
How can we reproduce the issue?
import numpy as np
a = np.array( #
[[-9.9801135e-01 + 6.3036762e-02j, 5.6710692e-06 + 8.1099635e-05j],
[5.6710610e-06 + 8.1099643e-05j, -9.9707150e-01 + 7.6473624e-02j]])
print(a @ (a.conj().T))
# output:
# [[ 1.00000029e+00+0.00000000e+00j -2.83078694e-12+2.75849112e-11j]
# [-2.83078694e-12-2.75849112e-11j 9.99999798e-01+0.00000000e+00j]]
pauli_operators = quantum_info.SparsePauliOp.from_operator(a)
print(pauli_operators.compose(pauli_operators.adjoint()).simplify())
# output:
# SparsePauliOp(['I', 'Z'],
# coeffs=[1.00000004e+00+3.95107524e-18j, 2.48406162e-07+0.00000000e+00j])
print(pauli_operators.is_unitary(atol=1e-5, rtol=1e-3))
# output: False
# expect: True
What should happen?
See the how to reproduce section.
Any suggestions?
Change
val = self.compose(self.adjoint()).simplify()
to
val = self.compose(self.adjoint()).simplify(atol=atol, rtol=rtol)
Thanks for the report - yeah, this seems like a straightforwards bug in the logic. Sorry it took a while for the response - would you like to make a PR?
Thanks for replying. However, I am not working with qiskit now 😅. If someone can make a PR, it will be great.
@jakelishman I'm fairly new to this, but can I be assigned this issue?
@jakelishman , can you take a look of my PR ? It is pending approval to be built.
@jakelishman Can you take a look of the PR ? https://github.com/Qiskit/qiskit/pull/14676
@jakelishman, if this issue is still open, would like to give it a shot.
If this is still open, can I be assigned this issue?
This is already being worked on, there's an open PR for this