-
Notifications
You must be signed in to change notification settings - Fork 80
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
base: master
Are you sure you want to change the base?
Conversation
@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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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.
Thank you for your feedback. I will review and address the suggested changes. |
Description
Closes #652
Changes