Skip to content

Use SVD-based fidelity for density matrices and add numerical stability test #7292

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

RevanthGundala
Copy link
Contributor

Summary

Fixes #4819

This PR replaces the existing eigen‑decomposition formula for two density matrices

state1_sqrt = _sqrt_positive_semidefinite_matrix(state1)
eigs = eigvalsh(state1_sqrt @ state2 @ state1_sqrt)
trace = sum(sqrt(abs(eigs)))
fidelity = trace**2

with an equivalent but more accurate form

rho1_sqrt = scipy.linalg.sqrtm(rho1)
rho2_sqrt = scipy.linalg.sqrtm(rho2)
fidelity = np.sum(scipy.linalg.svdvals(rho1_sqrt @ rho2_sqrt)) ** 2

This ensures fidelity never exceeds 1 due to round‑off error in higher dimensions.

What’s Changed

In cirq/qis/measures.py, the density‑matrix branch of _fidelity_state_vectors_or_density_matrices now uses sqrtm + svdvals.
A new unit test test_fidelity_numerical_stability_high_dim in measures_test.py constructs a 10‑qubit pure state, traces out one qubit, and verifies that the old eigen‑value‑based fidelity exceeds 1, and the new SVD‑based fidelity ≈ 1.

Testing

Ran pytest cirq-core/cirq/qis/measures_test.py::test_fidelity_numerical_stability_high_dim — old formula fails (>1), new passes (≈1).

@RevanthGundala RevanthGundala requested review from vtomole and a team as code owners April 21, 2025 21:36
@RevanthGundala RevanthGundala requested a review from viathor April 21, 2025 21:36
Copy link

codecov bot commented Apr 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.68%. Comparing base (e9a592d) to head (9a46b19).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7292   +/-   ##
=======================================
  Coverage   98.68%   98.68%           
=======================================
  Files        1112     1112           
  Lines       97599    97616   +17     
=======================================
+ Hits        96315    96334   +19     
+ Misses       1284     1282    -2     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
@github-actions github-actions bot added the size: S 10< lines changed <50 label Apr 30, 2025
@mhucka
Copy link
Contributor

mhucka commented May 16, 2025

@RevanthGundala would you be able to take a look at the merge conflicts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
2 participants