-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global instances of NumPy/SciPy aliases (#260)
Co-authored-by: Kento Ueda <[email protected]>
- Loading branch information
1 parent
a2f4902
commit 842d735
Showing
9 changed files
with
333 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.