-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
base: main
Are you sure you want to change the base?
Conversation
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 11379353519Warning: 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.
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, |
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.
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.
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.
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);
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.
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?
&self, | ||
unitary: PyReadonlyArray2<Complex64>, | ||
atol: f64, | ||
) -> PyResult<TwoQubitGateSequence> { |
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 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.
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.
Could you please explain how to convert TwoQubitGateSequence
to CircuitData
?
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.
@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
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! how to convert gate.2
which is of type SmallVec<[u8; 2]>
into Qubit
?
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.
OK, I found a solution, in 02cd73e
self._inner_decomposition = two_qubit_decompose.TwoQubitControlledUDecomposer( | ||
rxx_equivalent_gate._standard_gate |
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.
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:
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.
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.
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.
I would appreciate @levbishop opinion regarding this question as he wrote the original Python code. qiskit/test/python/synthesis/test_synthesis.py Line 1429 in fbfe738
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. |
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