diff --git a/qiskit/compiler/transpiler.py b/qiskit/compiler/transpiler.py index d93d0ce4e380..b69c8695c280 100644 --- a/qiskit/compiler/transpiler.py +++ b/qiskit/compiler/transpiler.py @@ -503,10 +503,10 @@ def _parse_initial_layout(initial_layout): return initial_layout if isinstance(initial_layout, dict): return Layout(initial_layout) + initial_layout = list(initial_layout) if all(phys is None or isinstance(phys, Qubit) for phys in initial_layout): return Layout.from_qubit_list(initial_layout) - else: - return initial_layout + return initial_layout def _parse_instruction_durations(backend, inst_durations, dt, circuit): diff --git a/releasenotes/notes/fix-input-normalization-of-transpile-initial-layout.yaml b/releasenotes/notes/fix-input-normalization-of-transpile-initial-layout.yaml new file mode 100644 index 000000000000..a996861ce992 --- /dev/null +++ b/releasenotes/notes/fix-input-normalization-of-transpile-initial-layout.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixed a regression in :func:`.transpile`, where an ``initial_layout`` given + as a :class:`range` object would no longer be treated as a valid input. + Fixed `#10544 `__. diff --git a/test/python/transpiler/test_set_layout.py b/test/python/transpiler/test_set_layout.py new file mode 100644 index 000000000000..d12c1f448d2f --- /dev/null +++ b/test/python/transpiler/test_set_layout.py @@ -0,0 +1,34 @@ +# This code is part of Qiskit. +# +# (C) Copyright IBM 2023 +# +# This code is licensed under the Apache License, Version 2.0. You may +# obtain a copy of this license in the LICENSE.txt file in the root directory +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +# +# Any modifications or derivative works of this code must retain this +# copyright notice, and modified files need to carry a notice indicating +# that they have been altered from the originals. + +# pylint: disable=missing-function-docstring + +"""Tests the SetLayout pass""" + +from qiskit.compiler import transpile +from qiskit.circuit import QuantumCircuit +from qiskit.transpiler import CouplingMap +from qiskit.test import QiskitTestCase + + +class TestSetLayout(QiskitTestCase): + """Test the SetLayout pass.""" + + def test_initial_layout_consistency_for_range_and_list(self): + qc = QuantumCircuit(3) + qc.h(0) + qc.cx(0, 1) + qc.cx(0, 2) + cmap = CouplingMap.from_line(3, bidirectional=False) + tqc_1 = transpile(qc, coupling_map=cmap, initial_layout=range(3), seed_transpiler=42) + tqc_2 = transpile(qc, coupling_map=cmap, initial_layout=list(range(3)), seed_transpiler=42) + self.assertEqual(tqc_1.layout.initial_layout, tqc_2.layout.initial_layout)