SoquetT becomes a protocol#1721
Merged
mpharrigan merged 8 commits intoquantumlib:mainfrom Mar 9, 2026
Merged
Conversation
476259c to
b8ff0f0
Compare
Collaborator
Author
Contributor
|
/gemini review |
Contributor
There was a problem hiding this comment.
Code Review
This pull request refactors SoquetT from a Union to a typing.Protocol to better support duck typing. This is a significant improvement that makes the type system more flexible and expressive. The changes throughout the codebase to adopt the new SoquetT protocol and the BloqBuilder.is_single/is_ndarray type guards are well-executed. My review includes a couple of suggestions to improve type consistency in the BloqBuilder's free and split methods to fully align with the new protocol-based approach.
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.
Switches the
SoquetTunion type alias to atyping.Protocol. This supports "duck typing", which is appropriate for the situation whereSoquetTis used as a type annotation.Specifically,
SoquetTis now anything that has.shapeand.item(*args). With the addition of these properties/methods to theSoquetclass, now this is satisfied bySoquetandNDArray. Note: we can't use mypy to annotate things withNDArray[Soquet]now or before due to limitations in numpy.When users or developers need to dispatch depending on whether a
SoquetTis a single soquet or an array thereof, they should use the newBloqBuildermethods. The example from the new docstring:In the protocol implementations and bloq standard library, this PR:
Soquet_infra/and protocols modules. Following this, pytest passes but there are mypy issues in thebloqs/standard librarybloqs/standard library.uses new alias(removed from this PR)soq.dtypeinstead ofsoq.reg.dtype.Regarding #1720: this PR shows the changes to the standard library that would be required to avoid
isinstance(x, Soquet)checks