Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix input normalisation of transpile(initial_layout=...) (backport #11031) #11058

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions qiskit/compiler/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/Qiskit/qiskit/issues/10554>`__.
34 changes: 34 additions & 0 deletions test/python/transpiler/test_set_layout.py
Original file line number Diff line number Diff line change
@@ -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)
Loading