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

Extends SetLayout to take a list #10344

Merged
merged 13 commits into from
Jul 14, 2023
29 changes: 24 additions & 5 deletions qiskit/transpiler/passes/layout/set_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
# that they have been altered from the originals.

"""Set the ``layout`` property to the given layout."""


from qiskit.transpiler import Layout, TranspilerError
from qiskit.transpiler.basepasses import AnalysisPass


Expand All @@ -27,19 +26,39 @@ def __init__(self, layout):
"""SetLayout initializer.

Args:
layout (Layout): the layout to set.
layout (Layout or List[int]): the layout to set. It can be:

* a :class:`Layout` instance: sets that layout.
* a list of integers: takes the index in the list as the physical position in which the
virtual qubit is going to be mapped.

"""
super().__init__()
self.layout = layout

def run(self, dag):
"""Run the SetLayout pass on `dag`.
"""Run the SetLayout pass on ``dag``.

Args:
dag (DAGCircuit): DAG to map.

Returns:
DAGCircuit: the original DAG.
"""
self.property_set["layout"] = None if self.layout is None else self.layout.copy()
if isinstance(self.layout, list):
if len(self.layout) != len(dag.qubits):
raise TranspilerError(
"The length of the layout is different than the size of the "
f"circuit: {len(self.layout)} <> {len(dag.qubits)}"
)
layout = Layout({phys: dag.qubits[i] for i, phys in enumerate(self.layout)})
elif isinstance(self.layout, Layout):
layout = self.layout.copy()
elif self.layout is None:
layout = None
else:
raise TranspilerError(
f"SetLayout was intialized with the layout type: {type(self.layout)}"
)
self.property_set["layout"] = layout
return dag
4 changes: 4 additions & 0 deletions releasenotes/notes/fixes_8060-ae91e0da9d53a288.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features:
- |
The transpiler pass :class:`SetLayout` now is able to be constructed with a list of integers that represent the physical qubits on which the quantum circuit will be mapped on. That is, the first qubit in the circuit will be allocated in the physical qubit in the position zero of the list, and so on.
114 changes: 114 additions & 0 deletions test/python/transpiler/test_setlayout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# 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.

"""Test the SetLayout pass"""

import unittest

from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
from qiskit.transpiler import CouplingMap, Layout
from qiskit.transpiler.passes import SetLayout, ApplyLayout, FullAncillaAllocation
from qiskit.test import QiskitTestCase
from qiskit.transpiler import PassManager, TranspilerError


class TestSetLayout(QiskitTestCase):
"""Tests the SetLayout pass"""

def assertEqualToReference(self, result_to_compare):
"""Compare result_to_compare to a reference

┌───┐ ░ ┌─┐
q_0 -> 0 ┤ H ├─░─┤M├───────────────
├───┤ ░ └╥┘┌─┐
q_1 -> 1 ┤ H ├─░──╫─┤M├────────────
├───┤ ░ ║ └╥┘ ┌─┐
q_4 -> 2 ┤ H ├─░──╫──╫───────┤M├───
├───┤ ░ ║ ║ ┌─┐ └╥┘
q_2 -> 3 ┤ H ├─░──╫──╫─┤M├────╫────
└───┘ ░ ║ ║ └╥┘ ║
ancilla_0 -> 4 ─────────╫──╫──╫─────╫────
┌───┐ ░ ║ ║ ║ ┌─┐ ║
q_3 -> 5 ┤ H ├─░──╫──╫──╫─┤M├─╫────
├───┤ ░ ║ ║ ║ └╥┘ ║ ┌─┐
q_5 -> 6 ┤ H ├─░──╫──╫──╫──╫──╫─┤M├
└───┘ ░ ║ ║ ║ ║ ║ └╥┘
meas: 6/═════════╩══╩══╩══╩══╩══╩═
0 1 2 3 4 5
"""
qr = QuantumRegister(6, "q")
ancilla = QuantumRegister(1, "ancilla")
cl = ClassicalRegister(6, "meas")
reference = QuantumCircuit(qr, ancilla, cl)
reference.h(qr)
reference.barrier(qr)
reference.measure(qr, cl)
pass_manager = PassManager()
pass_manager.append(
SetLayout(
Layout({qr[0]: 0, qr[1]: 1, qr[4]: 2, qr[2]: 3, ancilla[0]: 4, qr[3]: 5, qr[5]: 6})
)
)
pass_manager.append(ApplyLayout())
self.assertEqual(result_to_compare, pass_manager.run(reference))

def test_setlayout_as_Layout(self):
"""Construct SetLayout with a Layout."""

qr = QuantumRegister(6, "q")
circuit = QuantumCircuit(qr)
circuit.h(qr)
circuit.measure_all()

pass_manager = PassManager()
pass_manager.append(
SetLayout(Layout.from_intlist([0, 1, 3, 5, 2, 6], QuantumRegister(6, "q")))
)
pass_manager.append(FullAncillaAllocation(CouplingMap.from_line(7)))
pass_manager.append(ApplyLayout())
result = pass_manager.run(circuit)

self.assertEqualToReference(result)

def test_setlayout_as_list(self):
"""Construct SetLayout with a list."""

qr = QuantumRegister(6, "q")
circuit = QuantumCircuit(qr)
circuit.h(qr)
circuit.measure_all()

pass_manager = PassManager()
pass_manager.append(SetLayout([0, 1, 3, 5, 2, 6]))
pass_manager.append(FullAncillaAllocation(CouplingMap.from_line(7)))
pass_manager.append(ApplyLayout())
result = pass_manager.run(circuit)

self.assertEqualToReference(result)

def test_raise_when_layout_len_does_not_match(self):
"""Test error is raised if layout defined as list does not match the circuit size."""

qr = QuantumRegister(42, "q")
circuit = QuantumCircuit(qr)

pass_manager = PassManager()
pass_manager.append(SetLayout([0, 1, 3, 5, 2, 6]))
pass_manager.append(FullAncillaAllocation(CouplingMap.from_line(7)))
pass_manager.append(ApplyLayout())

with self.assertRaises(TranspilerError):
pass_manager.run(circuit)


if __name__ == "__main__":
unittest.main()