Skip to content

Commit

Permalink
remove PauliList support as observable type for Estimator.run (#11521)
Browse files Browse the repository at this point in the history
* remove PauliList support as observable type for Estimator.run

* raise only when PauliList

* adjust test
  • Loading branch information
1ucian0 authored Jan 17, 2024
1 parent 786c0e9 commit de7a0b1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
13 changes: 4 additions & 9 deletions qiskit/primitives/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""
from __future__ import annotations

import warnings
from collections.abc import Iterable

import numpy as np
Expand All @@ -25,6 +24,7 @@
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
from qiskit.exceptions import QiskitError


def init_circuit(state: QuantumCircuit | Statevector) -> QuantumCircuit:
Expand Down Expand Up @@ -54,6 +54,8 @@ def init_observable(observable: BaseOperator | str) -> SparsePauliOp:
Returns:
The observable as :class:`~qiskit.quantum_info.SparsePauliOp`.
Raises:
QiskitError: when observable type cannot be converted to SparsePauliOp.
"""

if isinstance(observable, SparsePauliOp):
Expand All @@ -62,14 +64,7 @@ def init_observable(observable: BaseOperator | str) -> SparsePauliOp:
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,
)
raise QiskitError(f"observable type not supported: {type(observable)}")
return SparsePauliOp(observable)


Expand Down
10 changes: 5 additions & 5 deletions releasenotes/notes/dep-estimator-paulilist-ef2bb2865b66f012.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
deprecations:
upgrade:
- |
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.
Using a :class:`~.PauliList` as an observable
when calling
:meth:`.Estimator.run` is not further supported. Users should instead explicitly convert the argument
using ``SparsePauliOp(pauli_list)`` first (see :class:`~.SparsePauliOp`).
2 changes: 1 addition & 1 deletion test/python/primitives/test_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def test_validate_observables(self, obsevables, expected):
@unpack
def test_validate_observables_deprecated(self, obsevables, expected):
"""Test obsevables standardization."""
with self.assertRaises(DeprecationWarning):
with self.assertRaises(QiskitError):
self.assertEqual(validation._validate_observables(obsevables), expected)

@data(None, "ERROR")
Expand Down

0 comments on commit de7a0b1

Please sign in to comment.