Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random orthonormal basis in matrix_props #821

Merged
merged 5 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/refs.bib
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,15 @@ @misc{Russo_2017_Extended
}

#Last name begins with S

@misc{SE_1688950,
author = "Stack Exchange Mathematics",
title = "Why do the columns of a unitary matrix form an orthonormal basis?",
howpublished = {https://math.stackexchange.com/q/1688950}

}


@misc{Seshadri_2021_Git,
author = "Seshadri, Akshay",
title = "Minimax Fidelity Estimation",
Expand Down
3 changes: 2 additions & 1 deletion toqito/matrix_props/is_orthonormal.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ def is_orthonormal(vectors: list[np.ndarray]) -> bool:
:return: True if vectors are orthonormal; False otherwise.

"""
return is_mutually_orthogonal(vectors) and np.allclose(np.dot(vectors, vectors.T), np.eye(vectors.shape[0]))
return is_mutually_orthogonal(vectors) and np.allclose(np.dot(vectors, np.conjugate(vectors).T), np.eye(
vectors.shape[0]))
1 change: 1 addition & 0 deletions toqito/rand/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from toqito.rand.random_state_vector import random_state_vector
from toqito.rand.random_states import random_states
from toqito.rand.random_circulant_gram_matrix import random_circulant_gram_matrix
from toqito.rand.random_orthonormal_basis import random_orthonormal_basis
40 changes: 40 additions & 0 deletions toqito/rand/random_orthonormal_basis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Generate random orthonormal basis."""
import numpy as np

from toqito.matrix_props import is_orthonormal
from toqito.rand import random_unitary


def random_orthonormal_basis(dim: int, is_real: bool = False) -> list[np.ndarray]:
r"""Generate a real random orthonormal basis of given dimension :math:`d`.

The basis is generated from the columns of a random unitary matrix of the same dimension
as the columns of a unitary matrix typically form an orthonormal basis :cite:`SE_1688950`.

Examples
==========
To generate a random orthonormal basis of dimension :math:`4`,

>>> from toqito.rand import random_orthonormal_basis
>>> random_orthonormal_basis(4, is_real = True) # doctest: +SKIP
[array([0.52188745, 0.4983613 , 0.69049811, 0.04981832]),
array([-0.48670459, 0.58756912, -0.10226756, 0.63829658]),
array([ 0.23965404, -0.58538248, 0.187136 , 0.75158061]),
array([ 0.658269 , 0.25243989, -0.69118291, 0.158815 ])]

References
==========
.. bibliography::
:filter: docname in docnames

dim: int
Number of elements in the random orthonormal basis.

"""
random_mat = random_unitary(dim, is_real)

rand_orth_basis = [random_mat[:, i] for i in range(dim)]

return rand_orth_basis


17 changes: 17 additions & 0 deletions toqito/rand/tests/test_random_orthonormal_basis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Tests for random orthonormal basis."""

import numpy as np
import pytest

from toqito.matrix_props import is_orthonormal
from toqito.rand import random_orthonormal_basis


@pytest.mark.parametrize("input_dim", range(2, 5))
@pytest.mark.parametrize("bool", [False, True])
def test_random_orth_basis_int_dim(input_dim, bool):
"""Test function works as expected for a valid int input."""
gen_basis = random_orthonormal_basis(dim=input_dim, is_real = bool)
assert len(gen_basis) == input_dim
assert is_orthonormal(np.array(gen_basis))