Skip to content

vectors_from_gram_matrix

Calculates the vectors associated to a Gram matrix.

vectors_from_gram_matrix

vectors_from_gram_matrix(gram: ndarray) -> list[ndarray]

Obtain the corresponding ensemble of states from the Gram matrix 1.

The function attempts to compute the Cholesky decomposition of the given Gram matrix. If the matrix is positive definite, the Cholesky decomposition is returned. If the matrix is not positive definite, the function falls back to eigendecomposition.

Parameters:

  • gram (ndarray) –

    A square, symmetric matrix representing the Gram matrix.

Returns:

  • list[ndarray]

    A list of vectors (np.ndarray) corresponding to the ensemble of states.

======= print(vectors) ```

d965b901 (Fix: Reorder docstring sections in matrix_ops (#1455))

Raises: LinAlgError: If the Gram matrix is not square.

Examples: Example of a positive definite matrix: ```python exec="1" source="above" result="text" import numpy as np from toqito.matrix_ops import vectors_from_gram_matrix

gram_matrix = np.array([[2, -1], [-1, 2]])
vectors = vectors_from_gram_matrix(gram_matrix)

print(vectors)
```

Example of a matrix that is not positive definite:
```python exec="1" source="above" result="text"
import numpy as np
from toqito.matrix_ops import vectors_from_gram_matrix

gram_matrix = np.array([[0, 1], [1, 0]])
vectors = vectors_from_gram_matrix(gram_matrix)

    <<<<<<< HEAD
```

References

1 Wikipedia. Gram Matrix. link.

Source code in toqito/matrix_ops/vectors_from_gram_matrix.py
def vectors_from_gram_matrix(gram: np.ndarray) -> list[np.ndarray]:
    r"""Obtain the corresponding ensemble of states from the Gram matrix [@wikipediagram].

    The function attempts to compute the Cholesky decomposition of the given Gram matrix. If the matrix is positive
    definite, the Cholesky decomposition is returned. If the matrix is not positive definite, the function falls back to
    eigendecomposition.

    Args:
        gram: A square, symmetric matrix representing the Gram matrix.

    Returns:
        A list of vectors (np.ndarray) corresponding to the ensemble of states.
    =======
        print(vectors)
    ```
    >>>>>>> d965b901 (Fix: Reorder docstring sections in matrix_ops (#1455))

    Raises:
        LinAlgError: If the Gram matrix is not square.

    Examples:
        Example of a positive definite matrix:
        ```python exec="1" source="above" result="text"
        import numpy as np
        from toqito.matrix_ops import vectors_from_gram_matrix

        gram_matrix = np.array([[2, -1], [-1, 2]])
        vectors = vectors_from_gram_matrix(gram_matrix)

        print(vectors)
        ```

        Example of a matrix that is not positive definite:
        ```python exec="1" source="above" result="text"
        import numpy as np
        from toqito.matrix_ops import vectors_from_gram_matrix

        gram_matrix = np.array([[0, 1], [1, 0]])
        vectors = vectors_from_gram_matrix(gram_matrix)

            <<<<<<< HEAD
        ```

    """
    dim = gram.shape[0]
    if gram.shape[0] != gram.shape[1]:
        raise np.linalg.LinAlgError("The Gram matrix must be square.")

    # If matrix is PD, can do Cholesky decomposition:
    try:
        decomp = np.linalg.cholesky(gram)
        return [decomp[i][:] for i in range(dim)]
    # Otherwise, need to do eigendecomposition:
    except np.linalg.LinAlgError:
        warnings.warn("Matrix is not positive semidefinite. Using eigendecomposition as alternative.")
        d, v = np.linalg.eig(gram)
        return [scipy.linalg.sqrtm(np.diag(d)) @ v[i].conj().T for i in range(dim)]