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

Port TwoQubitControlledUDecomposer to rust #13139

Merged
merged 31 commits into from
Nov 13, 2024

Conversation

ShellyGarion
Copy link
Member

@ShellyGarion ShellyGarion commented Sep 12, 2024

Summary

close #12907

Details and comments

depends on #13168

Performance improvement:
Synthesizing 100 random 4x4 unitary matrices into each of the basis gates [RXX, RYY, RZZ, RZX, CRZ, CPhase] takes:
in Python: 20.57 sec
in Rust: 8.85

@ShellyGarion ShellyGarion added synthesis Rust This PR or issue is related to Rust code in the repository labels Sep 12, 2024
@ShellyGarion ShellyGarion added this to the 1.3.0 milestone Sep 12, 2024
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @levbishop

@coveralls
Copy link

coveralls commented Sep 12, 2024

Pull Request Test Coverage Report for Build 11812175968

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

  • 352 of 380 (92.63%) changed or added relevant lines in 2 files are covered.
  • 1761 unchanged lines in 86 files lost coverage.
  • Overall coverage increased (+0.2%) to 88.929%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/two_qubit_decompose.rs 345 373 92.49%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 92.2%
qiskit/circuit/library/boolean_logic/inner_product.py 1 96.0%
crates/qasm2/src/expr.rs 1 94.02%
qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py 1 94.54%
qiskit/circuit/store.py 1 97.06%
qiskit/circuit/library/iqp.py 1 96.15%
crates/qasm2/src/lex.rs 1 92.73%
qiskit/circuit/library/boolean_logic/quantum_or.py 1 98.08%
qiskit/circuit/library/arithmetic/adders/draper_qft_adder.py 1 96.97%
qiskit/circuit/parameter.py 1 98.36%
Totals Coverage Status
Change from base Build 11582517625: 0.2%
Covered Lines: 79333
Relevant Lines: 89209

💛 - Coveralls

@ShellyGarion ShellyGarion changed the title [WIP] Port TwoQubitControlledUDecomposer to rust Port TwoQubitControlledUDecomposer to rust Oct 10, 2024
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 the code looks good, I left a few inline comments on how to simplify or optimize things a bit. The biggest concern I have right now though is the backwards incompatibility of the API. This PR restricts the gate objects that TwoQubitControlledUDecomposer class can work with to just standard gates defined in Qiskit. This wasn't a limitation before the rust rewrite and would be considered a breaking api change. However, I'm not sure the class is considered public, it's never been externally documented from what I can tell. I'm curious of your thoughts on this though because I'm not sure what you're thinking about making this class public or not.

#[pyclass(module = "qiskit._accelerate.two_qubit_decompose", subclass)]
pub struct TwoQubitControlledUDecomposer {
#[pyo3(get)]
rxx_equivalent_gate: StandardGate,
Copy link
Member

Choose a reason for hiding this comment

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

Do you think we should treat this class as public at all? The python space definition wasn't limited to standard gates, it would support any python space Gate object including a custom defined gate. This will error in the case of a custom PyGate though.

Copy link
Member Author

@ShellyGarion ShellyGarion Oct 15, 2024

Choose a reason for hiding this comment

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

Currently, rxx_equivalent_gate could be only a StandardGate since in the code we need to invert it, and there is currently no inverse function for other types, see these lines:

                let circ_c = self.to_rxx_gate(gamma)?;
                ...
                for gate in circ_c.gates.into_iter().rev() {
                    let (inv_gate_name, inv_gate_params, inv_gate_qubits) = invert_2q_gate(gate);

Copy link
Member Author

Choose a reason for hiding this comment

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

As for making this class public, I think that it may be useful. Should we do it now or only after it's added to the UnitarySynthesis transpiler pass #13320?

Copy link
Member

@mtreinish mtreinish Oct 29, 2024

Choose a reason for hiding this comment

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

I'd say lets do it later as the PR is large and intricate enough that we should review the change making it public on its own.

crates/accelerate/src/two_qubit_decompose.rs Outdated Show resolved Hide resolved
crates/accelerate/src/two_qubit_decompose.rs Outdated Show resolved Hide resolved
qiskit/synthesis/two_qubit/two_qubit_decompose.py Outdated Show resolved Hide resolved
qiskit/synthesis/two_qubit/two_qubit_decompose.py Outdated Show resolved Hide resolved
crates/accelerate/src/two_qubit_decompose.rs Outdated Show resolved Hide resolved
crates/accelerate/src/two_qubit_decompose.rs Outdated Show resolved Hide resolved
crates/accelerate/src/two_qubit_decompose.rs Outdated Show resolved Hide resolved
crates/accelerate/src/two_qubit_decompose.rs Outdated Show resolved Hide resolved
crates/accelerate/src/two_qubit_decompose.rs Outdated Show resolved Hide resolved
@ShellyGarion
Copy link
Member Author

Overall the code looks good, I left a few inline comments on how to simplify or optimize things a bit. The biggest concern I have right now though is the backwards incompatibility of the API. This PR restricts the gate objects that TwoQubitControlledUDecomposer class can work with to just standard gates defined in Qiskit. This wasn't a limitation before the rust rewrite and would be considered a breaking api change. However, I'm not sure the class is considered public, it's never been externally documented from what I can tell. I'm curious of your thoughts on this though because I'm not sure what you're thinking about making this class public or not.

I would appreciate @levbishop opinion regarding this question as he wrote the original Python code.
I agree that as far as I know this class has never been published as part of Qiskit API. We can make it public if we think it will be useful. perhaps after adding it to the UnitarySynthesis transpiler pass #13320.
In addition, in the tests we test only standard gates:

for gate in [RXXGate, RYYGate, RZZGate, RZXGate, CPhaseGate, CRZGate]:

So I wonder if this class was ever meant to be used with basic gates that are not standard gates.
Anyway, since the Pulse library is being deprecated in #13164, the users will not be able to calibrate their own basic gates with Qiskit and hence this code will be useful only for standard gates.

@mtreinish mtreinish added Changelog: None Do not include in changelog performance labels Oct 29, 2024
@raynelfss raynelfss modified the milestones: 1.3.0, 2.0.0 Nov 7, 2024
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.

This LGTM now. I have one small suggestion on the test code but once that's fixed I think we're good to merge.

test/python/synthesis/test_synthesis.py Outdated Show resolved Hide resolved
@mtreinish mtreinish added this pull request to the merge queue Nov 13, 2024
Merged via the queue into Qiskit:main with commit 5de0232 Nov 13, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog performance Rust This PR or issue is related to Rust code in the repository synthesis
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port TwoQubitControlledUDecomposer to rust
6 participants