Skip to content

Make tests in test_noisy_density_matrix.py use the BranchSelector feature - #425

Merged
thierry-martinez merged 9 commits into
TeamGraphix:masterfrom
tibisabau:416-makes-backend-tests-deterministic
Feb 21, 2026
Merged

Make tests in test_noisy_density_matrix.py use the BranchSelector feature#425
thierry-martinez merged 9 commits into
TeamGraphix:masterfrom
tibisabau:416-makes-backend-tests-deterministic

Conversation

@tibisabau

Copy link
Copy Markdown
Contributor

Before submitting, please check the following:

  • Make sure you have tests for the new code and that test passes (run nox)
  • If applicable, add a line to the [unreleased] part of CHANGELOG.md, following keep-a-changelog.
  • Format added code by ruff
    • See CONTRIBUTING.md for more details
  • Make sure the checks (github actions) pass.

Then, please fill in below:

Context (if applicable):

The noisy density matrix backend tests in test_noisy_density_matrix.py were non-deterministic because they relied on random measurement outcomes. These tests used or conditions to check multiple possible outcomes, making them fail intermittently. The BranchSelector feature (introduced in PR #300) allows fixing measurement outcomes deterministically.

Description of the change:

Modified all affected tests in test_noisy_density_matrix.py to use BranchSelector (ConstBranchSelector and FixedBranchSelector) to fix measurement outcomes, making the tests fully deterministic. Updated test logic based on understanding that noise channels are applied unconditionally after correction commands in the depolarising noise model.

Related issue:

Fixes#416

@codecov

codecovBot commented Feb 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.76%. Comparing base (03a89c9) to head (fff9592).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@ Coverage Diff @@## master #425 +/- ##
=======================================
Coverage 88.76% 88.76% =======================================
Files 44 44 Lines 6308 6308 =======================================
Hits 5599 5599 Misses 709 709 

☔ 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.

Comment threadtests/test_noisy_density_matrix.py Outdated
Comment threadtests/test_noisy_density_matrix.py Outdated
Comment threadtests/test_noisy_density_matrix.py Outdated
Comment threadtests/test_noisy_density_matrix.py Outdated
@thierry-martinez

Copy link
Copy Markdown
Collaborator

@tibisabau Thank you for your last commits. I pointed just the first occurrences of each problem, but you need to solve all the other occurrences for ruff and mypy to be happy. You can run ruff and mypy (and even nox) locally to check that all CI checks will pass before pushing.

Comment threadtests/test_noisy_density_matrix.py Outdated
Comment on lines +366 to +375
# M(0) determines Z, M(1) determines X
results: dict[int, Outcome] = {}
cmd_count = 0
for cmd in rzpattern:
if cmd.kind.name == "M":
if cmd_count == 0:
results[cast("M", cmd).node] = outcome_z
elif cmd_count == 1:
results[cast("M", cmd).node] = outcome_x
cmd_count += 1

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

My previous comment (#425 (comment)) applies here and lines 410 and below as well: in particular, we don't need to use cast here.

Comment threadtests/test_noisy_density_matrix.py Outdated
)

def test_noisy_z_rz(self, fx_rng: Generator) -> None:
@pytest.mark.parametrize(("outcome_z", "outcome_x"), [(0, 0), (0, 1), (1, 0), (1, 1)])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For homogeneity, I would prefer if we do the same here as for line 324 (and this applies to line 398 and 442 too).

@mgarnier59

Copy link
Copy Markdown
Contributor

@tibisabau thanks for your PR! While checking it, we realised that the behaviour in the test was the one we wanted but not the one taht was implemented. Son instead of making the tests fit the implementation, we'll modify the implementation see #428 for more details.

Thanks again and keep an eye on further developments!

@mgarnier59

mgarnier59 commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

Thanks @tibisabau for updating the PR. It looks good! However I still see previous history in your PR. Do you know why that is? If you can't fix it, it will disappear when squashing and merging anyways.

Graphix requires 2 approvals before merging so another colleague will take a look soon. Thanks again for conributing!

tibisabauand others added 8 commits February 19, 2026 10:51
Co-authored-by: thierry-martinez <thierry.martinez@inria.fr>
Co-authored-by: thierry-martinez <thierry.martinez@inria.fr>
Co-authored-by: thierry-martinez <thierry.martinez@inria.fr>
Co-authored-by: thierry-martinez <thierry.martinez@inria.fr>
@tibisabau
tibisabauforce-pushed the 416-makes-backend-tests-deterministic branch from b8c62a7 to 737d36dCompareFebruary 19, 2026 09:59

@mgarnier59mgarnier59 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few extra commits remain for some reason but looks good. Thanks for the good work!

@thierry-martinezthierry-martinez left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM! Thanks.

@thierry-martinez
thierry-martinez merged commit fd708d9 into TeamGraphix:masterFeb 21, 2026
24 checks passed
Sign up for freeto 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.

Make the noisy density matrix backend tests deterministic with the BranchSelector feature

3 participants

@tibisabau@thierry-martinez@mgarnier59