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 Array class #343

Merged
merged 6 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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: 0 additions & 4 deletions docs/tutorials/optimizing_pulse_sequence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,6 @@ entry on :ref:`JAX-compatible pulse schedules <how-to use pulse schedules for ja

.. jupyter-execute::

# how to get rid of this?
from qiskit_dynamics.array import Array
Array.set_default_backend("jax")

import sympy as sym
from qiskit import pulse

Expand Down
17 changes: 17 additions & 0 deletions qiskit_dynamics/array/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@

.. currentmodule:: qiskit_dynamics.array


.. warning::

The ``array`` and ``dispatch`` submodules of Qiskit Dynamics have been deprecated as of version
0.5.0. The use of the ``Array`` class is no longer required to work with different array
libraries in Qiskit Dynamics, and is broken in some cases. Refer to the user guide entry on
using different array libraries with Qiskit Dynamics. Users can now work directly with the
supported array type of their choice, without the need to wrap them to enable dispatching. The
``array`` and ``dispatch`` submodules will be removed in version 0.6.0.

This module contains an :class:`Array` class that wraps N-dimensional array objects from different
libraries. It enables working with different array libraries through a common NumPy-based interface,
along with other functionality for writing array-library agnostic code.
Expand All @@ -41,6 +51,13 @@
Basic Usage
-----------

.. jupyter-execute::
:hide-code:

# suppress deprecation warnings
import warnings
warnings.simplefilter('ignore', category=DeprecationWarning)

When using the default ``numpy`` backend :class:`Array`, objects can be used interchangably with
``numpy.ndarray``. When ``numpy`` functions are applied to an :class:`Array` object the return type
will be an :class:`Array` instead of an ``numpy.ndarray``.
Expand Down
29 changes: 27 additions & 2 deletions qiskit_dynamics/array/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
# pylint: disable=invalid-name,global-statement

"""Array Class"""

import warnings
import copy
from functools import wraps
from types import BuiltinMethodType, MethodType
Expand All @@ -25,9 +27,29 @@

__all__ = ["Array"]

_deprecation_raised = False


def _array_deprecation_warn():
global _deprecation_raised
if not _deprecation_raised:
warnings.warn(
"""The array and dispatch submodules of Qiskit Dynamics have been deprecated as of
version 0.5.0. The use of the Array class is no longer required to work with different
array libraries in Qiskit Dynamics, and is broken in some cases. Refer to the user guide
entry on using different array libraries with Qiskit Dynamics. Users can now work
directly with the supported array type of their choice, without the need to wrap them to
enable dispatching. The array and dispatch submodules will be removed in version 0.6.0.
""",
DeprecationWarning,
stacklevel=3,
)
_deprecation_raised = True


class Array(NDArrayOperatorsMixin):
"""Qiskit array class.
"""Qiskit array class. This class is deprecated as of version 0.5.0 and will be removed in
version 0.6.0.

This class provides a Numpy compatible wrapper to supported Python array libraries. Supported
backends are ``"numpy"`` and ``"jax"``.
Expand Down Expand Up @@ -55,7 +77,7 @@ def __init__(
Raises:
ValueError: If input cannot be converted to an :class:`Array`.
"""

_array_deprecation_warn()
# Check if we can override setattr and
# set _data and _backend directly
if (
Expand Down Expand Up @@ -108,18 +130,21 @@ def backend(self, value: str):
@classmethod
def set_default_backend(cls, backend: Union[str, None]):
"""Set the default array backend."""
_array_deprecation_warn()
if backend is not None:
Dispatch.validate_backend(backend)
Dispatch.DEFAULT_BACKEND = backend

@classmethod
def default_backend(cls) -> str:
"""Return the default array backend."""
_array_deprecation_warn()
return Dispatch.DEFAULT_BACKEND

@classmethod
def available_backends(cls) -> Set[str]:
"""Return a tuple of available array backends."""
_array_deprecation_warn()
return Dispatch.REGISTERED_BACKENDS

def __repr__(self):
Expand Down
9 changes: 9 additions & 0 deletions qiskit_dynamics/arraylias/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@

.. currentmodule:: qiskit_dynamics.arraylias

.. warning::

The ``array`` and ``dispatch`` submodules of Qiskit Dynamics have been deprecated as of version
0.5.0. The use of the ``Array`` class is no longer required to work with different array
libraries in Qiskit Dynamics, and is broken in some cases. Refer to the user guide entry on
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible add any link to this userguide?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes thanks, good idea. I've just added the link to this text.

Also this comment made me realize I accidentally put this warning in the arraylias submodule docs, as opposed to the dispatch submodule docs :D. I've also corrected this.

using different array libraries with Qiskit Dynamics. Users can now work directly with the
supported array type of their choice, without the need to wrap them to enable dispatching. The
``array`` and ``dispatch`` submodules will be removed in version 0.6.0.

This module contains Qiskit Dynamics-global extensions of the default NumPy and SciPy aliases
provided by `Arraylias <https://qiskit-extensions.github.io/arraylias/>`_. These are used to manage
dispatching of array operations for the different array types supported by Qiskit Dynamics. They
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
upgrade:
- |
The ``array`` and ``dispatch`` submodules of Qiskit Dynamics have been deprecated as of version
0.5.0. The use of the ``Array`` class is no longer required to work with different array
libraries in Qiskit Dynamics, and is broken in some cases. Refer to the user guide entry on
using different array libraries with Qiskit Dynamics. Users can now work directly with the
supported array type of their choice, without the need to wrap them to enable dispatching. The
``array`` and ``dispatch`` submodules will be removed in version 0.6.0.
Loading