Repository: Qiskit/qiskit

Issue:
MCXVChain gate being incorrectly assembled as MCX gate
### Informations

- **Qiskit Aer version**: 0.6.0
- **Python version**: 3.6.x
- **Operating system**: Linux

### What is the current behavior?

As reported on external Qiskit slack workspace, in aqua channel, Aqua's Grover is failing on a 4 qubit problem, but appears to work on a 3 qubit one.

In investigating I tried, 3 4 and 5 qubit problems.  When using BasicAer they all work, ie amplify the expected value(s),  but fail under Aer. Well the 3 qubit one under Aer does amplify the right value but the amplification over the others values is much less.

In discussion with @chriseclectic he suggested I try with fundamental basis gate set. When doing this then the outcome is as expected and comparable to BasicAer

### Steps to reproduce the problem

Here is sample code to reproduce the problem. I left other lines in there, e.g setting basis gates as well, so they can be (un)commented as needed to see the difference.

```
from matplotlib import pyplot
from qiskit import BasicAer, Aer
from qiskit.visualization import plot_histogram
from qiskit.aqua import QuantumInstance
from qiskit.aqua.algorithms import Grover
from qiskit.aqua.components.oracles import TruthTableOracle

#oracle = TruthTableOracle('10010000000000010000000000000001') # 5 (more ones), fails
#oracle = TruthTableOracle('10000000000000000000000000000000') # 5, fails
oracle = TruthTableOracle('1000000000000000') # 4, Fails
#oracle = TruthTableOracle('10000000')  # 3 Qubits, So, so
grover = Grover(oracle = oracle)
simulator = Aer.get_backend('qasm_simulator')
#quantum_instance = QuantumInstance(simulator, shots=1024, basis_gates=['u1', 'u2', 'u3', 'cx', 'id'])
quantum_instance = QuantumInstance(simulator, shots=1024)
result = grover.run(quantum_instance)
print(result['circuit'])
plot_histogram(result.get('measurement'))
pyplot.show()
```

### What is the expected behavior?

Expected value(s) amplified correctly in the outcome when using the default set of Aer basis gates.



Discussion:
After investigating this I don't think its an Aer / simulator issue so much as an issue with the transpiler/assembler and dealing with the gates recently added to Terra from Aqua.

In this case it is the multi-controlled XX gate in the circuit. For 3-qubits your example `grover.construct_circuit().draw()` looks like:

```
       ┌──────────┐    ┌───┐          ┌───┐ ░ ┌─────────────┐┌─────────────┐»
v_0: ──┤ U2(0,pi) ├────┤ X ├──────■───┤ X ├─░─┤ U2(-2pi,pi) ├┤ U3(pi,0,pi) ├»
       ├──────────┤    ├───┤      │   ├───┤ ░ ├─────────────┤├─────────────┤»
v_1: ──┤ U2(0,pi) ├────┤ X ├──────■───┤ X ├─░─┤ U2(-2pi,pi) ├┤ U3(pi,0,pi) ├»
       ├──────────┤    ├───┤      │   ├───┤ ░ ├─────────────┤├─────────────┤»
v_2: ──┤ U2(0,pi) ├────┤ X ├──────■───┤ X ├─░─┤ U2(-2pi,pi) ├┤ U3(pi,0,pi) ├»
     ┌─┴──────────┴┐┌──┴───┴───┐┌─┴──┐└───┘ ░ └─────────────┘└─────────────┘»
o_0: ┤ U3(pi,0,pi) ├┤ U2(0,pi) ├┤0   ├──────────────────────────────────────»
     └─────────────┘└──────────┘│  X │                                      »
a_0: ───────────────────────────┤1   ├──────────────────────────────────────»
                                └────┘                                      »
«                      ┌─────────────┐  ┌──────────┐              ░ 
«v_0: ──────────────■──┤ U3(pi,0,pi) ├──┤ U2(0,pi) ├──────────────░─
«                   │  ├─────────────┤  ├──────────┤              ░ 
«v_1: ──────────────■──┤ U3(pi,0,pi) ├──┤ U2(0,pi) ├──────────────░─
«     ┌──────────┐┌─┴─┐└─┬──────────┬┘┌─┴──────────┴┐┌──────────┐ ░ 
«v_2: ┤ U2(0,pi) ├┤ X ├──┤ U2(0,pi) ├─┤ U3(pi,0,pi) ├┤ U2(0,pi) ├─░─
«     └──────────┘└───┘  └──────────┘ └─────────────┘└──────────┘ ░ 
«o_0: ──────────────────────────────────────────────────────────────
«                                                                   
«a_0: ──────────────────────────────────────────────────────────────
«                   
```

However when it is run on the `Aer` qasm simulator it is being assembled into the qobj with instructions
```
[QasmQobjInstruction(name='u2', params=[0.0, 3.141592653589793], qubits=[0]),
 QasmQobjInstruction(name='x', qubits=[0]),
 QasmQobjInstruction(name='u2', params=[0.0, 3.141592653589793], qubits=[1]),
 QasmQobjInstruction(name='x', qubits=[1]),
 QasmQobjInstruction(name='u2', params=[0.0, 3.141592653589793], qubits=[2]),
 QasmQobjInstruction(name='x', qubits=[2]),
 QasmQobjInstruction(name='u2', params=[3.141592653589793, 3.141592653589793], qubits=[3]),
 QasmQobjInstruction(name='mcx', qubits=[0, 1, 2, 3, 4]),
 QasmQobjInstruction(name='x', qubits=[0]),
 QasmQobjInstruction(name='x', qubits=[1]),
 QasmQobjInstruction(name='x', qubits=[2]),
 QasmQobjInstruction(name='barrier', qubits=[0, 1, 2]),
 QasmQobjInstruction(name='u2', params=[0.0, 6.283185307179586], qubits=[0]),
 QasmQobjInstruction(name='u2', params=[0.0, 6.283185307179586], qubits=[1]),
 QasmQobjInstruction(name='u1', params=[9.42477796076938], qubits=[2]),
 QasmQobjInstruction(name='ccx', qubits=[0, 1, 2]),
 QasmQobjInstruction(name='u2', params=[3.141592653589793, 3.141592653589793], qubits=[0]),
 QasmQobjInstruction(name='u2', params=[3.141592653589793, 3.141592653589793], qubits=[1]),
 QasmQobjInstruction(name='u1', params=[9.42477796076938], qubits=[2]),
 QasmQobjInstruction(name='barrier', qubits=[0, 1, 2])]
```

The `'mcx'` simulator instruction is a mutli-controlled X gate, so in this case it is simulating CCCCX. But that is different to the gate used in the original circuit. The fix is the transpiler should not treat this gate as an mcx basis gate, it should unroll it to gates supported by the simulator.

This isn't an issue for BasicAer because it only supposed `id, u1, u2, u3, cx, unitary` as basis gates, and shouldn't be an issue for Aer if you explicitly specify basis gates that does not include `mcx`.

I'll transfer this issue to the Terra github.
I think the easiest (and therefore maybe the best) fix is to give the special decompositions of MCX  different names. E.g. rename `MCXVChain` from `mcx` to `mcx_chain` and `MCXRecursion` from `mcx` to `mcx_recursion`.

Resolving patch:
diff --git a/qiskit/circuit/library/standard_gates/x.py b/qiskit/circuit/library/standard_gates/x.py
--- a/qiskit/circuit/library/standard_gates/x.py
+++ b/qiskit/circuit/library/standard_gates/x.py
@@ -758,10 +758,10 @@ def __new__(cls, num_ctrl_qubits=None, label=None, ctrl_state=None):
             return gate
         return super().__new__(cls)
 
-    def __init__(self, num_ctrl_qubits, label=None, ctrl_state=None):
+    def __init__(self, num_ctrl_qubits, label=None, ctrl_state=None, _name='mcx'):
         """Create new MCX gate."""
         num_ancilla_qubits = self.__class__.get_num_ancilla_qubits(num_ctrl_qubits)
-        super().__init__('mcx', num_ctrl_qubits + 1 + num_ancilla_qubits, [],
+        super().__init__(_name, num_ctrl_qubits + 1 + num_ancilla_qubits, [],
                          num_ctrl_qubits=num_ctrl_qubits, label=label, ctrl_state=ctrl_state)
         self.base_gate = XGate()
 
@@ -819,6 +819,9 @@ class MCXGrayCode(MCXGate):
     This delegates the implementation to the MCU1 gate, since :math:`X = H \cdot U1(\pi) \cdot H`.
     """
 
+    def __init__(self, num_ctrl_qubits, label=None, ctrl_state=None):
+        super().__init__(num_ctrl_qubits, label=label, ctrl_state=ctrl_state, _name='mcx_gray')
+
     def _define(self):
         """Define the MCX gate using the Gray code."""
         from .u1 import MCU1Gate
@@ -838,6 +841,9 @@ class MCXRecursive(MCXGate):
     for these we have a concrete implementation that do not require ancillas.
     """
 
+    def __init__(self, num_ctrl_qubits, label=None, ctrl_state=None):
+        super().__init__(num_ctrl_qubits, label=label, ctrl_state=ctrl_state, _name='mcx_recursive')
+
     @staticmethod
     def get_num_ancilla_qubits(num_ctrl_qubits, mode='recursion'):
         """Get the number of required ancilla qubits."""
@@ -889,7 +895,7 @@ def __new__(cls, num_ctrl_qubits=None, dirty_ancillas=False,  # pylint: disable=
         return super().__new__(cls, num_ctrl_qubits, label=label, ctrl_state=ctrl_state)
 
     def __init__(self, num_ctrl_qubits, dirty_ancillas=False, label=None, ctrl_state=None):
-        super().__init__(num_ctrl_qubits, label=label, ctrl_state=ctrl_state)
+        super().__init__(num_ctrl_qubits, label=label, ctrl_state=ctrl_state, _name='mcx_vchain')
         self._dirty_ancillas = dirty_ancillas
 
     @staticmethod


Test patch: