diff --git a/qiskit/transpiler/preset_passmanagers/__init__.py b/qiskit/transpiler/preset_passmanagers/__init__.py index bec81501aab5..ee1e407ae87d 100644 --- a/qiskit/transpiler/preset_passmanagers/__init__.py +++ b/qiskit/transpiler/preset_passmanagers/__init__.py @@ -61,6 +61,7 @@ from qiskit.transpiler.passmanager_config import PassManagerConfig from qiskit.transpiler.target import target_to_backend_properties +from qiskit.transpiler import CouplingMap from .level0 import level_0_pass_manager from .level1 import level_1_pass_manager @@ -130,7 +131,7 @@ def generate_preset_pass_manager( circuit, transpiler attaches the custom gate definition to the circuit. This enables one to flexibly override the low-level instruction implementation. - coupling_map (CouplingMap): Directed graph represented a coupling + coupling_map (CouplingMap or list): Directed graph represented a coupling map. instruction_durations (InstructionDurations): Dictionary of duration (in dt) for each instruction. @@ -213,6 +214,9 @@ def generate_preset_pass_manager( stacklevel=2, ) + if coupling_map is not None and not isinstance(coupling_map, CouplingMap): + coupling_map = CouplingMap(coupling_map) + if target is not None: if coupling_map is None: coupling_map = target.build_coupling_map() diff --git a/releasenotes/notes/preset-passmanager-coupling-map-89e588e4260cb214.yaml b/releasenotes/notes/preset-passmanager-coupling-map-89e588e4260cb214.yaml new file mode 100644 index 000000000000..5830a57d2e2d --- /dev/null +++ b/releasenotes/notes/preset-passmanager-coupling-map-89e588e4260cb214.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Enhanced the :func:`.generate_preset_pass_manager` function to allow users to + provide a coupling map as a legacy-format list. Previously, users were required + to provide a :class:`.CouplingMap` object. diff --git a/test/python/transpiler/test_preset_passmanagers.py b/test/python/transpiler/test_preset_passmanagers.py index c1df58eb03cf..d37d5c2cbaad 100644 --- a/test/python/transpiler/test_preset_passmanagers.py +++ b/test/python/transpiler/test_preset_passmanagers.py @@ -1272,7 +1272,7 @@ def test_size_optimization(self, level): @ddt -class TestGeenratePresetPassManagers(QiskitTestCase): +class TestGeneratePresetPassManagers(QiskitTestCase): """Test generate_preset_pass_manager function.""" @data(0, 1, 2, 3) @@ -1445,6 +1445,37 @@ def get_translation_stage_plugin(self): ] self.assertIn("RemoveResetInZeroState", post_translation_pass_list) + def test_generate_preset_pass_manager_with_list_coupling_map(self): + """Test that generate_preset_pass_manager can handle list-based coupling_map.""" + + # Define the coupling map as a list + coupling_map_list = [[0, 1]] + coupling_map_object = CouplingMap(coupling_map_list) + + # Circuit that doesn't fit in the coupling map + qc = QuantumCircuit(2) + qc.h(0) + qc.cx(0, 1) + qc.cx(1, 0) + qc.measure_all() + + pm_list = generate_preset_pass_manager( + optimization_level=0, coupling_map=coupling_map_list, seed_transpiler=42 + ) + pm_object = generate_preset_pass_manager( + optimization_level=0, coupling_map=coupling_map_object, seed_transpiler=42 + ) + + transpiled_circuit_list = pm_list.run(qc) + transpiled_circuit_object = pm_object.run(qc) + + # Check if both are instances of PassManager + self.assertIsInstance(pm_list, PassManager) + self.assertIsInstance(pm_object, PassManager) + + # Ensure the DAGs from both methods are identical + self.assertEqual(transpiled_circuit_list, transpiled_circuit_object) + @ddt class TestIntegrationControlFlow(QiskitTestCase):