Skip to content

fix: prevent Constant Folding disconnecting linear outputs even if constant#1793

Merged
acl-cqc merged 6 commits into
mainfrom
acl/fix_constfold_linear_sum
Jul 7, 2026
Merged

fix: prevent Constant Folding disconnecting linear outputs even if constant#1793
acl-cqc merged 6 commits into
mainfrom
acl/fix_constfold_linear_sum

Conversation

@acl-cqc

@acl-cqc acl-cqc commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

For a Sum types where some variants are quantum but others are classical, we can deduce a constant value of the classical variant, but "breaking the wire" to insert the constant leads to an unconnected port of the sum type, which is linear...

Ideal solution in many cases is probably #1796, but that may not apply in all cases. Same problem could also happen for CustomConsts of linear type (BorrowArray?) if we constant-fold those.

For now the workaround is to avoid breaking any linear wire. This is overly conservative; we could insert consts and break such wires if the sources can also be removed, but that requires traversal done only in DCE.

First commit adds a test where the Hugr fails validation after constant-folding; second fixes.

@acl-cqc acl-cqc changed the title Fix: Constant Folding can disconnect linear outputs if they are constant fix: Constant Folding can disconnect linear outputs if they are constant Jul 3, 2026
@acl-cqc acl-cqc marked this pull request as ready for review July 3, 2026 17:51
@acl-cqc acl-cqc requested a review from a team as a code owner July 3, 2026 17:51
@acl-cqc acl-cqc requested a review from maximilianruesch July 3, 2026 17:51
@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.75%. Comparing base (d591a3d) to head (b8edaae).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1793      +/-   ##
==========================================
+ Coverage   85.57%   85.75%   +0.18%     
==========================================
  Files         198      198              
  Lines       31087    31478     +391     
  Branches    29642    30033     +391     
==========================================
+ Hits        26603    26995     +392     
+ Misses       3210     3205       -5     
- Partials     1274     1278       +4     
Flag Coverage Δ
python 91.91% <ø> (ø)
qis-compiler 74.17% <ø> (ø)
rust 85.45% <100.00%> (+0.19%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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 thread tket/src/passes/const_fold.rs Outdated
@maximilianruesch

Copy link
Copy Markdown
Collaborator

Note that this may be related / partially solving #1724

Comment thread tket/src/passes/const_fold.rs Outdated
// Overly conservative: see https://github.com/Quantinuum/tket2/issues/1794,
// https://github.com/Quantinuum/tket2/issues/1795,
// https://github.com/Quantinuum/tket2/issues/1796
.flat_map(move |(outp, ty)| ty.copyable().then_some((n, outp)))

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.

nit: It could also have been a .filter(...).map(...), but works that same anyways.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, longer but clearer, so done

@aborgna-q

aborgna-q commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Note that this may be related / partially solving #1724

I think so too, we should do some testing.

@acl-cqc acl-cqc changed the title fix: Constant Folding can disconnect linear outputs if they are constant fix: prevent Constant Folding disconnecting linear outputs even if constant Jul 7, 2026
@acl-cqc acl-cqc enabled auto-merge July 7, 2026 12:17
@acl-cqc acl-cqc added this pull request to the merge queue Jul 7, 2026
Merged via the queue into main with commit f494f3a Jul 7, 2026
27 checks passed
@acl-cqc acl-cqc deleted the acl/fix_constfold_linear_sum branch July 7, 2026 12:27
@hugrbot hugrbot mentioned this pull request Jul 7, 2026
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.

3 participants