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

Conversation

purva-thakre
Copy link
Collaborator

Description

Fixes #435

Changes

Notable changes that this PR has either accomplished or will accomplish. Feel free to add more lines to the itemized list
below.

  • Change 1

Checklist

Before marking your PR ready for review, make sure you checked the following locally. If this is your first PR, you might be notified of some workflow failures after a maintainer has approved the workflow jobs to be run on your PR.

Additional information is available in the documentation.

  • Use ruff for errors related to code style and formatting.
  • Verify all previous and newly added unit tests pass in pytest.
  • Check the documentation build does not lead to any failures. Sphinx build can be checked locally for any failures related to your PR
  • Use linkcheck to check for broken links in the documentation
  • Use doctest to verify the examples in the function docstrings work as expected.

Copy link

codecov bot commented Oct 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.8%. Comparing base (5c18ff5) to head (ce60082).
Report is 18 commits behind head on master.

Additional details and impacted files
@@          Coverage Diff           @@
##           master    #821   +/-   ##
======================================
  Coverage    97.8%   97.8%           
======================================
  Files         166     168    +2     
  Lines        3242    3261   +19     
  Branches      794     800    +6     
======================================
+ Hits         3172    3191   +19     
  Misses         46      46           
  Partials       24      24           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 38 to 40
if not is_orthonormal(np.array(rand_orth_basis)):
raise ValueError("Random orthonormal basis could not constructed.")

Copy link
Collaborator Author

@purva-thakre purva-thakre Oct 3, 2024

Choose a reason for hiding this comment

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

when is_real = False, perhaps is_orthonormal requires a tolerance?

return is_mutually_orthogonal(vectors) and np.allclose(np.dot(vectors, vectors.T), np.eye(vectors.shape[0]))

@purva-thakre
Copy link
Collaborator Author

purva-thakre commented Oct 3, 2024

@vprusso As discussed, here's the minimum working example when is_real=False in random_unitary. The created basis elements are mutually orthogonal but they fail at the second check.

np.dot on L42 of is_orthonormal is not creating a matrix close to np.eye. I don't think .T works that well when the matrix elements are complex.

import numpy as np
from toqito.rand import random_unitary
from toqito.matrix_props import is_orthonormal, is_unitary
from toqito.state_props import is_mutually_orthogonal

dim = 4
random_mat = random_unitary(dim, is_real=False)

if is_unitary(random_mat):
    print("Random matrix is unitary.")

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

if is_mutually_orthogonal(rand_orth_basis):
    print("The generated basis elements are mutually orthogonal.")

if not is_orthonormal(np.array(rand_orth_basis)):
    raise ValueError("Random orthonormal basis could not constructed.")

Does not work

np.dot(np.array(rand_orth_basis), np.array(rand_orth_basis).T)

Works

np.dot(np.array(rand_orth_basis), np.conjugate(np.array(rand_orth_basis)).T)

@purva-thakre purva-thakre marked this pull request as ready for review October 3, 2024 22:30
Copy link
Owner

@vprusso vprusso left a comment

Choose a reason for hiding this comment

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

Minor comment, but aside from that approved! LGTM!

toqito/rand/random_orthonormal_basis.py Outdated Show resolved Hide resolved
Co-authored-by: Vincent Russo <vincentrusso1@gmail.com>
@purva-thakre purva-thakre merged commit d4a4e0f into master Oct 4, 2024
17 checks passed
@purva-thakre purva-thakre deleted the rand_orth_basis branch October 4, 2024 00:23
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.

Feature: Generate random orthonormal basis
2 participants