From 75df28616ba029138756c15f5f16c02d84085c5a Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Tue, 9 Jan 2024 14:00:18 +0100 Subject: [PATCH 1/3] remove PauliList support as observable type for Estimator.run --- qiskit/primitives/utils.py | 17 +++++------------ ...ep-estimator-paulilist-ef2bb2865b66f012.yaml | 10 +++++----- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/qiskit/primitives/utils.py b/qiskit/primitives/utils.py index 75d25abbbe30..187bb8414664 100644 --- a/qiskit/primitives/utils.py +++ b/qiskit/primitives/utils.py @@ -14,7 +14,6 @@ """ from __future__ import annotations -import warnings from collections.abc import Iterable import numpy as np @@ -22,9 +21,10 @@ from qiskit.circuit import Instruction, QuantumCircuit from qiskit.circuit.bit import Bit from qiskit.circuit.library.data_preparation import Initialize -from qiskit.quantum_info import SparsePauliOp, Statevector, PauliList +from qiskit.quantum_info import SparsePauliOp, Statevector 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: @@ -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): @@ -61,16 +63,7 @@ def init_observable(observable: BaseOperator | str) -> SparsePauliOp: 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) + raise QiskitError(f"observable type not supported: {type(observable)}") def final_measurement_mapping(circuit: QuantumCircuit) -> dict[int, int]: diff --git a/releasenotes/notes/dep-estimator-paulilist-ef2bb2865b66f012.yaml b/releasenotes/notes/dep-estimator-paulilist-ef2bb2865b66f012.yaml index 4fad2f28e073..adbf1f5f9da0 100644 --- a/releasenotes/notes/dep-estimator-paulilist-ef2bb2865b66f012.yaml +++ b/releasenotes/notes/dep-estimator-paulilist-ef2bb2865b66f012.yaml @@ -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`). From b5a260dd528209a7cf0f12b15f4730383e064854 Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Wed, 10 Jan 2024 11:26:45 +0100 Subject: [PATCH 2/3] raise only when PauliList --- qiskit/primitives/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/qiskit/primitives/utils.py b/qiskit/primitives/utils.py index 187bb8414664..be4b932ec768 100644 --- a/qiskit/primitives/utils.py +++ b/qiskit/primitives/utils.py @@ -21,7 +21,7 @@ from qiskit.circuit import Instruction, 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 from qiskit.exceptions import QiskitError @@ -63,7 +63,9 @@ def init_observable(observable: BaseOperator | str) -> SparsePauliOp: elif isinstance(observable, BaseOperator) and not isinstance(observable, BasePauli): return SparsePauliOp.from_operator(observable) else: - raise QiskitError(f"observable type not supported: {type(observable)}") + if isinstance(observable, PauliList): + raise QiskitError(f"observable type not supported: {type(observable)}") + return SparsePauliOp(observable) def final_measurement_mapping(circuit: QuantumCircuit) -> dict[int, int]: From 80688549175fb84b3226d1b5a4eefbd9d24082d5 Mon Sep 17 00:00:00 2001 From: Luciano Bello Date: Wed, 10 Jan 2024 11:34:07 +0100 Subject: [PATCH 3/3] adjust test --- test/python/primitives/test_estimator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/python/primitives/test_estimator.py b/test/python/primitives/test_estimator.py index 7a190cbe0034..efa7c3856bb2 100644 --- a/test/python/primitives/test_estimator.py +++ b/test/python/primitives/test_estimator.py @@ -377,7 +377,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")