Fix Box.truncation discarding requested dims for array-backed boxes - #34
Draft
giodefelice-agents wants to merge 2 commits into
Draft
giodefelice-agents wants to merge 2 commits into
giodefelice-agents wants to merge 2 commits into
Conversation
[WIP] @evening-2026-08-04 20:41
Closes #28. `Box.truncation`'s array branch built its result with `Dim(2) ** len(dom/cod)` unconditionally, ignoring the input_dims/ output_dims callers ask it to build against -- correct for bit/qubit wires (always dimension 2) but silently wrong for any array-backed box with a mode/qmode wire above cutoff two. determine_output_dimensions had the matching bug for the empty-dom (state) case, where there is no input to derive the codomain from. Box.array_wire_dims and Box.array_truncation (public, no leading underscore, per STYLE.md's no-secrets rule) compute per-wire dims from the requested input_dims/output_dims, falling back to the array's own shape when dom is empty, and raise loudly on a size mismatch instead of building a tensor.Box whose declared type does not match its data. [x] @evening-2026-08-04 21:05
This was referenced Aug 15, 2026
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.
Closes #28, flagged for 🌙 Evening on memory#16's board. No verbatim human prompt —
TODO.mdquotes the issue instead, per the discopy#513/#514 precedent for self-contained bug+fix pairs.The bug
Box.truncation's array branch built its result withDim(2) ** len(dom/cod)unconditionally, ignoring theinput_dims/output_dimsits caller (Diagram.to_tensor) asks it to build against. That's correct forbit/qubitwires (always dimension 2) but silently wrong for any array-backed box with amode/qmodewire above cutoff two — reproduced both symptoms from #28:dom=Ty()) with a 3-entry array over onemodewire raisedValueError: cannot reshape array of size 3 into shape (2,)modewire with a 3×3 array reportedDim(2) -> Dim(2)instead ofDim(3) -> Dim(3), then raised oneval()determine_output_dimensionshad the matching bug for the empty-dom case: it returnedinput_dimsunchanged, which is[]for a state, so there was nothing to derive the codomain from.The fix
Box.array_wire_dimsandBox.array_truncation(public, no leading underscore, perSTYLE.md's no-secrets rule — matches the pattern #29 is applying elsewhere) compute per-wire dimensions:bitwires stay fixed at 2,modewires take the requestedinput_dims/output_dims.determine_output_dimensionsfalls back to the array's own shape whendomis empty, since that's the only case with no input to derive the codomain from. A size mismatch between the array and the requested dims now raisesValueErrorloudly instead of building atensor.Boxwhose declared type doesn't match its data (the "worth deciding" question #28 raised — resolved in favour of the loud error).Two per wire stays correct for
bit/qubit, so nothing changes for the boxes that already used this path (zx.H,zx.SWAP— both overridetruncationdirectly and never reach the array branch anyway).Tests
test/test_box_truncation.py— both #28 repro symptoms fixed and asserted,bitwires still fixed at 2 with no explicit dims, and the array/dims mismatch now raises.Verification
pflake8 optyx test/test_box_truncation.py: cleanpylint optyx --fail-under=9: 9.55/10 (main: 9.54/10 per No secrets in channel.py and core/diagram.py, and make pylint report something #29)coverage run -m pytest: 1151 passed (1147 + 4 new)coverage report --fail-under=95: 95%Generated by Claude Code