qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

SparsePauliOp.is_unitary() doesn't respect the input tolerance values

Open IvanaGyro opened this issue 9 months ago • 3 comments

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)

IvanaGyro avatar Mar 26 '25 16:03 IvanaGyro

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?

jakelishman avatar Jun 06 '25 16:06 jakelishman

Thanks for replying. However, I am not working with qiskit now 😅. If someone can make a PR, it will be great.

IvanaGyro avatar Jun 06 '25 21:06 IvanaGyro

@jakelishman I'm fairly new to this, but can I be assigned this issue?

hillerj1 avatar Jun 13 '25 22:06 hillerj1

@jakelishman , can you take a look of my PR ? It is pending approval to be built.

littlebullGit avatar Jul 07 '25 00:07 littlebullGit

@jakelishman Can you take a look of the PR ? https://github.com/Qiskit/qiskit/pull/14676

littlebullGit avatar Jul 19 '25 14:07 littlebullGit

@jakelishman, if this issue is still open, would like to give it a shot.

SoardRaspi avatar Aug 22 '25 19:08 SoardRaspi

If this is still open, can I be assigned this issue?

Adithya2685-git avatar Sep 05 '25 12:09 Adithya2685-git

This is already being worked on, there's an open PR for this

Cryoris avatar Sep 05 '25 14:09 Cryoris