Skip to content

Commit

Permalink
Fix input normalisation of transpile(initial_layout=...) (#11031)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
henryzou50 and jakelishman authored Oct 19, 2023
1 parent 9f3a7cf commit 6148f90
Show file tree
Hide file tree
Showing 3 changed files with 18 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 @@ -512,10 +512,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>`__.
10 changes: 10 additions & 0 deletions test/python/transpiler/test_transpile_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,13 @@ def test_initial_virtual_layout_filter_ancillas(self):
}
),
)

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_index_layout(), tqc_2.layout.initial_index_layout())

0 comments on commit 6148f90

Please sign in to comment.