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

Add 2q fractional gates to the UnitarySynthesis transpiler pass #13568

Merged
merged 34 commits into from
Feb 10, 2025

Conversation

ShellyGarion
Copy link
Member

Summary

close #13320

With the introduction of new 2q-fractional gates, such as RZZGate as part of the QPU, we add them to the UnitarySynthesis transpiler pass.
https://www.ibm.com/quantum/blog/fractional-gates

Details and comments

@ShellyGarion ShellyGarion added the mod: transpiler Issues and PRs related to Transpiler label Dec 16, 2024
@ShellyGarion ShellyGarion added this to the 2.0.0 milestone Dec 16, 2024
@ShellyGarion ShellyGarion requested a review from ElePT December 16, 2024 11:06
@ShellyGarion ShellyGarion requested review from alexanderivrii and a team as code owners December 16, 2024 11:06
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @levbishop

@ElePT ElePT self-assigned this Dec 16, 2024
@coveralls
Copy link

coveralls commented Dec 16, 2024

Pull Request Test Coverage Report for Build 13237385047

Details

  • 235 of 246 (95.53%) changed or added relevant lines in 4 files are covered.
  • 19 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.02%) to 88.313%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/unitary_synthesis.rs 119 124 95.97%
crates/accelerate/src/two_qubit_decompose.rs 104 110 94.55%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 2 93.29%
crates/qasm2/src/lex.rs 5 92.48%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 13204846880: 0.02%
Covered Lines: 78777
Relevant Lines: 89202

💛 - Coveralls

Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

Hi @ShellyGarion! thanks for opening the PR, I left a couple of preliminary comments. I hope they make sense :)

@ShellyGarion
Copy link
Member Author

ShellyGarion commented Jan 23, 2025

I think this closes #13428. How about adding a test case of consecutive RZZ (RXX, and RYY) gates?

@t-imamichi - unfortunately this PR does not close #13428 yet, since the ConsolidateBlocks transpiler pass does not consolidate the consecutive RZZ gates into a single unitary.
This should be fixed in #13419.
I will therefore transfer your comment there.

Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

Thanks for the tests! I took a look and they look good to me.

@@ -2516,7 +2517,7 @@ impl TwoQubitControlledUDecomposer {
.collect::<SmallVec<_>>();
Ok((Some(inv_gate.0), inv_gate_params, qubits))
}
RXXEquivalent::CustomPython(gate_cls) => {
RXXEquivalent::CustomPython(gate_cls) => Python::with_gil(|py: Python| {
Copy link
Member

Choose a reason for hiding this comment

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

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

See the discussion here: #13568 (comment)
I wanted to have a purely-rust new_inner function, so I copied the solution from your PR :)

@@ -270,32 +270,46 @@ class TwoQubitControlledUDecomposer:
:math:`U \sim U_d(\alpha, 0, 0) \sim \text{Ctrl-U}`
gate that is locally equivalent to an :class:`.RXXGate`."""

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.
Copy link
Member

Choose a reason for hiding this comment

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

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.

@ElePT ElePT changed the title Add 2q fractional gates to the UnitarySynthesis tranpiler pass Add 2q fractional gates to the UnitarySynthesis transpiler pass Jan 29, 2025
… address TwoQubitControlledUDecomposer issue, it appends gates outside of basis set (h/s/sdg)
Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines 893 to 897
# 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)))
Copy link
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member Author

@ShellyGarion ShellyGarion Jan 30, 2025

Choose a reason for hiding this comment

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

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)

Copy link
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

see 5bb55b5
I decomposed the S, Sdg and H gates using unitary_to_gate_sequence_inner (which is more general then having lookup tables)

Copy link
Member Author

Choose a reason for hiding this comment

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

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?

Comment on lines 9 to 10
Add two-qubit fractional basis gates, such as :class:`.RZZGate`, to the
:class:`.UnitarySynthesis` transpiler pass.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as above, we should add an example usage.

Suggested change
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.

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 55556f8

Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

Approving, as this will unblock follow-up work.

@ElePT ElePT added this pull request to the merge queue Feb 10, 2025
Merged via the queue into Qiskit:main with commit 02850cf Feb 10, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mod: transpiler Issues and PRs related to Transpiler synthesis
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add TwoQubitControlledUDecomposer to the UnitarySynthesis tranpiler pass
7 participants