Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
15 changes: 11 additions & 4 deletions qiskit/transpiler/passes/layout/full_ancilla_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ def run(self, dag):
if layout is None:
raise TranspilerError('FullAncillaAllocation pass requires property_set["layout"].')

layout_physical_qubits = layout.get_physical_bits().keys()
coupling_physical_qubits = self.coupling_map.physical_qubits
idle_physical_qubits = [q for q in coupling_physical_qubits
if q not in layout_physical_qubits]
if layout:
layout_physical_qubits = list(range(max(layout.get_physical_bits()) + 1))
else:
layout_physical_qubits = []

idle_physical_qubits = [q for q in layout_physical_qubits
if q not in layout.get_physical_bits()]

if self.coupling_map:
idle_physical_qubits = [q for q in self.coupling_map.physical_qubits
if q not in layout.get_physical_bits()]

if idle_physical_qubits:
if self.ancilla_name in dag.qregs:
Expand Down
3 changes: 3 additions & 0 deletions qiskit/transpiler/passes/utils/check_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def run(self, dag):
"""
self.property_set['is_swap_mapped'] = True

if self.coupling_map is None:
return

for gate in dag.two_qubit_ops():
physical_q0 = gate.qargs[0].index
physical_q1 = gate.qargs[1].index
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/preset_passmanagers/level0.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def _direction_condition(property_set):

# Build pass manager
pm0 = PassManager()
if coupling_map:
if coupling_map or initial_layout:
pm0.append(_given_layout)
pm0.append(_choose_layout, condition=_choose_layout_condition)
pm0.append(_embed)
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/preset_passmanagers/level1.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def _opt_control(property_set):

# Build pass manager
pm1 = PassManager()
if coupling_map:
if coupling_map or initial_layout:
pm1.append(_given_layout)
pm1.append(_choose_layout_and_score, condition=_choose_layout_condition)
pm1.append(_improve_layout, condition=_not_perfect_yet)
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/preset_passmanagers/level2.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def _opt_control(property_set):

# Build pass manager
pm2 = PassManager()
if coupling_map:
if coupling_map or initial_layout:
pm2.append(_given_layout)
pm2.append(_choose_layout_1, condition=_choose_layout_condition)
pm2.append(_choose_layout_2, condition=_choose_layout_condition)
Expand Down
2 changes: 1 addition & 1 deletion qiskit/transpiler/preset_passmanagers/level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def _opt_control(property_set):
pm3 = PassManager()
pm3.append(_unroll3q)
pm3.append(_reset + _meas)
if coupling_map:
if coupling_map or initial_layout:
pm3.append(_given_layout)
pm3.append(_choose_layout_1, condition=_choose_layout_condition)
pm3.append(_choose_layout_2, condition=_choose_layout_condition)
Expand Down
34 changes: 34 additions & 0 deletions test/python/transpiler/test_preset_passmanagers.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,40 @@ def test_symmetric_coupling_map(self, level):
self.assertIn('ApplyLayout', self.passes)
self.assertNotIn('CheckCXDirection', self.passes)

@data(0, 1, 2, 3)
def test_inital_layout_fully_connected_cm(self, level):
"""Honor initial_layout when coupling_map=None
See: https://github.com/Qiskit/qiskit-terra/issues/5345
"""
qr = QuantumRegister(2, 'q')
qc = QuantumCircuit(qr)
qc.h(qr[0])
qc.cx(qr[0], qr[1])

transpiled = transpile(qc, initial_layout=[0, 1], optimization_level=level,
callback=self.callback)

self.assertIn('SetLayout', self.passes)
self.assertIn('ApplyLayout', self.passes)
self.assertEqual(len(transpiled._layout), 2)

@data(0, 1, 2, 3)
def test_partial_layout_fully_connected_cm(self, level):
"""Honor initial_layout (partially defined) when coupling_map=None
See: https://github.com/Qiskit/qiskit-terra/issues/5345
"""
qr = QuantumRegister(2, 'q')
qc = QuantumCircuit(qr)
qc.h(qr[0])
qc.cx(qr[0], qr[1])

transpiled = transpile(qc, initial_layout=[4, 2], optimization_level=level,
callback=self.callback)
Comment on lines +197 to +198

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so what does transpiled look like here? is it a 2 qubit circuit or a 5 qubit circuit?

@1ucian0 1ucian0 Nov 9, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5, see line 202 and #5345 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think taking the max of the initial layout makes sense for these situations.


self.assertIn('SetLayout', self.passes)
self.assertIn('ApplyLayout', self.passes)
self.assertEqual(len(transpiled._layout), 5)


@ddt
class TestInitialLayouts(QiskitTestCase):
Expand Down