-
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.
Operator collections arraylias integration (#291)
Co-authored-by: Kento Ueda <[email protected]>
- Loading branch information
1 parent
21d4efe
commit da27bf1
Showing
13 changed files
with
921 additions
and
1,227 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
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. | ||
|
||
""" | ||
Registering conjugate. | ||
""" | ||
|
||
|
||
def register_conjugate(alias): | ||
"""Register linear functions for each array library.""" | ||
|
||
try: | ||
from jax.dtypes import canonicalize_dtype | ||
import jax.numpy as jnp | ||
from jax.experimental.sparse import sparsify | ||
|
||
# can be changed to sparsify(jnp.conjugate) when implemented | ||
def conj_workaround(x): | ||
if jnp.issubdtype(x.dtype, canonicalize_dtype(jnp.complex128)): | ||
return x.real - 1j * x.imag | ||
return x | ||
|
||
alias.register_function(func=sparsify(conj_workaround), lib="jax_sparse", path="conjugate") | ||
|
||
except ImportError: | ||
pass |
51 changes: 51 additions & 0 deletions
51
qiskit_dynamics/arraylias/register_functions/linear_combo.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,51 @@ | ||
# -*- 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. | ||
|
||
""" | ||
Registering linear_combo functions to alias. This computes a linear combination of matrices (given | ||
by a 3d array). | ||
""" | ||
|
||
import numpy as np | ||
|
||
|
||
def register_linear_combo(alias): | ||
"""Register linear functions for each array library.""" | ||
|
||
@alias.register_default(path="linear_combo") | ||
def _(coeffs, mats): | ||
return np.tensordot(coeffs, mats, axes=1) | ||
|
||
@alias.register_function(lib="numpy", path="linear_combo") | ||
def _(coeffs, mats): | ||
return np.tensordot(coeffs, mats, axes=1) | ||
|
||
try: | ||
import jax.numpy as jnp | ||
|
||
@alias.register_function(lib="jax", path="linear_combo") | ||
def _(coeffs, mats): | ||
return jnp.tensordot(coeffs, mats, axes=1) | ||
|
||
from jax.experimental.sparse import sparsify | ||
|
||
jsparse_sum = sparsify(jnp.sum) | ||
|
||
@alias.register_function(lib="jax_sparse", path="linear_combo") | ||
def _(coeffs, mats): | ||
# pylint: disable=unexpected-keyword-arg | ||
return jsparse_sum(jnp.broadcast_to(coeffs[:, None, None], mats.shape) * mats, axis=0) | ||
|
||
except ImportError: | ||
pass |
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,31 @@ | ||
# -*- 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. | ||
|
||
""" | ||
Registering transpose. | ||
""" | ||
|
||
|
||
def register_transpose(alias): | ||
"""Register linear functions for each array library.""" | ||
|
||
try: | ||
from jax.experimental.sparse import bcoo_transpose | ||
|
||
@alias.register_function(lib="jax_sparse", path="transpose") | ||
def _(arr, axes=None): | ||
return bcoo_transpose(arr, permutation=axes) | ||
|
||
except ImportError: | ||
pass |
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
Oops, something went wrong.