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

Fix flaky behavior of test_xeb_fidelity #6337

Merged

Conversation

pavoljuhas
Copy link
Collaborator

Increase tolerance for fidelities calculated with and without state
vector amplitudes. Avoid flaky, random-seed dependent test failures
on a slightly excessive difference.

Example: https://github.com/quantumlib/Cirq/actions/runs/6726576064/job/18283075054#step:6:598

Increase tolerance for fidelities calculated with and without state
vector amplitudes.  Avoid flaky, random-seed dependent test failures
on a slightly excessive difference.

Example: https://github.com/quantumlib/Cirq/actions/runs/6726576064/job/18283075054#step:6:598
Copy link

codecov bot commented Nov 2, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (2f7d732) 97.83% compared to head (a53a76c) 97.83%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6337      +/-   ##
==========================================
- Coverage   97.83%   97.83%   -0.01%     
==========================================
  Files        1108     1108              
  Lines       96352    96352              
==========================================
- Hits        94270    94269       -1     
- Misses       2082     2083       +1     
Files Coverage Δ
...-core/cirq/experiments/fidelity_estimation_test.py 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

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

@@ -81,7 +81,7 @@ def test_xeb_fidelity(depolarization, estimator):
f2 = cirq.xeb_fidelity(
circuit, bitstrings, qubits, amplitudes=amplitudes, estimator=estimator
)
assert np.abs(f - f2) < 1e-6
assert np.abs(f - f2) < 2e-6
Copy link
Collaborator

Choose a reason for hiding this comment

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

lets use pytest.approx instead

assert f2 == pytest.approx(f, rel=1e-6)

This will assert that $f2$ equals $f$ up to relative error of 1e-6. This should be more robust unless $f$ is almost zero in which case we can make it

assert f2 == pytest.approx(f, rel=1e-6, abs=1e-6)

so that it works for all cases

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@NoureldinYosri - thank you for the suggestion, done in 9ddcbaa.
Good to know about pytest.approx it is however not used in cirq. numpy.allclose does the same thing here so I opted for that one.

Copy link
Collaborator

Choose a reason for hiding this comment

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

relative tolerance works only for large numbers. for small number absolute tolerance is needed so

np.allclose(f, f2, atol=1e-6, rtol=1e-6)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In this case the initial absolute value comparison was better as the fidelity is supposed to be on the [0, 1] interval.
For some reason the round-off errors on Mac got it over the threshold.

@pavoljuhas pavoljuhas merged commit aa312bc into quantumlib:master Nov 2, 2023
35 checks passed
@pavoljuhas pavoljuhas deleted the fix-test_xeb_fidelity-flakiness branch November 3, 2023 00:39
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.

None yet

2 participants