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

Deprecate PauliList estimator observables #11055

Merged
merged 6 commits into from
Nov 2, 2023
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
12 changes: 11 additions & 1 deletion qiskit/primitives/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
from __future__ import annotations

import warnings
import sys
import typing
from collections.abc import Iterable
Expand All @@ -23,7 +24,7 @@
from qiskit.circuit import Instruction, ParameterExpression, QuantumCircuit
from qiskit.circuit.bit import Bit
from qiskit.circuit.library.data_preparation import Initialize
from qiskit.quantum_info import SparsePauliOp, Statevector
from qiskit.quantum_info import SparsePauliOp, Statevector, PauliList
from qiskit.quantum_info.operators.base_operator import BaseOperator
from qiskit.quantum_info.operators.symplectic.base_pauli import BasePauli

Expand Down Expand Up @@ -82,6 +83,15 @@ def init_observable(observable: BaseOperator | PauliSumOp | str) -> SparsePauliO
elif isinstance(observable, BaseOperator) and not isinstance(observable, BasePauli):
return SparsePauliOp.from_operator(observable)
else:
if isinstance(observable, PauliList):
warnings.warn(
"Implicit conversion from a PauliList to a SparsePauliOp with coeffs=1 in"
" estimator observable arguments is deprecated as of Qiskit 0.46 and will be"
" in Qiskit 1.0. You should explicitly convert to a SparsePauli op using"
" SparsePauliOp(pauli_list) to avoid this warning.",
DeprecationWarning,
stacklevel=2,
)
return SparsePauliOp(observable)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
deprecations:
- |
Deprecates using a :class:`~.PauliList` as an observable that is implicitly
converted to a :class:`~.SparsePauliOp` with coefficients 1 when calling
:meth:`.Estimator.run`. Users should instead explicitly convert the argument
using ``SparsePauliOp(pauli_list)`` first.
18 changes: 13 additions & 5 deletions test/python/primitives/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ class TestObservableValidation(QiskitTestCase):
@data(
("IXYZ", (SparsePauliOp("IXYZ"),)),
(Pauli("IXYZ"), (SparsePauliOp("IXYZ"),)),
(PauliList("IXYZ"), (SparsePauliOp("IXYZ"),)),
(SparsePauliOp("IXYZ"), (SparsePauliOp("IXYZ"),)),
(PauliSumOp(SparsePauliOp("IXYZ")), (SparsePauliOp("IXYZ"),)),
(
Expand All @@ -360,10 +359,6 @@ class TestObservableValidation(QiskitTestCase):
[Pauli("IXYZ"), Pauli("ZYXI")],
(SparsePauliOp("IXYZ"), SparsePauliOp("ZYXI")),
),
(
[PauliList("IXYZ"), PauliList("ZYXI")],
(SparsePauliOp("IXYZ"), SparsePauliOp("ZYXI")),
),
(
[SparsePauliOp("IXYZ"), SparsePauliOp("ZYXI")],
(SparsePauliOp("IXYZ"), SparsePauliOp("ZYXI")),
Expand All @@ -378,6 +373,19 @@ def test_validate_observables(self, obsevables, expected):
"""Test obsevables standardization."""
self.assertEqual(validation._validate_observables(obsevables), expected)

@data(
(PauliList("IXYZ"), (SparsePauliOp("IXYZ"),)),
(
[PauliList("IXYZ"), PauliList("ZYXI")],
(SparsePauliOp("IXYZ"), SparsePauliOp("ZYXI")),
),
)
@unpack
def test_validate_observables_deprecated(self, obsevables, expected):
"""Test obsevables standardization."""
with self.assertRaises(DeprecationWarning):
self.assertEqual(validation._validate_observables(obsevables), expected)

@data(None, "ERROR")
def test_qiskit_error(self, observables):
"""Test qiskit error if invalid input."""
Expand Down