Matrix exponentiation#

Matrix exponentiation backends.

class TensorFlowExpmBackend[source]#

Bases: ExpmBackend

Compute the matrix exponential using TensorFlow. Tends to be faster than scipy. Note that tensorflow is an optional dependency and thus needs to be installed separately. GPU acceleration may be available depending on the underlying hardware. Tends to be faster than SciPyExpmBackend for large matrices and highly parallelized computations.

Note

Recommended backend for fast and reliable matrix exponentiation.

compute(m: ndarray)[source]#

Compute the matrix exponential using TensorFlow.

Parameters:

m (ndarray) – Matrix.

Return type:

ndarray

Returns:

Matrix exponential

class SciPyExpmBackend(precision: ~typing.Literal['np.float32', 'np.float64'] = <class 'numpy.float64'>)[source]#

Bases: ExpmBackend

Compute the matrix exponential using SciPy.

Note

This is the default backend. Recommended for smaller matrices. Consider switching to other backends for larger matrices, such as JaxExpmBackend, which is both efficient and lightweight to install.

__init__(precision: ~typing.Literal['np.float32', 'np.float64'] = <class 'numpy.float64'>)[source]#

Initialize the backend.

Parameters:

precision (Literal['np.float32', 'np.float64']) – Precision of the matrix exponential, defaults to double precision. A lower precision may be faster but much more prone to numerical issues, so please use with caution.

precision#

Precision of the matrix exponential

compute(m: ndarray)[source]#

Compute the matrix exponential using SciPy.

Parameters:

m (ndarray) – Matrix

Return type:

ndarray

Returns:

Matrix exponential

class JaxExpmBackend(max_squarings: int = 1024)[source]#

Bases: ExpmBackend

Compute the matrix exponential using Jax. Note that jax is an optional dependency and thus needs to be installed separately. GPU acceleration may be available depending on the underlying hardware. Tends to be faster than SciPyExpmBackend for larger matrices and highly parallelized computations.

__init__(max_squarings: int = 1024)[source]#

Initialize the backend.

Parameters:

max_squarings (int) – Maximum number of squarings (see jax.scipy.linalg.expm).

max_squarings#

Maximum number of squarings

compute(m: ndarray)[source]#

Compute the matrix exponential using Jax.

Parameters:

m (ndarray) – Matrix

Return type:

ndarray

Returns:

Matrix exponential

class PyTorchExpmBackend[source]#

Bases: ExpmBackend

Compute the matrix exponential using PyTorch. Note that PyTorch is an optional dependency and thus needs to be installed separately. GPU acceleration may be available depending on the underlying hardware.

compute(m: ndarray)[source]#

Compute the matrix exponential using PyTorch.

Parameters:

m (ndarray) – Matrix

Return type:

ndarray

Returns:

Matrix exponential

class Backend[source]#

Bases: ABC

Configure the backend for matrix exponentiation.

backend: ExpmBackend = <phasegen.expm.SciPyExpmBackend object>#

Backend for matrix exponentiation

abstract classmethod expm(m: ndarray)[source]#

Compute the matrix exponential.

Return type:

ndarray

classmethod register(backend: ExpmBackend)[source]#

Register a backend.