Skip to content

Commit

Permalink
Fix input normalisation of transpile(initial_layout=...) (backport #…
Browse files Browse the repository at this point in the history
…11031) (#11058)

* Fix input normalisation of `transpile(initial_layout=...)` (#11031)

* Fixed issue10554 by modifying _parse_initial_layout in transpile to normalize input of range to a list

* Added test and bugfix note

* Handle consumable iterables

* Fix release-note cross references

---------

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
(cherry picked from commit 6148f90)

# Conflicts:
#	test/python/transpiler/test_transpile_layout.py

* Fix backport merge conflicts

The backport of this commit from main failed because the test for this
bug fix was added to an existing file on the main branch but that didn't
exist on the stable/0.25 branch. To fix this the test is moved to a
separate standalone file. Arguably, this should be done on main and
stable/0.45 too as the file the test was added to was for a different
component.

---------

Co-authored-by: Henry Zou <87874865+henryzou50@users.noreply.github.com>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
  • Loading branch information
3 people authored Oct 24, 2023
1 parent c3b652c commit 7080a22
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
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)

0 comments on commit 7080a22

Please sign in to comment.