qalgebra.core.matrix_algebra module

Matrices of Expressions.

Summary

Classes:

Matrix

Matrix of Expressions.

Functions:

block_matrix

Generate the operator matrix with quadrants

diagm

Generalizes the diagonal matrix creation capabilities of numpy.diag to Matrix objects.

hstackm

Generalizes numpy.hstack to Matrix objects.

identity_matrix

Generate the N-dimensional identity matrix.

vstackm

Generalizes numpy.vstack to Matrix objects.

zerosm

Generalizes numpy.zeros to Matrix objects.

__all__: Matrix, block_matrix, diagm, hstackm, identity_matrix, vstackm, zerosm

Reference

class qalgebra.core.matrix_algebra.Matrix(m)[source]

Bases: object

Matrix of Expressions.

matrix = None
property shape

The shape of the matrix (nrows, ncols).

property block_structure

For square matrices this gives the block (-diagonal) structure of the matrix as a tuple of integers that sum up to the full dimension.

Return type

tuple

property is_zero

Are all elements of the matrix zero?

transpose()[source]

The transpose matrix

conjugate()[source]

The element-wise conjugate matrix.

This is defined only if all the entries in the matrix have a defined conjugate (i.e., they have a conjugate method). This is not the case for a matrix of operators. In such a case, only an elementwise adjoint() would be applicable, but this is mathematically different from a complex conjugate.

Raises

NoConjugateMatrix – if any entries have no conjugate method

property real

Element-wise real part.

Raises

NoConjugateMatrix – if entries have no conjugate method and no other way to determine the real part

Note

A mathematically equivalent way to obtain a real matrix from a complex matrix M is:

(M.conjugate() + M) / 2

However, the result may not be identical to M.real, as the latter tries to convert elements of the matrix to real values directly, if possible, and only uses the conjugate as a fall-back

property imag

Element-wise imaginary part.

Raises

NoConjugateMatrix – if entries have no conjugate method and no other way to determine the imaginary part

Note

A mathematically equivalent way to obtain an imaginary matrix from a complex matrix M is:

(M.conjugate() - M) / (I * 2)

with same same caveats as real.

property T

Alias for transpose().

adjoint()[source]

Adjoint of the matrix.

This is the transpose and the Hermitian adjoint of all elements.

dag()

Adjoint of the matrix.

This is the transpose and the Hermitian adjoint of all elements.

trace()[source]
property H

Alias for adjoint().

element_wise(func, *args, **kwargs)[source]

Apply a function to each matrix element and return the result in a new operator matrix of the same shape.

Parameters
  • func (callable) – A function to be applied to each element. It must take the element as its first argument.

  • args – Additional positional arguments to be passed to func

  • kwargs – Additional keyword arguments to be passed to func

Returns

Matrix with results of func, applied element-wise.

Return type

Matrix

series_expand(param, about, order)[source]

Expand the matrix expression as a truncated power series in a scalar parameter.

Parameters
  • param (Symbol) – Expansion parameter.

  • about (Scalar) – Point about which to expand.

  • order (int) – Maximum order of expansion >= 0

Returns

tuple of length (order+1), where the entries are the expansion coefficients.

expand()[source]

Expand each matrix element distributively.

Returns

Expanded matrix.

Return type

Matrix

substitute(var_map)[source]

Perform a substitution in all element of the matrix.

Equivalent to applying substitute() element-wise.

Returns

Matrix with substitutions

Return type

Matrix

property free_symbols

Free symbols, across all elements.

property space

Combined Hilbert space of all matrix elements.

If none of the elements have an associated hilbert space, TrivialSpace.

simplify_scalar(func=<function simplify>)[source]

Simplify all scalar expressions appearing in the Matrix.

qalgebra.core.matrix_algebra.block_matrix(A, B, C, D)[source]

Generate the operator matrix with quadrants

\[\begin{split}\begin{pmatrix} A B \\ C D \end{pmatrix}\end{split}\]
Parameters
  • A (Matrix) – Matrix of shape (n, m)

  • B (Matrix) – Matrix of shape (n, k)

  • C (Matrix) – Matrix of shape (l, m)

  • D (Matrix) – Matrix of shape (l, k)

Returns

The combined block matrix [[A, B], [C, D]].

Return type

Matrix

qalgebra.core.matrix_algebra.diagm(v, k=0)[source]

Generalizes the diagonal matrix creation capabilities of numpy.diag to Matrix objects.

qalgebra.core.matrix_algebra.hstackm(matrices)[source]

Generalizes numpy.hstack to Matrix objects.

qalgebra.core.matrix_algebra.identity_matrix(N)[source]

Generate the N-dimensional identity matrix.

Parameters

N (int) – Dimension

Returns

Identity matrix in N dimensions

Return type

Matrix

qalgebra.core.matrix_algebra.vstackm(matrices)[source]

Generalizes numpy.vstack to Matrix objects.

qalgebra.core.matrix_algebra.zerosm(shape, *args, **kwargs)[source]

Generalizes numpy.zeros to Matrix objects.