-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add the MCX synthesis algorithm by Huang and Palsberg #14666
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
Conversation
Co-authored-by: Jens Palsberg <palsberg@ucla.edu> Co-authored-by: Keli Huang <kelihuang@cs.ucla.edu>
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 16807106158Warning: 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 |
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.
Thanks @alexanderivrii for your important contribution and to Keli Huang and Jans Palsberg for providing their python code and their help.
I mainly have a few comments on the documentation and tests.
In addition, in the benchmarks of your PR, could you please add which of the qulin algorithms was actually used?
Some answers to your questions:
-
I don't think that
Increment1
andDecrement1
should have gates. These are helper functions and not standalone building blocks. -
I don't think that we need a native "relative MCX gate" in Qiskit. It's a helper function and not a standalone building block, and it's also not well defined.
/// | ||
/// A quantum circuit with :math:`2 * n` qubits. | ||
fn increment_n_dirty(n: u32) -> PyResult<CircuitData> { | ||
if n <= 10 { |
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.
is there a reference to a theorem stating why n=10 is the bound here?
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.
I wouldn't call this a theorem. There are two constructions given in the paper (+ supplementary material): the construction in Fig. 18 and the construction in Fig. 10. The first has better asymptotic behavior but the second is better for small values of n. Numerically, the switch happens when n = 10.
// mot efficient to construct the true MCX gate that uses num_controls ancillas. | ||
// An interesting question is whether there are relative-MCX implmentations that | ||
// use ancilla qubits. | ||
if num_controls < 11 { |
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.
is there a reference to a theorem stating why n=11 is the bound here?
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.
It's pretty much the same answer than before: there is one construction that is better asymptotically (i.e. for large values of n) and another construction that is better for small values of n. I believe that in the future we will see more constructions of relative MCX gates with ancillas, in which case we would be able to further fine-tune this function.
Synthesize a multi-controlled X gate with :math:`k` controls based on | ||
the work by Huang and Palsberg. | ||
|
||
Produces a quantum circuit with :math:`k + 1` 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.
should it also mention that "The number of CX-gates is linear in k" ?
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.
Adding.
|
||
For a multi-controlled X gate with :math:`k` control qubits this synthesis | ||
method requires no additional clean auxiliary qubits. The synthesized | ||
circuit consists of :math:`k + 1` 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.
should it also mention that "The number of CX-gates is linear in k" ?
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.
Adding.
@@ -192,6 +193,14 @@ def test_mcx_noaux_v24(self, num_ctrl_qubits: int): | |||
XGate(), num_ctrl_qubits, synthesized_circuit, clean_ancillas=False | |||
) | |||
|
|||
@data(1, 2, 3, 4, 5, 6, 7, 8) | |||
def test_mcx_noaux_hp24(self, num_ctrl_qubits: int): |
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.
I think that some more tests are needed.
-
Since there are two different synthesis methods, for small and large number of qubits, should we also check the large here? maybe using the
Statevector
simulator? -
Can we add a
test_mcx_noaux_hp24_cx_count
test? based on some formulas from the paper (Theorem 4.5)?
usually we check it forn=5,10,15
, should we also checkn>22
here? -
The bound in the tests in line 469 should be updates:
if isinstance(base_gate, (XGate, YGate, ZGate, HGate)):
# MCX gate and other locally equivalent multi-controlled gates
expected = {1: 1, 2: 6, 3: 14, 4: 36, 5: 84, 6: 140, 7: 220, 8: 324}
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.
I have addressed the second and the third comments (both are great suggestions).
Regarding the first point. I completely agree that it would be very nice to add some correctness checking for large values of n
, especially that the algorithm follows different compilation schemes for large values of n
. However, at this point constructing either Operator
or Statevector
is impossible, do you agree? What can we do instead?
Consructs a unitary matrix from a CircuitData with only unitary operations, exactly as what Operator(quantum_circuit).data does. The code is a simple wrapper on top of unitary_compose.
Update: this PR is now based on top of #14746 as to add at least some tests for our Rust synthesis code. We have discussed this offline, and the general consensus was that we should have Rust-space tests for different helper functions that are normally not explored for small numbers of qubits. So far, I have added a test that @ShellyGarion, are there any more tests you think we should add? For the record, this is the command I am using to run Rust synthesis tests: |
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.
a few more comments on the docs and tests. other than that, it looks great!
use super::{increment_n_dirty_large, increment_n_dirty_small}; | ||
|
||
#[test] | ||
fn test_increment_n_dirty() { |
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.
it's great to see these tests!
- perhaps the range of
nq
could be larger? we usually take 1,2,...,8 (in most tests) - are there tests needed also for
increment_1_dirty
andincrement_2_dirty
? - are there tests needed for
synth_relative_mcx
?
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.
- For larger values of
nq
the simulation starts taking > 30 seconds on my laptop and I am not sure if we should add slow in-Rust tests. Hopefully the existing tests for smaller values ofnq
rule out most of the "obvious" bugs. - I am less worried about
increment_1_dirty
andincrement_2_dirty
since these functions are tested for smaller values ofnq
(though with different implementations of some internal components). - We could add tests for
synth_relative_mcx
as we have discussed offline, however for small values ofnq
this is covered in our Python testing, and for larger values ofnq
this is just oursynth_mcx_n_dirty_i15
algorithm, which is also covered by our Python testing. So, thinking about this, we can skip such tests for now.
I do agree that we should think how to generally improve the testing strategy for synthesis methods, but let's do this in a follow-up. On this topic, I believe it could even be possible to formally prove correctness of certain synthesis methods (software verification/theorem proving) but this would be a huge project in itself.
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.
It's a great contribution to qiskit!
I only have a minor comment on the release notes. we can handle it now or as part of the 2.2 release PR.
releasenotes/notes/add-new-mcx-noaux-method-6f38c3cd3708df3c.yaml
Outdated
Show resolved
Hide resolved
releasenotes/notes/add-new-mcx-noaux-method-6f38c3cd3708df3c.yaml
Outdated
Show resolved
Hide resolved
In the recently merged Qiskit#14666 two small clippy issues with latest stable slipped into the PR. We run clippy with our MSRV in CI for a stable CI as newer versions of Rust/clippy introduce new rules and/or change/improve existing rules so a new rust release would break CI which is disruptive. However, you can still use newer clippy for local development and when you do the issues it finds highlight real things that need to be fixed in the code. This commit fixes the two things that clippy flags in the mcx plugin code, one is just a code style change but the other is a small docs formatting issue which would have resulted in weird formatting for the rendered rustdoc.
In the recently merged #14666 two small clippy issues with latest stable slipped into the PR. We run clippy with our MSRV in CI for a stable CI as newer versions of Rust/clippy introduce new rules and/or change/improve existing rules so a new rust release would break CI which is disruptive. However, you can still use newer clippy for local development and when you do the issues it finds highlight real things that need to be fixed in the code. This commit fixes the two things that clippy flags in the mcx plugin code, one is just a code style change but the other is a small docs formatting issue which would have resulted in weird formatting for the rendered rustdoc.
Summary
This PR implements the MCX synthesis algorithm from https://dl.acm.org/doi/10.1145/3656436. The algorithm (called Qulin) significantly improves on the number of 2-qubit gates when no auxiliary qubits are available (details below).
This is a collaboration with the authors of the paper, Keli Huang and Jens Palsberg, who have very kindly provided me with the Python version of their code, which I have then ported to Rust (with some modifications).
Details and comments
Let us say that$2^n$ . The new algorithm implements
Increment1(n)
andDecrement1(n)
are high-level gates operating onn
qubits that respectively add and subtract 1 moduloMCX(n)
gates usingIncrement1
/Decrement1
and smallerMCX
gates, while cleverly leveraging available ancillas. We do not haveIncrement1
/Decrement1
gates natively in Qiskit, and I expect these to be useful for other applications as well; see Question 1 below.Internally, the code also provides the following synthesis methods:
Increment1(n)
/Decrement1(n)
usingn
dirty auxiliary qubits. There are in fact two methods for this, one being more efficient whenn
is large, and one being more efficient whenn
is small.Increment1(n)
,Decrement1(n)
using1
auxiliary qubits whenn
is odd, and using2
auxiliary qubits for both even and odd values ofn
.Interesting questions:
Question 1: Do we want to add high-level gates
Increment1
andDecrement1
to Qiskit?Question 2: Do we need a native "relative MCX gate" in Qiskit? Note that such a gate is not precisely defined: the requirement is that it should be the same as MCX up to some diagonal matrix. There might be multiple useful relative MCX gates, both in terms of the operator and the synthesized circuit. In particular, a recent Update synth_mcx_kg24 to use relative phase Toffolis #14394 introduced a very different implementation for the MCX gate (I have not checked if the corresponding operators match, but I doubt that this is the case).
Question 3: In fact, for this PR we need an implementation of a relative MCX(n) gate when
n
auxiliary qubits available. Currently, whenn
is small we use a recursive algorithm for building a relative MCX gate that does not use any auxiliary qubits, and whenn
is large we switch to an algorithm that constructs a true MCX gate that usesn
auxiliary qubits. Do we have an algorithm that constructs a relative MCX gate using auxiliary qubits? Can Update synth_mcx_kg24 to use relative phase Toffolis #14394 help?Question 4: the experimental data shows that the new algorithm is much better than Qiskit's when no auxiliary qubits are available, however is significantly worse when at leas 1 auxiliary qubit is available (see Update synth_mcx_kg24 to use relative phase Toffolis #14394 for the latter data). One of the reasons for this is that the new algorithm assumes that initially no auxilary qubits are available, and while it cleverly manages its own qubits as auxiliary qubits for different parts of the problem, it does not leverage other available qubits in the larger circuit. What is the best way to achieve that?
Comparison without and with this PR
The following data compares the number of CX-gates introduced by the Qiskit's default synthesis function
qiskit
(akasynth_mcx_noaux_v24
) against the new methodqulin
(akasynth_mcx_noaux_hp24
) for synthesizing theMCX(n)
gate (wheren
is the number of controls), when no auxiliary qubits are available. We can clearly see that the number of CX-gates is improved by about 3x for largen
.However, when at least 1 ancillary qubit is available, Qiskit produces much better gate counts than either of these two methods above.
ToDo: