Skip to content
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

Merged
merged 20 commits into from
Jul 17, 2023

Conversation

alexanderivrii
Copy link
Contributor

@alexanderivrii alexanderivrii commented May 1, 2023

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 method extend_with_identity which allows to extend a linear function over k qubits to a linear function over n >= 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() and function_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

qc = QuantumCircuit(4)
qc.cx(0, 1)
qc.swap(1, 2)
qc.cx(2, 3)
qc.swap(1, 3)
lf = LinearFunction(qc)
print(lf)
print(lf.mat_str())
print(lf.function_str())

produces the following output

Instruction(name='linear_function', num_qubits=4, num_clbits=0, params=[array([[ True, False, False, False],
       [ True,  True, False,  True],
       [ True,  True, False, False],
       [False, False,  True, False]]), <qiskit.circuit.quantumcircuit.QuantumCircuit object at 0x0000024C99EDD6F0>])
[[1 0 0 0]
 [1 1 0 1]
 [1 1 0 0]
 [0 0 1 0]]
(x_0, x_0 + x_1 + x_3, x_0 + x_1, x_2)

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. The mat_str printing method is useful to think of the linear function as a binary matrix, while the function_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).

@alexanderivrii alexanderivrii requested a review from a team as a code owner May 1, 2023 13:01
@qiskit-bot
Copy link
Collaborator

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:

@coveralls
Copy link

coveralls commented May 2, 2023

Pull Request Test Coverage Report for Build 5561081491

  • 62 of 79 (78.48%) changed or added relevant lines in 1 file are covered.
  • 419 unchanged lines in 26 files lost coverage.
  • Overall coverage increased (+0.07%) to 86.057%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/library/generalized_gates/linear_function.py 62 79 78.48%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 93.76%
crates/qasm2/src/lex.rs 1 91.9%
qiskit/qasm/init.py 1 80.0%
qiskit/transpiler/passes/layout/set_layout.py 1 95.0%
qiskit/transpiler/passes/routing/layout_transformation.py 1 76.09%
qiskit/circuit/controlflow/_builder_utils.py 2 97.94%
qiskit/circuit/quantumcircuitdata.py 2 86.36%
qiskit/transpiler/passes/routing/algorithms/token_swapper.py 2 92.31%
qiskit/qasm/pygments/init.py 3 0%
qiskit/circuit/controlflow/if_else.py 4 97.71%
Totals Coverage Status
Change from base Build 5490783039: 0.07%
Covered Lines: 72312
Relevant Lines: 84028

💛 - Coveralls

Copy link
Member

@ShellyGarion ShellyGarion left a 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
    -- calculate Clifford(qc), calculate LinearFunction(qc), and check that you get the proper linear matrix inside the clifford.

@ShellyGarion ShellyGarion added this to the 0.25.0 milestone Jul 10, 2023
@alexanderivrii
Copy link
Contributor Author

@ShellyGarion, thanks for the review!

could you add the pretty printing functions to the release notes?

This is done in 68c2aa9.

does it make sense to add a basic tests for the printing of function_str?

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.

perhaps it's worth to add some pseudo-random tests

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 random_linear_circuit function (from test_linear_function.py) to support barriers, delays, permutation gates, linear functions, cliffords (but only corresponding to linear functions), and nested linear subcircuits. Though the default behavior matches the old behavior of the function: only flat circuit with CX and SWAP gates are generated.

ShellyGarion
ShellyGarion previously approved these changes Jul 12, 2023
Copy link
Member

@ShellyGarion ShellyGarion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @alexanderivrii !

Copy link
Member

@mtreinish mtreinish left a 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.

@mtreinish mtreinish added the Changelog: New Feature Include in the "Added" section of the changelog label Jul 14, 2023
Copy link
Member

@mtreinish mtreinish left a 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

@mtreinish mtreinish added this pull request to the merge queue Jul 17, 2023
Merged via the queue into Qiskit:main with commit 93feba7 Jul 17, 2023
13 checks passed
@alexanderivrii alexanderivrii deleted the linear-function-constructor branch October 23, 2023 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants