Add 2q fractional gates to the UnitarySynthesis transpiler pass#13568
Conversation
|
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 13237385047Details
💛 - Coveralls |
ElePT
left a comment
There was a problem hiding this comment.
Hi @ShellyGarion! thanks for opening the PR, I left a couple of preliminary comments. I hope they make sense :)
@t-imamichi - unfortunately this PR does not close #13428 yet, since the |
ElePT
left a comment
There was a problem hiding this comment.
Thanks for the tests! I took a look and they look good to me.
| Ok((Some(inv_gate.0), inv_gate_params, qubits)) | ||
| } | ||
| RXXEquivalent::CustomPython(gate_cls) => { | ||
| RXXEquivalent::CustomPython(gate_cls) => Python::with_gil(|py: Python| { |
There was a problem hiding this comment.
I'm fine with this changes since longer term we'll need to do this when running this in a parallel context. But I'm curious what prompted these changes?
There was a problem hiding this comment.
See the discussion here: #13568 (comment)
I wanted to have a purely-rust new_inner function, so I copied the solution from your PR :)
|
|
||
| def __init__(self, rxx_equivalent_gate: Type[Gate]): | ||
| def __init__(self, rxx_equivalent_gate: Type[Gate], euler_basis: str = "ZYZ"): | ||
| r"""Initialize the KAK decomposition. |
There was a problem hiding this comment.
I would avoid changing the defaults for any other classes it causes necessary churn. I think the choice was more just out of habit/standard gates to use. In this case using zxz as the default is fine since it's new, (although I'd be inclined to use zsxx as the default). But it doesn't really matter because either you don't care about the euler basis, or you do and you will specify it.
UnitarySynthesis tranpiler passUnitarySynthesis transpiler pass
… address TwoQubitControlledUDecomposer issue, it appends gates outside of basis set (h/s/sdg)
There was a problem hiding this comment.
Hi @ShellyGarion, I have pushed a commit that includes the work I had been doing on integrating non-standard gates. As you know, I included a couple of unit tests which have pointed to a new issue we hadn't realized until now. Even though the CI checks might not fully pass I wanted to expose this commit to make sure we could all look for solutions with the latest information.
| # TODO: fix this assertion | ||
| # opcount = qc_transpiled.count_ops() | ||
| # print(opcount) | ||
| # self.assertTrue(set(opcount).issubset({"rz", "rx", CustomXXGate.name})) | ||
| # self.assertTrue(np.allclose(Operator(qc_transpiled), Operator(qc))) |
There was a problem hiding this comment.
This test has unveiled an issue with the integration of the TwoQubitControlledUDecomposer. The expectation for the unitary synthesis pass is that the decomposed circuit will respect the user-given basis gates (directly or through a target). However, the TwoQubitControlledUDecomposer will always append a series of h, s and sdg to convert from the canonical form to the given basis. We could only run the decomposer if the target basis gates contain h, s and sdg, but this would leave out essentially all IBM backends. After discussing it with @Cryoris we think it might make sense to express this conversion from canonical form in terms of ibm-supported basis gates, which doesn't fix the issue completely, but would make the pass more usable in the current scenarios we know. If we did that, we could follow this simple logic where we run the decomposer when the gates used match the given basis set. What do you think?
There was a problem hiding this comment.
As there are only 12 euler bases to pick from in the 1q decomposer I think the approach I would look at here would be to just make a small lookup table in for each of the euler basis values and then substitute in the appropriate gates based on the selected euler basis. For example in the two qubit decomposer define something like:
static S_GATE_LUT = [[Option<(StandardGate, [f64; 3])>; 3]; EULER_BASIS_SIZE] = [[Some((StandardGate::U3, [0.,0.,PI_2]), None, None], ...]for all 3 gates. Then in the current code where it's adding the gates just do a lookup with S_GATE_LUT[self.euler_basis as usize] and add the gates to the output accordingly. That should minimize the overhead here but meet the expectations of the transpiler pass.
There was a problem hiding this comment.
Thanks @ElePT for adding the code and tests, and @mtreinish for your suggestion!
So this update should be done in the file two_qubit_decompose.rs when we add the gates S, Sdg and H?
Is there a reason not to use the unitary_to_gate_sequence_inner to decompose the matrices of the S, Sdg and H gates? (so we wouldn't need to update the static array if we add another euler basis)
There was a problem hiding this comment.
The new test runs UnitarySynthesis in isolation while test_parameterized_basis_gate_in_target runs the full transpiler pipeline, it looks like these gates are translated in later stages and that's why they don't show up in the final result. The test with the custom gate fails if you try to run transpile because the basis translator doesn't know how to handle the circuit. However, even if the pipeline compensates the extra gates, I think the pass in isolation should work following the assumptions (i.e. it should follow the specified basis).
There was a problem hiding this comment.
see 5bb55b5
I decomposed the S, Sdg and H gates using unitary_to_gate_sequence_inner (which is more general then having lookup tables)
There was a problem hiding this comment.
@ElePT - following your question below. I think that the correct place to add lookup tables for non-parametric 1-qubit gates (like S, Sdg and H) is in the function unitary_to_gate_sequence_inner, since the two_qubit_decompose code should handle 2-qubit gates and not 1-qubit gates.
I'm not sure how much it will impact the performance here (since we need to decompose other 1-qubit and 2-qubit gates). We can consider it in a follow-up PR.
There was a problem hiding this comment.
after talking with @mtreinish I don't think that the one-qubit synthesis is a bottleneck since it should be negligible compared to the two-qubit synthesis. We can still improve it in a follow-up PR if needed.
ElePT
left a comment
There was a problem hiding this comment.
I think that the PR is in a good state, the functionality is covered and tested. Some parts of the code could be cleaned up (like my score_instruction comment), but this could be done in a follow-up (I will have to refactor the code a bit to add support for basis_gates inputs anyway). The only open point would be the performance of the pass, and the use of the lookup table vs the euler decomposer. I believe that the lookup table will be faster, @ShellyGarion, and the likelihood of adding a new basis to the euler list I think is pretty low. Would you be willing to give it a try?
| Add two-qubit fractional basis gates, such as :class:`.RZZGate`, to the | ||
| :class:`.UnitarySynthesis` transpiler pass. |
There was a problem hiding this comment.
Same comment as above, we should add an example usage.
| Add two-qubit fractional basis gates, such as :class:`.RZZGate`, to the | |
| :class:`.UnitarySynthesis` transpiler pass. | |
| Added support for two-qubit fractional basis gates, such as :class:`.RZZGate`, to the | |
| :class:`.UnitarySynthesis` transpiler pass. The decomposition is done using the :class:`.TwoQubitControlledUDecomposer`, and supports both standard and custom basis gates. |
ElePT
left a comment
There was a problem hiding this comment.
Approving, as this will unblock follow-up work.
Summary
close #13320
With the introduction of new 2q-fractional gates, such as
RZZGateas part of the QPU, we add them to theUnitarySynthesistranspiler pass.https://www.ibm.com/quantum/blog/fractional-gates
Details and comments