toqito icon indicating copy to clipboard operation
toqito copied to clipboard

Feature: Generate random linearly independent set of vectors

Open vprusso opened this issue 8 months ago • 9 comments

This function is often useful for certain cases of quantum state distinguishability where the states being considered have the property of being linearly independent.

Something like the following could be adapted:

def generate_random_independent_vectors(num_vectors: np.ndarray, dim: int, is_real: bool = True) -> list[np.ndarray]:
    r"""Generates a set of linearly independent random vectors.

    This function generates a random collection of linearly independent (possibly complex) vectors.

    Examples
    ==========
    (TODO)

    :param num_vectors: The number of vectors to generate.
    :param dim: The dimension of the vector space.
    :param is_real: Boolean denoting whether the returned vector will have all real entries or not.
                    Default is :code:`False`.
    :param seed: A seed used to instantiate numpy's random number generator.
    :return: A (dim x num_vectors) matrix whose columns are the generated independent vectors.

    """
    if num_vectors > dim:
        raise ValueError("Cannot have more independent vectors than the dimension of the space.")
    
    # Keep generating until we get a matrix with independent columns.
    while True:
        if is_real:
            # Generate a random real matrix.
            A = np.random.randn(dim, num_vectors)
        else:
            # Generate a random complex matrix: real + i*imag.
            A = np.random.randn(dim, num_vectors) + 1j * np.random.randn(dim, num_vectors)

        # Check that the rank equals num_vectors.
        if np.linalg.matrix_rank(A) == num_vectors:
            return A

The test cases should using toqito.matrix_props.is_linearly_independent to check to ensure that the randomly generated vectors are indeed linearly independent. It should also ensure that hitting the num_vectors > dim branch causes a raise for the ValueError.

This function should be placed in the rand/ module along with corresponding unit tests in the rand/tests/ directory. Testing coverage of this function should be 100% and we should include examples in the docstring.

vprusso avatar Apr 13 '25 15:04 vprusso

I thought we already had an issue for this. Did it get closed accidentally? I can't find it.

purva-thakre avatar Apr 13 '25 17:04 purva-thakre

Maybe you're thinking of https://github.com/vprusso/toqito/issues/142 (which is a "check for" instead of "generation of"). Other than that, I don't see any existing open or previously closed issues on this.

vprusso avatar Apr 13 '25 18:04 vprusso

You're correct! I confused myself with the check issue. Thanks!

purva-thakre avatar Apr 13 '25 19:04 purva-thakre

Hi @vprusso ! I was hoping to contribute to an OS software project, and was interested in your work in this repo. I read your contribution guide, but wasn't sure how issues get claimed. Can I be assigned to this issue?

Elizabetht1 avatar Apr 14 '25 20:04 Elizabetht1

@Elizabetht1 You simply ask an issue to be assigned to you. :)

purva-thakre avatar Apr 14 '25 20:04 purva-thakre

Hi @Elizabetht1

Are you still planning on working on this issue?

vprusso avatar Apr 28 '25 11:04 vprusso

Hi! I am – planning on finishing next week (May 5th). if you need it done sooner, feel free to assign to someone else

Elizabetht1 avatar Apr 28 '25 22:04 Elizabetht1

@Elizabetht1 of course, no rush! Thank you for the heads up, just wanted to ensure you weren't blocked!

vprusso avatar Apr 29 '25 02:04 vprusso

thanks! appreciate the clarification :)

Elizabetht1 avatar Apr 29 '25 14:04 Elizabetht1