-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into rotating_frame
- Loading branch information
Showing
7 changed files
with
241 additions
and
9 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
53 changes: 53 additions & 0 deletions
53
test/dynamics/arraylias/register_functions/test_asarray.py
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,53 @@ | ||
# -*- 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. | ||
|
||
""" | ||
Test asarray functions | ||
""" | ||
import unittest | ||
import numpy as np | ||
from scipy.sparse import csr_matrix | ||
from qiskit.quantum_info.operators import Operator | ||
|
||
|
||
from qiskit_dynamics import DYNAMICS_NUMPY_ALIAS | ||
from qiskit_dynamics import DYNAMICS_NUMPY as unp | ||
|
||
|
||
class TestAsarrayFunction(unittest.TestCase): | ||
"""Test cases for asarray functions registered in dynamics_numpy_alias.""" | ||
|
||
def test_register_default(self): | ||
"""Test register_default.""" | ||
arr = Operator.from_label("X") | ||
self.assertTrue(isinstance(unp.asarray(arr), np.ndarray)) | ||
|
||
def test_scipy_sparse(self): | ||
"""Test asarray for scipy_sparse.""" | ||
arr = np.array([[1, 0], [0, 1]]) | ||
sparse_arr = csr_matrix([[1, 0], [0, 1]]) | ||
self.assertTrue(isinstance(unp.asarray(sparse_arr), csr_matrix)) | ||
self.assertTrue(isinstance(DYNAMICS_NUMPY_ALIAS(like=sparse_arr).asarray(arr), csr_matrix)) | ||
|
||
def test_jax_sparse(self): | ||
"""Test asarray for jax_sparse.""" | ||
try: | ||
from jax.experimental.sparse import BCOO | ||
|
||
arr = np.array([[1, 0], [0, 1]]) | ||
sparse_arr = BCOO.fromdense([[1, 0], [0, 1]]) | ||
self.assertTrue(isinstance(unp.asarray(sparse_arr), BCOO)) | ||
self.assertTrue(isinstance(DYNAMICS_NUMPY_ALIAS(like=sparse_arr).asarray(arr), BCOO)) | ||
except ImportError as err: | ||
raise unittest.SkipTest("Skipping jax tests.") from err |
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,46 @@ | ||
# -*- 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. | ||
|
||
""" | ||
Test matmul functions | ||
""" | ||
import unittest | ||
import numpy as np | ||
from scipy.sparse import csr_matrix | ||
|
||
from qiskit_dynamics import DYNAMICS_NUMPY as unp | ||
from ...common import QiskitDynamicsTestCase | ||
|
||
|
||
class TestMatmulFunction(QiskitDynamicsTestCase): | ||
"""Test cases for matmul functions registered in dynamics_numpy_alias.""" | ||
|
||
def test_scipy_sparse(self): | ||
"""Test matmul for scipy_sparse.""" | ||
x = csr_matrix([[1, 0], [0, 1]]) | ||
y = csr_matrix([[2, 2], [2, 2]]) | ||
self.assertTrue(isinstance(unp.matmul(x, y), csr_matrix)) | ||
self.assertAllClose(csr_matrix.toarray(unp.matmul(x, y)), [[2, 2], [2, 2]]) | ||
|
||
def test_jax_sparse(self): | ||
"""Test matmul for jax_sparse.""" | ||
try: | ||
from jax.experimental.sparse import BCOO | ||
|
||
x = BCOO.fromdense([[1, 0], [0, 1]]) | ||
y = BCOO.fromdense([[2, 2], [2, 2]]) | ||
self.assertTrue(isinstance(unp.matmul(x, y), BCOO)) | ||
self.assertAllClose(BCOO.todense(unp.matmul(x, y)), [[2, 2], [2, 2]]) | ||
except ImportError as err: | ||
raise unittest.SkipTest("Skipping jax tests.") from err |
53 changes: 53 additions & 0 deletions
53
test/dynamics/arraylias/register_functions/test_multiply.py
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,53 @@ | ||
# -*- 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. | ||
|
||
""" | ||
Test multiply functions | ||
""" | ||
import unittest | ||
import numpy as np | ||
from scipy.sparse import csr_matrix | ||
|
||
from qiskit_dynamics import DYNAMICS_NUMPY as unp | ||
from ...common import QiskitDynamicsTestCase | ||
|
||
|
||
class TestMultiplyFunction(QiskitDynamicsTestCase): | ||
"""Test cases for multiply functions registered in dynamics_numpy_alias.""" | ||
|
||
def test_register_fallback(self): | ||
"""Test register_fallback.""" | ||
x = np.array([1, 0]) | ||
y = np.array([1, 0]) | ||
self.assertTrue(isinstance(unp.multiply(x, y), np.ndarray)) | ||
self.assertAllClose(unp.multiply(x, y), [1, 0]) | ||
|
||
def test_scipy_sparse(self): | ||
"""Test multiply for scipy_sparse.""" | ||
x = csr_matrix([[1, 0], [0, 1]]) | ||
y = csr_matrix([[2, 2], [2, 2]]) | ||
self.assertTrue(isinstance(unp.multiply(x, y), csr_matrix)) | ||
self.assertAllClose(csr_matrix.toarray(unp.multiply(x, y)), [[2, 0], [0, 2]]) | ||
|
||
def test_jax_sparse(self): | ||
"""Test multiply for jax_sparse.""" | ||
try: | ||
from jax.experimental.sparse import BCOO | ||
|
||
x = BCOO.fromdense([[1, 0], [0, 1]]) | ||
y = BCOO.fromdense([[2, 2], [2, 2]]) | ||
self.assertTrue(isinstance(unp.multiply(x, y), BCOO)) | ||
self.assertAllClose(BCOO.todense(unp.multiply(x, y)), [[2, 0], [0, 2]]) | ||
except ImportError as err: | ||
raise unittest.SkipTest("Skipping jax tests.") from err |
65 changes: 65 additions & 0 deletions
65
test/dynamics/arraylias/register_functions/test_rmatmul.py
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,65 @@ | ||
# -*- 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. | ||
|
||
""" | ||
Test rmatmul functions | ||
""" | ||
import unittest | ||
import numpy as np | ||
from scipy.sparse import csr_matrix | ||
|
||
from qiskit_dynamics import DYNAMICS_NUMPY as unp | ||
from ...common import QiskitDynamicsTestCase | ||
|
||
|
||
class TestRmatmulFunction(QiskitDynamicsTestCase): | ||
"""Test cases for rmatmul functions registered in dynamics_numpy_alias.""" | ||
|
||
def test_numpy(self): | ||
"""Test rmatmul for numpy.""" | ||
x = np.array([[1, 1], [1, 1]]) | ||
y = np.array([[1, 2], [3, 4]]) | ||
self.assertTrue(isinstance(unp.rmatmul(x, y), np.ndarray)) | ||
self.assertAllClose(unp.rmatmul(x, y), [[3, 3], [7, 7]]) | ||
|
||
def test_scipy_sparse(self): | ||
"""Test rmatmul for scipy_sparse.""" | ||
x = csr_matrix([[1, 1], [1, 1]]) | ||
y = csr_matrix([[1, 2], [3, 4]]) | ||
self.assertTrue(isinstance(unp.rmatmul(x, y), csr_matrix)) | ||
self.assertAllClose(csr_matrix.toarray(unp.rmatmul(x, y)), [[3, 3], [7, 7]]) | ||
|
||
def test_jax(self): | ||
"""Test rmatmul for jax.""" | ||
try: | ||
import jax.numpy as jnp | ||
|
||
x = jnp.array([[1, 1], [1, 1]]) | ||
y = jnp.array([[1, 2], [3, 4]]) | ||
self.assertTrue(isinstance(unp.rmatmul(x, y), jnp.ndarray)) | ||
self.assertAllClose(unp.rmatmul(x, y), [[3, 3], [7, 7]]) | ||
except ImportError as err: | ||
raise unittest.SkipTest("Skipping jax tests.") from err | ||
|
||
def test_jax_sparse(self): | ||
"""Test rmatmul for jax_sparse.""" | ||
try: | ||
from jax.experimental.sparse import BCOO | ||
|
||
x = BCOO.fromdense([[1, 1], [1, 1]]) | ||
y = BCOO.fromdense([[1, 2], [3, 4]]) | ||
self.assertTrue(isinstance(unp.rmatmul(x, y), BCOO)) | ||
self.assertAllClose(BCOO.todense(unp.rmatmul(x, y)), [[3, 3], [7, 7]]) | ||
except ImportError as err: | ||
raise unittest.SkipTest("Skipping jax tests.") from err |
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