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

Update Split2QUnitaries to handle SWAP unitary gates #13531

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

Conversation

gadial
Copy link
Contributor

@gadial gadial commented Dec 5, 2024

Summary

Updates the Split2QUnitaries transpiler pass to handle 2-qubit unitary gates which can be decomposed as a swap gate and 1-qubit gates.

Closes #13476

Details and comments

This PR extends the Split2QUnitaries in the following manner:

  1. During the initial pass Split2QUnitaries performs, it checks whether any unitary encountered is a SWAP gate.
  2. If SWAP gates were found, another pass is performed on the DAG, replacing SWAP gates by the 1-qubit unitary gates in their decomposition (as is currently done in Split2QUnitaries) and instead of adding SWAP gates, performs a virtual swap by dynamically changing the qubit layout (affecting subsequent gates and the final layout) similar to what the ElidePermutations does.

Since step 2 requires generating a new DAG (as opposed to what Split2QUnitaries does to identity-equivalent unitary gates), it is not performed during the initial pass, only if SWAP gates were detected.

@gadial gadial requested a review from a team as a code owner December 5, 2024 15:28
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@ShellyGarion ShellyGarion added this to the 2.0.0 milestone Dec 5, 2024
@coveralls
Copy link

coveralls commented Dec 5, 2024

Pull Request Test Coverage Report for Build 12580112547

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

  • 130 of 134 (97.01%) changed or added relevant lines in 2 files are covered.
  • 80 unchanged lines in 8 files lost coverage.
  • Overall coverage increased (+0.02%) to 88.97%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/split_2q_unitaries.rs 116 120 96.67%
Files with Coverage Reduction New Missed Lines %
qiskit/circuit/library/quantum_volume.py 1 93.18%
crates/qasm2/src/expr.rs 1 94.02%
crates/accelerate/src/split_2q_unitaries.rs 1 96.32%
qiskit/circuit/library/arithmetic/adders/adder.py 2 91.49%
crates/qasm2/src/lex.rs 6 92.23%
qiskit/quantum_info/states/stabilizerstate.py 11 93.89%
crates/circuit/src/circuit_data.rs 19 95.43%
qiskit/transpiler/passes/synthesis/hls_plugins.py 39 87.02%
Totals Coverage Status
Change from base Build 12182055039: 0.02%
Covered Lines: 79550
Relevant Lines: 89412

💛 - Coveralls

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.

Thanks for doing this, from a quick glance the code looks good (but I only quickly skimmed it on my phone, I'll do a deeper reviewer later). But I had some quick comments on the tests to start.

Comment on lines 291 to 308
def test_2q_swap_with_1_qubit_gates(self):
"""Test that a 2q unitary matching a swap gate with 1-qubit gates before and after is correctly processed."""
qc = QuantumCircuit(2)
qc.h(0)
qc.x(1)
qc.swap(0,1)
qc.sx(0)
qc.sdg(1)
qc.global_phase += 1.2345
qc_split = QuantumCircuit(2)
qc_split.append(UnitaryGate(Operator(qc)), [0, 1])

pm = PassManager()
pm.append(Collect2qBlocks())
pm.append(ConsolidateBlocks())
pm.append(Split2QUnitaries())
res = pm.run(qc_split)
res_op = Operator.from_circuit(res, final_layout=res.layout.final_layout)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test that has this in it as part of a larger circuit that runs through the full transpilation pipeline? I want to make sure that we validating that we're tracking and composing the final permutations correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Also added a smaller test which does non-trivial swapping (with swap gates removed using the elide permutation pass) before the unitary test which surfaced a possible bug in the layout composition phase. We need to discuss whether in this phase we should compose on the existing layout or (as was the case before my change) on its inverse.

self.assertTrue(expected_op.equiv(res_op))
self.assertTrue(
matrix_equal(expected_op.data, res_op.data, ignore_phase=False)
)
Copy link
Member

Choose a reason for hiding this comment

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

Can we add an assertion here that swap has been removed from the output?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added self.assertNotIn("swap", res.count_ops()) but I don't think it's enough - there's also the possibility that we didn't split the unitary at all (note that it's also not being checked in the tests relating to the original split unitary code).

Any idea how to easily verify that the unitary was removed? Note that just using count_ops is tricky since we have a unitary both before (as the 2-qubit unitary) and after (as two individual 1-qubit unitaries). I can compare the count itself (1 before vs 2 after) but it feel less robust than it should.

self.assertTrue(expected_op.equiv(res_op))
self.assertTrue(
matrix_equal(expected_op.data, res_op.data, ignore_phase=False)
)
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 add an assertion that we've removed the swap gate from the circuit. This test could still pass if there was no transformation on the input circuit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@alexanderivrii
Copy link
Contributor

If I recall correctly, we don't run the ElidePermutations pass if the user explicitly sets the initial layout (to prevent the transpiler from messing up circuits that are already routed). Is this something we should worry about for this pass as well? Would it make sense to have an additional flow where the pass would replace a 2-qubit unitary by a SWAP + 1-qubit unitaries without merging the SWAP into the final permutation?

Copy link
Member

@ShellyGarion ShellyGarion 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 your contribution. I have a few minor comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for SWAP specialization in Split2qUnitaries
6 participants