fix: prevent Constant Folding disconnecting linear outputs even if constant#1793
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
acl-cqc
commented
Jul 5, 2026
10 tasks
Collaborator
|
Note that this may be related / partially solving #1724 |
aborgna-q
approved these changes
Jul 7, 2026
| // 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))) |
Collaborator
There was a problem hiding this comment.
nit: It could also have been a .filter(...).map(...), but works that same anyways.
Contributor
Author
There was a problem hiding this comment.
Yeah, longer but clearer, so done
Collaborator
I think so too, we should do some testing. |
Draft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.