We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Can't invert gate created from QuantumCircuit.to_gate
<!-- ⚠️ 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**:
- **Python version**:
- **Operating system**:

### What is the current behavior?
When inverting a gate created from QuantumCircuit.to_gate the following exception is raised:

`ValueError: not enough values to unpack (expected 3, got 2)`


### Steps to reproduce the problem
```
qc = QuantumCircuit(1)
qc.x(0)
gate = qc.to_gate()
gate.inverse()
```

### What is the expected behavior?



### 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/converters/circuit_to_gate.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 
3 # This code is part of Qiskit.
4 #
5 # (C) Copyright IBM 2017, 2019.
6 #
7 # This code is licensed under the Apache License, Version 2.0. You may
8 # obtain a copy of this license in the LICENSE.txt file in the root directory
9 # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
10 #
11 # Any modifications or derivative works of this code must retain this
12 # copyright notice, and modified files need to carry a notice indicating
13 # that they have been altered from the originals.
14 
15 """Helper function for converting a circuit to a gate"""
16 
17 from qiskit.circuit.gate import Gate
18 from qiskit.circuit.quantumregister import QuantumRegister, Qubit
19 from qiskit.exceptions import QiskitError
20 
21 
22 def circuit_to_gate(circuit, parameter_map=None):
23     """Build a ``Gate`` object from a ``QuantumCircuit``.
24 
25     The gate is anonymous (not tied to a named quantum register),
26     and so can be inserted into another circuit. The gate will
27     have the same string name as the circuit.
28 
29     Args:
30         circuit (QuantumCircuit): the input circuit.
31         parameter_map (dict): For parameterized circuits, a mapping from
32            parameters in the circuit to parameters to be used in the gate.
33            If None, existing circuit parameters will also parameterize the
34            Gate.
35 
36     Raises:
37         QiskitError: if circuit is non-unitary or if
38             parameter_map is not compatible with circuit
39 
40     Return:
41         Gate: a Gate equivalent to the action of the
42             input circuit. Upon decomposition, this gate will
43             yield the components comprising the original circuit.
44     """
45     for inst, _, _ in circuit.data:
46         if not isinstance(inst, Gate):
47             raise QiskitError('One or more instructions in this instruction '
48                               'cannot be converted to a gate')
49 
50     if parameter_map is None:
51         parameter_dict = {p: p for p in circuit.parameters}
52     else:
53         parameter_dict = circuit._unroll_param_dict(parameter_map)
54 
55     if parameter_dict.keys() != circuit.parameters:
56         raise QiskitError(('parameter_map should map all circuit parameters. '
57                            'Circuit parameters: {}, parameter_map: {}').format(
58                                circuit.parameters, parameter_dict))
59 
60     gate = Gate(name=circuit.name,
61                 num_qubits=sum([qreg.size for qreg in circuit.qregs]),
62                 params=sorted(parameter_dict.values(), key=lambda p: p.name))
63     gate.condition = None
64 
65     def find_bit_position(bit):
66         """find the index of a given bit (Register, int) within
67         a flat ordered list of bits of the circuit
68         """
69         if isinstance(bit, Qubit):
70             ordered_regs = circuit.qregs
71         else:
72             ordered_regs = circuit.cregs
73         reg_index = ordered_regs.index(bit.register)
74         return sum([reg.size for reg in ordered_regs[:reg_index]]) + bit.index
75 
76     target = circuit.copy()
77     target._substitute_parameters(parameter_dict)
78 
79     definition = target.data
80 
81     if gate.num_qubits > 0:
82         q = QuantumRegister(gate.num_qubits, 'q')
83 
84     definition = list(map(lambda x:
85                           (x[0], list(map(lambda y: q[find_bit_position(y)], x[1]))),
86                           definition))
87     gate.definition = definition
88 
89     return gate
90
```
--- 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/converters/circuit_to_gate.py b/qiskit/converters/circuit_to_gate.py
--- a/qiskit/converters/circuit_to_gate.py
+++ b/qiskit/converters/circuit_to_gate.py
@@ -81,9 +81,14 @@
     if gate.num_qubits > 0:
         q = QuantumRegister(gate.num_qubits, 'q')
 
-    definition = list(map(lambda x:
-                          (x[0], list(map(lambda y: q[find_bit_position(y)], x[1]))),
-                          definition))
+    # The 3rd parameter in the output tuple) is hard coded to [] because
+    # Gate objects do not have cregs set and we've verified that all
+    # instructions are gates
+    definition = list(map(
+        lambda x: (x[0],
+                   list(map(lambda y: q[find_bit_position(y)], x[1])),
+                   []),
+        definition))
     gate.definition = definition
 
     return gate

{"golden_diff": "diff --git a/qiskit/converters/circuit_to_gate.py b/qiskit/converters/circuit_to_gate.py\n--- a/qiskit/converters/circuit_to_gate.py\n+++ b/qiskit/converters/circuit_to_gate.py\n@@ -81,9 +81,14 @@\n     if gate.num_qubits > 0:\n         q = QuantumRegister(gate.num_qubits, 'q')\n \n-    definition = list(map(lambda x:\n-                          (x[0], list(map(lambda y: q[find_bit_position(y)], x[1]))),\n-                          definition))\n+    # The 3rd parameter in the output tuple) is hard coded to [] because\n+    # Gate objects do not have cregs set and we've verified that all\n+    # instructions are gates\n+    definition = list(map(\n+        lambda x: (x[0],\n+                   list(map(lambda y: q[find_bit_position(y)], x[1])),\n+                   []),\n+        definition))\n     gate.definition = definition\n \n     return gate\n", "issue": "Can't invert gate created from QuantumCircuit.to_gate\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- **Qiskit Terra version**:\r\n- **Python version**:\r\n- **Operating system**:\r\n\r\n### What is the current behavior?\r\nWhen inverting a gate created from QuantumCircuit.to_gate the following exception is raised:\r\n\r\n`ValueError: not enough values to unpack (expected 3, got 2)`\r\n\r\n\r\n### Steps to reproduce the problem\r\n```\r\nqc = QuantumCircuit(1)\r\nqc.x(0)\r\ngate = qc.to_gate()\r\ngate.inverse()\r\n```\r\n\r\n### What is the expected behavior?\r\n\r\n\r\n\r\n### Suggested solutions\r\n\r\n\r\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\n# This code is part of Qiskit.\n#\n# (C) Copyright IBM 2017, 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\n\"\"\"Helper function for converting a circuit to a gate\"\"\"\n\nfrom qiskit.circuit.gate import Gate\nfrom qiskit.circuit.quantumregister import QuantumRegister, Qubit\nfrom qiskit.exceptions import QiskitError\n\n\ndef circuit_to_gate(circuit, parameter_map=None):\n    \"\"\"Build a ``Gate`` object from a ``QuantumCircuit``.\n\n    The gate is anonymous (not tied to a named quantum register),\n    and so can be inserted into another circuit. The gate will\n    have the same string name as the circuit.\n\n    Args:\n        circuit (QuantumCircuit): the input circuit.\n        parameter_map (dict): For parameterized circuits, a mapping from\n           parameters in the circuit to parameters to be used in the gate.\n           If None, existing circuit parameters will also parameterize the\n           Gate.\n\n    Raises:\n        QiskitError: if circuit is non-unitary or if\n            parameter_map is not compatible with circuit\n\n    Return:\n        Gate: a Gate equivalent to the action of the\n            input circuit. Upon decomposition, this gate will\n            yield the components comprising the original circuit.\n    \"\"\"\n    for inst, _, _ in circuit.data:\n        if not isinstance(inst, Gate):\n            raise QiskitError('One or more instructions in this instruction '\n                              'cannot be converted to a gate')\n\n    if parameter_map is None:\n        parameter_dict = {p: p for p in circuit.parameters}\n    else:\n        parameter_dict = circuit._unroll_param_dict(parameter_map)\n\n    if parameter_dict.keys() != circuit.parameters:\n        raise QiskitError(('parameter_map should map all circuit parameters. '\n                           'Circuit parameters: {}, parameter_map: {}').format(\n                               circuit.parameters, parameter_dict))\n\n    gate = Gate(name=circuit.name,\n                num_qubits=sum([qreg.size for qreg in circuit.qregs]),\n                params=sorted(parameter_dict.values(), key=lambda p: p.name))\n    gate.condition = None\n\n    def find_bit_position(bit):\n        \"\"\"find the index of a given bit (Register, int) within\n        a flat ordered list of bits of the circuit\n        \"\"\"\n        if isinstance(bit, Qubit):\n            ordered_regs = circuit.qregs\n        else:\n            ordered_regs = circuit.cregs\n        reg_index = ordered_regs.index(bit.register)\n        return sum([reg.size for reg in ordered_regs[:reg_index]]) + bit.index\n\n    target = circuit.copy()\n    target._substitute_parameters(parameter_dict)\n\n    definition = target.data\n\n    if gate.num_qubits > 0:\n        q = QuantumRegister(gate.num_qubits, 'q')\n\n    definition = list(map(lambda x:\n                          (x[0], list(map(lambda y: q[find_bit_position(y)], x[1]))),\n                          definition))\n    gate.definition = definition\n\n    return gate\n", "path": "qiskit/converters/circuit_to_gate.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\n# This code is part of Qiskit.\n#\n# (C) Copyright IBM 2017, 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\n\"\"\"Helper function for converting a circuit to a gate\"\"\"\n\nfrom qiskit.circuit.gate import Gate\nfrom qiskit.circuit.quantumregister import QuantumRegister, Qubit\nfrom qiskit.exceptions import QiskitError\n\n\ndef circuit_to_gate(circuit, parameter_map=None):\n    \"\"\"Build a ``Gate`` object from a ``QuantumCircuit``.\n\n    The gate is anonymous (not tied to a named quantum register),\n    and so can be inserted into another circuit. The gate will\n    have the same string name as the circuit.\n\n    Args:\n        circuit (QuantumCircuit): the input circuit.\n        parameter_map (dict): For parameterized circuits, a mapping from\n           parameters in the circuit to parameters to be used in the gate.\n           If None, existing circuit parameters will also parameterize the\n           Gate.\n\n    Raises:\n        QiskitError: if circuit is non-unitary or if\n            parameter_map is not compatible with circuit\n\n    Return:\n        Gate: a Gate equivalent to the action of the\n            input circuit. Upon decomposition, this gate will\n            yield the components comprising the original circuit.\n    \"\"\"\n    for inst, _, _ in circuit.data:\n        if not isinstance(inst, Gate):\n            raise QiskitError('One or more instructions in this instruction '\n                              'cannot be converted to a gate')\n\n    if parameter_map is None:\n        parameter_dict = {p: p for p in circuit.parameters}\n    else:\n        parameter_dict = circuit._unroll_param_dict(parameter_map)\n\n    if parameter_dict.keys() != circuit.parameters:\n        raise QiskitError(('parameter_map should map all circuit parameters. '\n                           'Circuit parameters: {}, parameter_map: {}').format(\n                               circuit.parameters, parameter_dict))\n\n    gate = Gate(name=circuit.name,\n                num_qubits=sum([qreg.size for qreg in circuit.qregs]),\n                params=sorted(parameter_dict.values(), key=lambda p: p.name))\n    gate.condition = None\n\n    def find_bit_position(bit):\n        \"\"\"find the index of a given bit (Register, int) within\n        a flat ordered list of bits of the circuit\n        \"\"\"\n        if isinstance(bit, Qubit):\n            ordered_regs = circuit.qregs\n        else:\n            ordered_regs = circuit.cregs\n        reg_index = ordered_regs.index(bit.register)\n        return sum([reg.size for reg in ordered_regs[:reg_index]]) + bit.index\n\n    target = circuit.copy()\n    target._substitute_parameters(parameter_dict)\n\n    definition = target.data\n\n    if gate.num_qubits > 0:\n        q = QuantumRegister(gate.num_qubits, 'q')\n\n    # The 3rd parameter in the output tuple) is hard coded to [] because\n    # Gate objects do not have cregs set and we've verified that all\n    # instructions are gates\n    definition = list(map(\n        lambda x: (x[0],\n                   list(map(lambda y: q[find_bit_position(y)], x[1])),\n                   []),\n        definition))\n    gate.definition = definition\n\n    return gate\n", "path": "qiskit/converters/circuit_to_gate.py"}]}