From 8c60a22f88eb6a5ef577fb3094a78b6dd2d4e008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Fri, 10 Jan 2025 14:50:03 +0100 Subject: [PATCH 1/5] Confirm 2q basis gate candidates are in fact 2-qubit gates. Add test using target with global gates. --- crates/accelerate/src/unitary_synthesis.rs | 7 ++++++- test/python/transpiler/test_unitary_synthesis.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/accelerate/src/unitary_synthesis.rs b/crates/accelerate/src/unitary_synthesis.rs index fa5880b1697a..527caa79392b 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,11 @@ fn get_2q_decomposers_from_target( OperationRef::Standard(_) => (), _ => continue, } - + // Filter out non-2q-gate candidates + match op.operation.num_qubits() { + 2 => (), + _ => continue, + } available_2q_basis.insert(key, replace_parametrized_gate(op.clone())); if target.contains_key(key) { 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() From 1fc3ec11a63c8bb2f936ade146cf94a6601d3bb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= Date: Fri, 10 Jan 2025 14:55:40 +0100 Subject: [PATCH 2/5] Add reno --- ...ix-unitary-synthesis-global-gates-19b93840b28cfcf7.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 releasenotes/notes/fix-unitary-synthesis-global-gates-19b93840b28cfcf7.yaml 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..733bdce93d16 --- /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 incuded in the available 2 qubit basis, + causing the ``TwoQubitWeylDecomposition`` to panic because of + the dimension mismatch. \ No newline at end of file From 0ecc5982e5c5e1ac33b522429592d9aae14803c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= <57907331+ElePT@users.noreply.github.com> Date: Fri, 10 Jan 2025 15:19:52 +0100 Subject: [PATCH 3/5] Apply Matt's suggestion Co-authored-by: Matthew Treinish --- crates/accelerate/src/unitary_synthesis.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/accelerate/src/unitary_synthesis.rs b/crates/accelerate/src/unitary_synthesis.rs index 527caa79392b..67d7ebf2472a 100644 --- a/crates/accelerate/src/unitary_synthesis.rs +++ b/crates/accelerate/src/unitary_synthesis.rs @@ -599,9 +599,8 @@ fn get_2q_decomposers_from_target( _ => continue, } // Filter out non-2q-gate candidates - match op.operation.num_qubits() { - 2 => (), - _ => continue, + if op.operation.num_qubits() != 2 { + continue } available_2q_basis.insert(key, replace_parametrized_gate(op.clone())); From 34265f90388f421f910575ccc45c83257ac2d9a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= <57907331+ElePT@users.noreply.github.com> Date: Fri, 10 Jan 2025 15:39:48 +0100 Subject: [PATCH 4/5] Add semicolon --- crates/accelerate/src/unitary_synthesis.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/accelerate/src/unitary_synthesis.rs b/crates/accelerate/src/unitary_synthesis.rs index 67d7ebf2472a..439b9ba80f67 100644 --- a/crates/accelerate/src/unitary_synthesis.rs +++ b/crates/accelerate/src/unitary_synthesis.rs @@ -600,7 +600,7 @@ fn get_2q_decomposers_from_target( } // Filter out non-2q-gate candidates if op.operation.num_qubits() != 2 { - continue + continue; } available_2q_basis.insert(key, replace_parametrized_gate(op.clone())); From d45e247740900d6dd0f37e84e1759638f793c2ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elena=20Pe=C3=B1a=20Tapia?= <57907331+ElePT@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:07:29 +0100 Subject: [PATCH 5/5] Apply Shelly's suggestion --- .../fix-unitary-synthesis-global-gates-19b93840b28cfcf7.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/releasenotes/notes/fix-unitary-synthesis-global-gates-19b93840b28cfcf7.yaml b/releasenotes/notes/fix-unitary-synthesis-global-gates-19b93840b28cfcf7.yaml index 733bdce93d16..21310a4729a8 100644 --- a/releasenotes/notes/fix-unitary-synthesis-global-gates-19b93840b28cfcf7.yaml +++ b/releasenotes/notes/fix-unitary-synthesis-global-gates-19b93840b28cfcf7.yaml @@ -2,6 +2,6 @@ fixes: - | Fixed a bug in the :class:`.UnitarySynthesis` transpiler pass where - non-2-qubit gates would be incuded in the available 2 qubit basis, + non-2-qubit gates would be included in the available 2 qubit basis, causing the ``TwoQubitWeylDecomposition`` to panic because of - the dimension mismatch. \ No newline at end of file + the dimension mismatch.