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 2 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.
10 changes: 9 additions & 1 deletion test/python/algorithms/algorithms_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@

"""Algorithms Test Case"""

import warnings
ikkoham marked this conversation as resolved.
Show resolved Hide resolved
from qiskit.test import QiskitTestCase


class QiskitAlgorithmsTestCase(QiskitTestCase):
"""Algorithms test Case"""

pass
@classmethod
def setUpClass(cls):
super().setUpClass()
allow_dep_warning_message = [
r"Implicit conversion from a PauliList to a SparsePauliOp*",
]
for msg in allow_dep_warning_message:
warnings.filterwarnings("default", category=DeprecationWarning, message=msg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Can we add a comment explaining why this needs to be a warning skip and the tests/library cannot be fixed, including the criteria for when the skip can be removed? It could also do with a module argument setting to limit the skip to precisely the allowed code, not allowed in general in all code.
  2. This skip is never torn down again but affects the global state of the warnings module, so it actually applies to all test cases, because qiskit.algorithms tests run first alphabetically.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the algorithms will be removed, how about merging this PR after that?
#11086

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point 1 is still important because Algorithms isn't disappearing completely, it's just in a separate package. We should know how much work it is that we're asking downstream packages to do to manage a deprecation, and if the only option is a warning skip (though I don't think that is the case here), that's an indication that there are use cases that the new form cannot supply. It's a good thing that this warning was triggered before Algorithms moved out and we'd have seen it too late.

The warnings and errors aren't there to be annoying, they're there to ensure that the effect of deprecations on consumers is properly accounted for, and skipping a warning that we emitted is very rarely the correct action.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriseclectic I don't recall any algorithm/test in qiskit.algorithms explicitly using PauliList, but I guess that this warning filter was introduced after some tests raised warnings. Can you point out which tests did? This would help address Jake's first point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected that used around opflow, but not explicitly by algorithms. Searching here shows PauliList in 8 test files, one in opflow, nothing explicit in algorithms. Code use in primitives, visualization and of course quantum_info modules. Searching qiskit_algorithms repo shows no occurrence of PauliList at all

Copy link
Member

@t-imamichi t-imamichi Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran the test locally and found that no algorithm tests raise the deprecation warning due to PauliList. The deprecation warning is raised by only test.python.primitives.test_estimator.TestObservableValidation.test_validate_observables_deprecated_{1,2} as expected. So, I don't think filterwarnings is necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I ran the test locally and all tests passed.

18 changes: 13 additions & 5 deletions test/python/primitives/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,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 @@ -359,10 +358,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 @@ -377,6 +372,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)
ikkoham marked this conversation as resolved.
Show resolved Hide resolved

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