Repository: Qiskit/qiskit

Issue:
Default transpiler pipeline
<!-- ⚠️ 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 behavior?
This is part of cleaning up the transpiler. Currently we have preserved backwards compatibility by following the old code path of circuit transformations when no pass manager is used in `compile()`. That old path is a bunch of hardcoded transformations. To fully complete the transition to the transpiler architecture, we need to remove that old path, and create a default pipeline of passes that gets used when `pass_manager=None`.

Note that a PR that closes this issue must also close #157.


Discussion:
on hold until we move mapper and unroller as passes.
Everything is a pass! Removing `on hold`
The PR #1574 is trying to address this. 

Resolving patch:
diff --git a/qiskit/transpiler/passes/mapping/cx_direction.py b/qiskit/transpiler/passes/mapping/cx_direction.py
--- a/qiskit/transpiler/passes/mapping/cx_direction.py
+++ b/qiskit/transpiler/passes/mapping/cx_direction.py
@@ -56,10 +56,8 @@ def run(self, dag):
         new_dag = DAGCircuit()
 
         if self.layout is None:
-            if self.property_set["layout"]:
-                self.layout = self.property_set["layout"]
-            else:
-                self.layout = Layout.generate_trivial_layout(*dag.qregs.values())
+            # LegacySwap renames the register in the DAG and does not match the property set
+            self.layout = Layout.generate_trivial_layout(*dag.qregs.values())
 
         for layer in dag.serial_layers():
             subdag = layer['graph']
diff --git a/qiskit/transpiler/passes/mapping/enlarge_with_ancilla.py b/qiskit/transpiler/passes/mapping/enlarge_with_ancilla.py
--- a/qiskit/transpiler/passes/mapping/enlarge_with_ancilla.py
+++ b/qiskit/transpiler/passes/mapping/enlarge_with_ancilla.py
@@ -43,12 +43,12 @@ def run(self, dag):
         Raises:
             TranspilerError: If there is not layout in the property set or not set at init time.
         """
+
+        self.layout = self.layout or self.property_set['layout']
+
         if self.layout is None:
-            if self.property_set["layout"]:
-                self.layout = self.property_set["layout"]
-            else:
-                raise TranspilerError(
-                    "EnlargeWithAncilla requires self.property_set[\"layout\"] to run")
+            raise TranspilerError("EnlargeWithAncilla requires property_set[\"layout\"] or"
+                                  " \"layout\" parameter to run")
 
         # Idle physical qubits are those physical qubits that no virtual qubit corresponds to.
         # Add extra virtual qubits to make the DAG and CouplingMap the same size.
@@ -63,7 +63,8 @@ def run(self, dag):
             else:
                 qreg = QuantumRegister(num_idle_physical_qubits, name=self.ancilla_name)
                 dag.add_qreg(qreg)
+
         for index, idle_physical_bit in enumerate(self.layout.idle_physical_bits()):
             self.layout[idle_physical_bit] = (qreg, index)
-        self.property_set['layout'] = self.layout
+
         return dag
diff --git a/qiskit/transpiler/transpiler.py b/qiskit/transpiler/transpiler.py
--- a/qiskit/transpiler/transpiler.py
+++ b/qiskit/transpiler/transpiler.py
@@ -24,7 +24,6 @@
 from .passes.optimize_1q_gates import Optimize1qGates
 from .passes.fixed_point import FixedPoint
 from .passes.depth import Depth
-from .passes.mapping.barrier_before_final_measurements import BarrierBeforeFinalMeasurements
 from .passes.mapping.check_map import CheckMap
 from .passes.mapping.cx_direction import CXDirection
 from .passes.mapping.dense_layout import DenseLayout
@@ -190,56 +189,41 @@ def transpile_dag(dag, basis_gates=None, coupling_map=None,
     if initial_layout is None:
         initial_layout = Layout.generate_trivial_layout(*dag.qregs.values())
 
-    if pass_manager:
-        # run the passes specified by the pass manager
-        # TODO return the property set too. See #1086
-        dag = pass_manager.run_passes(dag)
-    else:
+    if pass_manager is None:
         # default set of passes
-        # TODO: move each step here to a pass, and use a default passmanager below
-        name = dag.name
 
-        dag = Unroller(basis_gates).run(dag)
-        dag = BarrierBeforeFinalMeasurements().run(dag)
+        pass_manager = PassManager()
+        pass_manager.append(Unroller(basis_gates))
 
         # if a coupling map is given compile to the map
         if coupling_map:
-            logger.info("pre-mapping properties: %s",
-                        dag.properties())
-
             coupling = CouplingMap(coupling_map)
 
             # Extend and enlarge the the dag/layout with ancillas using the full coupling map
-            logger.info("initial layout: %s", initial_layout)
-            pass_ = ExtendLayout(coupling)
-            pass_.property_set['layout'] = initial_layout
-            pass_.run(dag)
-            initial_layout = pass_.property_set['layout']
-            pass_ = EnlargeWithAncilla(initial_layout)
-            dag = pass_.run(dag)
-            initial_layout = pass_.property_set['layout']
-            logger.info("initial layout (ancilla extended): %s", initial_layout)
+            pass_manager.property_set['layout'] = initial_layout
+            pass_manager.append(ExtendLayout(coupling))
+            pass_manager.append(EnlargeWithAncilla(initial_layout))
 
             # Swap mapper
-            dag = LegacySwap(coupling, initial_layout, trials=20, seed=seed_mapper).run(dag)
+            pass_manager.append(LegacySwap(coupling, initial_layout, trials=20, seed=seed_mapper))
 
             # Expand swaps
-            dag = Decompose(SwapGate).run(dag)
-            # Change cx directions
-            dag = CXDirection(coupling).run(dag)
+            pass_manager.append(Decompose(SwapGate))
+
+            # Change CX directions
+            pass_manager.append(CXDirection(coupling))
+
             # Unroll to the basis
-            dag = Unroller(['u1', 'u2', 'u3', 'id', 'cx']).run(dag)
+            pass_manager.append(Unroller(['u1', 'u2', 'u3', 'id', 'cx']))
 
             # Simplify single qubit gates and CXs
-            pm_4_optimization = PassManager()
-            pm_4_optimization.append([Optimize1qGates(),
-                                      CXCancellation(),
-                                      Depth(),
-                                      FixedPoint('depth')],
-                                     do_while=lambda property_set: not property_set[
-                                         'depth_fixed_point'])
-            dag = transpile_dag(dag, pass_manager=pm_4_optimization)
-
-        dag.name = name
+            pass_manager.append([Optimize1qGates(), CXCancellation(), Depth(), FixedPoint('depth')],
+                                do_while=lambda property_set: not property_set['depth_fixed_point'])
+
+    # run the passes specified by the pass manager
+    # TODO return the property set too. See #1086
+    name = dag.name
+    dag = pass_manager.run_passes(dag)
+    dag.name = name
 
     return dag


Test patch:
diff --git a/test/python/compiler/test_compiler.py b/test/python/compiler/test_compiler.py
--- a/test/python/compiler/test_compiler.py
+++ b/test/python/compiler/test_compiler.py
@@ -616,15 +616,6 @@ def test_final_measurement_barrier_for_devices(self, mock_pass):
 
         self.assertTrue(mock_pass.called)
 
-    @patch.object(BarrierBeforeFinalMeasurements, 'run', wraps=barrier_pass.run)
-    def test_final_measurement_barrier_for_simulators(self, mock_pass):
-        """Verify BarrierBeforeFinalMeasurements pass is in default pipeline for simulators."""
-        circ = QuantumCircuit.from_qasm_file(self._get_resource_path('example.qasm', Path.QASMS))
-        dag_circuit = circuit_to_dag(circ)
-        transpile_dag(dag_circuit)
-
-        self.assertTrue(mock_pass.called)
-
     def test_optimize_to_nothing(self):
         """ Optimze gates up to fixed point in the default pipeline
         See https://github.com/Qiskit/qiskit-terra/issues/2035 """
diff --git a/test/python/transpiler/test_enlarge_with_ancilla_pass.py b/test/python/transpiler/test_enlarge_with_ancilla_pass.py
--- a/test/python/transpiler/test_enlarge_with_ancilla_pass.py
+++ b/test/python/transpiler/test_enlarge_with_ancilla_pass.py
@@ -57,7 +57,7 @@ def test_with_extension(self):
         self.assertEqual(2, len(qregs))
         self.assertEqual(self.qr3, qregs[0])
         self.assertEqual(QuantumRegister(2, name='ancilla'), qregs[1])
-        self.assertEqual(final_layout, pass_.property_set['layout'].get_physical_bits())
+        self.assertEqual(final_layout, layout.get_physical_bits())
 
     def test_name_collision(self):
         """Name collision during ancilla extension."""