Repository: Qiskit/qiskit

Issue:
optimization_level=1 always uses Trivial layout
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **Qiskit Terra version**: master
- **Python version**:
- **Operating system**:

### What is the current behavior?
```
               ┌───┐                ░ ┌─┐            
q37_0: |0>─────┤ X ├────────────────░─┤M├────────────
          ┌───┐└─┬─┘┌───┐           ░ └╥┘┌─┐         
q37_1: |0>┤ H ├──■──┤ X ├───────────░──╫─┤M├─────────
          ├───┤     └─┬─┘┌───┐      ░  ║ └╥┘┌─┐      
q37_2: |0>┤ H ├───────■──┤ X ├──────░──╫──╫─┤M├──────
          ├───┤          └─┬─┘┌───┐ ░  ║  ║ └╥┘┌─┐   
q37_3: |0>┤ H ├────────────■──┤ X ├─░──╫──╫──╫─┤M├───
          ├───┤               └─┬─┘ ░  ║  ║  ║ └╥┘┌─┐
q37_4: |0>┤ H ├─────────────────■───░──╫──╫──╫──╫─┤M├
          └───┘                     ░  ║  ║  ║  ║ └╥┘
  c5_0: 0 ═════════════════════════════╩══╬══╬══╬══╬═
                                          ║  ║  ║  ║ 
  c5_1: 0 ════════════════════════════════╩══╬══╬══╬═
                                             ║  ║  ║ 
  c5_2: 0 ═══════════════════════════════════╩══╬══╬═
                                                ║  ║ 
  c5_3: 0 ══════════════════════════════════════╩══╬═
                                                   ║ 
  c5_4: 0 ═════════════════════════════════════════╩═
```
```python
backend = provider.get_backend('ibmq_20_tokyo')
new_qc = transpile(qc, backend, optimization_level=1)
```

shows initial layout as the trivial layout, which is disconnected:

<img width="296" alt="Screen Shot 2019-07-22 at 4 14 54 PM" src="https://user-images.githubusercontent.com/1249193/61662177-e564af00-ac9b-11e9-94bb-0dbe3fbe0e20.png">

However part #2 in the source says `_layout_check` does better layout 
if the circuit needs swaps, 

```python
# 2. Use a better layout on densely connected qubits, if circuit needs swaps
```
which is obviously the case here, but no other layouts
are actually selected (or imported).

### Steps to reproduce the problem



### What is the expected behavior?
Expected that the `DenseLayout` is used if there are swaps needed.


### Suggested solutions





Discussion:
Confirmed.
```python
qr = QuantumRegister(5)
cr = ClassicalRegister(5)
qc = QuantumCircuit(qr, cr)

qc.h(qr[1])
qc.h(qr[2])
qc.h(qr[3])
qc.h(qr[4])
qc.cx(qr[0], qr[1])
qc.cx(qr[1], qr[2])
qc.cx(qr[2], qr[3])
qc.cx(qr[3], qr[4])
qc.barrier(qr)
qc.measure(qr, cr)

backend = IBMQ.get_backend('ibmq_20_tokyo')
new_qc = transpile(qc, backend, optimization_level=1)
print('\n'.join([str((i, j)) for i, j in new_qc.layout._p2v.values() if i.name != 'ancilla']))
```
```
(QuantumRegister(5, 'q0'), 0)
(QuantumRegister(5, 'q0'), 1)
(QuantumRegister(5, 'q0'), 2)
(QuantumRegister(5, 'q0'), 3)
(QuantumRegister(5, 'q0'), 4)
```

Is also `optimization_level=2` having a similar issue?
```python
new_qc = transpile(qc, backend, optimization_level=2)
print('\n'.join([str((i, j)) for i, j in new_qc.layout._p2v.values() if i.name != 'ancilla']))
```
```
(QuantumRegister(5, 'q0'), 1)
(QuantumRegister(5, 'q0'), 4)
(QuantumRegister(5, 'q0'), 2)
(QuantumRegister(5, 'q0'), 3)
(QuantumRegister(5, 'q0'), 0)
```

Resolving patch:
diff --git a/qiskit/transpiler/preset_passmanagers/level1.py b/qiskit/transpiler/preset_passmanagers/level1.py
--- a/qiskit/transpiler/preset_passmanagers/level1.py
+++ b/qiskit/transpiler/preset_passmanagers/level1.py
@@ -27,7 +27,7 @@
 from qiskit.transpiler.passes import CheckMap
 from qiskit.transpiler.passes import CXDirection
 from qiskit.transpiler.passes import SetLayout
-from qiskit.transpiler.passes import TrivialLayout
+from qiskit.transpiler.passes import DenseLayout
 from qiskit.transpiler.passes import BarrierBeforeFinalMeasurements
 from qiskit.transpiler.passes import StochasticSwap
 from qiskit.transpiler.passes import FullAncillaAllocation
@@ -65,13 +65,13 @@ def level_1_pass_manager(transpile_config):
     initial_layout = transpile_config.initial_layout
     seed_transpiler = transpile_config.seed_transpiler
 
-    # 1. Use trivial layout if no layout given
+    # 1. Use dense layout if no layout given
     _given_layout = SetLayout(initial_layout)
 
     def _choose_layout_condition(property_set):
         return not property_set['layout']
 
-    _choose_layout = TrivialLayout(coupling_map)
+    _choose_layout = DenseLayout(coupling_map)
 
     # 2. Use a better layout on densely connected qubits, if circuit needs swaps
     _layout_check = CheckMap(coupling_map)


Test patch:
diff --git a/test/python/compiler/test_transpiler.py b/test/python/compiler/test_transpiler.py
--- a/test/python/compiler/test_transpiler.py
+++ b/test/python/compiler/test_transpiler.py
@@ -18,19 +18,19 @@
 import unittest
 from unittest.mock import patch
 
-from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
 from qiskit import BasicAer
-from qiskit.extensions.standard import CnotGate
-from qiskit.transpiler import PassManager
+from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
+from qiskit.circuit import Parameter
 from qiskit.compiler import transpile
 from qiskit.converters import circuit_to_dag
+from qiskit.dagcircuit.exceptions import DAGCircuitError
+from qiskit.extensions.standard import CnotGate
 from qiskit.test import QiskitTestCase, Path
 from qiskit.test.mock import FakeMelbourne, FakeRueschlikon
-from qiskit.transpiler.passes import BarrierBeforeFinalMeasurements, CXDirection
 from qiskit.transpiler import Layout, CouplingMap
-from qiskit.circuit import Parameter
-from qiskit.dagcircuit.exceptions import DAGCircuitError
+from qiskit.transpiler import PassManager
 from qiskit.transpiler.exceptions import TranspilerError
+from qiskit.transpiler.passes import BarrierBeforeFinalMeasurements, CXDirection
 
 
 class TestTranspile(QiskitTestCase):
@@ -160,9 +160,9 @@ def test_already_mapped_1(self):
         qc.cx(qr[13], qr[4])
         qc.measure(qr, cr)
 
-        new_qc = transpile(qc, coupling_map=coupling_map, basis_gates=basis_gates)
-        cx_qubits = [qargs for (gate, qargs, _) in new_qc.data
-                     if gate.name == "cx"]
+        new_qc = transpile(qc, coupling_map=coupling_map, basis_gates=basis_gates,
+                           initial_layout=Layout.generate_trivial_layout(qr))
+        cx_qubits = [qargs for (gate, qargs, _) in new_qc.data if gate.name == "cx"]
         cx_qubits_physical = [[ctrl.index, tgt.index] for [ctrl, tgt] in cx_qubits]
         self.assertEqual(sorted(cx_qubits_physical),
                          [[3, 4], [3, 14], [5, 4], [9, 8], [12, 11], [13, 4]])
@@ -426,7 +426,7 @@ def test_parameterized_circuit_for_device(self):
 
         qr = QuantumRegister(14, 'q')
         expected_qc = QuantumCircuit(qr)
-        expected_qc.u1(theta, qr[0])
+        expected_qc.u1(theta, qr[1])
 
         self.assertEqual(expected_qc, transpiled_qc)
 
@@ -461,7 +461,7 @@ def test_parameter_expression_circuit_for_device(self):
 
         qr = QuantumRegister(14, 'q')
         expected_qc = QuantumCircuit(qr)
-        expected_qc.u1(square, qr[0])
+        expected_qc.u1(square, qr[1])
 
         self.assertEqual(expected_qc, transpiled_qc)
 
diff --git a/test/python/transpiler/test_preset_passmanagers.py b/test/python/transpiler/test_preset_passmanagers.py
--- a/test/python/transpiler/test_preset_passmanagers.py
+++ b/test/python/transpiler/test_preset_passmanagers.py
@@ -16,10 +16,10 @@
 from test import combine
 from ddt import ddt, data
 
-from qiskit.test import QiskitTestCase
-from qiskit.compiler import transpile, assemble
 from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
+from qiskit.compiler import transpile, assemble
 from qiskit.extensions.standard import U2Gate, U3Gate
+from qiskit.test import QiskitTestCase
 from qiskit.test.mock import (FakeTenerife, FakeMelbourne,
                               FakeRueschlikon, FakeTokyo, FakePoughkeepsie)
 
@@ -237,3 +237,47 @@ def test_layout_2503(self, level):
         self.assertEqual(qubits_0[0].index, 6)
         self.assertIsInstance(gate_1, U2Gate)
         self.assertEqual(qubits_1[0].index, 12)
+
+
+@ddt
+class TestFinalLayouts(QiskitTestCase):
+    """Test final layouts after preset transpilation"""
+
+    @data(0, 1, 2, 3)
+    def test_layout_tokyo_2845(self, level):
+        """Test that final layout in tokyo #2845
+        See: https://github.com/Qiskit/qiskit-terra/issues/2845
+        """
+        qr1 = QuantumRegister(3, 'qr1')
+        qr2 = QuantumRegister(2, 'qr2')
+        qc = QuantumCircuit(qr1, qr2)
+        qc.cx(qr1[0], qr1[1])
+        qc.cx(qr1[1], qr1[2])
+        qc.cx(qr1[2], qr2[0])
+        qc.cx(qr2[0], qr2[1])
+
+        ancilla = QuantumRegister(15, 'ancilla')
+
+        # TrivialLayout
+        expected_layout_level0 = {0: qr1[0], 1: qr1[1], 2: qr1[2], 3: qr2[0], 4: qr2[1],
+                                  5: ancilla[0], 6: ancilla[1], 7: ancilla[2], 8: ancilla[3],
+                                  9: ancilla[4], 10: ancilla[5], 11: ancilla[6], 12: ancilla[7],
+                                  13: ancilla[8], 14: ancilla[9], 15: ancilla[10], 16: ancilla[11],
+                                  17: ancilla[12], 18: ancilla[13], 19: ancilla[14]}
+        # DenseLayout
+        expected_layout_level1 = {0: qr2[1], 1: ancilla[0], 2: ancilla[1], 3: ancilla[2],
+                                  4: ancilla[3], 5: qr2[0], 6: qr1[2], 7: ancilla[4], 8: ancilla[5],
+                                  9: ancilla[6], 10: qr1[1], 11: qr1[0], 12: ancilla[7],
+                                  13: ancilla[8], 14: ancilla[9], 15: ancilla[10], 16: ancilla[11],
+                                  17: ancilla[12], 18: ancilla[13], 19: ancilla[14]}
+
+        # NoiseAdaptiveLayout (in FakeTokyo, no errors. Therefore, TrivialLayout)
+        expected_layout_level2 = expected_layout_level0
+        expected_layout_level3 = expected_layout_level0
+        expected_layouts = [expected_layout_level0,
+                            expected_layout_level1,
+                            expected_layout_level2,
+                            expected_layout_level3]
+        backend = FakeTokyo()
+        result = transpile(qc, backend, optimization_level=level, seed_transpiler=42)
+        self.assertEqual(result._layout._p2v, expected_layouts[level])