Skip to content

Commit c68c835

Browse files
mergify[bot]ElePT
andauthored
Fix UnitarySynthesis pass bug when target contains global gates (#13651) (#13656)
* 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]> (cherry picked from commit d041d5b) Co-authored-by: Elena Peña Tapia <[email protected]>
1 parent c306c47 commit c68c835

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

crates/accelerate/src/unitary_synthesis.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ fn get_2q_decomposers_from_target(
545545
let mut available_2q_props: IndexMap<&str, (Option<f64>, Option<f64>)> = IndexMap::new();
546546

547547
let mut qubit_gate_map = IndexMap::new();
548+
548549
match target.operation_names_for_qargs(Some(&qubits)) {
549550
Ok(direct_keys) => {
550551
qubit_gate_map.insert(&qubits, direct_keys);
@@ -597,7 +598,10 @@ fn get_2q_decomposers_from_target(
597598
OperationRef::Standard(_) => (),
598599
_ => continue,
599600
}
600-
601+
// Filter out non-2q-gate candidates
602+
if op.operation.num_qubits() != 2 {
603+
continue;
604+
}
601605
available_2q_basis.insert(key, replace_parametrized_gate(op.clone()));
602606

603607
if target.contains_key(key) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed a bug in the :class:`.UnitarySynthesis` transpiler pass where
5+
non-2-qubit gates would be included in the available 2 qubit basis,
6+
causing the ``TwoQubitWeylDecomposition`` to panic because of
7+
the dimension mismatch.

test/python/transpiler/test_unitary_synthesis.py

+15
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,21 @@ def test_3q_measure_all(self):
11011101
self.assertIn("cx", ops)
11021102
self.assertIn("measure", ops)
11031103

1104+
def test_target_with_global_gates(self):
1105+
"""Test that 2q decomposition can handle a target with global gates."""
1106+
1107+
basis_gates = ["h", "p", "cp", "rz", "cx", "ccx", "swap"]
1108+
target = Target.from_configuration(basis_gates=basis_gates)
1109+
1110+
bell = QuantumCircuit(2)
1111+
bell.h(0)
1112+
bell.cx(0, 1)
1113+
bell_op = Operator(bell)
1114+
qc = QuantumCircuit(2)
1115+
qc.unitary(bell_op, [0, 1])
1116+
tqc = transpile(qc, target=target)
1117+
self.assertTrue(set(tqc.count_ops()).issubset(basis_gates))
1118+
11041119

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

0 commit comments

Comments
 (0)