diff --git a/crates/accelerate/src/unitary_synthesis.rs b/crates/accelerate/src/unitary_synthesis.rs index fa5880b1697a..439b9ba80f67 100644 --- a/crates/accelerate/src/unitary_synthesis.rs +++ b/crates/accelerate/src/unitary_synthesis.rs @@ -545,6 +545,7 @@ fn get_2q_decomposers_from_target( let mut available_2q_props: IndexMap<&str, (Option, Option)> = 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); @@ -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) { diff --git a/releasenotes/notes/fix-unitary-synthesis-global-gates-19b93840b28cfcf7.yaml b/releasenotes/notes/fix-unitary-synthesis-global-gates-19b93840b28cfcf7.yaml new file mode 100644 index 000000000000..21310a4729a8 --- /dev/null +++ b/releasenotes/notes/fix-unitary-synthesis-global-gates-19b93840b28cfcf7.yaml @@ -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. diff --git a/test/python/transpiler/test_unitary_synthesis.py b/test/python/transpiler/test_unitary_synthesis.py index 7f2bb2f89911..e126b555107d 100644 --- a/test/python/transpiler/test_unitary_synthesis.py +++ b/test/python/transpiler/test_unitary_synthesis.py @@ -1101,6 +1101,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()