Skip to content

Commit

Permalink
Fix UnitarySynthesis pass bug when target contains global gates (#1…
Browse files Browse the repository at this point in the history
…3651)

* Confirm 2q basis gate candidates are in fact 2-qubit gates. Add test using target with global gates.

* Add reno

* Apply Matt's suggestion

Co-authored-by: Matthew Treinish <[email protected]>

* Add semicolon

* Apply Shelly's suggestion

---------

Co-authored-by: Matthew Treinish <[email protected]>
  • Loading branch information
ElePT and mtreinish authored Jan 13, 2025
1 parent d3a434d commit d041d5b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion crates/accelerate/src/unitary_synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ fn get_2q_decomposers_from_target(
let mut available_2q_props: IndexMap<&str, (Option<f64>, Option<f64>)> = IndexMap::new();

let mut qubit_gate_map = IndexMap::new();

match target.operation_names_for_qargs(Some(&qubits)) {
Ok(direct_keys) => {
qubit_gate_map.insert(&qubits, direct_keys);
Expand Down Expand Up @@ -597,7 +598,10 @@ fn get_2q_decomposers_from_target(
OperationRef::Standard(_) => (),
_ => continue,
}

// Filter out non-2q-gate candidates
if op.operation.num_qubits() != 2 {
continue;
}
available_2q_basis.insert(key, replace_parametrized_gate(op.clone()));

if target.contains_key(key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fixed a bug in the :class:`.UnitarySynthesis` transpiler pass where
non-2-qubit gates would be included in the available 2 qubit basis,
causing the ``TwoQubitWeylDecomposition`` to panic because of
the dimension mismatch.
15 changes: 15 additions & 0 deletions test/python/transpiler/test_unitary_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,21 @@ def test_3q_measure_all(self):
self.assertIn("cx", ops)
self.assertIn("measure", ops)

def test_target_with_global_gates(self):
"""Test that 2q decomposition can handle a target with global gates."""

basis_gates = ["h", "p", "cp", "rz", "cx", "ccx", "swap"]
target = Target.from_configuration(basis_gates=basis_gates)

bell = QuantumCircuit(2)
bell.h(0)
bell.cx(0, 1)
bell_op = Operator(bell)
qc = QuantumCircuit(2)
qc.unitary(bell_op, [0, 1])
tqc = transpile(qc, target=target)
self.assertTrue(set(tqc.count_ops()).issubset(basis_gates))


if __name__ == "__main__":
unittest.main()

0 comments on commit d041d5b

Please sign in to comment.