Skip to content
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

Fix SparsePauliOp initialization with dense Y labels #13580

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
10 changes: 9 additions & 1 deletion qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,15 @@ def __init__(
# move the phase of `pauli_list` to `self._coeffs`
phase = pauli_list._phase
count_y = pauli_list._count_y()
self._coeffs = np.asarray((-1j) ** (phase - count_y) * coeffs, dtype=coeffs.dtype)

# Compute exponentiation via integer arithmetic and lookup table to avoid floating point errors
ShellyGarion marked this conversation as resolved.
Show resolved Hide resolved
exponent = (phase - count_y) % 4
lookup = np.array([1 + 0j, -1j, -1 + 0j, 1j], dtype=coeffs.dtype)

vals = lookup[exponent]
self._coeffs = vals * coeffs

# Update pauli_list phase
pauli_list._phase = np.mod(count_y, 4)
self._pauli_list = pauli_list

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fixes:
- |
Fixed a bug where a initializing :class:`.SparsePauliOp` with a large
number of Pauli-``Y`` terms (typically :math:`\geq 100`) and no explicit
``coeffs`` would result in a coefficient close to 1 but with a floating point
error. The coefficient is now correctly 1 per default.
Fixed `#13522 <https://github.com/Qiskit/qiskit/issues/13522>`__.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def test_sparse_pauli_op_init(self):
coeffs[:] = 0
self.assertEqual(spp_op, ref_op)

def test_sparse_pauli_op_init_long_ys(self):
"""Test heavy-weight SparsePauliOp initialization."""
yyy = SparsePauliOp("Y" * 100)
print(yyy.coeffs)
yy = SparsePauliOp("Y" * 99)
print(yy.coeffs)
ShellyGarion marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(yyy.coeffs, yy.coeffs)


@ddt.ddt
class TestSparsePauliOpConversions(QiskitTestCase):
Expand Down
Loading