Repository: Qiskit/qiskit

Issue:
Error in the transpilation of double controlled gate
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Informations

- **Qiskit Aer version**: 0.7.2
- **Python version**: 3.8.5
- **Operating system**: MacOS

### What is the current behavior?

A statevector simulation of a simple quantum circuit with two gates gives incorrect results. The circuit consists from an X gate and RYGate controlled by two qubits. 

The following code compares the problematic circuit with double controlled gate (gate.control(...).control(...)) with its equivalent which is implemented using gate.control(num_cuntrol_qubits=2, ...):

```
from qiskit import QuantumCircuit, Aer, execute
from qiskit.circuit.library import RYGate

backend = Aer.get_backend('statevector_simulator')

qc = QuantumCircuit(3)
gate = RYGate(1.5).control(num_ctrl_qubits=1, ctrl_state=0).control(num_ctrl_qubits=1, ctrl_state=1)
qc.x(0)
qc.compose(other=gate, qubits=[0, 1, 2], inplace=True)
strings = list(execute(qc, backend, shots=1).result().get_counts().keys())
assert strings == ['001'], "counts of qc are be ['001']"

qc2 = QuantumCircuit(3)
gate2 = RYGate(1.5).control(num_ctrl_qubits=2, ctrl_state='01')
qc2.x(0)
qc2.compose(other=gate2, qubits=[0, 1, 2], inplace=True)
strings2 = list(execute(qc2, backend, shots=1).result().get_counts().keys())
assert strings2 == ['001', '101'], "counts of qc2 are ['001', '101']
```



### Steps to reproduce the problem

The code above is self contained and should work in any setup.

### What is the expected behavior?

The code should return ['001', '101'] in both cases.

### Suggested solutions

I believe the problems lies in the transpilation process, due to the different transpiled circuits. In the doubly controlled circuit there are extra controls on the transpilation of the inner gate. I'm not sure how to correct this issue.  



Discussion:
Im transferring issue to Terra since I think it is a transpiler issue. If you look at the transpiled circuit that is run by the simulator for the two cases they are quite different:

Circuit1:
```
     ┌───┐ 
q_0: ┤ X ├──■────■─────────────────────■────────────────────■────■────────────────────■─────────────────────■────■───────
     ├───┤┌─┴─┐  │                     │                    │    │                    │                     │  ┌─┴─┐┌───┐
q_1: ┤ X ├┤ X ├──┼─────────────────────┼────────────────────■────┼────────────────────┼─────────────────────■──┤ X ├┤ X ├
     └───┘└───┘┌─┴─┐┌───────────────┐┌─┴─┐┌──────────────┐┌─┴─┐┌─┴─┐┌──────────────┐┌─┴─┐┌───────────────┐┌─┴─┐└───┘└───┘
q_2: ──────────┤ X ├┤ U(-0.375,0,0) ├┤ X ├┤ U(0.375,0,0) ├┤ X ├┤ X ├┤ U(0.375,0,0) ├┤ X ├┤ U(-0.375,0,0) ├┤ X ├──────────
               └───┘└───────────────┘└───┘└──────────────┘└───┘└───┘└──────────────┘└───┘└───────────────┘└───┘          
```
Circuit 2
```
        ┌───┐                                
q_0: ───┤ X ├──────■─────────────────■───────
        ├───┤      │                 │  ┌───┐
q_1: ───┤ X ├──────■─────────────────■──┤ X ├
     ┌──┴───┴───┐┌─┴─┐┌───────────┐┌─┴─┐└───┘
q_2: ┤ RY(0.75) ├┤ X ├┤ RY(-0.75) ├┤ X ├─────
     └──────────┘└───┘└───────────┘└───┘     
```

Resolving patch:
diff --git a/qiskit/circuit/add_control.py b/qiskit/circuit/add_control.py
--- a/qiskit/circuit/add_control.py
+++ b/qiskit/circuit/add_control.py
@@ -51,19 +51,8 @@ def add_control(
         Controlled version of gate.
 
     """
-    import qiskit.circuit.library.standard_gates as standard
-
     if ctrl_state is None:
         ctrl_state = 2 ** num_ctrl_qubits - 1
-    if isinstance(operation, standard.RZGate) or operation.name == "rz":
-        # num_ctrl_qubits > 1
-        # the condition matching 'name' above is to catch a test case,
-        # 'TestControlledGate.test_rotation_gates', where the rz gate
-        # gets converted to a circuit before becoming a generic Gate object.
-        cgate = standard.CRZGate(*operation.params)
-        cngate = cgate.control(num_ctrl_qubits - 1)
-        cngate.ctrl_state = ctrl_state
-        return cngate
     if isinstance(operation, UnitaryGate):
         # attempt decomposition
         operation._define()
@@ -108,6 +97,8 @@ def control(
     q_target = QuantumRegister(operation.num_qubits, name="target")
     q_ancillae = None  # TODO: add
     controlled_circ = QuantumCircuit(q_control, q_target, name="c_{}".format(operation.name))
+    if isinstance(operation, controlledgate.ControlledGate):
+        original_ctrl_state = operation.ctrl_state
     global_phase = 0
     if operation.name == "x" or (
         isinstance(operation, controlledgate.ControlledGate) and operation.base_gate.name == "x"
@@ -117,6 +108,8 @@ def control(
             global_phase += operation.definition.global_phase
     else:
         basis = ["p", "u", "x", "z", "rx", "ry", "rz", "cx"]
+        if isinstance(operation, controlledgate.ControlledGate):
+            operation.ctrl_state = None
         unrolled_gate = _unroll_gate(operation, basis_gates=basis)
         if unrolled_gate.definition.global_phase:
             global_phase += unrolled_gate.definition.global_phase
@@ -224,6 +217,7 @@ def control(
         else:
             controlled_circ.mcp(global_phase, q_control[:-1], q_control[-1])
     if isinstance(operation, controlledgate.ControlledGate):
+        operation.ctrl_state = original_ctrl_state
         new_num_ctrl_qubits = num_ctrl_qubits + operation.num_ctrl_qubits
         new_ctrl_state = operation.ctrl_state << num_ctrl_qubits | ctrl_state
         base_name = operation.base_gate.name


Test patch:
diff --git a/test/python/circuit/test_controlled_gate.py b/test/python/circuit/test_controlled_gate.py
--- a/test/python/circuit/test_controlled_gate.py
+++ b/test/python/circuit/test_controlled_gate.py
@@ -239,6 +239,15 @@ def test_single_controlled_composite_gate(self):
         ref_mat = Operator(qc).data
         self.assertTrue(matrix_equal(cop_mat, ref_mat, ignore_phase=True))
 
+    def test_control_open_controlled_gate(self):
+        """Test control(2) vs control.control where inner gate has open controls."""
+        gate1pre = ZGate().control(1, ctrl_state=0)
+        gate1 = gate1pre.control(1, ctrl_state=1)
+        gate2 = ZGate().control(2, ctrl_state=1)
+        expected = Operator(_compute_control_matrix(ZGate().to_matrix(), 2, ctrl_state=1))
+        self.assertEqual(expected, Operator(gate1))
+        self.assertEqual(expected, Operator(gate2))
+
     def test_multi_control_z(self):
         """Test a multi controlled Z gate."""
         qc = QuantumCircuit(1)