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 PrimitivesV1 reference implementations and their utils #12575

Merged
merged 25 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
48e7182
Deprecate V1 Primitives and their utils
LeanderCS Jun 13, 2024
6f7d158
Fix tests
LeanderCS Jun 13, 2024
24c4609
Fix yaml error
LeanderCS Jun 13, 2024
3678783
Fix build
LeanderCS Jun 13, 2024
594b7d9
Merge branch 'main' into deprecate-primitives-v1
LeanderCS Jun 14, 2024
49e35bf
Merge branch 'main' into deprecate-primitives-v1
LeanderCS Jun 22, 2024
9e03a86
Fix error after mc
LeanderCS Jun 23, 2024
c349353
Fix error after mc
LeanderCS Jun 24, 2024
e81e86f
Apply comments
LeanderCS Jun 24, 2024
71ad3fb
Use correct deprecate version for warning message
LeanderCS Jun 25, 2024
620707e
Update deprecation messages
LeanderCS Jul 4, 2024
e3e0f54
Add missed ``
LeanderCS Jul 4, 2024
143960f
update releasenote
t-imamichi Jul 5, 2024
fbc65dd
Merge branch 'main' into deprecate-primitives-v1
t-imamichi Jul 5, 2024
c0103f7
Deprecate SamplerResult and EstimatorResult
LeanderCS Jul 8, 2024
e7094eb
fix deprecation warning for SamplerResult and EstimatorResult
t-imamichi Jul 9, 2024
94486dd
apply review comments
t-imamichi Jul 16, 2024
4ca4f77
Merge branch 'main' into deprecate-primitives-v1
t-imamichi Jul 17, 2024
696a196
Applying the agreement of deprecations.
t-imamichi Jul 24, 2024
0b66c6a
revert SamplerResult, EstimatorResult, and BasePrimitiveResult
t-imamichi Jul 24, 2024
17c34b4
fix test_backend_sampler
t-imamichi Jul 24, 2024
593e751
revert tox.ini
t-imamichi Jul 24, 2024
02a288f
revise deprecation warning for BaseSampler and BaseEstimator
t-imamichi Jul 25, 2024
4de78e2
update reno
t-imamichi Jul 25, 2024
3704f35
revert BaseSampler and BaseEstimator
t-imamichi Jul 25, 2024
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
2 changes: 2 additions & 0 deletions qiskit/primitives/backend_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
Optimize1qGatesDecomposition,
SetLayout,
)
from qiskit.utils.deprecation import deprecate_func

from .base import BaseEstimator, EstimatorResult
from .primitive_job import PrimitiveJob
Expand Down Expand Up @@ -104,6 +105,7 @@ class BackendEstimator(BaseEstimator[PrimitiveJob[EstimatorResult]]):
precludes doing any provider- or backend-specific optimizations.
"""

@deprecate_func(since="1.2", additional_msg="Use BackendEstimatorV2 instead.")
def __init__(
self,
backend: BackendV1 | BackendV2,
Expand Down
2 changes: 2 additions & 0 deletions qiskit/primitives/backend_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from qiskit.providers.options import Options
from qiskit.result import QuasiDistribution, Result
from qiskit.transpiler.passmanager import PassManager
from qiskit.utils.deprecation import deprecate_func

from .backend_estimator import _prepare_counts, _run_circuits
from .base import BaseSampler, SamplerResult
Expand All @@ -46,6 +47,7 @@ class BackendSampler(BaseSampler[PrimitiveJob[SamplerResult]]):
precludes doing any provider- or backend-specific optimizations.
"""

@deprecate_func(since="1.2", additional_msg="Use BackendSamplerV2 instead.")
def __init__(
self,
backend: BackendV1 | BackendV2,
Expand Down
24 changes: 22 additions & 2 deletions qiskit/primitives/base/base_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

r"""Base Estimator Classes"""
"""Base Estimator Classes"""

from __future__ import annotations

Expand All @@ -23,6 +23,7 @@
from qiskit.providers import JobV1 as Job
from qiskit.quantum_info.operators import SparsePauliOp
from qiskit.quantum_info.operators.base_operator import BaseOperator
from qiskit.utils.deprecation import deprecate_func

from ..containers import (
DataBin,
Expand Down Expand Up @@ -187,7 +188,26 @@ def _run(
raise NotImplementedError("The subclass of BaseEstimator must implement `_run` method.")


BaseEstimator = BaseEstimatorV1
class BaseEstimator(BaseEstimatorV1[T]):
"""DEPRECATED. Type alias of Estimator V1 base class.

See :class:`.BaseEstimatorV1` for details.
"""

@deprecate_func(since="1.2", additional_msg="Use BaseEstimatorV2 instead.")
def __init__(
self,
*,
options: dict | None = None,
):
"""
Creating an instance of an Estimator, or using one in a ``with`` context opens a session that
holds resources until the instance is ``close()`` ed or the context is exited.

Args:
options: Default options.
"""
super().__init__(options=options)


class BaseEstimatorV2(ABC):
Expand Down
4 changes: 2 additions & 2 deletions qiskit/primitives/base/base_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Primitive abstract base class."""
"""Primitive V1 abstract base class."""

from __future__ import annotations

Expand All @@ -20,7 +20,7 @@


class BasePrimitive(ABC):
"""Primitive abstract base class."""
"""Primitive V1 abstract base class."""

def __init__(self, options: dict | None = None):
self._run_options = Options()
Expand Down
4 changes: 2 additions & 2 deletions qiskit/primitives/base/base_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
Primitive result abstract base class
Primitive V1 result abstract base class
"""

from __future__ import annotations
Expand All @@ -27,7 +27,7 @@

class _BasePrimitiveResult(ABC):
"""
Base class for deprecated Primitive result methods.
Base class for deprecated Primitive V1 result methods.
"""

def __post_init__(self) -> None:
Expand Down
19 changes: 18 additions & 1 deletion qiskit/primitives/base/base_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from qiskit.circuit import QuantumCircuit
from qiskit.providers import JobV1 as Job
from qiskit.utils.deprecation import deprecate_func

from ..containers.primitive_result import PrimitiveResult
from ..containers.sampler_pub import SamplerPubLike
Expand Down Expand Up @@ -150,7 +151,23 @@ def _run(
raise NotImplementedError("The subclass of BaseSampler must implement `_run` method.")


BaseSampler = BaseSamplerV1
class BaseSampler(BaseSamplerV1[T]):
"""DEPRECATED. Type alias of Sampler V1 base class

See :class:`.BaseSamplerV1` for details.
"""

@deprecate_func(since="1.2", additional_msg="Use BaseSamplerV2 instead.")
def __init__(
self,
*,
options: dict | None = None,
):
"""
Args:
options: Default options.
"""
super().__init__(options=options)


class BaseSamplerV2(ABC):
Expand Down
4 changes: 2 additions & 2 deletions qiskit/primitives/base/estimator_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
Estimator result class
Estimator V1 result class
"""

from __future__ import annotations
Expand All @@ -26,7 +26,7 @@

@dataclass(frozen=True)
class EstimatorResult(_BasePrimitiveResult):
"""Result of Estimator.
"""Result of Estimator V1.

.. code-block:: python

Expand Down
4 changes: 2 additions & 2 deletions qiskit/primitives/base/sampler_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
Sampler result class
Sampler V1 result class
"""

from __future__ import annotations
Expand All @@ -25,7 +25,7 @@

@dataclass(frozen=True)
class SamplerResult(_BasePrimitiveResult):
"""Result of Sampler.
"""Result of Sampler V1.

.. code-block:: python

Expand Down
2 changes: 2 additions & 0 deletions qiskit/primitives/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from qiskit.exceptions import QiskitError
from qiskit.quantum_info import Statevector
from qiskit.quantum_info.operators.base_operator import BaseOperator
from qiskit.utils.deprecation import deprecate_func

from .base import BaseEstimator, EstimatorResult
from .primitive_job import PrimitiveJob
Expand Down Expand Up @@ -51,6 +52,7 @@ class Estimator(BaseEstimator[PrimitiveJob[EstimatorResult]]):
this option is ignored.
"""

@deprecate_func(since="1.2", additional_msg="Use StatevectorEstimator instead.")
def __init__(self, *, options: dict | None = None):
"""
Args:
Expand Down
2 changes: 2 additions & 0 deletions qiskit/primitives/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from qiskit.exceptions import QiskitError
from qiskit.quantum_info import Statevector
from qiskit.result import QuasiDistribution
from qiskit.utils.deprecation import deprecate_func

from .base import BaseSampler, SamplerResult
from .primitive_job import PrimitiveJob
Expand Down Expand Up @@ -52,6 +53,7 @@ class Sampler(BaseSampler[PrimitiveJob[SamplerResult]]):
option is ignored.
"""

@deprecate_func(since="1.2", additional_msg="Use StatevectorSampler instead.")
def __init__(self, *, options: dict | None = None):
"""
Args:
Expand Down
16 changes: 16 additions & 0 deletions qiskit/primitives/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@
from qiskit.quantum_info import PauliList, SparsePauliOp, Statevector
from qiskit.quantum_info.operators.base_operator import BaseOperator
from qiskit.quantum_info.operators.symplectic.base_pauli import BasePauli
from qiskit.utils.deprecation import deprecate_func


@deprecate_func(
since="1.2",
additional_msg="To initialize a circuit from a ``Statevector`` instance, "
+ "use ``QuantumCircuit.initialize`` instead.",
)
def init_circuit(state: QuantumCircuit | Statevector) -> QuantumCircuit:
"""Initialize state by converting the input to a quantum circuit.

Expand All @@ -45,6 +51,10 @@ def init_circuit(state: QuantumCircuit | Statevector) -> QuantumCircuit:
return qc


@deprecate_func(
since="1.2",
additional_msg="Use the constructor of ``SparsePauliOp`` instead.",
)
def init_observable(observable: BaseOperator | str) -> SparsePauliOp:
"""Initialize observable by converting the input to a :class:`~qiskit.quantum_info.SparsePauliOp`.

Expand All @@ -68,6 +78,12 @@ def init_observable(observable: BaseOperator | str) -> SparsePauliOp:
return SparsePauliOp(observable)


@deprecate_func(
since="1.2",
additional_msg="Use ``QuantumCircuit.layout`` and ``SparsePauliOp.apply_layout`` "
+ "to adjust an operator for a layout. Otherwise, use ``mthree.utils.final_measurement_mapping``. "
+ "See https://qiskit-extensions.github.io/mthree/apidocs/utils.html for details.",
)
def final_measurement_mapping(circuit: QuantumCircuit) -> dict[int, int]:
"""Return the final measurement mapping for the circuit.

Expand Down
26 changes: 26 additions & 0 deletions releasenotes/notes/deprecate-primitives-v1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
deprecations_primitives:
- |
Primitives V1 is now deprecated and will be removed in no less than 3 months from the release date.

The following Primitives V1 classes are deprecated:

* :class:`.BaseEstimator`, use :class:`.BaseEstimatorV2` instead,
* :class:`.BaseSampler`, use :class:`.BaseSamplerV2` instead,
* :class:`.Estimator`, use :class:`.StatevectorEstimator` instead,
* :class:`.Sampler`, use :class:`.StatevectorSampler` instead,
* :class:`.BackendEstimator`, use :class:`.BackendEstimatorV2` instead,
* :class:`.BackendSampler`, use :class:`.BackendSamplerV2` instead,


In addition, the following utility functions are deprecated:

* :func:`.init_circuit`, to initialize a circuit from a :class:`.Statevector`,
use :meth:`.QuantumCircuit.initialize` instead,
* :func:`.init_observable`, use the constructor of :class:`.SparsePauliOp` instead,
* :func:`.final_measurement_mapping`, use :meth:`.QuantumCircuit.layout` and
:meth:`.SparsePauliOp.apply_layout` to adjust an operator for a layout.
Otherwise, use ``mthree.utils.final_measurement_mapping``.
See `Mthree Utility functions <https://qiskit-extensions.github.io/mthree/apidocs/utils.html>`__
for details.

Loading
Loading