Skip to content

is_commuting

Checks if the matrix is commuting.

is_commuting

is_commuting(
    mat_1: ndarray,
    mat_2: ndarray,
    rtol: float = 1e-05,
    atol: float = 1e-08,
) -> bool

Determine if two linear operators commute with each other 1.

For any pair of operators \(X, Y \in \text{L}(\mathcal{X})\), the Lie bracket \(\left[X, Y\right] \in \text{L}(\mathcal{X})\) is defined as

\[ \left[X, Y\right] = XY - YX. \]

It holds that \(\left[X,Y\right]=0\) if and only if \(X\) and \(Y\) commute (Section: Lie Brackets And Commutants from 2).

Parameters:

  • mat_1 (ndarray) –

    First matrix to check.

  • mat_2 (ndarray) –

    Second matrix to check.

  • rtol (float, default: 1e-05 ) –

    The relative tolerance parameter (default 1e-05).

  • atol (float, default: 1e-08 ) –

    The absolute tolerance parameter (default 1e-08).

Returns:

  • bool

    Return True if mat_1 commutes with mat_2 and False otherwise.

Examples:

Consider the following matrices:

\[ A = \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix}, \quad \text{and} \quad B = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}. \]

It holds that \(AB=0\), however

\[ BA = \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix} = A, \]

and hence, do not commute.

import numpy as np
from toqito.matrix_props import is_commuting

mat_1 = np.array([[0, 1], [0, 0]])
mat_2 = np.array([[1, 0], [0, 0]])

print(is_commuting(mat_1, mat_2))
False

Consider the following pair of matrices:

\[ A = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 1 & 0 & 2 \end{pmatrix} \quad \text{and} \quad B = \begin{pmatrix} 2 & 4 & 0 \\ 3 & 1 & 0 \\ -1 & -4 & 1 \end{pmatrix}. \]

It may be verified that \(AB = BA = 0\), and therefore \(A\) and \(B\) commute.

import numpy as np
from toqito.matrix_props import is_commuting

mat_1 = np.array([[1, 0, 0], [0, 1, 0], [1, 0, 2]])
mat_2 = np.array([[2, 4, 0], [3, 1, 0], [-1, -4, 1]])

print(is_commuting(mat_1, mat_2))
True

References

1 Wikipedia. Commuting matrices. link.
2 Watrous, John. The Theory of Quantum Information. (2018). doi:10.1017/9781316848142.

Source code in toqito/matrix_props/is_commuting.py
def is_commuting(mat_1: np.ndarray, mat_2: np.ndarray, rtol: float = 1e-05, atol: float = 1e-08) -> bool:
    r"""Determine if two linear operators commute with each other [@wikipediacommuting].

    For any pair of operators \(X, Y \in \text{L}(\mathcal{X})\), the
    Lie bracket \(\left[X, Y\right] \in \text{L}(\mathcal{X})\) is defined
    as

    \[
        \left[X, Y\right] = XY - YX.
    \]

    It holds that \(\left[X,Y\right]=0\) if and only if \(X\) and
    \(Y\) commute (Section: Lie Brackets And Commutants from [@watrous2018theory]).

    Args:
        mat_1: First matrix to check.
        mat_2: Second matrix to check.
        rtol: The relative tolerance parameter (default 1e-05).
        atol: The absolute tolerance parameter (default 1e-08).

    Returns:
        Return `True` if `mat_1` commutes with `mat_2` and False otherwise.

    Examples:
        Consider the following matrices:

        \[
            A = \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix},
            \quad \text{and} \quad
            B = \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix}.
        \]

        It holds that \(AB=0\), however

        \[
            BA = \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix} = A,
        \]

        and hence, do not commute.

        ```python exec="1" source="above" result="text"
        import numpy as np
        from toqito.matrix_props import is_commuting

        mat_1 = np.array([[0, 1], [0, 0]])
        mat_2 = np.array([[1, 0], [0, 0]])

        print(is_commuting(mat_1, mat_2))
        ```

        Consider the following pair of matrices:

        \[
            A = \begin{pmatrix}
                1 & 0 & 0 \\
                0 & 1 & 0 \\
                1 & 0 & 2
                \end{pmatrix} \quad \text{and} \quad
            B = \begin{pmatrix}
                2 & 4 & 0 \\
                3 & 1 & 0 \\
                -1 & -4 & 1
                \end{pmatrix}.
        \]

        It may be verified that \(AB = BA = 0\), and therefore \(A\) and
        \(B\) commute.

        ```python exec="1" source="above" result="text"
        import numpy as np
        from toqito.matrix_props import is_commuting

        mat_1 = np.array([[1, 0, 0], [0, 1, 0], [1, 0, 2]])
        mat_2 = np.array([[2, 4, 0], [3, 1, 0], [-1, -4, 1]])

        print(is_commuting(mat_1, mat_2))
        ```

    """
    return bool(np.allclose(mat_1 @ mat_2 - mat_2 @ mat_1, 0, rtol=rtol, atol=atol))