Skip to content

Commit

Permalink
Deprecate PauliList estimator observables (#11520)
Browse files Browse the repository at this point in the history
* Deprecate PauliList estimator observables (#11055)

* Deprecate PauliList estimator observables

Deprecates using a `PauliList` as an observable that is implicitly converted to a `SparsePauliOp` with coefficients 1 when calling `Estimator.run`. Users should instead explicitly convert the argument using `SparsePauliOp(pauli_list)` first.

* Revert algorithms_test_case.py

* Update test/python/algorithms/algorithms_test_case.py

Co-authored-by: Takashi Imamichi <[email protected]>

* Update test/python/primitives/test_estimator.py

---------

Co-authored-by: Takashi Imamichi <[email protected]>
Co-authored-by: ikkoham <[email protected]>
Co-authored-by: Christopher J. Wood <[email protected]>

* documentation

* typo in warning msg

* adapt test

---------

Co-authored-by: Takashi Imamichi <[email protected]>
Co-authored-by: ikkoham <[email protected]>
Co-authored-by: Christopher J. Wood <[email protected]>
  • Loading branch information
4 people authored Jan 23, 2024
1 parent 38d4de1 commit 1c4a4de
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
6 changes: 6 additions & 0 deletions qiskit/primitives/base/base_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ def run(
values = parameter_values[i].
.. deprecated:: 0.46.0
Implicit conversion from a ``PauliList`` to a ``SparsePauliOp`` with ``coeffs=1`` in
the ``observables`` arguments is deprecated as of Qiskit 0.46 and will be removed
in Qiskit 1.0. You should explicitly convert to a ``SparsePauli`` using
``SparsePauliOp(pauli_list)`` to avoid this warning.
Args:
circuits: one or more circuit objects.
observables: one or more observable objects. Several formats are allowed;
Expand Down
11 changes: 10 additions & 1 deletion qiskit/primitives/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,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 @@ -97,6 +97,15 @@ def init_observable(observable: BaseOperator | PauliSumOp | str) -> SparsePauliO
)
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"
" removed 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 @@ -366,7 +366,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 @@ -377,10 +376,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 @@ -395,6 +390,19 @@ def test_validate_observables(self, obsevables, expected):
"""Test obsevables standardization."""
self.assertEqual(BaseEstimator._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(BaseEstimator._validate_observables(obsevables), expected)

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

0 comments on commit 1c4a4de

Please sign in to comment.