Repository: Qiskit/qiskit

Issue:
Change the name Lookaheadmapper and basicmapper
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues to confirm this idea does not exist. -->

### What is the expected enhancement?
In the folder, it is not clear what is a mapper and what is not. My view (also @ajavadia) is all steps are a mapper. Mapping is all things combined: swap insertion, fix direction, unroll to native gates, etc.

- [x] Unroller
- [ ] LookaheadSwap
- [ ] StochasticSwap should happend in (#1520)
- [ ] BasicSwap  
- [ ] LayoutSelect should happen in (#1499)
- [x] CXDirection (pr #1521)






Discussion:
@ajavadia and @1ucian0 are we going to get these done. 
Yes I'll do it.

Resolving patch:
diff --git a/qiskit/transpiler/passes/__init__.py b/qiskit/transpiler/passes/__init__.py
--- a/qiskit/transpiler/passes/__init__.py
+++ b/qiskit/transpiler/passes/__init__.py
@@ -13,7 +13,7 @@
 from .decompose import Decompose
 from .barrier_before_final_measurements import BarrierBeforeFinalMeasurements
 from .mapping.check_map import CheckMap
-from .mapping.basic_mapper import BasicMapper
 from .mapping.cx_direction import CXDirection
 from .mapping.unroller import Unroller
-from .mapping.lookahead_mapper import LookaheadMapper
+from .mapping.basic_swap import BasicSwap
+from .mapping.lookahead_swap import LookaheadSwap
diff --git a/qiskit/transpiler/passes/mapping/basic_mapper.py b/qiskit/transpiler/passes/mapping/basic_swap.py
similarity index 97%
rename from qiskit/transpiler/passes/mapping/basic_mapper.py
rename to qiskit/transpiler/passes/mapping/basic_swap.py
--- a/qiskit/transpiler/passes/mapping/basic_mapper.py
+++ b/qiskit/transpiler/passes/mapping/basic_swap.py
@@ -21,7 +21,7 @@
 from qiskit.extensions.standard import SwapGate
 
 
-class BasicMapper(TransformationPass):
+class BasicSwap(TransformationPass):
     """
     Maps (with minimum effort) a DAGCircuit onto a `coupling_map` adding swap gates.
     """
@@ -42,7 +42,7 @@ def __init__(self,
 
     def run(self, dag):
         """
-        Runs the BasicMapper pass on `dag`.
+        Runs the BasicSwap pass on `dag`.
         Args:
             dag (DAGCircuit): DAG to map.
 
diff --git a/qiskit/transpiler/passes/mapping/lookahead_mapper.py b/qiskit/transpiler/passes/mapping/lookahead_swap.py
similarity index 99%
rename from qiskit/transpiler/passes/mapping/lookahead_mapper.py
rename to qiskit/transpiler/passes/mapping/lookahead_swap.py
--- a/qiskit/transpiler/passes/mapping/lookahead_mapper.py
+++ b/qiskit/transpiler/passes/mapping/lookahead_swap.py
@@ -52,11 +52,11 @@
 SEARCH_WIDTH = 4
 
 
-class LookaheadMapper(TransformationPass):
+class LookaheadSwap(TransformationPass):
     """Map input circuit onto a backend topology via insertion of SWAPs."""
 
     def __init__(self, coupling_map):
-        """Initialize a LookaheadMapper instance.
+        """Initialize a LookaheadSwap instance.
 
         Arguments:
             coupling_map (CouplingMap): CouplingMap of the target backend.


Test patch:
diff --git a/test/python/test_transpiler.py b/test/python/test_transpiler.py
deleted file mode 100644
--- a/test/python/test_transpiler.py
+++ /dev/null
@@ -1,177 +0,0 @@
-# -*- coding: utf-8 -*-
-
-# Copyright 2018, IBM.
-#
-# This source code is licensed under the Apache License, Version 2.0 found in
-# the LICENSE.txt file in the root directory of this source tree.
-
-# pylint: disable=redefined-builtin
-
-"""Tests for transpiler functionality"""
-
-from qiskit import QuantumRegister, QuantumCircuit
-from qiskit import compile, BasicAer
-from qiskit.transpiler import PassManager, transpile_dag, transpile
-from qiskit.tools.compiler import circuits_to_qobj
-from qiskit.transpiler.passes import CXCancellation, LookaheadMapper
-from qiskit.converters import circuit_to_dag
-from qiskit.mapper import CouplingMap
-
-from .common import QiskitTestCase
-
-
-class TestTranspiler(QiskitTestCase):
-    """Test combining and extending of QuantumCircuits."""
-
-    def test_pass_manager_empty(self):
-        """Test passing an empty PassManager() to the transpiler.
-
-        It should perform no transformations on the circuit.
-        """
-        qr = QuantumRegister(2)
-        circuit = QuantumCircuit(qr)
-        circuit.h(qr[0])
-        circuit.h(qr[0])
-        circuit.cx(qr[0], qr[1])
-        circuit.cx(qr[0], qr[1])
-        circuit.cx(qr[0], qr[1])
-        circuit.cx(qr[0], qr[1])
-        dag_circuit = circuit_to_dag(circuit)
-        resources_before = dag_circuit.count_ops()
-
-        pass_manager = PassManager()
-        dag_circuit = transpile_dag(dag_circuit, pass_manager=pass_manager)
-        resources_after = dag_circuit.count_ops()
-
-        self.assertDictEqual(resources_before, resources_after)
-
-    def test_pass_manager_none(self):
-        """Test passing the default (None) pass manager to the transpiler.
-
-        It should perform the default qiskit flow:
-        unroll, swap_mapper, cx_direction, cx_cancellation, optimize_1q_gates
-        and should be equivalent to using tools.compile
-        """
-        qr = QuantumRegister(2, 'qr')
-        circuit = QuantumCircuit(qr)
-        circuit.h(qr[0])
-        circuit.h(qr[0])
-        circuit.cx(qr[0], qr[1])
-        circuit.cx(qr[0], qr[1])
-        circuit.cx(qr[0], qr[1])
-        circuit.cx(qr[0], qr[1])
-
-        coupling_map = [[1, 0]]
-        basis_gates = 'u1,u2,u3,cx,id'
-
-        backend = BasicAer.get_backend('qasm_simulator')
-        circuit2 = transpile(circuit, backend, coupling_map=coupling_map, basis_gates=basis_gates,
-                             pass_manager=None)
-
-        qobj = compile(circuit, backend=backend, coupling_map=coupling_map, basis_gates=basis_gates)
-
-        qobj2 = circuits_to_qobj(circuit2, backend.name(), basis_gates=basis_gates,
-                                 coupling_map=coupling_map, qobj_id=qobj.qobj_id)
-
-        self.assertEqual(qobj, qobj2)
-
-    def test_pass_cx_cancellation(self):
-        """Test the cx cancellation pass.
-
-        It should cancel consecutive cx pairs on same qubits.
-        """
-        qr = QuantumRegister(2)
-        circuit = QuantumCircuit(qr)
-        circuit.h(qr[0])
-        circuit.h(qr[0])
-        circuit.cx(qr[0], qr[1])
-        circuit.cx(qr[0], qr[1])
-        circuit.cx(qr[0], qr[1])
-        circuit.cx(qr[0], qr[1])
-        circuit.cx(qr[1], qr[0])
-        circuit.cx(qr[1], qr[0])
-        dag_circuit = circuit_to_dag(circuit)
-
-        pass_manager = PassManager()
-        pass_manager.append(CXCancellation())
-        dag_circuit = transpile_dag(dag_circuit, pass_manager=pass_manager)
-        resources_after = dag_circuit.count_ops()
-
-        self.assertNotIn('cx', resources_after)
-
-    def test_lookahead_mapper_doesnt_modify_mapped_circuit(self):
-        """Test that lookahead mapper is idempotent.
-
-        It should not modify a circuit which is already compatible with the
-        coupling map, and can be applied repeatedly without modifying the circuit.
-        """
-
-        qr = QuantumRegister(3, name='q')
-        circuit = QuantumCircuit(qr)
-        circuit.cx(qr[0], qr[2])
-        circuit.cx(qr[0], qr[1])
-        original_dag = circuit_to_dag(circuit)
-
-        # Create coupling map which contains all two-qubit gates in the circuit.
-        coupling_map = CouplingMap(couplinglist=[(0, 1), (0, 2)])
-
-        pass_manager = PassManager()
-        pass_manager.append(LookaheadMapper(coupling_map))
-        mapped_dag = transpile_dag(original_dag, pass_manager=pass_manager)
-
-        self.assertEqual(original_dag, mapped_dag)
-
-        second_pass_manager = PassManager()
-        second_pass_manager.append(LookaheadMapper(coupling_map))
-        remapped_dag = transpile_dag(mapped_dag, pass_manager=second_pass_manager)
-
-        self.assertEqual(mapped_dag, remapped_dag)
-
-    def test_lookahead_mapper_should_add_a_single_swap(self):
-        """Test that lookahead mapper will insert a SWAP to match layout.
-
-        For a single cx gate which is not available in the current layout, test
-        that the mapper inserts a single swap to enable the gate.
-        """
-
-        qr = QuantumRegister(3)
-        circuit = QuantumCircuit(qr)
-        circuit.cx(qr[0], qr[2])
-        dag_circuit = circuit_to_dag(circuit)
-
-        coupling_map = CouplingMap(couplinglist=[(0, 1), (1, 2)])
-
-        pass_manager = PassManager()
-        pass_manager.append([LookaheadMapper(coupling_map)])
-        mapped_dag = transpile_dag(dag_circuit, pass_manager=pass_manager)
-
-        self.assertEqual(mapped_dag.count_ops().get('swap', 0),
-                         dag_circuit.count_ops().get('swap', 0) + 1)
-
-    def test_lookahead_mapper_finds_minimal_swap_solution(self):
-        """Of many valid SWAPs, test that lookahead finds the cheapest path.
-
-        For a two CNOT circuit: cx q[0],q[2]; cx q[0],q[1]
-        on the initial layout: qN -> qN
-        (At least) two solutions exist:
-        - SWAP q[0],[1], cx q[0],q[2], cx q[0],q[1]
-        - SWAP q[1],[2], cx q[0],q[2], SWAP q[1],q[2], cx q[0],q[1]
-
-        Verify that we find the first solution, as it requires fewer SWAPs.
-        """
-
-        qr = QuantumRegister(3)
-        circuit = QuantumCircuit(qr)
-        circuit.cx(qr[0], qr[2])
-        circuit.cx(qr[0], qr[1])
-
-        dag_circuit = circuit_to_dag(circuit)
-
-        coupling_map = CouplingMap(couplinglist=[(0, 1), (1, 2)])
-
-        pass_manager = PassManager()
-        pass_manager.append([LookaheadMapper(coupling_map)])
-        mapped_dag = transpile_dag(dag_circuit, pass_manager=pass_manager)
-
-        self.assertEqual(mapped_dag.count_ops().get('swap', 0),
-                         dag_circuit.count_ops().get('swap', 0) + 1)
diff --git a/test/python/transpiler/test_basic_mapper.py b/test/python/transpiler/test_basic_swap.py
similarity index 94%
rename from test/python/transpiler/test_basic_mapper.py
rename to test/python/transpiler/test_basic_swap.py
--- a/test/python/transpiler/test_basic_mapper.py
+++ b/test/python/transpiler/test_basic_swap.py
@@ -5,18 +5,18 @@
 # This source code is licensed under the Apache License, Version 2.0 found in
 # the LICENSE.txt file in the root directory of this source tree.
 
-"""Test the Basic Mapper pass"""
+"""Test the BasicSwap pass"""
 
 import unittest
-from qiskit.transpiler.passes import BasicMapper
+from qiskit.transpiler.passes import BasicSwap
 from qiskit.mapper import CouplingMap
 from qiskit.converters import circuit_to_dag
 from qiskit import QuantumRegister, QuantumCircuit
 from ..common import QiskitTestCase
 
 
-class TestBasicMapper(QiskitTestCase):
-    """ Tests the BasicMapper pass."""
+class TestBasicSwap(QiskitTestCase):
+    """ Tests the BasicSwap pass."""
 
     def test_trivial_case(self):
         """No need to have any swap, the CX are distance 1 to each other
@@ -37,7 +37,7 @@ def test_trivial_case(self):
         circuit.cx(qr[0], qr[2])
 
         dag = circuit_to_dag(circuit)
-        pass_ = BasicMapper(coupling)
+        pass_ = BasicSwap(coupling)
         after = pass_.run(dag)
 
         self.assertEqual(dag, after)
@@ -62,7 +62,7 @@ def test_trivial_in_same_layer(self):
         circuit.cx(qr[0], qr[1])
 
         dag = circuit_to_dag(circuit)
-        pass_ = BasicMapper(coupling)
+        pass_ = BasicSwap(coupling)
         after = pass_.run(dag)
 
         self.assertEqual(dag, after)
@@ -95,7 +95,7 @@ def test_a_single_swap(self):
         expected.swap(qr[1], qr[0])
         expected.cx(qr[0], qr[2])
 
-        pass_ = BasicMapper(coupling)
+        pass_ = BasicSwap(coupling)
         after = pass_.run(dag)
 
         self.assertEqual(circuit_to_dag(expected), after)
@@ -129,7 +129,7 @@ def test_keep_layout(self):
         expected.cx(qr[1], qr[2])
         expected.h(qr[1])
 
-        pass_ = BasicMapper(coupling)
+        pass_ = BasicSwap(coupling)
         after = pass_.run(dag)
 
         self.assertEqual(circuit_to_dag(expected), after)
@@ -169,7 +169,7 @@ def test_far_swap(self):
         expected.cx(qr[2], qr[3])
         expected.cx(qr[3], qr[2])
 
-        pass_ = BasicMapper(coupling)
+        pass_ = BasicSwap(coupling)
         after = pass_.run(dag)
 
         self.assertEqual(circuit_to_dag(expected), after)
@@ -209,7 +209,7 @@ def test_far_swap_with_gate_the_front(self):
         expected.swap(qr[2], qr[1])
         expected.cx(qr[1], qr[0])
 
-        pass_ = BasicMapper(coupling)
+        pass_ = BasicSwap(coupling)
         after = pass_.run(dag)
 
         self.assertEqual(circuit_to_dag(expected), after)
@@ -249,7 +249,7 @@ def test_far_swap_with_gate_the_back(self):
         expected.cx(qr[1], qr[0])
         expected.h(qr[1])
 
-        pass_ = BasicMapper(coupling)
+        pass_ = BasicSwap(coupling)
         after = pass_.run(dag)
 
         self.assertEqual(circuit_to_dag(expected), after)
@@ -291,7 +291,7 @@ def test_far_swap_with_gate_the_middle(self):
         expected.h(qr[1])
         expected.cx(qr[0], qr[1])
 
-        pass_ = BasicMapper(coupling)
+        pass_ = BasicSwap(coupling)
         after = pass_.run(dag)
 
         self.assertEqual(circuit_to_dag(expected), after)
@@ -328,7 +328,7 @@ def test_swap_between_qregs(self):
         expected.swap(qr1[0], qr0[0])
         expected.cx(qr0[0], qr1[1])
 
-        pass_ = BasicMapper(coupling)
+        pass_ = BasicSwap(coupling)
         after = pass_.run(dag)
 
         self.assertEqual(circuit_to_dag(expected), after)
diff --git a/test/python/transpiler/test_cx_cancellation.py b/test/python/transpiler/test_cx_cancellation.py
new file mode 100644
--- /dev/null
+++ b/test/python/transpiler/test_cx_cancellation.py
@@ -0,0 +1,45 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2018, IBM.
+#
+# This source code is licensed under the Apache License, Version 2.0 found in
+# the LICENSE.txt file in the root directory of this source tree.
+
+# pylint: disable=redefined-builtin
+
+"""Tests for pass cancelling 2 consecutive CNOTs on the same qubits."""
+
+from qiskit import QuantumRegister, QuantumCircuit
+from qiskit.transpiler import PassManager, transpile_dag
+from qiskit.transpiler.passes import CXCancellation
+from qiskit.converters import circuit_to_dag
+
+from ..common import QiskitTestCase
+
+
+class TestCXCancellation(QiskitTestCase):
+    """Test the CXCancellation pass."""
+
+    def test_pass_cx_cancellation(self):
+        """Test the cx cancellation pass.
+
+        It should cancel consecutive cx pairs on same qubits.
+        """
+        qr = QuantumRegister(2)
+        circuit = QuantumCircuit(qr)
+        circuit.h(qr[0])
+        circuit.h(qr[0])
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[1], qr[0])
+        circuit.cx(qr[1], qr[0])
+        dag_circuit = circuit_to_dag(circuit)
+
+        pass_manager = PassManager()
+        pass_manager.append(CXCancellation())
+        dag_circuit = transpile_dag(dag_circuit, pass_manager=pass_manager)
+        resources_after = dag_circuit.count_ops()
+
+        self.assertNotIn('cx', resources_after)
diff --git a/test/python/transpiler/test_lookahead_swap.py b/test/python/transpiler/test_lookahead_swap.py
new file mode 100644
--- /dev/null
+++ b/test/python/transpiler/test_lookahead_swap.py
@@ -0,0 +1,101 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2018, IBM.
+#
+# This source code is licensed under the Apache License, Version 2.0 found in
+# the LICENSE.txt file in the root directory of this source tree.
+
+"""Test the LookaheadSwap pass"""
+
+import unittest
+from qiskit.transpiler.passes import LookaheadSwap
+from qiskit.mapper import CouplingMap
+from qiskit.converters import circuit_to_dag
+from qiskit import QuantumRegister, QuantumCircuit
+from qiskit.transpiler import PassManager, transpile_dag
+from ..common import QiskitTestCase
+
+
+class TestLookaheadSwap(QiskitTestCase):
+    """Tests the LookaheadSwap pass."""
+
+    def test_lookahead_swap_doesnt_modify_mapped_circuit(self):
+        """Test that lookahead mapper is idempotent.
+
+        It should not modify a circuit which is already compatible with the
+        coupling map, and can be applied repeatedly without modifying the circuit.
+        """
+
+        qr = QuantumRegister(3, name='q')
+        circuit = QuantumCircuit(qr)
+        circuit.cx(qr[0], qr[2])
+        circuit.cx(qr[0], qr[1])
+        original_dag = circuit_to_dag(circuit)
+
+        # Create coupling map which contains all two-qubit gates in the circuit.
+        coupling_map = CouplingMap(couplinglist=[(0, 1), (0, 2)])
+
+        pass_manager = PassManager()
+        pass_manager.append(LookaheadSwap(coupling_map))
+        mapped_dag = transpile_dag(original_dag, pass_manager=pass_manager)
+
+        self.assertEqual(original_dag, mapped_dag)
+
+        second_pass_manager = PassManager()
+        second_pass_manager.append(LookaheadSwap(coupling_map))
+        remapped_dag = transpile_dag(mapped_dag, pass_manager=second_pass_manager)
+
+        self.assertEqual(mapped_dag, remapped_dag)
+
+    def test_lookahead_swap_should_add_a_single_swap(self):
+        """Test that LookaheadSwap will insert a SWAP to match layout.
+
+        For a single cx gate which is not available in the current layout, test
+        that the mapper inserts a single swap to enable the gate.
+        """
+
+        qr = QuantumRegister(3)
+        circuit = QuantumCircuit(qr)
+        circuit.cx(qr[0], qr[2])
+        dag_circuit = circuit_to_dag(circuit)
+
+        coupling_map = CouplingMap(couplinglist=[(0, 1), (1, 2)])
+
+        pass_manager = PassManager()
+        pass_manager.append([LookaheadSwap(coupling_map)])
+        mapped_dag = transpile_dag(dag_circuit, pass_manager=pass_manager)
+
+        self.assertEqual(mapped_dag.count_ops().get('swap', 0),
+                         dag_circuit.count_ops().get('swap', 0) + 1)
+
+    def test_lookahead_swap_finds_minimal_swap_solution(self):
+        """Of many valid SWAPs, test that LookaheadSwap finds the cheapest path.
+
+        For a two CNOT circuit: cx q[0],q[2]; cx q[0],q[1]
+        on the initial layout: qN -> qN
+        (At least) two solutions exist:
+        - SWAP q[0],[1], cx q[0],q[2], cx q[0],q[1]
+        - SWAP q[1],[2], cx q[0],q[2], SWAP q[1],q[2], cx q[0],q[1]
+
+        Verify that we find the first solution, as it requires fewer SWAPs.
+        """
+
+        qr = QuantumRegister(3)
+        circuit = QuantumCircuit(qr)
+        circuit.cx(qr[0], qr[2])
+        circuit.cx(qr[0], qr[1])
+
+        dag_circuit = circuit_to_dag(circuit)
+
+        coupling_map = CouplingMap(couplinglist=[(0, 1), (1, 2)])
+
+        pass_manager = PassManager()
+        pass_manager.append([LookaheadSwap(coupling_map)])
+        mapped_dag = transpile_dag(dag_circuit, pass_manager=pass_manager)
+
+        self.assertEqual(mapped_dag.count_ops().get('swap', 0),
+                         dag_circuit.count_ops().get('swap', 0) + 1)
+
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/test/python/transpiler/test_transpile.py b/test/python/transpiler/test_transpile.py
new file mode 100644
--- /dev/null
+++ b/test/python/transpiler/test_transpile.py
@@ -0,0 +1,74 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2018, IBM.
+#
+# This source code is licensed under the Apache License, Version 2.0 found in
+# the LICENSE.txt file in the root directory of this source tree.
+
+# pylint: disable=redefined-builtin
+
+"""Tests basic functionality of the transpile function"""
+
+from qiskit import QuantumRegister, QuantumCircuit
+from qiskit import compile, BasicAer
+from qiskit.transpiler import PassManager, transpile_dag, transpile
+from qiskit.tools.compiler import circuits_to_qobj
+from qiskit.converters import circuit_to_dag
+
+from ..common import QiskitTestCase
+
+
+class TestTranspile(QiskitTestCase):
+    """Test transpile function."""
+
+    def test_pass_manager_empty(self):
+        """Test passing an empty PassManager() to the transpiler.
+
+        It should perform no transformations on the circuit.
+        """
+        qr = QuantumRegister(2)
+        circuit = QuantumCircuit(qr)
+        circuit.h(qr[0])
+        circuit.h(qr[0])
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[0], qr[1])
+        dag_circuit = circuit_to_dag(circuit)
+        resources_before = dag_circuit.count_ops()
+
+        pass_manager = PassManager()
+        dag_circuit = transpile_dag(dag_circuit, pass_manager=pass_manager)
+        resources_after = dag_circuit.count_ops()
+
+        self.assertDictEqual(resources_before, resources_after)
+
+    def test_pass_manager_none(self):
+        """Test passing the default (None) pass manager to the transpiler.
+
+        It should perform the default qiskit flow:
+        unroll, swap_mapper, cx_direction, cx_cancellation, optimize_1q_gates
+        and should be equivalent to using tools.compile
+        """
+        qr = QuantumRegister(2, 'qr')
+        circuit = QuantumCircuit(qr)
+        circuit.h(qr[0])
+        circuit.h(qr[0])
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[0], qr[1])
+        circuit.cx(qr[0], qr[1])
+
+        coupling_map = [[1, 0]]
+        basis_gates = 'u1,u2,u3,cx,id'
+
+        backend = BasicAer.get_backend('qasm_simulator')
+        circuit2 = transpile(circuit, backend, coupling_map=coupling_map, basis_gates=basis_gates,
+                             pass_manager=None)
+
+        qobj = compile(circuit, backend=backend, coupling_map=coupling_map, basis_gates=basis_gates)
+
+        qobj2 = circuits_to_qobj(circuit2, backend.name(), basis_gates=basis_gates,
+                                 coupling_map=coupling_map, qobj_id=qobj.qobj_id)
+
+        self.assertEqual(qobj, qobj2)