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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

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 11379353519

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

  • 290 of 309 (93.85%) changed or added relevant lines in 2 files are covered.
  • 668 unchanged lines in 27 files lost coverage.
  • Overall coverage decreased (-0.2%) to 88.678%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/synthesis/two_qubit/two_qubit_decompose.py 6 7 85.71%
crates/accelerate/src/two_qubit_decompose.rs 284 302 94.04%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.02%
crates/accelerate/src/elide_permutations.rs 1 97.06%
crates/accelerate/src/error_map.rs 1 85.42%
crates/accelerate/src/circuit_library/quantum_volume.rs 1 96.69%
qiskit/circuit/library/quantum_volume.py 2 95.45%
crates/accelerate/src/barrier_before_final_measurement.rs 2 97.33%
crates/circuit/src/lib.rs 2 96.15%
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.19%
crates/circuit/src/bit_data.rs 2 94.62%
crates/accelerate/src/commutation_checker.rs 2 97.14%
Totals Coverage Status
Change from base Build 11241264267: -0.2%
Covered Lines: 73411
Relevant Lines: 82784

💛 - 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?

crates/accelerate/src/two_qubit_decompose.rs Outdated Show resolved Hide resolved
&self,
unitary: PyReadonlyArray2<Complex64>,
atol: f64,
) -> PyResult<TwoQubitGateSequence> {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason we can't return a CircuitData here? That way we're building the output circuit directly instead of this intermediate vec. This should speed up the python space class's __call__ function because we've built the circuit object in rust instead of in python.

Copy link
Member Author

Choose a reason for hiding this comment

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

Could you please explain how to convert TwoQubitGateSequence to CircuitData ?

Copy link
Contributor

Choose a reason for hiding this comment

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

@ShellyGarion you can iterate over the gates in the sequence and push them to a new CircuitData object, there is an example in TwoQubitWeylDecomposition: https:/Qiskit/qiskit/blob/main/crates/accelerate/src/two_qubit_decompose.rs#L1151-L1170

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks! how to convert gate.2 which is of type SmallVec<[u8; 2]> into Qubit ?

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I found a solution, in 02cd73e

qiskit/synthesis/two_qubit/two_qubit_decompose.py Outdated Show resolved Hide resolved
Comment on lines 282 to 283
self._inner_decomposition = two_qubit_decompose.TwoQubitControlledUDecomposer(
rxx_equivalent_gate._standard_gate
Copy link
Member

Choose a reason for hiding this comment

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

This is potentially a breaking API change if we treat this class as public. I think we can probably get away with considering it private since as far as I can tell we've never documented it. If we wanted to support arbitrary gates it wouldn't be too hard this is why TwoQubitGateSequence has an Option<StandardGate> in it. We'd have to pass the other details to get the matrix from the parameter value to rust but then we can keep the arbitrary gate support and when we encounter None we use that.

That being said if we're ok (I'm not 100% convinced one way or the other) making this API change to the class because we consider this internal only, we should change this to do something like:

Suggested change
self._inner_decomposition = two_qubit_decompose.TwoQubitControlledUDecomposer(
rxx_equivalent_gate._standard_gate
if (gate := rxx_equivalent_gate.getattr("_standard_gate", None)) is not None:
self._inner_decomposition = two_qubit_decompose.TwoQubitControlledUDecomposer(
rxx_equivalent_gate._standard_gate
else:
raise QiskitError("Must be initialized with a standard gate object")

so we raise a meaningful error if this is instantiated with a non-standard 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.

done in 3182838
I had to change the if line to
if rxx_equivalent_gate._standard_gate is not None:
otherwise the tests were failing.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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
5 participants