filter_functions.basis module

This module defines the Basis class, a subclass of NumPy’s ndarray, to represent operator bases.

Classes

Basis

The operator basis as an array of shape (d**2, d, d) with d the dimension of the Hilbert space

Functions

normalize()

Function to normalize a Basis instance

expand()

Function to expand an array of operators in a given basis

ggm_expand()

Fast function to expand an array of operators in a Generalized Gell-Mann basis

class Basis(basis_array: Sequence, traceless: bool | None = None, btype: str | None = None, labels: Sequence[str] | None = None)[source]

Bases: ndarray

Class for operator bases. There are several ways to instantiate a Basis object:

  • by just calling this constructor with a (possibly incomplete) array of basis matrices. No checks regarding orthonormality or hermiticity are performed.

  • by calling one of the classes alternative constructors (classmethods):

    • pauli(): Pauli operator basis

    • ggm(): Generalized Gell-Mann basis

    • from_partial() Generate an complete basis from partial elements

    These bases guarantee the following properties:

    • hermitian

    • orthonormal

    • [traceless] (can be controlled by a flag)

Since Basis is a subclass of NumPy’s ndarray, it inherits all of its attributes, e.g. shape. The following attributes behave slightly differently to a ndarray, however

  • A == B is True if all elements evaluate almost equal, i.e. equivalent to np.allclose(A, B).

  • basis.T transposes the last two axes of basis. For a full basis, this corresponds to transposing each element individually. For a basis element, it corresponds to normal transposition.

Parameters:
basis_array: array_like, shape (n, d, d)

An array or list of square matrices that are elements of an operator basis spanning \(\mathbb{C}^{d\times d}\). n should be smaller than or equal to d**2.

traceless: bool, optional (default: auto)

Controls whether a traceless basis is forced. Here, traceless means that the first element of the basis is the identity and the remaining elements are matrices of trace zero. If an element of basis_array is neither traceless nor the identity and traceless == True, an exception will be raised. Defaults to True if basis_array is traceless and False if not.

btype: str, optional (default: ``’custom’``)

A string describing the basis type. For example, a basis created by the factory method pauli() has btype ‘pauli’.

labels: sequence of str, optional

A list of labels for the individual basis elements. Defaults to ‘C_0’, ‘C_1’, …

Attributes:
Other than the attributes inherited from ``ndarray``, a ``Basis``
instance has the following attributes:
btype: str

Basis type.

labels: sequence of str

The labels for the basis elements.

d: int

Dimension of the space spanned by the basis.

H: Basis

Hermitian conjugate.

isherm: bool

If the basis is hermitian.

isorthonorm: bool

If the basis is orthonormal.

istraceless: bool

If the basis is traceless except for an identity element

iscomplete: bool

If the basis is complete, ie spans the full space.

sparse: COO, shape (n, d, d)

Representation in the COO format supplied by the sparse package.

four_element_traces: COO, shape (n, n, n, n)

Traces over all possible combinations of four elements of self. This is required for the calculation of the error transfer matrix and thus cached in the Basis instance.

Most of the attributes above are properties which are lazily
evaluated and cached.

Methods

Other than the methods inherited from ``ndarray``, a ``Basis``

instance has the following methods:

normalize(b)

Normalizes the basis (used internally when creating a basis from elements)

tidyup(eps_scale=None)

Cleans up floating point errors in-place to make zeros actual zeros. eps_scale is an optional argument multiplied to the data type’s eps to get the absolute tolerance.

Constructor.

property H: Basis

Return the basis hermitian conjugated element-wise.

property T: Basis

Return the basis transposed element-wise.

expand(M: ndarray, hermitian: bool = False, traceless: bool = False, tidyup: bool = False) ndarray[source]

Expand matrices M in this basis.

Parameters:
M: array_like

The square matrix (d, d) or array of square matrices (…, d, d) to be expanded in basis

hermitian: bool (default: False)

If M is hermitian along its last two axes, the result will be real.

tidyup: bool {False}

Whether to set values below the floating point eps to zero.

See also

expand

The function corresponding to this method.

property four_element_traces: COO

Return all traces of the form \(\mathrm{tr}(C_i C_j C_k C_l)\) as a sparse COO array for \(i,j,k,l > 0\) (i.e. excluding the identity).

classmethod from_partial(partial_basis_array: Sequence, traceless: bool | None = None, btype: str | None = None, labels: Sequence[str] | None = None) Basis[source]

Generate complete and orthonormal basis from a partial set.

The basis is completed using singular value decomposition to determine the null space of the expansion coefficients of the partial basis with respect to another complete basis.

Parameters:
partial_basis_array: array_like

A sequence of basis elements.

traceless: bool, optional

If a traceless basis should be generated (i.e. the first element is the identity and all the others have trace zero).

btype: str, optional

A custom identifier.

labels: Sequence[str], optional

A list of custom labels for each element. If len(labels) == len(partial_basis_array), the newly created elements get labels ‘C_i’.

Returns:
basis: Basis, shape (d**2, d, d)

The orthonormal basis.

Raises:
ValueError

If the given elements are not orthonormal.

ValueError

If the given elements are not traceless but traceless==True.

ValueError

If not len(partial_basis_array) or d**2 labels were given.

classmethod ggm(d: int) Basis[source]

Returns a generalized Gell-Mann basis in \(d\) dimensions [Bert08] where the elements \(\Lambda_i\) are normalized with respect to the Hilbert-Schmidt inner product,

\[\begin{split}\langle\Lambda_i,\Lambda_j\rangle &= \mathrm{Tr}\,\Lambda_i^\dagger\Lambda_j \\ &= \delta_{ij}.\end{split}\]
Parameters:
d: int

The dimensionality of the space spanned by the basis

Returns:
basis: Basis

The Basis object representing the GGM.

References

[Bert08]

Bertlmann, R. A., & Krammer, P. (2008). Bloch vectors for qudits. Journal of Physics A: Mathematical and Theoretical, 41(23). https://doi.org/10.1088/1751-8113/41/23/235303

property iscomplete: bool

Returns True if basis is complete.

property isherm: bool

Returns True if all basis elements are hermitian.

property isnorm: bool

Returns True if all basis elements are normalized.

property isorthogonal: bool

Returns True if all basis elements are mutually orthogonal.

property isorthonorm: bool

Returns True if basis is orthonormal.

property istraceless: bool

Returns True if basis is traceless except for possibly the identity.

normalize(copy: bool = False) None | Basis[source]

Normalize the basis.

classmethod pauli(n: int) Basis[source]

Returns a Pauli basis for \(n\) qubits, i.e. the basis spans the space \(\mathbb{C}^{d\times d}\) with \(d = 2^n\):

\[\mathcal{P} = \{I, X, Y, Z\}^{\otimes n}.\]

The elements \(\sigma_i\) are normalized with respect to the Hilbert-Schmidt inner product,

\[\begin{split}\langle\sigma_i,\sigma_j\rangle &= \mathrm{Tr}\,\sigma_i^\dagger\sigma_j \\ &= \delta_{ij}.\end{split}\]
Parameters:
n: int

The number of qubits.

Returns:
basis: Basis

The Basis object representing the Pauli basis.

property sparse: COO

Return the basis as a sparse COO array

tidyup(eps_scale: float | None = None) None[source]

Wraps util.remove_float_errors.

expand(M: ndarray | Basis, basis: ndarray | Basis, normalized: bool = True, hermitian: bool = False, tidyup: bool = False) ndarray[source]

Expand the array M in the basis given by basis.

Parameters:
M: array_like

The square matrix (d, d) or array of square matrices (…, d, d) to be expanded in basis

basis: array_like

The basis of shape (m, d, d) in which to expand.

normalized: bool {True}

Wether the basis is normalized.

hermitian: bool (default: False)

If M is hermitian along its last two axes, the result will be real.

tidyup: bool {False}

Whether to set values below the floating point eps to zero.

Returns:
coefficients: ndarray

The coefficient array with shape (…, m) or (m,) if M was 2-d

Notes

For an orthogonal matrix basis \(\mathcal{C} = \big\{C_k\in \mathbb{C}^{d\times d}: \langle C_k,C_l\rangle_\mathrm{HS} \propto \delta_{kl}\big\}_{k=0}^{d^2-1}\) with the Hilbert-Schmidt inner product as implemented by dot_HS() and \(M\in\mathbb{C}^{d\times d}\), the expansion of \(M\) in terms of \(\mathcal{C}\) is given by

\[\begin{split}M &= \sum_j c_j C_j, \\ c_j &= \frac{\mathrm{tr}\big(M C_j\big)} {\mathrm{tr}\big(C_j^\dagger C_j\big)}.\end{split}\]
ggm_expand(M: ndarray | Basis, traceless: bool = False, hermitian: bool = False, tidyup: bool = False) ndarray[source]

Expand the matrix M in a Generalized Gell-Mann basis [Bert08]. This function makes use of the explicit construction prescription of the basis and thus makes do without computing the expansion coefficients as the overlap between the matrix and each basis element.

Parameters:
M: array_like

The square matrix (d, d) or array of square matrices (…, d, d) to be expanded in a GGM basis.

traceless: bool (default: False)

Include the basis element proportional to the identity in the expansion. If it is known beforehand that M is traceless, the corresponding coefficient is zero and thus doesn’t need to be computed.

hermitian: bool (default: False)

If M is hermitian along its last two axes, the result will be real.

tidyup: bool {False}

Whether to set values below the floating point eps to zero.

Returns:
coefficients: ndarray

The real coefficient array with shape (d**2,) or (…, d**2)

References

[Bert08]

Bertlmann, R. A., & Krammer, P. (2008). Bloch vectors for qudits. Journal of Physics A: Mathematical and Theoretical, 41(23). https://doi.org/10.1088/1751-8113/41/23/235303

normalize(b: Basis) Basis[source]

Return a copy of the basis b normalized with respect to the Frobenius norm [Gol85]:

\(||A||_F = \left[\sum_{i,j} |a_{i,j}|^2\right]^{1/2}\)

or equivalently, with respect to the Hilbert-Schmidt inner product as implemented by dot_HS().

References

[Gol85]

G. H. Golub and C. F. Van Loan, Matrix Computations, Baltimore, MD, Johns Hopkins University Press, 1985, pg. 15