Skip to content

Better MCX Decomposition with logarithmic Toffoli Depth #13922

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

Merged
merged 61 commits into from
Mar 25, 2025

Conversation

patelvyom
Copy link
Contributor

Summary

Implement four new multi-controlled-x synthesis functions: synth_mcx_1_clean_kg24, synth_mcx_1_dirty_kg24, synth_mcx_2_clean_kg24, synth_mcx_2_dirty_kg24 as discussed in #13913.

Details and comments

Once implementations are confirmed, these should be added to the HLS transpiler synthesis plugin.

@patelvyom patelvyom requested a review from a team as a code owner February 25, 2025 06:05
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Feb 25, 2025
@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 following people are relevant to this code:

  • @Qiskit/terra-core

@CLAassistant
Copy link

CLAassistant commented Feb 25, 2025

CLA assistant check
All committers have signed the CLA.

@ShellyGarion
Copy link
Member

@patelvyom - thank you very much for your contribution to qiskit. You've made a great start!
Could you please follow the contribution guidelines? https://github.com/Qiskit/qiskit/blob/main/CONTRIBUTING.md
In particular, the style and lint: https://github.com/Qiskit/qiskit/blob/main/CONTRIBUTING.md#style-and-lint
and adding tests: https://github.com/Qiskit/qiskit/blob/main/CONTRIBUTING.md#testing

@coveralls
Copy link

coveralls commented Feb 25, 2025

Pull Request Test Coverage Report for Build 14063143389

Warning: 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

  • 104 of 108 (96.3%) changed or added relevant lines in 1 file are covered.
  • 628 unchanged lines in 14 files lost coverage.
  • Overall coverage increased (+0.02%) to 88.084%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/synthesis/multi_controlled/mcx_synthesis.py 104 108 96.3%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 94.79%
crates/qasm2/src/expr.rs 1 94.23%
qiskit/circuit/library/standard_gates/r.py 1 97.56%
crates/accelerate/src/basis/basis_translator/mod.rs 2 88.97%
qiskit/circuit/library/standard_gates/u1.py 3 94.57%
qiskit/visualization/counts_visualization.py 3 76.92%
qiskit/visualization/library.py 4 0.0%
qiskit/visualization/timeline/plotters/matplotlib.py 4 0.0%
qiskit/circuit/library/standard_gates/x.py 5 98.33%
crates/qasm2/src/lex.rs 6 92.73%
Totals Coverage Status
Change from base Build 13974018645: 0.02%
Covered Lines: 72739
Relevant Lines: 82579

💛 - Coveralls

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

Getting the style/norm of a project is a bit of work, so in addition to Shelly's provided links here are some more detailed infos (I didn't have a look at the logic itself yet 🙂).

@patelvyom
Copy link
Contributor Author

@ShellyGarion Existing MCX decompositions have test cases defined in test/python/transpiler/test_high_level_synthesis.py. Should I also add test cases in test_high_level_synthesis.py? That would require me to add new entries in the HLS plugin.

Also, I believe all style and linter-related errors have been fixed.

@ShellyGarion
Copy link
Member

ShellyGarion commented Feb 26, 2025

@ShellyGarion Existing MCX decompositions have test cases defined in test/python/transpiler/test_high_level_synthesis.py. Should I also add test cases in test_high_level_synthesis.py? That would require me to add new entries in the HLS plugin.

Also, I believe all style and linter-related errors have been fixed.

Could you please add a new test file of the synthesis methods called test_mcx_synthesis.py to the qiskit synthesis tests here?
Please check that the gate is synthesized correctly, and that you get the expected bound on the number of CX gates and/or depth.

Unfortunately, it seems that our mcx synthesis tests are somewhat hidden inside the circuit library tests (which we should refactor in the future). See for example these tests:

def test_mcxvchain_dirty_ancilla_cx_count(self, num_ctrl_qubits):

def test_mcxvchain_clean_ancilla_cx_count(self, num_ctrl_qubits):

def test_mcxrecursive_clean_ancilla_cx_count(self, num_ctrl_qubits):

def test_mcx_gates(self, num_ctrl_qubits):

@patelvyom
Copy link
Contributor Author

I've added the required tests. I couldn't add tests for checking depth for the 2 ancilla cases because the constant factors in O(log(n)) fluctuate a lot across num_ctrl_qubits.

@patelvyom patelvyom requested a review from jyu00 as a code owner March 20, 2025 15:14
@ShellyGarion ShellyGarion removed request for a team, nonhermitian and jyu00 March 20, 2025 16:37
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.

Thanks for your contribution to Qiskit. The PR looks great!
I only have a minor comment: in some places you write "synthesise" and not "synthesize".

Copy link
Member

@alexanderivrii alexanderivrii left a comment

Choose a reason for hiding this comment

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

Thanks for this work, @patelvyom! The code is very clean and I very much like that it uses the new testing framework. I have left a question about the difference between "1_clean" and "2_clean" methods (most probably because I have not actually read the paper) and a few suggestions on improving the release notes. I have also briefly experimented with the code and saw that the new 1-clean method adds less 2-qubit gates than the old 1-clean method (which makes it a very useful contribution). As a follow-up we should extend the synthesis plugin interface, including the default plugin.

cx_count = transpiled_circuit.count_ops()["cx"]
# Based on the bound from the Sec 5.2 of arXiv:2407.17966, assuming Toffoli decomposition
# requires 6 CX gates.
self.assertLessEqual(cx_count, 12 * num_ctrl_qubits - 18)
Copy link
Member

Choose a reason for hiding this comment

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

I have not read the paper but it seems (both from the assertions here and from manually running the code) that the numbers of CX gates produced by synth_mcx_1_clean_kg24 and by synth_mcx_2_clean_kg24 methods are exactly the same (with the similar statement true for dirty). What is the advantage of using 2_clean method over 1_clean and the other way around?

Copy link
Member

@ShellyGarion ShellyGarion Mar 25, 2025

Choose a reason for hiding this comment

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

if you look at the paper then you can see that the difference is at the circuit depth O(n) or O(log n).
but it's difficult to test it since there is no formulas for this bound.
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A long time ago I created this rough plot (using optimization_level = 2)
image

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.

@patelvyom - the PR looks great! thanks for your contribution!

@ShellyGarion ShellyGarion enabled auto-merge March 25, 2025 16:12
@ShellyGarion
Copy link
Member

BTW, @patelvyom - would you like to open another PR to add these methods to the transpiler synthesis plugins?
https://docs.quantum.ibm.com/api/qiskit/transpiler_synthesis_plugins#mcx-synthesis

@ShellyGarion ShellyGarion added this pull request to the merge queue Mar 25, 2025
Merged via the queue into Qiskit:main with commit fbdf950 Mar 25, 2025
20 checks passed
@patelvyom
Copy link
Contributor Author

BTW, @patelvyom - would you like to open another PR to add these methods to the transpiler synthesis plugins? https://docs.quantum.ibm.com/api/qiskit/transpiler_synthesis_plugins#mcx-synthesis

Yes, I will do that this week.

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 Community PR PRs from contributors that are not 'members' of the Qiskit repo synthesis
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Better MCX Decomposition with logarithmic Toffoli Depth
7 participants