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

Merged
merged 14 commits into from
Jan 6, 2025
11 changes: 10 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,16 @@ 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
exp_val = (phase - count_y) % 4
ShellyGarion marked this conversation as resolved.
Show resolved Hide resolved
p = (3 * exp_val) % 4 # (−i)^(phase - count_y) = i^(3*(phase - count_y))
lookup = np.array([1 + 0j, 1j, -1 + 0j, -1j], dtype=coeffs.dtype)
Cryoris marked this conversation as resolved.
Show resolved Hide resolved

vals = lookup[p]
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
Expand Up @@ -140,6 +140,17 @@ 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 SparsePauliOp initialization."""
with self.subTest(msg="make SparsePauliOp from SparsePauliOp"):
# produces non-zero imaginary component
yyy = SparsePauliOp("Y" * 100)
print(yyy.coeffs)
# correctly contains no imaginary component
yy = SparsePauliOp("Y" * 99)
print(yy.coeffs)
self.assertNotEqual(yyy, yy)


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