-
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
virtual padding for invalid section_size in synth_cnot_count_full_pmh #12712
virtual padding for invalid section_size in synth_cnot_count_full_pmh #12712
Conversation
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 following people are relevant to this code:
|
The circuits correctness in old vs. new code:
without virtual padding:
with virtual padding:
|
Pull Request Test Coverage Report for Build 9768925898Details
💛 - 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.
@Abdalla01001 - thanks for contributing to Qiskit and fixing the bug, but this PR needs some more work to fit into Qiskit standards.
I advise you to look at Qiskit contributing guidelines first.
-
Note that you should sign the CLA.
-
See the section on "Style and Lint".
-
See the section on "Release notes".
-
The functions API docs should not be removed. You can add a comment on virtual padding, but keep the existing format of the docstrings, since it appears here.
-
Please also add a test to this test file to check the correctness of the function with different values of
section_size
(which can be higher or smaller than the number of qubits).
See for example this test:
def test_synth_lnn_kms(self, num_qubits):
if section_size > num_qubits: | ||
padding_size = section_size - (num_qubits % section_size) |
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.
could this be simplified to padding_size = section_size - num_qubits
?
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.
@alexanderivrii the goal of padding is to extend the matrix to a size divisible by the section_size
. Just subtracting the number of qubits from the section size does not guarantee divisibility.
Following on this comment on the default value of @Cryoris noticed that in the paper they say one can choose See also this comment. |
Pull Request Test Coverage Report for Build 9774244854Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
) | ||
state = np.array(state) | ||
num_qubits = state.shape[0] | ||
|
||
# Virtual Padding |
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.
After discussing with @alexanderivrii and @ShellyGarion we came to the conclusion that the section size being larger than the number of qubits is an invalid input. It would be better to raise an error if section_size > num_qubits
, as originally done in #12166. Could you do that and remove the virtual padding?
We'll go forward with your PR then, as it still fixes the bad section_size
values due to your fixes on L131 and L148.
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.
That's right, I did it
Since the general plan is to port this code to Rust and we have a time pressure to make it to Qiskit 1.2 release, I'll close this PR is favor of #12588. |
Summary
Suggest using "virtual padding" or matrices filled with zeros to address empty and invalid circuits when the PMH algorithm cannot handle a
section_size
larger than the number of qubits.#12106
Details
The issue in the code stems from how the algorithm handles the
section_size
parameter. The algorithm is designed to divide the input matrix into sections and process these sections iteratively. However, when thesection_size
is larger than the matrix size, there are no sections to iterate over, causing the algorithm to fail.we can try to solve this by virtual padding, If the
section_size
exceeds the matrix size (n), we can virtually pad the matrix with additional columns of zeros to make it divisible by thesection_size
. This padding is "virtual" in the sense that it's not explicitly added to the matrix data structure, but rather accounted for in the algorithm's logic.The changes:
Virtual Padding: If the
section_size
is larger than the number of qubits (num_qubits
), the matrix is padded with zeros to the right usingnp.hstack
.Modified Loop: The loop in
_lwr_cnot_synth
now iterates up tostate.shape[1]
(the new padded size) to cover all sections.Handling Zero Sub-rows: The code now checks if a sub-row is all zeros
(np.sum(sub_row_patt) == 0)
and skips it if so.Removing Padding: When creating the circuit, we only add CNOT gates if both the control and target qubits are within the original
num_qubits
, effectively discarding operations on the padded columns.Modified Loop Range: The range of the inner loop in
_lwr_cnot_synth
is modified torange((sec - 1) * section_size, min(sec * section_size, num_qubits))
. This ensures that the loop only iterates over the original columns, even if thesection_size
is larger than the matrix size.