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 and their utils #11490

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 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 @@ -102,6 +103,7 @@ class BackendEstimator(BaseEstimator[PrimitiveJob[EstimatorResult]]):
precludes doing any provider- or backend-specific optimizations.
"""

@deprecate_func(since="0.46.0", additional_msg="Use BackendEstimatorV2 instead.")
def __init__(
self,
backend: BackendV1 | BackendV2,
Expand Down Expand Up @@ -236,7 +238,6 @@ def _call(
parameter_values: Sequence[Sequence[float]],
**run_options,
) -> EstimatorResult:

# Transpile
self._grouping = list(zip(circuits, observables))
transpiled_circuits = self.transpiled_circuits
Expand Down Expand Up @@ -368,7 +369,6 @@ def _postprocessing(
shots_list = []

for i, j in zip(accum, accum[1:]):

combined_expval = 0.0
combined_var = 0.0

Expand Down
3 changes: 2 additions & 1 deletion 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="0.46.0", additional_msg="Use BackendSamplerV2 instead.")
def __init__(
self,
backend: BackendV1 | BackendV2,
Expand Down Expand Up @@ -135,7 +137,6 @@ def _call(
parameter_values: Sequence[Sequence[float]],
**run_options,
) -> SamplerResult:

# This line does the actual transpilation
transpiled_circuits = self.transpiled_circuits
bound_circuits = [
Expand Down
5 changes: 3 additions & 2 deletions qiskit/primitives/base/base_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@
from copy import copy
from typing import Generic, TypeVar

from qiskit.utils.deprecation import deprecate_func
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.parametertable import ParameterView
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 .base_primitive import BasePrimitive
from . import validation
from .base_primitive import BasePrimitive

T = TypeVar("T", bound=Job)

Expand All @@ -107,6 +107,7 @@ class BaseEstimator(BasePrimitive, Generic[T]):

__hash__ = None

@deprecate_func(since="0.46.0", additional_msg="Use BaseEstimatorV2 instead.")
def __init__(
self,
*,
Expand Down
1 change: 1 addition & 0 deletions qiskit/primitives/base/base_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
class BasePrimitive(ABC):
"""Primitive abstract base class."""

@deprecate_func(since="0.46.0", additional_msg="Use BasePrimitiveV2 instead.")
def __init__(self, options: dict | None = None):
self._run_options = Options()
if options is not None:
Expand Down
8 changes: 7 additions & 1 deletion qiskit/primitives/base/base_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
from collections.abc import Iterator, Sequence
from dataclasses import fields
from typing import Any, Dict
from warnings import warn

from numpy import ndarray


ExperimentData = Dict[str, Any]


Expand All @@ -45,6 +45,12 @@ def __post_init__(self) -> None:
TypeError: If one of the data fields is not a Sequence or ``numpy.ndarray``.
ValueError: Inconsistent number of experiments across data fields.
"""
warn(
"The class ``BasePrimitiveResult`` is deprecated as of qiskit 0.46.0. "
"It will be removed no earlier than 3 months after the release date. "
"Use PrimitiveResult in PrimitiveV2 instead.",
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would help to show the path of the new PrimitiveResult class. From #11227 I take that would be:

Suggested change
"Use PrimitiveResult in PrimitiveV2 instead.",
"Use PrimitiveResult class in `qiskit.primitives.containers` instead.",

DeprecationWarning,
)
for value in self._field_values: # type: Sequence
# TODO: enforce all data fields to be tuples instead of sequences
if not isinstance(value, (Sequence, ndarray)) or isinstance(value, (str, bytes)):
Expand Down
1 change: 1 addition & 0 deletions qiskit/primitives/base/base_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class BaseSampler(BasePrimitive, Generic[T]):

__hash__ = None

@deprecate_func(since="0.46.0", additional_msg="Use BaseSamplerV2 instead.")
def __init__(
self,
*,
Expand Down
9 changes: 3 additions & 6 deletions qiskit/primitives/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@
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
from .utils import (
_circuit_key,
_observable_key,
bound_circuit_to_instruction,
init_observable,
)
from .utils import _circuit_key, _observable_key, bound_circuit_to_instruction, init_observable


class Estimator(BaseEstimator[PrimitiveJob[EstimatorResult]]):
Expand All @@ -51,6 +47,7 @@ class Estimator(BaseEstimator[PrimitiveJob[EstimatorResult]]):
this option is ignored.
"""

@deprecate_func(since="0.46.0", additional_msg="Use StatevectorEstimator instead.")
Copy link
Contributor

Choose a reason for hiding this comment

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

In the estimator V2 PR, it looks like the new class is also called Estimator, right? not StatevectorEstimator. But it can be told apart from the V1 estimator by the import path.

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="0.46.0", additional_msg="Use StatevectorSampler instead.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as with the Estimator.

def __init__(self, *, options: dict | None = None):
"""
Args:
Expand Down
6 changes: 5 additions & 1 deletion qiskit/primitives/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
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 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="0.46.0")
Copy link
Contributor

Choose a reason for hiding this comment

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

If the deprecation is with no replacement, we usually add a small message explaining it.

def init_circuit(state: QuantumCircuit | Statevector) -> QuantumCircuit:
"""Initialize state by converting the input to a quantum circuit.

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


@deprecate_func(since="0.46.0")
def init_observable(observable: BaseOperator | str) -> SparsePauliOp:
"""Initialize observable by converting the input to a :class:`~qiskit.quantum_info.SparsePauliOp`.

Expand Down Expand Up @@ -73,6 +76,7 @@ def init_observable(observable: BaseOperator | str) -> SparsePauliOp:
return SparsePauliOp(observable)


@deprecate_func(since="0.46.0")
def final_measurement_mapping(circuit: QuantumCircuit) -> dict[int, int]:
"""Return the final measurement mapping for the circuit.

Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/deprecate-v1-3cdeb826e55f98cb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
deprecations:
- |
PrimitivesV1 has been deprecated. Use PrimitivesV2 instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if people would understand what "PrimitivesV1" are, adding a list of what the deprecated classes are would help.

Loading