Repository: Qiskit/qiskit

Issue:
initial_layout cannot be used in compile with circuits of differing qubit number
<!-- ⚠️ 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?
You can not use an `initial_layout` when circuits with differing numbers of qubits are input to compile.  This is true even the circuits are smaller than the size of the layout passed, i.e. the layout is valid for the subset of qubits in the smaller width circuits.
```
q = QuantumRegister(3,'q')
c = ClassicalRegister(3)
qc = QuantumCircuit(q, c)

qc.h(q[0])
qc.cx(q[0], q[1])
qc.cx(q[1], q[2])
qc.measure(q, c)

q = QuantumRegister(2,'q')
c = ClassicalRegister(2)
qc2 = QuantumCircuit(q, c)

qc2.h(q[0])
qc2.cx(q[0], q[1])
qc2.measure(q, c)

layout = [None, None, (QuantumRegister(3, 'q'), 0), (QuantumRegister(3, 'q'), 1), (QuantumRegister(3, 'q'), 2)]

backend = IBMQ.get_backend('ibmqx4')
qobj = compile([qc, qc2], backend, initial_layout = layout)
```
`MapperError: 'initial_layout qubit q[2] not in input DAGCircuit'`


### Steps to reproduce the problem



### What is the expected behavior?
Given that we allow compiling circuits with different widths, I would expect that we can supply initial_layouts to these circuits.  If we are not allowing this, then terra should throw the proper warning before trying to compile the circuits.

### Suggested solutions





Discussion:

Resolving patch:
diff --git a/qiskit/transpiler/transpiler.py b/qiskit/transpiler/transpiler.py
--- a/qiskit/transpiler/transpiler.py
+++ b/qiskit/transpiler/transpiler.py
@@ -17,6 +17,7 @@
 from qiskit.extensions.standard import SwapGate
 from qiskit.mapper.layout import Layout
 from qiskit.transpiler.passmanager import PassManager
+from qiskit.transpiler.exceptions import TranspilerError
 
 from .passes.unroller import Unroller
 from .passes.cx_cancellation import CXCancellation
@@ -103,7 +104,14 @@ def _transpilation(circuit, basis_gates=None, coupling_map=None,
 
     Returns:
         QuantumCircuit: A transpiled circuit.
+
+    Raises:
+        TranspilerError: If the Layout does not matches the circuit
     """
+    if initial_layout is not None and set(circuit.qregs) != initial_layout.get_registers():
+        raise TranspilerError('The provided initial layout does not match the registers in '
+                              'the circuit "%s"' % circuit.name)
+
     if pass_manager and not pass_manager.working_list:
         return circuit
 


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
@@ -14,7 +14,7 @@
 
 from qiskit import BasicAer
 from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
-from qiskit.transpiler import PassManager, transpile, transpile_dag
+from qiskit.transpiler import PassManager, transpile, transpile_dag, TranspilerError
 from qiskit import compile, execute
 from qiskit.test import QiskitTestCase, Path
 from qiskit.test.mock import FakeRueschlikon, FakeTenerife
@@ -206,6 +206,25 @@ def test_compile_circuits_diff_registers(self):
         circuits = transpile(circuits, backend)
         self.assertIsInstance(circuits[0], QuantumCircuit)
 
+    def test_wrong_initial_layout(self):
+        """Test transpile with a bad initial layout.
+        """
+        backend = BasicAer.get_backend('qasm_simulator')
+
+        qubit_reg = QuantumRegister(2, name='q')
+        clbit_reg = ClassicalRegister(2, name='c')
+        qc = QuantumCircuit(qubit_reg, clbit_reg, name="bell")
+        qc.h(qubit_reg[0])
+        qc.cx(qubit_reg[0], qubit_reg[1])
+        qc.measure(qubit_reg, clbit_reg)
+
+        bad_initial_layout = [(QuantumRegister(3, 'q'), 0),
+                              (QuantumRegister(3, 'q'), 1),
+                              (QuantumRegister(3, 'q'), 2)]
+
+        self.assertRaises(TranspilerError, transpile,
+                          qc, backend, initial_layout=bad_initial_layout)
+
     def test_example_multiple_compile(self):
         """Test a toy example compiling multiple circuits.