Skip to content

Commit

Permalink
Add global instances of NumPy/SciPy aliases (#260)
Browse files Browse the repository at this point in the history
Co-authored-by: Kento Ueda <[email protected]>
  • Loading branch information
DanPuzzuoli and to24toro authored Oct 11, 2023
1 parent a2f4902 commit 842d735
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 37 deletions.
7 changes: 7 additions & 0 deletions qiskit_dynamics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
"""
from .version import __version__

from .arraylias.alias import (
DYNAMICS_NUMPY_ALIAS,
DYNAMICS_SCIPY_ALIAS,
DYNAMICS_NUMPY,
DYNAMICS_SCIPY,
)

from .models.rotating_frame import RotatingFrame

from .signals.signals import Signal, DiscreteSignal
Expand Down
25 changes: 25 additions & 0 deletions qiskit_dynamics/arraylias/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# 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.

r"""
=========================================
Models (:mod:`qiskit_dynamics.arraylias`)
=========================================
.. currentmodule:: qiskit_dynamics.arraylias
Module for Qiskit Dynamics global NumPy and SciPy aliases.
"""

from .alias import DYNAMICS_NUMPY_ALIAS, DYNAMICS_SCIPY_ALIAS, DYNAMICS_NUMPY, DYNAMICS_SCIPY
37 changes: 37 additions & 0 deletions qiskit_dynamics/arraylias/alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# 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.

"""
Global alias instances.
"""

from typing import Union

from arraylias import numpy_alias, scipy_alias

from qiskit_dynamics.array import Array

# global NumPy and SciPy aliases
DYNAMICS_NUMPY_ALIAS = numpy_alias()
DYNAMICS_SCIPY_ALIAS = scipy_alias()

DYNAMICS_NUMPY_ALIAS.register_type(Array, "numpy")
DYNAMICS_SCIPY_ALIAS.register_type(Array, "numpy")


DYNAMICS_NUMPY = DYNAMICS_NUMPY_ALIAS()
DYNAMICS_SCIPY = DYNAMICS_SCIPY_ALIAS()


ArrayLike = Union[DYNAMICS_NUMPY_ALIAS.registered_types()]
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"matplotlib>=3.0",
"qiskit-terra>=0.23.0",
"multiset>=3.0.1",
"sympy>=1.12"
"sympy>=1.12",
"arraylias"
]

jax_extras = ['jax>=0.4.0, <= 0.4.6',
Expand Down
15 changes: 15 additions & 0 deletions test/dynamics/arraylias/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# 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.

"""
Dynamics arraylias module tests.
"""
54 changes: 54 additions & 0 deletions test/dynamics/arraylias/test_alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# 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,no-member

"""
Test global alias instances.
"""

from functools import partial

import numpy as np
import scipy as sp

from qiskit_dynamics import DYNAMICS_NUMPY as unp
from qiskit_dynamics import DYNAMICS_SCIPY as usp

from ..common import QiskitDynamicsTestCase, test_array_backends


@partial(test_array_backends, array_libraries=["numpy", "jax", "array_numpy", "array_jax"])
class TestDynamicsNumpy(QiskitDynamicsTestCase):
"""Test cases for global numpy configuration."""

def test_simple_case(self):
"""Validate correct type and output."""
a = self.asarray([1.0, 2.0, 3.0])
output = unp.exp(a)
self.assertArrayType(output)

expected = np.exp(np.array([1.0, 2.0, 3.0]))
self.assertAllClose(output, expected)


@test_array_backends
class TestDynamicsScipy(QiskitDynamicsTestCase):
"""Test cases for global scipy configuration."""

def test_simple_case(self):
"""Validate correct type and output."""
a = self.asarray([1.0, 2.0, 3.0])
output = usp.fft.dct(a)
self.assertArrayType(output)

expected = sp.fft.dct(np.array([1.0, 2.0, 3.0]))
self.assertAllClose(output, expected)
Loading

0 comments on commit 842d735

Please sign in to comment.