[linq] Circuit convenience methods return self (closes #408)#419
Open
999purple999 wants to merge 1 commit into
Open
[linq] Circuit convenience methods return self (closes #408)#419999purple999 wants to merge 1 commit into
999purple999 wants to merge 1 commit into
Conversation
Collaborator
|
Thanks for the PR @999purple999! This is still in draft mode, so I am assuming your work is not completed yet. Code changes are pretty much good with me. Two things:
|
…andbox-quantum#408) Circuit.simplify(), .remove_redundant_gates(), .remove_small_rotations(), and .merge_rotations() all wrap a top-level "out-of-place" function and then splat its result back into self.__dict__. None of them returned anything, so Circuit([Gate("H", 0)] * 2).simplify() returned None even though the convenience function it wraps returns a Circuit. Worse, two of the four methods documented "Returns: Circuit: The circuit without ..." in their docstrings, so the documentation was already correct and only the implementation needed to catch up. This patch adds "return self" to all four methods, keeping the in-place semantic (existing callers that ignore the return value behave identically) while letting the expression form work: c = Circuit([Gate("H", 0)] * 2).simplify() # now an empty Circuit assert isinstance(c, Circuit) and len(c._gates) == 0 and c.width == 1 Docstrings on the two methods that did not have a Returns clause (merge_rotations, simplify) are updated to match. A new regression test test_simplify_returns_circuit_when_everything_cancels pins down both the original failing example and the analogous case for the other three methods. The existing test_simplify_circuit is unchanged and continues to pass.
4e172f3 to
1eb1da6
Compare
Author
|
Thanks @alexfleury-sb! Both points addressed in 1eb1da6:
The README content (root-cause walk-through + design rationale for Let me know if anything else needs a tweak. |
Author
|
Hi @alexfleury-sb — both changes applied in commit 1eb1da6:
The PR is no longer marked as draft. Ready for re-review whenever you have a moment — thanks for taking the time on this one. |
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 #408.
Circuit.simplify()returnsNoneTypeinstead of an empty circuit, because the convenience wrapper splats the underlying function's result intoself.__dict__and returns nothing. Three sibling methods onCircuithave the exact same shape, so this PR fixes all four at once for consistency.What changed
tangelo/linq/circuit.py— addedreturn selfto:Circuit.simplify()Circuit.remove_redundant_gates()Circuit.remove_small_rotations()Circuit.merge_rotations()The two docstrings that already promised
Returns: Circuit: The circuit without ...are tightened; the two that didn't get a matchingReturnsclause added.tangelo/linq/tests/test_circuits.py— new regression testtest_simplify_returns_circuit_when_everything_cancelsthat pins the exact failing one-liner from the issue body:Plus analogous assertions for the other three methods.
FIX_ISSUE_408_README.md— design rationale (whyreturn selfover a separatesimplified()API), full file listing.Compatibility
The in-place semantic is preserved — these methods still mutate
self.__dict__before returning. The existingtest_simplify_circuitpasses unchanged because the assertions on circuit state after.simplify()are still valid.The change enables the chainable expression form (
Circuit(...).simplify().draw()) without breaking any existing code that called these methods for their side-effects and discarded the return value.Open question for review
If you'd rather have a separate
simplified()/remove_redundant_gates_copy()API (returning a new circuit, leaving the original untouched), this PR can be reworked in that direction — happy to align on whichever shape fits the linq module's broader conventions.Francesco Pernice Botta · 999purple999