We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
mcry mode='basic' does not use ancillas
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

- **0.14.2**:
- **3**:
- **Ubuntu**:

### What is the current behavior?
The mcry gate (implemented in https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py) has a 'basic' mode that expects ancillas, but calls mct in its default 'noancilla' mode. (so both mcry(mode = 'noancilla') and mcry(mode='basic') use no ancillas and a lot of gates).


### Steps to reproduce the problem



### What is the expected behavior?
mcry in mode='basic' should call mct with mode='basic'


### Suggested solutions




--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py`
Content:
```
1 # This code is part of Qiskit.
2 #
3 # (C) Copyright IBM 2018, 2019.
4 #
5 # This code is licensed under the Apache License, Version 2.0. You may
6 # obtain a copy of this license in the LICENSE.txt file in the root directory
7 # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8 #
9 # Any modifications or derivative works of this code must retain this
10 # copyright notice, and modified files need to carry a notice indicating
11 # that they have been altered from the originals.
12 """
13 Multiple-Controlled U3 gate. Not using ancillary qubits.
14 """
15 
16 import logging
17 from math import pi
18 from qiskit.circuit import QuantumCircuit, QuantumRegister, Qubit
19 from qiskit.circuit.library.standard_gates.u3 import _generate_gray_code
20 from qiskit.exceptions import QiskitError
21 
22 logger = logging.getLogger(__name__)
23 
24 
25 def _apply_cu3(circuit, theta, phi, lam, control, target, use_basis_gates=True):
26     if use_basis_gates:
27         circuit.u1((lam + phi) / 2, control)
28         circuit.u1((lam - phi) / 2, target)
29         circuit.cx(control, target)
30         circuit.u3(-theta / 2, 0, -(phi + lam) / 2, target)
31         circuit.cx(control, target)
32         circuit.u3(theta / 2, phi, 0, target)
33     else:
34         circuit.cu3(theta, phi, lam, control, target)
35 
36 
37 def _apply_mcu3_graycode(circuit, theta, phi, lam, ctls, tgt, use_basis_gates):
38     """Apply multi-controlled u3 gate from ctls to tgt using graycode
39     pattern with single-step angles theta, phi, lam."""
40 
41     n = len(ctls)
42 
43     gray_code = _generate_gray_code(n)
44     last_pattern = None
45 
46     for pattern in gray_code:
47         if '1' not in pattern:
48             continue
49         if last_pattern is None:
50             last_pattern = pattern
51         # find left most set bit
52         lm_pos = list(pattern).index('1')
53 
54         # find changed bit
55         comp = [i != j for i, j in zip(pattern, last_pattern)]
56         if True in comp:
57             pos = comp.index(True)
58         else:
59             pos = None
60         if pos is not None:
61             if pos != lm_pos:
62                 circuit.cx(ctls[pos], ctls[lm_pos])
63             else:
64                 indices = [i for i, x in enumerate(pattern) if x == '1']
65                 for idx in indices[1:]:
66                     circuit.cx(ctls[idx], ctls[lm_pos])
67         # check parity and undo rotation
68         if pattern.count('1') % 2 == 0:
69             # inverse CU3: u3(theta, phi, lamb)^dagger = u3(-theta, -lam, -phi)
70             _apply_cu3(circuit, -theta, -lam, -phi, ctls[lm_pos], tgt,
71                        use_basis_gates=use_basis_gates)
72         else:
73             _apply_cu3(circuit, theta, phi, lam, ctls[lm_pos], tgt,
74                        use_basis_gates=use_basis_gates)
75         last_pattern = pattern
76 
77 
78 def mcrx(self, theta, q_controls, q_target, use_basis_gates=False):
79     """
80     Apply Multiple-Controlled X rotation gate
81 
82     Args:
83         self (QuantumCircuit): The QuantumCircuit object to apply the mcrx gate on.
84         theta (float): angle theta
85         q_controls (list(Qubit)): The list of control qubits
86         q_target (Qubit): The target qubit
87         use_basis_gates (bool): use u1, u2, u3, cx, id
88 
89     Raises:
90         QiskitError: parameter errors
91     """
92 
93     # check controls
94     if isinstance(q_controls, QuantumRegister):
95         control_qubits = list(q_controls)
96     elif isinstance(q_controls, list):
97         control_qubits = q_controls
98     else:
99         raise QiskitError(
100             'The mcrx gate needs a list of qubits or a quantum register for controls.')
101 
102     # check target
103     if isinstance(q_target, Qubit):
104         target_qubit = q_target
105     else:
106         raise QiskitError('The mcrx gate needs a single qubit as target.')
107 
108     all_qubits = control_qubits + [target_qubit]
109 
110     self._check_qargs(all_qubits)
111     self._check_dups(all_qubits)
112 
113     n_c = len(control_qubits)
114     if n_c == 1:  # cu3
115         _apply_cu3(self, theta, -pi/2, pi/2, control_qubits[0],
116                    target_qubit, use_basis_gates=use_basis_gates)
117     else:
118         theta_step = theta * (1 / (2 ** (n_c - 1)))
119         _apply_mcu3_graycode(self, theta_step, -pi/2, pi/2, control_qubits,
120                              target_qubit, use_basis_gates=use_basis_gates)
121 
122 
123 def mcry(self, theta, q_controls, q_target, q_ancillae, mode='basic',
124          use_basis_gates=False):
125     """
126     Apply Multiple-Controlled Y rotation gate
127 
128     Args:
129         self (QuantumCircuit): The QuantumCircuit object to apply the mcry gate on.
130         theta (float): angle theta
131         q_controls (list(Qubit)): The list of control qubits
132         q_target (Qubit): The target qubit
133         q_ancillae (QuantumRegister or tuple(QuantumRegister, int)): The list of ancillary qubits.
134         mode (string): The implementation mode to use
135         use_basis_gates (bool): use u1, u2, u3, cx, id
136 
137     Raises:
138         QiskitError: parameter errors
139     """
140 
141     # check controls
142     if isinstance(q_controls, QuantumRegister):
143         control_qubits = list(q_controls)
144     elif isinstance(q_controls, list):
145         control_qubits = q_controls
146     else:
147         raise QiskitError('The mcry gate needs a list of qubits or a quantum '
148                           'register for controls.')
149 
150     # check target
151     if isinstance(q_target, Qubit):
152         target_qubit = q_target
153     else:
154         raise QiskitError('The mcry gate needs a single qubit as target.')
155 
156     # check ancilla
157     if q_ancillae is None:
158         ancillary_qubits = []
159     elif isinstance(q_ancillae, QuantumRegister):
160         ancillary_qubits = list(q_ancillae)
161     elif isinstance(q_ancillae, list):
162         ancillary_qubits = q_ancillae
163     else:
164         raise QiskitError('The mcry gate needs None or a list of qubits or a '
165                           'quantum register for ancilla.')
166 
167     all_qubits = control_qubits + [target_qubit] + ancillary_qubits
168 
169     self._check_qargs(all_qubits)
170     self._check_dups(all_qubits)
171 
172     if mode == 'basic':
173         self.u3(theta / 2, 0, 0, q_target)
174         self.mct(q_controls, q_target, q_ancillae)
175         self.u3(-theta / 2, 0, 0, q_target)
176         self.mct(q_controls, q_target, q_ancillae)
177     elif mode == 'noancilla':
178         n_c = len(control_qubits)
179         if n_c == 1:  # cu3
180             _apply_cu3(self, theta, 0, 0, control_qubits[0],
181                        target_qubit, use_basis_gates=use_basis_gates)
182         else:
183             theta_step = theta * (1 / (2 ** (n_c - 1)))
184             _apply_mcu3_graycode(self, theta_step, 0, 0, control_qubits,
185                                  target_qubit, use_basis_gates=use_basis_gates)
186     else:
187         raise QiskitError('Unrecognized mode for building MCRY circuit: {}.'.format(mode))
188 
189 
190 def mcrz(self, lam, q_controls, q_target, use_basis_gates=False):
191     """
192     Apply Multiple-Controlled Z rotation gate
193 
194     Args:
195         self (QuantumCircuit): The QuantumCircuit object to apply the mcrz gate on.
196         lam (float): angle lambda
197         q_controls (list(Qubit)): The list of control qubits
198         q_target (Qubit): The target qubit
199         use_basis_gates (bool): use u1, u2, u3, cx, id
200 
201     Raises:
202         QiskitError: parameter errors
203     """
204 
205     # check controls
206     if isinstance(q_controls, QuantumRegister):
207         control_qubits = list(q_controls)
208     elif isinstance(q_controls, list):
209         control_qubits = q_controls
210     else:
211         raise QiskitError(
212             'The mcrz gate needs a list of qubits or a quantum register for controls.')
213 
214     # check target
215     if isinstance(q_target, Qubit):
216         target_qubit = q_target
217     else:
218         raise QiskitError('The mcrz gate needs a single qubit as target.')
219 
220     all_qubits = control_qubits + [target_qubit]
221 
222     self._check_qargs(all_qubits)
223     self._check_dups(all_qubits)
224 
225     n_c = len(control_qubits)
226     if n_c == 1:  # cu3
227         _apply_cu3(self, 0, 0, lam, control_qubits[0],
228                    target_qubit, use_basis_gates=use_basis_gates)
229     else:
230         lam_step = lam * (1 / (2 ** (n_c - 1)))
231         _apply_mcu3_graycode(self, 0, 0, lam_step, control_qubits,
232                              target_qubit, use_basis_gates=use_basis_gates)
233 
234 
235 QuantumCircuit.mcrx = mcrx
236 QuantumCircuit.mcry = mcry
237 QuantumCircuit.mcrz = mcrz
238
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py b/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py
--- a/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py
+++ b/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py
@@ -16,6 +16,7 @@
 import logging
 from math import pi
 from qiskit.circuit import QuantumCircuit, QuantumRegister, Qubit
+from qiskit.circuit.library.standard_gates.x import MCXGate
 from qiskit.circuit.library.standard_gates.u3 import _generate_gray_code
 from qiskit.exceptions import QiskitError
 
@@ -31,7 +32,7 @@
         circuit.cx(control, target)
         circuit.u3(theta / 2, phi, 0, target)
     else:
-        circuit.cu3(theta, phi, lam, control, target)
+        circuit.cu(theta, phi, lam, 0, control, target)
 
 
 def _apply_mcu3_graycode(circuit, theta, phi, lam, ctls, tgt, use_basis_gates):
@@ -120,7 +121,7 @@
                              target_qubit, use_basis_gates=use_basis_gates)
 
 
-def mcry(self, theta, q_controls, q_target, q_ancillae, mode='basic',
+def mcry(self, theta, q_controls, q_target, q_ancillae, mode=None,
          use_basis_gates=False):
     """
     Apply Multiple-Controlled Y rotation gate
@@ -169,11 +170,20 @@
     self._check_qargs(all_qubits)
     self._check_dups(all_qubits)
 
+    # auto-select the best mode
+    if mode is None:
+        # if enough ancillary qubits are provided, use the 'v-chain' method
+        additional_vchain = MCXGate.get_num_ancilla_qubits(len(control_qubits), 'v-chain')
+        if len(ancillary_qubits) >= additional_vchain:
+            mode = 'basic'
+        else:
+            mode = 'noancilla'
+
     if mode == 'basic':
-        self.u3(theta / 2, 0, 0, q_target)
-        self.mct(q_controls, q_target, q_ancillae)
-        self.u3(-theta / 2, 0, 0, q_target)
-        self.mct(q_controls, q_target, q_ancillae)
+        self.ry(theta / 2, q_target)
+        self.mcx(q_controls, q_target, q_ancillae, mode='v-chain')
+        self.ry(-theta / 2, q_target)
+        self.mcx(q_controls, q_target, q_ancillae, mode='v-chain')
     elif mode == 'noancilla':
         n_c = len(control_qubits)
         if n_c == 1:  # cu3

{"golden_diff": "diff --git a/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py b/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py\n--- a/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py\n+++ b/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py\n@@ -16,6 +16,7 @@\n import logging\n from math import pi\n from qiskit.circuit import QuantumCircuit, QuantumRegister, Qubit\n+from qiskit.circuit.library.standard_gates.x import MCXGate\n from qiskit.circuit.library.standard_gates.u3 import _generate_gray_code\n from qiskit.exceptions import QiskitError\n \n@@ -31,7 +32,7 @@\n         circuit.cx(control, target)\n         circuit.u3(theta / 2, phi, 0, target)\n     else:\n-        circuit.cu3(theta, phi, lam, control, target)\n+        circuit.cu(theta, phi, lam, 0, control, target)\n \n \n def _apply_mcu3_graycode(circuit, theta, phi, lam, ctls, tgt, use_basis_gates):\n@@ -120,7 +121,7 @@\n                              target_qubit, use_basis_gates=use_basis_gates)\n \n \n-def mcry(self, theta, q_controls, q_target, q_ancillae, mode='basic',\n+def mcry(self, theta, q_controls, q_target, q_ancillae, mode=None,\n          use_basis_gates=False):\n     \"\"\"\n     Apply Multiple-Controlled Y rotation gate\n@@ -169,11 +170,20 @@\n     self._check_qargs(all_qubits)\n     self._check_dups(all_qubits)\n \n+    # auto-select the best mode\n+    if mode is None:\n+        # if enough ancillary qubits are provided, use the 'v-chain' method\n+        additional_vchain = MCXGate.get_num_ancilla_qubits(len(control_qubits), 'v-chain')\n+        if len(ancillary_qubits) >= additional_vchain:\n+            mode = 'basic'\n+        else:\n+            mode = 'noancilla'\n+\n     if mode == 'basic':\n-        self.u3(theta / 2, 0, 0, q_target)\n-        self.mct(q_controls, q_target, q_ancillae)\n-        self.u3(-theta / 2, 0, 0, q_target)\n-        self.mct(q_controls, q_target, q_ancillae)\n+        self.ry(theta / 2, q_target)\n+        self.mcx(q_controls, q_target, q_ancillae, mode='v-chain')\n+        self.ry(-theta / 2, q_target)\n+        self.mcx(q_controls, q_target, q_ancillae, mode='v-chain')\n     elif mode == 'noancilla':\n         n_c = len(control_qubits)\n         if n_c == 1:  # cu3\n", "issue": "mcry mode='basic' does not use ancillas\n<!-- \u26a0\ufe0f If you do not respect this template, your issue will be closed -->\r\n<!-- \u26a0\ufe0f Make sure to browse the opened and closed issues -->\r\n\r\n### Information\r\n\r\n- **0.14.2**:\r\n- **3**:\r\n- **Ubuntu**:\r\n\r\n### What is the current behavior?\r\nThe mcry gate (implemented in https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py) has a 'basic' mode that expects ancillas, but calls mct in its default 'noancilla' mode. (so both mcry(mode = 'noancilla') and mcry(mode='basic') use no ancillas and a lot of gates).\r\n\r\n\r\n### Steps to reproduce the problem\r\n\r\n\r\n\r\n### What is the expected behavior?\r\nmcry in mode='basic' should call mct with mode='basic'\r\n\r\n\r\n### Suggested solutions\r\n\r\n\r\n\n", "before_files": [{"content": "# This code is part of Qiskit.\n#\n# (C) Copyright IBM 2018, 2019.\n#\n# This code is licensed under the Apache License, Version 2.0. You may\n# obtain a copy of this license in the LICENSE.txt file in the root directory\n# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.\n#\n# Any modifications or derivative works of this code must retain this\n# copyright notice, and modified files need to carry a notice indicating\n# that they have been altered from the originals.\n\"\"\"\nMultiple-Controlled U3 gate. Not using ancillary qubits.\n\"\"\"\n\nimport logging\nfrom math import pi\nfrom qiskit.circuit import QuantumCircuit, QuantumRegister, Qubit\nfrom qiskit.circuit.library.standard_gates.u3 import _generate_gray_code\nfrom qiskit.exceptions import QiskitError\n\nlogger = logging.getLogger(__name__)\n\n\ndef _apply_cu3(circuit, theta, phi, lam, control, target, use_basis_gates=True):\n    if use_basis_gates:\n        circuit.u1((lam + phi) / 2, control)\n        circuit.u1((lam - phi) / 2, target)\n        circuit.cx(control, target)\n        circuit.u3(-theta / 2, 0, -(phi + lam) / 2, target)\n        circuit.cx(control, target)\n        circuit.u3(theta / 2, phi, 0, target)\n    else:\n        circuit.cu3(theta, phi, lam, control, target)\n\n\ndef _apply_mcu3_graycode(circuit, theta, phi, lam, ctls, tgt, use_basis_gates):\n    \"\"\"Apply multi-controlled u3 gate from ctls to tgt using graycode\n    pattern with single-step angles theta, phi, lam.\"\"\"\n\n    n = len(ctls)\n\n    gray_code = _generate_gray_code(n)\n    last_pattern = None\n\n    for pattern in gray_code:\n        if '1' not in pattern:\n            continue\n        if last_pattern is None:\n            last_pattern = pattern\n        # find left most set bit\n        lm_pos = list(pattern).index('1')\n\n        # find changed bit\n        comp = [i != j for i, j in zip(pattern, last_pattern)]\n        if True in comp:\n            pos = comp.index(True)\n        else:\n            pos = None\n        if pos is not None:\n            if pos != lm_pos:\n                circuit.cx(ctls[pos], ctls[lm_pos])\n            else:\n                indices = [i for i, x in enumerate(pattern) if x == '1']\n                for idx in indices[1:]:\n                    circuit.cx(ctls[idx], ctls[lm_pos])\n        # check parity and undo rotation\n        if pattern.count('1') % 2 == 0:\n            # inverse CU3: u3(theta, phi, lamb)^dagger = u3(-theta, -lam, -phi)\n            _apply_cu3(circuit, -theta, -lam, -phi, ctls[lm_pos], tgt,\n                       use_basis_gates=use_basis_gates)\n        else:\n            _apply_cu3(circuit, theta, phi, lam, ctls[lm_pos], tgt,\n                       use_basis_gates=use_basis_gates)\n        last_pattern = pattern\n\n\ndef mcrx(self, theta, q_controls, q_target, use_basis_gates=False):\n    \"\"\"\n    Apply Multiple-Controlled X rotation gate\n\n    Args:\n        self (QuantumCircuit): The QuantumCircuit object to apply the mcrx gate on.\n        theta (float): angle theta\n        q_controls (list(Qubit)): The list of control qubits\n        q_target (Qubit): The target qubit\n        use_basis_gates (bool): use u1, u2, u3, cx, id\n\n    Raises:\n        QiskitError: parameter errors\n    \"\"\"\n\n    # check controls\n    if isinstance(q_controls, QuantumRegister):\n        control_qubits = list(q_controls)\n    elif isinstance(q_controls, list):\n        control_qubits = q_controls\n    else:\n        raise QiskitError(\n            'The mcrx gate needs a list of qubits or a quantum register for controls.')\n\n    # check target\n    if isinstance(q_target, Qubit):\n        target_qubit = q_target\n    else:\n        raise QiskitError('The mcrx gate needs a single qubit as target.')\n\n    all_qubits = control_qubits + [target_qubit]\n\n    self._check_qargs(all_qubits)\n    self._check_dups(all_qubits)\n\n    n_c = len(control_qubits)\n    if n_c == 1:  # cu3\n        _apply_cu3(self, theta, -pi/2, pi/2, control_qubits[0],\n                   target_qubit, use_basis_gates=use_basis_gates)\n    else:\n        theta_step = theta * (1 / (2 ** (n_c - 1)))\n        _apply_mcu3_graycode(self, theta_step, -pi/2, pi/2, control_qubits,\n                             target_qubit, use_basis_gates=use_basis_gates)\n\n\ndef mcry(self, theta, q_controls, q_target, q_ancillae, mode='basic',\n         use_basis_gates=False):\n    \"\"\"\n    Apply Multiple-Controlled Y rotation gate\n\n    Args:\n        self (QuantumCircuit): The QuantumCircuit object to apply the mcry gate on.\n        theta (float): angle theta\n        q_controls (list(Qubit)): The list of control qubits\n        q_target (Qubit): The target qubit\n        q_ancillae (QuantumRegister or tuple(QuantumRegister, int)): The list of ancillary qubits.\n        mode (string): The implementation mode to use\n        use_basis_gates (bool): use u1, u2, u3, cx, id\n\n    Raises:\n        QiskitError: parameter errors\n    \"\"\"\n\n    # check controls\n    if isinstance(q_controls, QuantumRegister):\n        control_qubits = list(q_controls)\n    elif isinstance(q_controls, list):\n        control_qubits = q_controls\n    else:\n        raise QiskitError('The mcry gate needs a list of qubits or a quantum '\n                          'register for controls.')\n\n    # check target\n    if isinstance(q_target, Qubit):\n        target_qubit = q_target\n    else:\n        raise QiskitError('The mcry gate needs a single qubit as target.')\n\n    # check ancilla\n    if q_ancillae is None:\n        ancillary_qubits = []\n    elif isinstance(q_ancillae, QuantumRegister):\n        ancillary_qubits = list(q_ancillae)\n    elif isinstance(q_ancillae, list):\n        ancillary_qubits = q_ancillae\n    else:\n        raise QiskitError('The mcry gate needs None or a list of qubits or a '\n                          'quantum register for ancilla.')\n\n    all_qubits = control_qubits + [target_qubit] + ancillary_qubits\n\n    self._check_qargs(all_qubits)\n    self._check_dups(all_qubits)\n\n    if mode == 'basic':\n        self.u3(theta / 2, 0, 0, q_target)\n        self.mct(q_controls, q_target, q_ancillae)\n        self.u3(-theta / 2, 0, 0, q_target)\n        self.mct(q_controls, q_target, q_ancillae)\n    elif mode == 'noancilla':\n        n_c = len(control_qubits)\n        if n_c == 1:  # cu3\n            _apply_cu3(self, theta, 0, 0, control_qubits[0],\n                       target_qubit, use_basis_gates=use_basis_gates)\n        else:\n            theta_step = theta * (1 / (2 ** (n_c - 1)))\n            _apply_mcu3_graycode(self, theta_step, 0, 0, control_qubits,\n                                 target_qubit, use_basis_gates=use_basis_gates)\n    else:\n        raise QiskitError('Unrecognized mode for building MCRY circuit: {}.'.format(mode))\n\n\ndef mcrz(self, lam, q_controls, q_target, use_basis_gates=False):\n    \"\"\"\n    Apply Multiple-Controlled Z rotation gate\n\n    Args:\n        self (QuantumCircuit): The QuantumCircuit object to apply the mcrz gate on.\n        lam (float): angle lambda\n        q_controls (list(Qubit)): The list of control qubits\n        q_target (Qubit): The target qubit\n        use_basis_gates (bool): use u1, u2, u3, cx, id\n\n    Raises:\n        QiskitError: parameter errors\n    \"\"\"\n\n    # check controls\n    if isinstance(q_controls, QuantumRegister):\n        control_qubits = list(q_controls)\n    elif isinstance(q_controls, list):\n        control_qubits = q_controls\n    else:\n        raise QiskitError(\n            'The mcrz gate needs a list of qubits or a quantum register for controls.')\n\n    # check target\n    if isinstance(q_target, Qubit):\n        target_qubit = q_target\n    else:\n        raise QiskitError('The mcrz gate needs a single qubit as target.')\n\n    all_qubits = control_qubits + [target_qubit]\n\n    self._check_qargs(all_qubits)\n    self._check_dups(all_qubits)\n\n    n_c = len(control_qubits)\n    if n_c == 1:  # cu3\n        _apply_cu3(self, 0, 0, lam, control_qubits[0],\n                   target_qubit, use_basis_gates=use_basis_gates)\n    else:\n        lam_step = lam * (1 / (2 ** (n_c - 1)))\n        _apply_mcu3_graycode(self, 0, 0, lam_step, control_qubits,\n                             target_qubit, use_basis_gates=use_basis_gates)\n\n\nQuantumCircuit.mcrx = mcrx\nQuantumCircuit.mcry = mcry\nQuantumCircuit.mcrz = mcrz\n", "path": "qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py"}], "after_files": [{"content": "# This code is part of Qiskit.\n#\n# (C) Copyright IBM 2018, 2019.\n#\n# This code is licensed under the Apache License, Version 2.0. You may\n# obtain a copy of this license in the LICENSE.txt file in the root directory\n# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.\n#\n# Any modifications or derivative works of this code must retain this\n# copyright notice, and modified files need to carry a notice indicating\n# that they have been altered from the originals.\n\"\"\"\nMultiple-Controlled U3 gate. Not using ancillary qubits.\n\"\"\"\n\nimport logging\nfrom math import pi\nfrom qiskit.circuit import QuantumCircuit, QuantumRegister, Qubit\nfrom qiskit.circuit.library.standard_gates.x import MCXGate\nfrom qiskit.circuit.library.standard_gates.u3 import _generate_gray_code\nfrom qiskit.exceptions import QiskitError\n\nlogger = logging.getLogger(__name__)\n\n\ndef _apply_cu3(circuit, theta, phi, lam, control, target, use_basis_gates=True):\n    if use_basis_gates:\n        circuit.u1((lam + phi) / 2, control)\n        circuit.u1((lam - phi) / 2, target)\n        circuit.cx(control, target)\n        circuit.u3(-theta / 2, 0, -(phi + lam) / 2, target)\n        circuit.cx(control, target)\n        circuit.u3(theta / 2, phi, 0, target)\n    else:\n        circuit.cu(theta, phi, lam, 0, control, target)\n\n\ndef _apply_mcu3_graycode(circuit, theta, phi, lam, ctls, tgt, use_basis_gates):\n    \"\"\"Apply multi-controlled u3 gate from ctls to tgt using graycode\n    pattern with single-step angles theta, phi, lam.\"\"\"\n\n    n = len(ctls)\n\n    gray_code = _generate_gray_code(n)\n    last_pattern = None\n\n    for pattern in gray_code:\n        if '1' not in pattern:\n            continue\n        if last_pattern is None:\n            last_pattern = pattern\n        # find left most set bit\n        lm_pos = list(pattern).index('1')\n\n        # find changed bit\n        comp = [i != j for i, j in zip(pattern, last_pattern)]\n        if True in comp:\n            pos = comp.index(True)\n        else:\n            pos = None\n        if pos is not None:\n            if pos != lm_pos:\n                circuit.cx(ctls[pos], ctls[lm_pos])\n            else:\n                indices = [i for i, x in enumerate(pattern) if x == '1']\n                for idx in indices[1:]:\n                    circuit.cx(ctls[idx], ctls[lm_pos])\n        # check parity and undo rotation\n        if pattern.count('1') % 2 == 0:\n            # inverse CU3: u3(theta, phi, lamb)^dagger = u3(-theta, -lam, -phi)\n            _apply_cu3(circuit, -theta, -lam, -phi, ctls[lm_pos], tgt,\n                       use_basis_gates=use_basis_gates)\n        else:\n            _apply_cu3(circuit, theta, phi, lam, ctls[lm_pos], tgt,\n                       use_basis_gates=use_basis_gates)\n        last_pattern = pattern\n\n\ndef mcrx(self, theta, q_controls, q_target, use_basis_gates=False):\n    \"\"\"\n    Apply Multiple-Controlled X rotation gate\n\n    Args:\n        self (QuantumCircuit): The QuantumCircuit object to apply the mcrx gate on.\n        theta (float): angle theta\n        q_controls (list(Qubit)): The list of control qubits\n        q_target (Qubit): The target qubit\n        use_basis_gates (bool): use u1, u2, u3, cx, id\n\n    Raises:\n        QiskitError: parameter errors\n    \"\"\"\n\n    # check controls\n    if isinstance(q_controls, QuantumRegister):\n        control_qubits = list(q_controls)\n    elif isinstance(q_controls, list):\n        control_qubits = q_controls\n    else:\n        raise QiskitError(\n            'The mcrx gate needs a list of qubits or a quantum register for controls.')\n\n    # check target\n    if isinstance(q_target, Qubit):\n        target_qubit = q_target\n    else:\n        raise QiskitError('The mcrx gate needs a single qubit as target.')\n\n    all_qubits = control_qubits + [target_qubit]\n\n    self._check_qargs(all_qubits)\n    self._check_dups(all_qubits)\n\n    n_c = len(control_qubits)\n    if n_c == 1:  # cu3\n        _apply_cu3(self, theta, -pi/2, pi/2, control_qubits[0],\n                   target_qubit, use_basis_gates=use_basis_gates)\n    else:\n        theta_step = theta * (1 / (2 ** (n_c - 1)))\n        _apply_mcu3_graycode(self, theta_step, -pi/2, pi/2, control_qubits,\n                             target_qubit, use_basis_gates=use_basis_gates)\n\n\ndef mcry(self, theta, q_controls, q_target, q_ancillae, mode=None,\n         use_basis_gates=False):\n    \"\"\"\n    Apply Multiple-Controlled Y rotation gate\n\n    Args:\n        self (QuantumCircuit): The QuantumCircuit object to apply the mcry gate on.\n        theta (float): angle theta\n        q_controls (list(Qubit)): The list of control qubits\n        q_target (Qubit): The target qubit\n        q_ancillae (QuantumRegister or tuple(QuantumRegister, int)): The list of ancillary qubits.\n        mode (string): The implementation mode to use\n        use_basis_gates (bool): use u1, u2, u3, cx, id\n\n    Raises:\n        QiskitError: parameter errors\n    \"\"\"\n\n    # check controls\n    if isinstance(q_controls, QuantumRegister):\n        control_qubits = list(q_controls)\n    elif isinstance(q_controls, list):\n        control_qubits = q_controls\n    else:\n        raise QiskitError('The mcry gate needs a list of qubits or a quantum '\n                          'register for controls.')\n\n    # check target\n    if isinstance(q_target, Qubit):\n        target_qubit = q_target\n    else:\n        raise QiskitError('The mcry gate needs a single qubit as target.')\n\n    # check ancilla\n    if q_ancillae is None:\n        ancillary_qubits = []\n    elif isinstance(q_ancillae, QuantumRegister):\n        ancillary_qubits = list(q_ancillae)\n    elif isinstance(q_ancillae, list):\n        ancillary_qubits = q_ancillae\n    else:\n        raise QiskitError('The mcry gate needs None or a list of qubits or a '\n                          'quantum register for ancilla.')\n\n    all_qubits = control_qubits + [target_qubit] + ancillary_qubits\n\n    self._check_qargs(all_qubits)\n    self._check_dups(all_qubits)\n\n    # auto-select the best mode\n    if mode is None:\n        # if enough ancillary qubits are provided, use the 'v-chain' method\n        additional_vchain = MCXGate.get_num_ancilla_qubits(len(control_qubits), 'v-chain')\n        if len(ancillary_qubits) >= additional_vchain:\n            mode = 'basic'\n        else:\n            mode = 'noancilla'\n\n    if mode == 'basic':\n        self.ry(theta / 2, q_target)\n        self.mcx(q_controls, q_target, q_ancillae, mode='v-chain')\n        self.ry(-theta / 2, q_target)\n        self.mcx(q_controls, q_target, q_ancillae, mode='v-chain')\n    elif mode == 'noancilla':\n        n_c = len(control_qubits)\n        if n_c == 1:  # cu3\n            _apply_cu3(self, theta, 0, 0, control_qubits[0],\n                       target_qubit, use_basis_gates=use_basis_gates)\n        else:\n            theta_step = theta * (1 / (2 ** (n_c - 1)))\n            _apply_mcu3_graycode(self, theta_step, 0, 0, control_qubits,\n                                 target_qubit, use_basis_gates=use_basis_gates)\n    else:\n        raise QiskitError('Unrecognized mode for building MCRY circuit: {}.'.format(mode))\n\n\ndef mcrz(self, lam, q_controls, q_target, use_basis_gates=False):\n    \"\"\"\n    Apply Multiple-Controlled Z rotation gate\n\n    Args:\n        self (QuantumCircuit): The QuantumCircuit object to apply the mcrz gate on.\n        lam (float): angle lambda\n        q_controls (list(Qubit)): The list of control qubits\n        q_target (Qubit): The target qubit\n        use_basis_gates (bool): use u1, u2, u3, cx, id\n\n    Raises:\n        QiskitError: parameter errors\n    \"\"\"\n\n    # check controls\n    if isinstance(q_controls, QuantumRegister):\n        control_qubits = list(q_controls)\n    elif isinstance(q_controls, list):\n        control_qubits = q_controls\n    else:\n        raise QiskitError(\n            'The mcrz gate needs a list of qubits or a quantum register for controls.')\n\n    # check target\n    if isinstance(q_target, Qubit):\n        target_qubit = q_target\n    else:\n        raise QiskitError('The mcrz gate needs a single qubit as target.')\n\n    all_qubits = control_qubits + [target_qubit]\n\n    self._check_qargs(all_qubits)\n    self._check_dups(all_qubits)\n\n    n_c = len(control_qubits)\n    if n_c == 1:  # cu3\n        _apply_cu3(self, 0, 0, lam, control_qubits[0],\n                   target_qubit, use_basis_gates=use_basis_gates)\n    else:\n        lam_step = lam * (1 / (2 ** (n_c - 1)))\n        _apply_mcu3_graycode(self, 0, 0, lam_step, control_qubits,\n                             target_qubit, use_basis_gates=use_basis_gates)\n\n\nQuantumCircuit.mcrx = mcrx\nQuantumCircuit.mcry = mcry\nQuantumCircuit.mcrz = mcrz\n", "path": "qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py"}]}