Skip to content

Commit

Permalink
Address BackendV1 deprecation warning oversights (#13868)
Browse files Browse the repository at this point in the history
* Add missing deprecation warnings from BackendV1 deprecation. Address tests.

* Raise deprecated input type warnings manually, add stacklevel argument to deprecate_arg.

* Fix typo in deprecation message

* Add filter for obj_id warning triggered by qiskit-aer returning a Result object.

* Assert warnings on visual tests

* Fix lint

* Revert "Add filter for obj_id warning triggered by qiskit-aer returning a Result object."

This reverts commit 8e64ea0.

* Deprecate use of positional arguments in Result. The result class is normally instantiated using from_dict so all arguments are already treated as kwargs. Additionally, warn about qobj_id going away also as a kwarg in 2.0

* Can't count

* Fix typo and add deprecation tests for positional args in Result

* Fix strange deprecation warning trace that made it difficult to filter warnings

* Fix redundant assertions

* Add reno and deprecation warning to result docs

* Fix lint

* Fix ambiguity

* Extend docstring

* Change attributes to args
  • Loading branch information
ElePT authored Feb 20, 2025
1 parent d31c6aa commit 7341628
Show file tree
Hide file tree
Showing 21 changed files with 610 additions and 119 deletions.
17 changes: 17 additions & 0 deletions qiskit/primitives/backend_estimator_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from __future__ import annotations

import math
import warnings
from collections import defaultdict
from collections.abc import Iterable
from dataclasses import dataclass
Expand Down Expand Up @@ -131,12 +132,28 @@ def __init__(
options: dict | None = None,
):
"""
.. deprecated:: 1.4
The method ``BackendEstimatorV2.__init__`` will stop supporting inputs of type
:class:`.BackendV1` in the `backend` parameter in a future release no
earlier than 2.0. :class:`.BackendV1` is deprecated and implementations should
move to :class:`.BackendV2`.
Args:
backend: The backend to run the primitive on.
options: The options to control the default precision (``default_precision``),
the operator grouping (``abelian_grouping``), and
the random seed for the simulator (``seed_simulator``).
"""

if not isinstance(backend, BackendV2):
warnings.warn(
"The method `BackendEstimatorV2.__init__` will stop supporting inputs of "
f"type `BackendV1` ( {backend} ) in the `backend` parameter in a future "
"release no earlier than 2.0. `BackendV1` is deprecated and implementations "
"should move to `BackendV2`.",
category=DeprecationWarning,
stacklevel=2,
)
self._backend = backend
self._options = Options(**options) if options else Options()

Expand Down
15 changes: 15 additions & 0 deletions qiskit/primitives/backend_sampler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,26 @@ def __init__(
options: dict | None = None,
):
"""
.. deprecated:: 1.4
The method ``BackendSamplerV2.__init__`` will stop supporting inputs of type
:class:`.BackendV1` in the `backend` parameter in a future release no
earlier than 2.0. :class:`.BackendV1` is deprecated and implementations should
move to :class:`.BackendV2`.
Args:
backend: The backend to run the primitive on.
options: The options to control the default shots (``default_shots``) and
the random seed for the simulator (``seed_simulator``).
"""
if not isinstance(backend, BackendV2):
warnings.warn(
"The method `BackendSamplerV2.__init__` will stop supporting inputs of "
f"type `BackendV1` ( {backend} ) in the `backend` parameter in a future "
"release no earlier than 2.0. `BackendV1` is deprecated and implementations "
"should move to `BackendV2`.",
category=DeprecationWarning,
stacklevel=2,
)
self._backend = backend
self._options = Options(**options) if options else Options()

Expand Down
34 changes: 23 additions & 11 deletions qiskit/providers/backend_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@
logger = logging.getLogger(__name__)


@deprecate_func(
since="1.4",
removal_timeline="in the 2.0 release",
additional_msg="With the deprecation of `qiskit.providers.models` this utility function "
"is not needed.",
)
@deprecate_pulse_arg("defaults")
def convert_to_target(
configuration: BackendConfiguration,
Expand Down Expand Up @@ -66,9 +60,19 @@ def convert_to_target(
Returns:
A ``Target`` instance.
"""
return _convert_to_target(
configuration, properties, defaults, custom_name_mapping, add_delay, filter_faulty
)
# If a deprecated error is raised during the conversion, we should not return the
# deprecation warning to the user,as it is not actionable for them.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=DeprecationWarning,
message=".*``qiskit.providers.exceptions.BackendPropertyError``",
module="qiskit",
)
target = _convert_to_target(
configuration, properties, defaults, custom_name_mapping, add_delay, filter_faulty
)
return target


def _convert_to_target(
Expand Down Expand Up @@ -323,7 +327,6 @@ def _get_value(prop_dict, prop_name):
def qubit_props_list_from_props(
properties: BackendProperties,
) -> List[QubitProperties]:
# TODO Remove this function with BackendProperties
"""Uses BackendProperties to construct
and return a list of QubitProperties.
"""
Expand Down Expand Up @@ -440,7 +443,16 @@ def target(self):
:rtype: Target
"""
if self._target is None:
# If a deprecated error is raised during the conversion,
# we should not return the deprecation warning to the user,
# as it is not actionable for them.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=DeprecationWarning,
message=".*``qiskit.providers.exceptions.BackendPropertyError``",
module="qiskit",
)
# convert_to_target is deprecated along BackendV2Converter
# They both need to be removed at the same time
warnings.filterwarnings(
Expand All @@ -449,7 +461,7 @@ def target(self):
message=r".+qiskit\.providers\.backend_compat\.convert_to_target.+",
module="qiskit",
)
self._target = convert_to_target(
self._target = _convert_to_target(
configuration=self._config,
properties=self._properties,
defaults=self._defaults,
Expand Down
1 change: 0 additions & 1 deletion qiskit/providers/basic_provider/basic_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ def _run_job(self, job_id: str, qobj: QasmQobj) -> Result:
"time_taken": (end - start),
"header": qobj.header.to_dict(),
}

return Result.from_dict(result)

@deprecate_func(
Expand Down
25 changes: 23 additions & 2 deletions qiskit/providers/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""Exceptions for errors raised while handling Backends and Jobs."""

from qiskit.exceptions import QiskitError
from qiskit.utils import deprecate_func


class JobError(QiskitError):
Expand All @@ -36,10 +37,30 @@ class QiskitBackendNotFoundError(QiskitError):
class BackendPropertyError(QiskitError):
"""Base class for errors raised while looking for a backend property."""

pass
@deprecate_func(
since="1.4",
removal_timeline="in the 2.0 release",
additional_msg="The models in ``qiskit.providers.models`` and related objects are part "
"of the deprecated `BackendV1` workflow, and no longer necessary for `BackendV2`. If a user "
"workflow requires these representations it likely relies on deprecated functionality and "
"should be updated to use `BackendV2`.",
stacklevel=2,
)
def __init__(self, *message):
super().__init__(*message)


class BackendConfigurationError(QiskitError):
"""Base class for errors raised by the BackendConfiguration."""

pass
@deprecate_func(
since="1.4",
removal_timeline="in the 2.0 release",
additional_msg="The models in ``qiskit.providers.models`` and related objects are part "
"of the deprecated `BackendV1` workflow, and no longer necessary for `BackendV2`. If a user "
"workflow requires these representations it likely relies on deprecated functionality and "
"should be updated to use `BackendV2`.",
stacklevel=2,
)
def __init__(self, *message):
super().__init__(*message)
20 changes: 19 additions & 1 deletion qiskit/providers/models/backendproperties.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class Nduv:
value: value.
"""

@deprecate_func(
since="1.4",
removal_timeline="in the 2.0 release",
additional_msg="The models in ``qiskit.providers.models`` and related objects are part "
"of the deprecated `BackendV1` workflow, and no longer necessary for `BackendV2`. If a user "
"workflow requires these representations it likely relies on deprecated functionality and "
"should be updated to use `BackendV2`.",
stacklevel=2,
)
def __init__(self, date, name, unit, value):
"""Initialize a new name-date-unit-value object
Expand Down Expand Up @@ -97,6 +106,15 @@ class GateProperties:

_data = {}

@deprecate_func(
since="1.4",
removal_timeline="in the 2.0 release",
additional_msg="The models in ``qiskit.providers.models`` and related objects are part "
"of the deprecated `BackendV1` workflow, and no longer necessary for `BackendV2`. If a user "
"workflow requires these representations it likely relies on deprecated functionality and "
"should be updated to use `BackendV2`.",
stacklevel=2,
)
def __init__(self, qubits, gate, parameters, **kwargs):
"""Initialize a new :class:`GateProperties` object
Expand Down Expand Up @@ -180,7 +198,7 @@ class BackendProperties:
"of the deprecated `BackendV1` workflow, and no longer necessary for `BackendV2`. If a user "
"workflow requires these representations it likely relies on deprecated functionality and "
"should be updated to use `BackendV2`.",
stacklevel=3,
stacklevel=2,
)
def __init__(
self, backend_name, backend_version, last_update_date, qubits, gates, general, **kwargs
Expand Down
10 changes: 10 additions & 0 deletions qiskit/providers/models/backendstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@

import html
from qiskit.exceptions import QiskitError
from qiskit.utils import deprecate_func


class BackendStatus:
"""Class representing Backend Status."""

@deprecate_func(
since="1.4",
removal_timeline="in the 2.0 release",
additional_msg="The models in ``qiskit.providers.models`` and related objects are part "
"of the deprecated `BackendV1` workflow, and no longer necessary for `BackendV2`. If a user "
"workflow requires these representations it likely relies on deprecated functionality and "
"should be updated to use `BackendV2`.",
stacklevel=2,
)
def __init__(
self,
backend_name: str,
Expand Down
11 changes: 11 additions & 0 deletions qiskit/providers/models/jobstatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

"""Class for job status."""

from qiskit.utils import deprecate_func


class JobStatus:
"""Model for JobStatus.
Expand All @@ -24,6 +26,15 @@ class JobStatus:

_data = {}

@deprecate_func(
since="1.4",
removal_timeline="in the 2.0 release",
additional_msg="The models in ``qiskit.providers.models`` and related objects are part "
"of the deprecated `BackendV1` workflow, and no longer necessary for `BackendV2`. If a user "
"workflow requires these representations it likely relies on deprecated functionality and "
"should be updated to use `BackendV2`.",
stacklevel=2,
)
def __init__(self, job_id, status, status_msg, **kwargs):
self._data = {}
self.job_id = job_id
Expand Down
Loading

0 comments on commit 7341628

Please sign in to comment.