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 2 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.67%. Comparing base (5112418) to head (b791895).
Report is 10 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7292   +/-   ##
=======================================
  Coverage   98.66%   98.67%           
=======================================
  Files        1106     1106           
  Lines       96086    96103   +17     
=======================================
+ Hits        94808    94825   +17     
  Misses       1278     1278           

☔ 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
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fidelity calculation for density matrices improvement
1 participant