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

Added a function to generate random positive semi definite operator. #1013

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ShanmukhaKumar13
Copy link

Description

Closes #652

Changes

  • Added the function random_psd_operator that generates random positive semi definite operator.
  • Added test_random_psd_operator

@ShanmukhaKumar13
Copy link
Author

@vprusso This PR implements the function to generate a random positive semidefinite operator and is ready for a review. Additionally, I have an issue with the documentation. In the example section the matrix shape is deformed. Please assist me in rectifying it.

===========================
Generate a positive semi-definite operator of dimension 3.

>>> dim = 3
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example should showcase the use of the function. Not a redefinition of the whole thing.

>>> random_mat = random_mat / np.trace(random_mat)
>>> random_mat

[[0.35636099 0.17920518 0.14620245]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the matrices are not formatting well for you, refer to any other function that uses matrices in toqito for reference.

.. bibliography::
:filter: docname in docnames

:param dim (int): The dimension of the operator.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types provided in the type-hinting args, so don't need to state these here.


:return (np.ndarray): A random positive semi-definite operator with dimensions dim x dim.

:raises ValueError: If dim is not a positive integer.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does a value get raised? This comment is incorrect.


A positive semi-definite operator is a Hermitian operator that has only real and non-negative eigenvalues.

This function generates a random positive semi-definite operator by constructing a symmetric matrix,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring needs to be written more clearly. For instance "eigenvalues" (not "eigen values"), statements like "The resultant of the function ..." could be worded more clearly, "creates a symmetry matrix" (it's not a "symmetry matrix" it's a "symmetric matrix", etc.

import numpy as np

def random_psd_operator(dim: int) -> np.ndarray:
r''' Generate a random positive semi definite operator.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use """ not '''

import numpy as np

def random_psd_operator(dim: int) -> np.ndarray:
r''' Generate a random positive semi definite operator.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"semidefinite" not "semi definite"


'''

random_mat = np.random.rand(dim, dim)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way this function is written is difficult to follow. Something more concise could be:

    mat = (np.random.rand(dim, dim) + np.random.rand(dim, dim).T) / 2
    vals, vecs = np.linalg.eigh(mat)
    mat = vecs @ np.diag(np.abs(vals)) @ vecs.T
    return mat / np.trace(mat)

assert_equal(rand_psd_operator.shape, (dim, dim))

# Check if the matrix is a valid density matrix
assert is_density(rand_psd_operator), "Matrix should be a valid density matrix"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does a random PSD matrix need to have trace 1? This would only be true if we were generating a random density matrix.

@ShanmukhaKumar13
Copy link
Author

ShanmukhaKumar13 commented Mar 9, 2025

Thank you for your feedback. I will review and address the suggested changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate random positive semidefinite operator
2 participants