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

Add layout score analysis pass #3321

Merged
merged 20 commits into from
Nov 1, 2019
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
1 change: 1 addition & 0 deletions qiskit/transpiler/passes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from .mapping.dense_layout import DenseLayout
from .mapping.noise_adaptive_layout import NoiseAdaptiveLayout
from .mapping.basic_swap import BasicSwap
from .mapping.layout_score import LayoutScore
from .mapping.lookahead_swap import LookaheadSwap
from .remove_diagonal_gates_before_measure import RemoveDiagonalGatesBeforeMeasure
from .mapping.stochastic_swap import StochasticSwap
Expand Down
63 changes: 63 additions & 0 deletions qiskit/transpiler/passes/mapping/layout_score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2019.
#
# 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.

"""Evaluate how good the layout selection was.

No CX direction is considered.
Saves in `property_set['layout_score']` the sum of distances for each circuit CX.
The lower the number, the better the selection.
Therefore, 0 is a perfect layout selection.
"""

from qiskit.transpiler.basepasses import AnalysisPass


class LayoutScore(AnalysisPass):
"""Evaluate how good the layout selection was.

Saves in `property_set['layout_score']` the sum of distances for each circuit CX.
The lower the number, the better the selection. Therefore, 0 is a perfect layout selection.
No CX direction is considered.
"""
def __init__(self, coupling_map, initial_layout=None):
"""LayoutScore initializer.

Args:
coupling_map (CouplingMap): Directed graph represented a coupling map.
initial_layout (Layout): The layout to evaluate.
"""
super().__init__()
self.layout = initial_layout
self.coupling_map = coupling_map

def run(self, dag):
"""
Run the LayoutScore pass on `dag`.
Args:
dag (DAGCircuit): DAG to evaluate.
"""
self.layout = self.layout or self.property_set["layout"]

if self.layout is None:
return

distances = []

for gate in dag.twoQ_gates():
physical_q0 = self.layout[gate.qargs[0]]
physical_q1 = self.layout[gate.qargs[1]]

distances.append(self.coupling_map.distance(physical_q0, physical_q1)-1)

self.property_set['layout_score'] = sum(distances)
106 changes: 106 additions & 0 deletions test/python/transpiler/test_layout_score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2019.
#
# 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 Layout Score pass"""

import unittest

from qiskit import QuantumRegister, QuantumCircuit
from qiskit.transpiler.passes import LayoutScore
from qiskit.transpiler import CouplingMap, Layout
from qiskit.converters import circuit_to_dag
from qiskit.test import QiskitTestCase


class TestLayoutScoreError(QiskitTestCase):
"""Test error-ish of Layout Score"""

def test_no_layout(self):
"""No Layout. Empty Circuit CouplingMap map: None. Result: None
"""
qr = QuantumRegister(3, 'qr')
circuit = QuantumCircuit(qr)
coupling = CouplingMap()
layout = None

dag = circuit_to_dag(circuit)
pass_ = LayoutScore(coupling, layout)
pass_.run(dag)

self.assertIsNone(pass_.property_set['layout_score'])


class TestTrivialLayoutScore(QiskitTestCase):
""" Trivial layout scenarios"""

def test_no_cx(self):
"""Empty Circuit CouplingMap map: None. Result: 0
"""
qr = QuantumRegister(3, 'qr')
circuit = QuantumCircuit(qr)
coupling = CouplingMap()
layout = Layout().generate_trivial_layout(qr)

dag = circuit_to_dag(circuit)
pass_ = LayoutScore(coupling, layout)
pass_.run(dag)

self.assertEqual(pass_.property_set['layout_score'], 0)

def test_swap_mapped_true(self):
""" Mapped circuit. Good Layout
qr0 (0):--(+)---(+)-
| |
qr1 (1):---.-----|--
|
qr2 (2):---------.--

CouplingMap map: [1]--[0]--[2]
"""
qr = QuantumRegister(3, 'qr')
circuit = QuantumCircuit(qr)
circuit.cx(qr[0], qr[1])
circuit.cx(qr[0], qr[2])
coupling = CouplingMap([[0, 1], [0, 2]])
layout = Layout().generate_trivial_layout(qr)

dag = circuit_to_dag(circuit)
pass_ = LayoutScore(coupling, layout)
pass_.run(dag)

self.assertEqual(pass_.property_set['layout_score'], 0)

def test_swap_mapped_false(self):
""" Needs [0]-[1] in a [0]--[2]--[1] Result:1
qr0:--(+)--
|
qr1:---.---

CouplingMap map: [0]--[2]--[1]
"""
qr = QuantumRegister(2, 'qr')
circuit = QuantumCircuit(qr)
circuit.cx(qr[0], qr[1])
coupling = CouplingMap([[0, 2], [2, 1]])
layout = Layout().generate_trivial_layout(qr)

dag = circuit_to_dag(circuit)
pass_ = LayoutScore(coupling, layout)
pass_.run(dag)

self.assertEqual(pass_.property_set['layout_score'], 1)


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