Repository: Qiskit/qiskit

Issue:
Mapping Error in BasicAer
`'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q0[5];\ncreg c0[3];\nx q0[4];\nmeasure q0[1] -> c0[0];\nmeasure q0[3] -> c0[1];\nmeasure q0[4] -> c0[2];\n'`

The above qasm should give all shots in `100`, but they are all in `000`. Only happens in `BasicAer`, not in `Aer`. 

### Informations

- **Qiskit Terra version**: 0.7
- **Python version**: 3.6
- **Operating system**:

### What is the current behavior?



### Steps to reproduce the problem



### What is the expected behavior?



### Suggested solutions





Discussion:
I confirm there is a bug

```python
qr = QuantumRegister(5)
cr = ClassicalRegister(3)
circuit = QuantumCircuit(qr, cr)
circuit.x(qr[4])
circuit.measure(qr[1], cr[0])
circuit.measure(qr[3], cr[1])
circuit.measure(qr[4], cr[2])

backend = BasicAer.get_backend('qasm_simulator')
job = execute(circuit, backend)
result = job.result()
counts1 = result.get_counts()
print(counts1)

backend = Aer.get_backend('qasm_simulator')
job = execute(circuit, backend)
result = job.result()
counts2 = result.get_counts()
print(counts2)
```
gives the outcome david is saying. 

```
{'000': 1024}
{'100': 1024}
```

@chriseclectic can you have a look at this. 
The bug is in the BasicAer python simulator. Specifically it is occurring in the `_add_sample_measure` function, which is responsible for finding a probability distribution from the statevector and sampling from that. This function cannot handle the case where only a subset of the total qubits are measured.

Resolving patch:
diff --git a/qiskit/providers/builtinsimulators/qasm_simulator.py b/qiskit/providers/builtinsimulators/qasm_simulator.py
--- a/qiskit/providers/builtinsimulators/qasm_simulator.py
+++ b/qiskit/providers/builtinsimulators/qasm_simulator.py
@@ -206,8 +206,8 @@ def _add_sample_measure(self, measure_params, num_samples):
         memory = []
         for sample in samples:
             classical_state = self._classical_state
-            for qubit, cbit in measure_params:
-                qubit_outcome = int((sample & (1 << qubit)) >> qubit)
+            for count, (qubit, cbit) in enumerate(sorted(measure_params)):
+                qubit_outcome = int((sample & (1 << count)) >> count)
                 bit = 1 << cbit
                 classical_state = (classical_state & (~bit)) | (qubit_outcome << cbit)
             value = bin(classical_state)[2:]


Test patch:
diff --git a/test/python/tools/test_compiler.py b/test/python/tools/test_compiler.py
--- a/test/python/tools/test_compiler.py
+++ b/test/python/tools/test_compiler.py
@@ -22,7 +22,7 @@
 from qiskit.providers.ibmq import least_busy
 from .._mockutils import FakeBackend
 from ..common import QiskitTestCase
-from ..common import requires_qe_access, requires_cpp_simulator
+from ..common import requires_qe_access
 
 
 class TestCompiler(QiskitTestCase):
@@ -472,13 +472,12 @@ def test_compile_coupling_map(self):
         threshold = 0.05 * shots
         self.assertDictAlmostEqual(counts, target, threshold)
 
-    @requires_cpp_simulator
     def test_example_swap_bits(self):
         """Test a toy example swapping a set bit around.
 
         Uses the mapper. Pass if results are correct.
         """
-        backend = qiskit.Aer.get_backend('qasm_simulator')
+        backend = qiskit.BasicAer.get_backend('qasm_simulator')
         coupling_map = [[0, 1], [0, 8], [1, 2], [1, 9], [2, 3], [2, 10],
                         [3, 4], [3, 11], [4, 5], [4, 12], [5, 6], [5, 13],
                         [6, 7], [6, 14], [7, 15], [8, 9], [9, 10], [10, 11],