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 #8992 (SparsePauliOp.dot with real coefficients) #8998

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __init__(self, data, coeffs=None, *, ignore_pauli_phase=False, copy=True):

pauli_list = PauliList(data.copy() if copy and hasattr(data, "copy") else data)

dtype = coeffs.dtype if isinstance(coeffs, np.ndarray) else complex
dtype = object if isinstance(coeffs, np.ndarray) and coeffs.dtype == object else complex
Cryoris marked this conversation as resolved.
Show resolved Hide resolved

if coeffs is None:
coeffs = np.ones(pauli_list.size, dtype=dtype)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fix :meth:`.SparsePauliOp.dot` between operators with real coefficients.
To fix this, the dtype that :class:`SparsePauliOp` can take is restricted to
``np.complex128`` and ``object``.
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,13 @@ def commutes(left: Pauli, right: Pauli) -> bool:
)
)

def test_dot_real(self):
"""Test dot for real coefficiets."""
x = SparsePauliOp("X", np.array([1]))
y = SparsePauliOp("Y", np.array([1]))
iz = SparsePauliOp("Z", 1j)
self.assertEqual(x.dot(y), iz)


if __name__ == "__main__":
unittest.main()