-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Linear function usability improvements #10053
Linear function usability improvements #10053
Conversation
…adding __eq__ method
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the the following people are requested to review this: |
Pull Request Test Coverage Report for Build 5561081491
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for this PR, it is very helpful!
I have a few comments and suggestions:
- could you add the pretty printing functions to the release notes?
- does it make sense to add a basic tests for the printing of
function_str
? - perhaps it's worth to add some pseudo-random tests, to check that all these elements can go well inside the circuit:
-- choose pseudo-randomly linear gates, barriers, delays, permutations, linear functions, linear cliffords and create a circuit qc
-- calculateClifford(qc)
, calculateLinearFunction(qc)
, and check that you get the proper linear matrix inside the clifford.
@ShellyGarion, thanks for the review!
This is done in 68c2aa9.
I don't know if it's worth checking the output of pretty-printing functions, so I didn't add such a test for now.
I agree that this is a good idea; this is done in 6190778 following your suggested approach. To support this I had to extend the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @alexanderivrii !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this LGTM, I just had a couple of small comments and questions inline.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for the quick updates
Summary
This PR adds a few usability improvements to
LinearFunctions
:Constructing linear functions from more general quantum circuits that may contain barriers and delays, permutations, other linear functions, cliffords (when these represent valid linear functions), and nested quantum circuits of this form. (Note that this is now similar to how Cliffords can be constructed).
Adding the
__eq__
method, so that two linear functions are considered equivalent when their defining binary matrices are.Required for the first bullet (and very similar to Clifford's
_pad_with_identity
method), there is also a new methodextend_with_identity
which allows to extend a linear function overk
qubits to a linear function overn >= k
qubits, specifying the new positions of the original qubits and padding with identities on the remaining qubits.Added methods for pretty-printing
LinearFunctions
, either as a binary matrix or as the corresponding linear transformation.Additional possible improvements (not implemented):
In principle we could also easily add that ability to construct a linear function from a general operator, by first attempting to construct a clifford out of it (as per Add Clifford.from_matrix #9475), and then attempting to construct a linear function from this clifford.
When "n == k", the
extend_with_identity
method can be used to permute the qubits of the linear function, which could be useful in itself.Details:
For pretty-printing (aka visualizing) linear functions, I have added the methods
mat_str()
andfunction_str()
, please feel free to suggest better names. Personally, I find all of the three printing methods quite useful (with the last one specifically requested by @ShellyGarion).For example, running the following code
produces the following output
The default representation is a bit ugly, however serves to remind that the entries of the matrix are booleans and not integers (same as for
Cliffords
) and also shows that the linear matrix has the "original_circuit" field. Themat_str
printing method is useful to think of the linear function as a binary matrix, while thefunction_str
method is useful to think of the linear function as a linear transformation aka a function that maps (x_0, x_1, x_2, x_3) to (x_0, x_0 + x_1 + x_3, x_0 + x_1, x_2).