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

MCMTGate & plugins #13150

Merged
merged 19 commits into from
Oct 4, 2024
Merged

MCMTGate & plugins #13150

merged 19 commits into from
Oct 4, 2024

Conversation

Cryoris
Copy link
Contributor

@Cryoris Cryoris commented Sep 13, 2024

Summary

Make the MCMT circuits a gate with pluggable synthesis method, plus port the V-chain synthesis to Rust. This is part of #13046.

Details and comments

Even though the focus of this PR is the restructuring into a block that the compiler can reason about, there's also a nice speedup due to the port to Rust:

New runtime / Old runtime = 0.63 (geo mean)
-- n = 50 ntarg = 1
main: 5.604e-04 +- 1.514e-04
this: 3.490e-04 +- 1.771e-04
fraction: 0.64

-- n = 50 targ = 5
main: 5.612e-04 +- 1.180e-04
this: 3.051e-04 +- 1.267e-04
fraction: 0.54

-- n = 50 targ = 25
main: 7.027e-04 +- 1.188e-04
this: 3.727e-04 +- 1.225e-04
fraction: 0.53

-- n = 200 targ = 1
main: 2.074e-03 +- 2.704e-04
this: 1.631e-03 +- 2.565e-03
fraction: 0.78

-- n = 200 targ = 20
main: 2.693e-03 +- 2.488e-03
this: 1.646e-03 +- 2.433e-03
fraction: 0.62

-- n = 200 targ = 100
main: 2.616e-03 +- 2.670e-04
this: 1.880e-03 +- 2.594e-03
fraction: 0.71

-- n = 1000 targ = 1
main: 1.087e-02 +- 4.227e-03
this: 6.820e-03 +- 5.478e-03
fraction: 0.62

-- n = 1000 targ = 100
main: 1.251e-02 +- 6.234e-03
this: 7.843e-03 +- 6.380e-03
fraction: 0.62

-- n = 1000 targ = 500
main: 1.415e-02 +- 5.152e-03
this: 9.360e-03 +- 6.755e-03
fraction: 0.66

@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Cryoris
  • @Qiskit/terra-core
  • @ajavadia
  • @kevinhartman
  • @mtreinish

@Cryoris Cryoris added Rust This PR or issue is related to Rust code in the repository mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Sep 13, 2024
@Cryoris Cryoris changed the title Rust/mcmt MCMTGate & plugins Sep 13, 2024
@coveralls
Copy link

coveralls commented Sep 13, 2024

Pull Request Test Coverage Report for Build 11176416050

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

  • 193 of 209 (92.34%) changed or added relevant lines in 9 files are covered.
  • 14 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.02%) to 88.893%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/transpiler/passes/synthesis/hls_plugins.py 25 27 92.59%
crates/accelerate/src/synthesis/multi_controlled/mcmt.rs 94 101 93.07%
qiskit/circuit/library/generalized_gates/mcmt.py 57 64 89.06%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/parse.rs 6 97.61%
crates/qasm2/src/lex.rs 8 91.48%
Totals Coverage Status
Change from base Build 11161049152: 0.02%
Covered Lines: 74476
Relevant Lines: 83782

💛 - Coveralls

@ShellyGarion
Copy link
Member

thanks @Cryoris for opening this PR! It also handles issue #12863.
What do you think about issue #10698 ?
I think it should be a small addition, but could also be handled in a follow-up PR.

Copy link
Contributor

@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 @Cryoris, this looks very nice. I have left a few minor comments and questions.

Comment on lines 183 to 187
This default implementations requires no ancilla qubits, by broadcasting the target gate
to the number of target qubits and using Qiskit's generic control routine to control the
broadcasted target on the control qubits. If ancilla qubits are available, a more efficient
variant using the so-called V-chain decomposition can be used. This is implemented in
:class:`~qiskit.circuit.library.MCMTVChain`.
Copy link
Contributor

Choose a reason for hiding this comment

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

This part of the docstring probably needs to be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point 👍🏻 related question: do you know whether the synthesis methods like synth_mcx_... are API-documented?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think so. The synthesis methods are described in https://docs.quantum.ibm.com/api/qiskit/synthesis. Also, the synthesis plugins are described in https://docs.quantum.ibm.com/api/qiskit/transpiler_synthesis_plugins.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, I added the Sphinx doc in 646279a 👍🏻

qiskit/circuit/library/generalized_gates/mcmt.py Outdated Show resolved Hide resolved
qiskit/circuit/library/generalized_gates/mcmt.py Outdated Show resolved Hide resolved
Comment on lines 253 to 258
if isinstance(gate, ControlledGate):
base_gate = gate.base_gate
elif isinstance(gate, Gate):
base_gate = gate
else:
raise TypeError(f"Invalid gate type {type(gate)}.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to consider a control-annotated operation as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could if we additionally ran HLS to apply the other modifiers that are not control. A cleaner way might be to completely disallow 2q gates, from which we only extract the 1q base gate. That would require us to handle less cases and might be a leaner workflow.

crates/accelerate/src/synthesis/multi_controlled/mcmt.rs Outdated Show resolved Hide resolved
crates/accelerate/src/synthesis/multi_controlled/mcmt.rs Outdated Show resolved Hide resolved
crates/accelerate/src/synthesis/multi_controlled/mcmt.rs Outdated Show resolved Hide resolved
@alexanderivrii
Copy link
Contributor

Lol, the tests have failed due to cargo clippy warnings. And I caught one of them in the review. :)

@alexanderivrii
Copy link
Contributor

Thank you very much! As per your question, could you please add a table for documenting MCMT synthesis plugins (in hls_plugins.py, the output should be in https://docs.quantum.ibm.com/api/qiskit/transpiler_synthesis_plugins) and the new synthesis function (the output should be in https://docs.quantum.ibm.com/api/qiskit/synthesis). Also this deserves a release note, listing new functionality and deprecations.

Comment on lines 4 to 7
Added the :class:`.MCMTGate` to represent multi-control multi-target
operations in a gate. This gate representation of the existing :class:`.MCMT`
circuit allows the compiler to select the best available implementation
according to the number of auxiliary qubits present in the circuit.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is super-minor, please feel free to ignore it:

Suggested change
Added the :class:`.MCMTGate` to represent multi-control multi-target
operations in a gate. This gate representation of the existing :class:`.MCMT`
circuit allows the compiler to select the best available implementation
according to the number of auxiliary qubits present in the circuit.
Added the :class:`.MCMTGate` to represent a multi-control multi-target
operation as a gate. This gate representation of the existing :class:`.MCMT`
circuit allows the compiler to select the best available implementation
according to the number and the state of auxiliary qubits present in the circuit.

Do we want to list pending deprecations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would just list the deprecations once they are not pending anymore 🙂 (I think that's what we typically have as well 🙂)

Co-authored-by: Alexander Ivrii <alexi@il.ibm.com>
alexanderivrii
alexanderivrii previously approved these changes Oct 1, 2024
Copy link
Contributor

@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.

LGTM!

alexanderivrii
alexanderivrii previously approved these changes Oct 3, 2024
Copy link
Contributor

@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.

Reapproving after the symengine fix.

@alexanderivrii alexanderivrii added this pull request to the merge queue Oct 3, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 3, 2024
@Cryoris Cryoris added this pull request to the merge queue Oct 3, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 3, 2024
@mtreinish
Copy link
Member

This has a merge conflict now that 642debf has merged. The interface for CircuitData::from_packed_operations has changed to include a result and its usage here will need to be updated.

@Cryoris
Copy link
Contributor Author

Cryoris commented Oct 3, 2024

Done in 60a82a3 👍🏻

@@ -501,6 +501,7 @@
Permutation,
PermutationGate,
GMS,
MCMTGate,
Copy link
Member

Choose a reason for hiding this comment

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

You should also add it to line 188 in this file - it currently does not appear in the circuit library docs!

alexanderivrii
alexanderivrii previously approved these changes Oct 4, 2024
@alexanderivrii alexanderivrii added this pull request to the merge queue Oct 4, 2024
github-merge-queue bot pushed a commit that referenced this pull request Oct 4, 2024
* MCMT in Rust & Plugin

* test plugins

* more docs

* add support for ``ctrl_state``

and fix tests

* Sasha's review comments

* fix cyclic imports

* add sphinx doc for mcmt_vchain

* review comments & reno

* Update releasenotes/notes/mcmt-gate-a201d516f05c7d56.yaml

Co-authored-by: Alexander Ivrii <alexi@il.ibm.com>

* Fix plugin name

* Update from_packed_operations usage

---------

Co-authored-by: Alexander Ivrii <alexi@il.ibm.com>
@alexanderivrii alexanderivrii removed this pull request from the merge queue due to a manual request Oct 4, 2024
Copy link
Contributor

@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.

Reapproving once more now that both Matthew's and Shelly's comments have been addressed! Thanks all!

@alexanderivrii alexanderivrii added this pull request to the merge queue Oct 4, 2024
Merged via the queue into Qiskit:main with commit 8b6e47e Oct 4, 2024
15 checks passed
@Cryoris Cryoris deleted the rust/mcmt branch October 4, 2024 11:19
ElePT pushed a commit to ElePT/qiskit that referenced this pull request Oct 7, 2024
* MCMT in Rust & Plugin

* test plugins

* more docs

* add support for ``ctrl_state``

and fix tests

* Sasha's review comments

* fix cyclic imports

* add sphinx doc for mcmt_vchain

* review comments & reno

* Update releasenotes/notes/mcmt-gate-a201d516f05c7d56.yaml

Co-authored-by: Alexander Ivrii <alexi@il.ibm.com>

* Fix plugin name

* Update from_packed_operations usage

* add missing MCMTGate to docs

---------

Co-authored-by: Alexander Ivrii <alexi@il.ibm.com>
@ShellyGarion ShellyGarion added the Changelog: New Feature Include in the "Added" section of the changelog label Nov 5, 2024
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 mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants