We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
load_qasm_{file, string} converts `t` gate inappropriately
### Informations

- **QISKit (Python SDK) version**: 0.5.3
- **Python version**: 3.6.5
- **Operating system**: macOS 10.13.5

### What is the current behavior?

`load_qasm_file` and `load_qasm_string` converts `t` gate into `tdg`.


### Steps to reproduce the problem

Run the following script.
```
from qiskit.wrapper import load_qasm_string

qasm_string="""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
creg c[1];
t q[0];
"""
circuit = load_qasm_string(qasm_string, name='tmp', basis_gates='t')
print(circuit.qasm())
```

The result is as follows.
```
OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
creg c[1];
tdg q[0];
```

### What is the expected behavior?

Do not change `t` gate.

### Suggested solutions

I have no idea.


--- 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/unroll/_circuitbackend.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 
3 # Copyright 2017, IBM.
4 #
5 # This source code is licensed under the Apache License, Version 2.0 found in
6 # the LICENSE.txt file in the root directory of this source tree.
7 
8 """
9 Backend for the unroller that produces a QuantumCircuit.
10 """
11 
12 from qiskit import QuantumCircuit
13 from qiskit import ClassicalRegister
14 from qiskit import QuantumRegister
15 from ._backenderror import BackendError
16 from ._unrollerbackend import UnrollerBackend
17 
18 
19 class CircuitBackend(UnrollerBackend):
20     """Backend for the unroller that produces a QuantumCircuit.
21 
22     By default, basis gates are the QX gates.
23     """
24 
25     def __init__(self, basis=None):
26         """Setup this backend.
27 
28         basis is a list of operation name strings.
29         """
30         super().__init__(basis)
31         self.creg = None
32         self.cval = None
33         if basis:
34             self.basis = basis
35         else:
36             self.basis = ["cx", "u1", "u2", "u3"]
37         self.gates = {}
38         self.listen = True
39         self.in_gate = ""
40         self.circuit = QuantumCircuit()
41 
42     def set_basis(self, basis):
43         """Declare the set of user-defined gates to emit.
44 
45         basis is a list of operation name strings.
46         """
47         self.basis = basis
48 
49     def version(self, version):
50         """Ignore the version string.
51 
52         v is a version number.
53         """
54         pass
55 
56     def new_qreg(self, name, size):
57         """Create a new quantum register.
58 
59         name = name of the register
60         sz = size of the register
61         """
62         assert size >= 0, "invalid qreg size"
63         q_register = QuantumRegister(size, name)
64         self.circuit.add(q_register)
65 
66     def new_creg(self, name, size):
67         """Create a new classical register.
68 
69         name = name of the register
70         sz = size of the register
71         """
72         assert size >= 0, "invalid creg size"
73         c_register = ClassicalRegister(size, name)
74         self.circuit.add(c_register)
75 
76     def define_gate(self, name, gatedata):
77         """Define a new quantum gate.
78 
79         We don't check that the definition and name agree.
80 
81         name is a string.
82         gatedata is the AST node for the gate.
83         """
84         self.gates[name] = gatedata
85 
86     def _map_qubit(self, qubit):
87         """Map qubit tuple (regname, index) to (QuantumRegister, index)."""
88         qregs = self.circuit.get_qregs()
89         if qubit[0] not in qregs:
90             raise BackendError("qreg %s does not exist" % qubit[0])
91         return (qregs[qubit[0]], qubit[1])
92 
93     def _map_bit(self, bit):
94         """Map bit tuple (regname, index) to (ClassicalRegister, index)."""
95         cregs = self.circuit.get_cregs()
96         if bit[0] not in cregs:
97             raise BackendError("creg %s does not exist" % bit[0])
98         return (cregs[bit[0]], bit[1])
99 
100     def _map_creg(self, creg):
101         """Map creg name to ClassicalRegister."""
102         cregs = self.circuit.get_cregs()
103         if creg not in cregs:
104             raise BackendError("creg %s does not exist" % creg)
105         return cregs[creg]
106 
107     def u(self, arg, qubit, nested_scope=None):
108         """Fundamental single qubit gate.
109 
110         arg is 3-tuple of Node expression objects.
111         qubit is (regname,idx) tuple.
112         nested_scope is a list of dictionaries mapping expression variables
113         to Node expression objects in order of increasing nesting depth.
114         """
115         if self.listen:
116             if "U" not in self.basis:
117                 self.basis.append("U")
118 
119             (theta, phi, lam) = list(map(lambda x: x.sym(nested_scope), arg))
120             this_gate = self.circuit.u_base(theta, phi, lam,
121                                             self._map_qubit(qubit))
122             if self.creg is not None:
123                 this_gate.c_if(self._map_creg(self.creg), self.cval)
124 
125     def cx(self, qubit0, qubit1):
126         """Fundamental two qubit gate.
127 
128         qubit0 is (regname,idx) tuple for the control qubit.
129         qubit1 is (regname,idx) tuple for the target qubit.
130         """
131         if self.listen:
132             if "CX" not in self.basis:
133                 self.basis.append("CX")
134             this_gate = self.circuit.cx_base(self._map_qubit(qubit0),
135                                              self._map_qubit(qubit1))
136             if self.creg is not None:
137                 this_gate.c_if(self._map_creg(self.creg), self.cval)
138 
139     def measure(self, qubit, bit):
140         """Measurement operation.
141 
142         qubit is (regname, idx) tuple for the input qubit.
143         bit is (regname, idx) tuple for the output bit.
144         """
145         if "measure" not in self.basis:
146             self.basis.append("measure")
147         this_op = self.circuit.measure(self._map_qubit(qubit),
148                                        self._map_bit(bit))
149         if self.creg is not None:
150             this_op.c_if(self._map_creg(self.creg), self.cval)
151 
152     def barrier(self, qubitlists):
153         """Barrier instruction.
154 
155         qubitlists is a list of lists of (regname, idx) tuples.
156         """
157         if self.listen:
158             if "barrier" not in self.basis:
159                 self.basis.append("barrier")
160             flatlist = map(self._map_qubit,
161                            [qubit for qubitlist in qubitlists
162                             for qubit in qubitlist])
163             self.circuit.barrier(*list(flatlist))
164 
165     def reset(self, qubit):
166         """Reset instruction.
167 
168         qubit is a (regname, idx) tuple.
169         """
170         if "reset" not in self.basis:
171             self.basis.append("reset")
172         this_op = self.circuit.reset(self._map_qubit(qubit))
173         if self.creg is not None:
174             this_op.c_if(self._map_creg(self.creg), self.cval)
175 
176     def set_condition(self, creg, cval):
177         """Attach a current condition.
178 
179         creg is a name string.
180         cval is the integer value for the test.
181         """
182         self.creg = creg
183         self.cval = cval
184 
185     def drop_condition(self):
186         """Drop the current condition."""
187         self.creg = None
188         self.cval = None
189 
190     def start_gate(self, name, args, qubits, nested_scope=None):
191         """Begin a custom gate.
192 
193         name is name string.
194         args is list of Node expression objects.
195         qubits is list of (regname, idx) tuples.
196         nested_scope is a list of dictionaries mapping expression variables
197         to Node expression objects in order of increasing nesting depth.
198         """
199         if self.listen and name not in self.basis \
200            and self.gates[name]["opaque"]:
201             raise BackendError("opaque gate %s not in basis" % name)
202         if self.listen and name in self.basis:
203             self.in_gate = name
204             self.listen = False
205             # Gate names mapped to number of arguments and qubits
206             # and method to invoke on [args, qubits]
207             lut = {"ccx": [(0, 3),
208                            lambda x: self.circuit.ccx(x[1][0], x[1][1],
209                                                       x[1][2])],
210                    "ch": [(0, 2),
211                           lambda x: self.circuit.ch(x[1][0], x[1][1])],
212                    "crz": [(1, 2),
213                            lambda x: self.circuit.crz(x[0][0], x[1][0],
214                                                       x[1][1])],
215                    "cswap": [(0, 3),
216                              lambda x: self.circuit.cswap(x[1][0],
217                                                           x[1][1],
218                                                           x[1][2])],
219                    "cu1": [(1, 2),
220                            lambda x: self.circuit.cu1(x[0][0], x[1][0],
221                                                       x[1][1])],
222                    "cu3": [(3, 2), lambda x: self.circuit.cu3(x[0][0],
223                                                               x[0][1],
224                                                               x[0][2],
225                                                               x[1][0],
226                                                               x[1][1])],
227                    "cx": [(0, 2), lambda x: self.circuit.cx(x[1][0], x[1][1])],
228                    "cy": [(0, 2), lambda x: self.circuit.cy(x[1][0], x[1][1])],
229                    "cz": [(0, 2), lambda x: self.circuit.cz(x[1][0], x[1][1])],
230                    "swap": [(0, 2), lambda x: self.circuit.swap(x[1][0], x[1][1])],
231                    "h": [(0, 1), lambda x: self.circuit.h(x[1][0])],
232                    "id": [(0, 1), lambda x: self.circuit.iden(x[1][0])],
233                    "rx": [(1, 1), lambda x: self.circuit.rx(x[0][0], x[1][0])],
234                    "ry": [(1, 1), lambda x: self.circuit.ry(x[0][0], x[1][0])],
235                    "rz": [(1, 1), lambda x: self.circuit.rz(x[0][0], x[1][0])],
236                    "s": [(0, 1), lambda x: self.circuit.s(x[1][0])],
237                    "sdg": [(0, 1), lambda x: self.circuit.s(x[1][0]).inverse()],
238                    "t": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],
239                    "tdg": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],
240                    "u1": [(1, 1), lambda x: self.circuit.u1(x[0][0], x[1][0])],
241                    "u2": [(2, 1), lambda x: self.circuit.u2(x[0][0], x[0][1],
242                                                             x[1][0])],
243                    "u3": [(3, 1), lambda x: self.circuit.u3(x[0][0], x[0][1],
244                                                             x[0][2], x[1][0])],
245                    "x": [(0, 1), lambda x: self.circuit.x(x[1][0])],
246                    "y": [(0, 1), lambda x: self.circuit.y(x[1][0])],
247                    "z": [(0, 1), lambda x: self.circuit.z(x[1][0])]}
248             if name not in lut:
249                 raise BackendError("gate %s not in standard extensions" %
250                                    name)
251             gate_data = lut[name]
252             if gate_data[0] != (len(args), len(qubits)):
253                 raise BackendError("gate %s signature (%d, %d) is " %
254                                    (name, len(args), len(qubits)) +
255                                    "incompatible with the standard " +
256                                    "extensions")
257             this_gate = gate_data[1]([list(map(lambda x:
258                                                x.sym(nested_scope), args)),
259                                       list(map(self._map_qubit, qubits))])
260             if self.creg is not None:
261                 this_gate.c_if(self._map_creg(self.creg), self.cval)
262 
263     def end_gate(self, name, args, qubits, nested_scope=None):
264         """End a custom gate.
265 
266         name is name string.
267         args is list of Node expression objects.
268         qubits is list of (regname, idx) tuples.
269         nested_scope is a list of dictionaries mapping expression variables
270         to Node expression objects in order of increasing nesting depth.
271         """
272         if name == self.in_gate:
273             self.in_gate = ""
274             self.listen = True
275 
276     def get_output(self):
277         """Return the QuantumCircuit object."""
278         return self.circuit
279
```
--- 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/unroll/_circuitbackend.py b/qiskit/unroll/_circuitbackend.py
--- a/qiskit/unroll/_circuitbackend.py
+++ b/qiskit/unroll/_circuitbackend.py
@@ -235,7 +235,7 @@
                    "rz": [(1, 1), lambda x: self.circuit.rz(x[0][0], x[1][0])],
                    "s": [(0, 1), lambda x: self.circuit.s(x[1][0])],
                    "sdg": [(0, 1), lambda x: self.circuit.s(x[1][0]).inverse()],
-                   "t": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],
+                   "t": [(0, 1), lambda x: self.circuit.t(x[1][0])],
                    "tdg": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],
                    "u1": [(1, 1), lambda x: self.circuit.u1(x[0][0], x[1][0])],
                    "u2": [(2, 1), lambda x: self.circuit.u2(x[0][0], x[0][1],

{"golden_diff": "diff --git a/qiskit/unroll/_circuitbackend.py b/qiskit/unroll/_circuitbackend.py\n--- a/qiskit/unroll/_circuitbackend.py\n+++ b/qiskit/unroll/_circuitbackend.py\n@@ -235,7 +235,7 @@\n                    \"rz\": [(1, 1), lambda x: self.circuit.rz(x[0][0], x[1][0])],\n                    \"s\": [(0, 1), lambda x: self.circuit.s(x[1][0])],\n                    \"sdg\": [(0, 1), lambda x: self.circuit.s(x[1][0]).inverse()],\n-                   \"t\": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],\n+                   \"t\": [(0, 1), lambda x: self.circuit.t(x[1][0])],\n                    \"tdg\": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],\n                    \"u1\": [(1, 1), lambda x: self.circuit.u1(x[0][0], x[1][0])],\n                    \"u2\": [(2, 1), lambda x: self.circuit.u2(x[0][0], x[0][1],\n", "issue": "load_qasm_{file, string} converts `t` gate inappropriately\n### Informations\r\n\r\n- **QISKit (Python SDK) version**: 0.5.3\r\n- **Python version**: 3.6.5\r\n- **Operating system**: macOS 10.13.5\r\n\r\n### What is the current behavior?\r\n\r\n`load_qasm_file` and `load_qasm_string` converts `t` gate into `tdg`.\r\n\r\n\r\n### Steps to reproduce the problem\r\n\r\nRun the following script.\r\n```\r\nfrom qiskit.wrapper import load_qasm_string\r\n\r\nqasm_string=\"\"\"\r\nOPENQASM 2.0;\r\ninclude \"qelib1.inc\";\r\nqreg q[1];\r\ncreg c[1];\r\nt q[0];\r\n\"\"\"\r\ncircuit = load_qasm_string(qasm_string, name='tmp', basis_gates='t')\r\nprint(circuit.qasm())\r\n```\r\n\r\nThe result is as follows.\r\n```\r\nOPENQASM 2.0;\r\ninclude \"qelib1.inc\";\r\nqreg q[1];\r\ncreg c[1];\r\ntdg q[0];\r\n```\r\n\r\n### What is the expected behavior?\r\n\r\nDo not change `t` gate.\r\n\r\n### Suggested solutions\r\n\r\nI have no idea.\r\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\n# Copyright 2017, IBM.\n#\n# This source code is licensed under the Apache License, Version 2.0 found in\n# the LICENSE.txt file in the root directory of this source tree.\n\n\"\"\"\nBackend for the unroller that produces a QuantumCircuit.\n\"\"\"\n\nfrom qiskit import QuantumCircuit\nfrom qiskit import ClassicalRegister\nfrom qiskit import QuantumRegister\nfrom ._backenderror import BackendError\nfrom ._unrollerbackend import UnrollerBackend\n\n\nclass CircuitBackend(UnrollerBackend):\n    \"\"\"Backend for the unroller that produces a QuantumCircuit.\n\n    By default, basis gates are the QX gates.\n    \"\"\"\n\n    def __init__(self, basis=None):\n        \"\"\"Setup this backend.\n\n        basis is a list of operation name strings.\n        \"\"\"\n        super().__init__(basis)\n        self.creg = None\n        self.cval = None\n        if basis:\n            self.basis = basis\n        else:\n            self.basis = [\"cx\", \"u1\", \"u2\", \"u3\"]\n        self.gates = {}\n        self.listen = True\n        self.in_gate = \"\"\n        self.circuit = QuantumCircuit()\n\n    def set_basis(self, basis):\n        \"\"\"Declare the set of user-defined gates to emit.\n\n        basis is a list of operation name strings.\n        \"\"\"\n        self.basis = basis\n\n    def version(self, version):\n        \"\"\"Ignore the version string.\n\n        v is a version number.\n        \"\"\"\n        pass\n\n    def new_qreg(self, name, size):\n        \"\"\"Create a new quantum register.\n\n        name = name of the register\n        sz = size of the register\n        \"\"\"\n        assert size >= 0, \"invalid qreg size\"\n        q_register = QuantumRegister(size, name)\n        self.circuit.add(q_register)\n\n    def new_creg(self, name, size):\n        \"\"\"Create a new classical register.\n\n        name = name of the register\n        sz = size of the register\n        \"\"\"\n        assert size >= 0, \"invalid creg size\"\n        c_register = ClassicalRegister(size, name)\n        self.circuit.add(c_register)\n\n    def define_gate(self, name, gatedata):\n        \"\"\"Define a new quantum gate.\n\n        We don't check that the definition and name agree.\n\n        name is a string.\n        gatedata is the AST node for the gate.\n        \"\"\"\n        self.gates[name] = gatedata\n\n    def _map_qubit(self, qubit):\n        \"\"\"Map qubit tuple (regname, index) to (QuantumRegister, index).\"\"\"\n        qregs = self.circuit.get_qregs()\n        if qubit[0] not in qregs:\n            raise BackendError(\"qreg %s does not exist\" % qubit[0])\n        return (qregs[qubit[0]], qubit[1])\n\n    def _map_bit(self, bit):\n        \"\"\"Map bit tuple (regname, index) to (ClassicalRegister, index).\"\"\"\n        cregs = self.circuit.get_cregs()\n        if bit[0] not in cregs:\n            raise BackendError(\"creg %s does not exist\" % bit[0])\n        return (cregs[bit[0]], bit[1])\n\n    def _map_creg(self, creg):\n        \"\"\"Map creg name to ClassicalRegister.\"\"\"\n        cregs = self.circuit.get_cregs()\n        if creg not in cregs:\n            raise BackendError(\"creg %s does not exist\" % creg)\n        return cregs[creg]\n\n    def u(self, arg, qubit, nested_scope=None):\n        \"\"\"Fundamental single qubit gate.\n\n        arg is 3-tuple of Node expression objects.\n        qubit is (regname,idx) tuple.\n        nested_scope is a list of dictionaries mapping expression variables\n        to Node expression objects in order of increasing nesting depth.\n        \"\"\"\n        if self.listen:\n            if \"U\" not in self.basis:\n                self.basis.append(\"U\")\n\n            (theta, phi, lam) = list(map(lambda x: x.sym(nested_scope), arg))\n            this_gate = self.circuit.u_base(theta, phi, lam,\n                                            self._map_qubit(qubit))\n            if self.creg is not None:\n                this_gate.c_if(self._map_creg(self.creg), self.cval)\n\n    def cx(self, qubit0, qubit1):\n        \"\"\"Fundamental two qubit gate.\n\n        qubit0 is (regname,idx) tuple for the control qubit.\n        qubit1 is (regname,idx) tuple for the target qubit.\n        \"\"\"\n        if self.listen:\n            if \"CX\" not in self.basis:\n                self.basis.append(\"CX\")\n            this_gate = self.circuit.cx_base(self._map_qubit(qubit0),\n                                             self._map_qubit(qubit1))\n            if self.creg is not None:\n                this_gate.c_if(self._map_creg(self.creg), self.cval)\n\n    def measure(self, qubit, bit):\n        \"\"\"Measurement operation.\n\n        qubit is (regname, idx) tuple for the input qubit.\n        bit is (regname, idx) tuple for the output bit.\n        \"\"\"\n        if \"measure\" not in self.basis:\n            self.basis.append(\"measure\")\n        this_op = self.circuit.measure(self._map_qubit(qubit),\n                                       self._map_bit(bit))\n        if self.creg is not None:\n            this_op.c_if(self._map_creg(self.creg), self.cval)\n\n    def barrier(self, qubitlists):\n        \"\"\"Barrier instruction.\n\n        qubitlists is a list of lists of (regname, idx) tuples.\n        \"\"\"\n        if self.listen:\n            if \"barrier\" not in self.basis:\n                self.basis.append(\"barrier\")\n            flatlist = map(self._map_qubit,\n                           [qubit for qubitlist in qubitlists\n                            for qubit in qubitlist])\n            self.circuit.barrier(*list(flatlist))\n\n    def reset(self, qubit):\n        \"\"\"Reset instruction.\n\n        qubit is a (regname, idx) tuple.\n        \"\"\"\n        if \"reset\" not in self.basis:\n            self.basis.append(\"reset\")\n        this_op = self.circuit.reset(self._map_qubit(qubit))\n        if self.creg is not None:\n            this_op.c_if(self._map_creg(self.creg), self.cval)\n\n    def set_condition(self, creg, cval):\n        \"\"\"Attach a current condition.\n\n        creg is a name string.\n        cval is the integer value for the test.\n        \"\"\"\n        self.creg = creg\n        self.cval = cval\n\n    def drop_condition(self):\n        \"\"\"Drop the current condition.\"\"\"\n        self.creg = None\n        self.cval = None\n\n    def start_gate(self, name, args, qubits, nested_scope=None):\n        \"\"\"Begin a custom gate.\n\n        name is name string.\n        args is list of Node expression objects.\n        qubits is list of (regname, idx) tuples.\n        nested_scope is a list of dictionaries mapping expression variables\n        to Node expression objects in order of increasing nesting depth.\n        \"\"\"\n        if self.listen and name not in self.basis \\\n           and self.gates[name][\"opaque\"]:\n            raise BackendError(\"opaque gate %s not in basis\" % name)\n        if self.listen and name in self.basis:\n            self.in_gate = name\n            self.listen = False\n            # Gate names mapped to number of arguments and qubits\n            # and method to invoke on [args, qubits]\n            lut = {\"ccx\": [(0, 3),\n                           lambda x: self.circuit.ccx(x[1][0], x[1][1],\n                                                      x[1][2])],\n                   \"ch\": [(0, 2),\n                          lambda x: self.circuit.ch(x[1][0], x[1][1])],\n                   \"crz\": [(1, 2),\n                           lambda x: self.circuit.crz(x[0][0], x[1][0],\n                                                      x[1][1])],\n                   \"cswap\": [(0, 3),\n                             lambda x: self.circuit.cswap(x[1][0],\n                                                          x[1][1],\n                                                          x[1][2])],\n                   \"cu1\": [(1, 2),\n                           lambda x: self.circuit.cu1(x[0][0], x[1][0],\n                                                      x[1][1])],\n                   \"cu3\": [(3, 2), lambda x: self.circuit.cu3(x[0][0],\n                                                              x[0][1],\n                                                              x[0][2],\n                                                              x[1][0],\n                                                              x[1][1])],\n                   \"cx\": [(0, 2), lambda x: self.circuit.cx(x[1][0], x[1][1])],\n                   \"cy\": [(0, 2), lambda x: self.circuit.cy(x[1][0], x[1][1])],\n                   \"cz\": [(0, 2), lambda x: self.circuit.cz(x[1][0], x[1][1])],\n                   \"swap\": [(0, 2), lambda x: self.circuit.swap(x[1][0], x[1][1])],\n                   \"h\": [(0, 1), lambda x: self.circuit.h(x[1][0])],\n                   \"id\": [(0, 1), lambda x: self.circuit.iden(x[1][0])],\n                   \"rx\": [(1, 1), lambda x: self.circuit.rx(x[0][0], x[1][0])],\n                   \"ry\": [(1, 1), lambda x: self.circuit.ry(x[0][0], x[1][0])],\n                   \"rz\": [(1, 1), lambda x: self.circuit.rz(x[0][0], x[1][0])],\n                   \"s\": [(0, 1), lambda x: self.circuit.s(x[1][0])],\n                   \"sdg\": [(0, 1), lambda x: self.circuit.s(x[1][0]).inverse()],\n                   \"t\": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],\n                   \"tdg\": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],\n                   \"u1\": [(1, 1), lambda x: self.circuit.u1(x[0][0], x[1][0])],\n                   \"u2\": [(2, 1), lambda x: self.circuit.u2(x[0][0], x[0][1],\n                                                            x[1][0])],\n                   \"u3\": [(3, 1), lambda x: self.circuit.u3(x[0][0], x[0][1],\n                                                            x[0][2], x[1][0])],\n                   \"x\": [(0, 1), lambda x: self.circuit.x(x[1][0])],\n                   \"y\": [(0, 1), lambda x: self.circuit.y(x[1][0])],\n                   \"z\": [(0, 1), lambda x: self.circuit.z(x[1][0])]}\n            if name not in lut:\n                raise BackendError(\"gate %s not in standard extensions\" %\n                                   name)\n            gate_data = lut[name]\n            if gate_data[0] != (len(args), len(qubits)):\n                raise BackendError(\"gate %s signature (%d, %d) is \" %\n                                   (name, len(args), len(qubits)) +\n                                   \"incompatible with the standard \" +\n                                   \"extensions\")\n            this_gate = gate_data[1]([list(map(lambda x:\n                                               x.sym(nested_scope), args)),\n                                      list(map(self._map_qubit, qubits))])\n            if self.creg is not None:\n                this_gate.c_if(self._map_creg(self.creg), self.cval)\n\n    def end_gate(self, name, args, qubits, nested_scope=None):\n        \"\"\"End a custom gate.\n\n        name is name string.\n        args is list of Node expression objects.\n        qubits is list of (regname, idx) tuples.\n        nested_scope is a list of dictionaries mapping expression variables\n        to Node expression objects in order of increasing nesting depth.\n        \"\"\"\n        if name == self.in_gate:\n            self.in_gate = \"\"\n            self.listen = True\n\n    def get_output(self):\n        \"\"\"Return the QuantumCircuit object.\"\"\"\n        return self.circuit\n", "path": "qiskit/unroll/_circuitbackend.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\n# Copyright 2017, IBM.\n#\n# This source code is licensed under the Apache License, Version 2.0 found in\n# the LICENSE.txt file in the root directory of this source tree.\n\n\"\"\"\nBackend for the unroller that produces a QuantumCircuit.\n\"\"\"\n\nfrom qiskit import QuantumCircuit\nfrom qiskit import ClassicalRegister\nfrom qiskit import QuantumRegister\nfrom ._backenderror import BackendError\nfrom ._unrollerbackend import UnrollerBackend\n\n\nclass CircuitBackend(UnrollerBackend):\n    \"\"\"Backend for the unroller that produces a QuantumCircuit.\n\n    By default, basis gates are the QX gates.\n    \"\"\"\n\n    def __init__(self, basis=None):\n        \"\"\"Setup this backend.\n\n        basis is a list of operation name strings.\n        \"\"\"\n        super().__init__(basis)\n        self.creg = None\n        self.cval = None\n        if basis:\n            self.basis = basis\n        else:\n            self.basis = [\"cx\", \"u1\", \"u2\", \"u3\"]\n        self.gates = {}\n        self.listen = True\n        self.in_gate = \"\"\n        self.circuit = QuantumCircuit()\n\n    def set_basis(self, basis):\n        \"\"\"Declare the set of user-defined gates to emit.\n\n        basis is a list of operation name strings.\n        \"\"\"\n        self.basis = basis\n\n    def version(self, version):\n        \"\"\"Ignore the version string.\n\n        v is a version number.\n        \"\"\"\n        pass\n\n    def new_qreg(self, name, size):\n        \"\"\"Create a new quantum register.\n\n        name = name of the register\n        sz = size of the register\n        \"\"\"\n        assert size >= 0, \"invalid qreg size\"\n        q_register = QuantumRegister(size, name)\n        self.circuit.add(q_register)\n\n    def new_creg(self, name, size):\n        \"\"\"Create a new classical register.\n\n        name = name of the register\n        sz = size of the register\n        \"\"\"\n        assert size >= 0, \"invalid creg size\"\n        c_register = ClassicalRegister(size, name)\n        self.circuit.add(c_register)\n\n    def define_gate(self, name, gatedata):\n        \"\"\"Define a new quantum gate.\n\n        We don't check that the definition and name agree.\n\n        name is a string.\n        gatedata is the AST node for the gate.\n        \"\"\"\n        self.gates[name] = gatedata\n\n    def _map_qubit(self, qubit):\n        \"\"\"Map qubit tuple (regname, index) to (QuantumRegister, index).\"\"\"\n        qregs = self.circuit.get_qregs()\n        if qubit[0] not in qregs:\n            raise BackendError(\"qreg %s does not exist\" % qubit[0])\n        return (qregs[qubit[0]], qubit[1])\n\n    def _map_bit(self, bit):\n        \"\"\"Map bit tuple (regname, index) to (ClassicalRegister, index).\"\"\"\n        cregs = self.circuit.get_cregs()\n        if bit[0] not in cregs:\n            raise BackendError(\"creg %s does not exist\" % bit[0])\n        return (cregs[bit[0]], bit[1])\n\n    def _map_creg(self, creg):\n        \"\"\"Map creg name to ClassicalRegister.\"\"\"\n        cregs = self.circuit.get_cregs()\n        if creg not in cregs:\n            raise BackendError(\"creg %s does not exist\" % creg)\n        return cregs[creg]\n\n    def u(self, arg, qubit, nested_scope=None):\n        \"\"\"Fundamental single qubit gate.\n\n        arg is 3-tuple of Node expression objects.\n        qubit is (regname,idx) tuple.\n        nested_scope is a list of dictionaries mapping expression variables\n        to Node expression objects in order of increasing nesting depth.\n        \"\"\"\n        if self.listen:\n            if \"U\" not in self.basis:\n                self.basis.append(\"U\")\n\n            (theta, phi, lam) = list(map(lambda x: x.sym(nested_scope), arg))\n            this_gate = self.circuit.u_base(theta, phi, lam,\n                                            self._map_qubit(qubit))\n            if self.creg is not None:\n                this_gate.c_if(self._map_creg(self.creg), self.cval)\n\n    def cx(self, qubit0, qubit1):\n        \"\"\"Fundamental two qubit gate.\n\n        qubit0 is (regname,idx) tuple for the control qubit.\n        qubit1 is (regname,idx) tuple for the target qubit.\n        \"\"\"\n        if self.listen:\n            if \"CX\" not in self.basis:\n                self.basis.append(\"CX\")\n            this_gate = self.circuit.cx_base(self._map_qubit(qubit0),\n                                             self._map_qubit(qubit1))\n            if self.creg is not None:\n                this_gate.c_if(self._map_creg(self.creg), self.cval)\n\n    def measure(self, qubit, bit):\n        \"\"\"Measurement operation.\n\n        qubit is (regname, idx) tuple for the input qubit.\n        bit is (regname, idx) tuple for the output bit.\n        \"\"\"\n        if \"measure\" not in self.basis:\n            self.basis.append(\"measure\")\n        this_op = self.circuit.measure(self._map_qubit(qubit),\n                                       self._map_bit(bit))\n        if self.creg is not None:\n            this_op.c_if(self._map_creg(self.creg), self.cval)\n\n    def barrier(self, qubitlists):\n        \"\"\"Barrier instruction.\n\n        qubitlists is a list of lists of (regname, idx) tuples.\n        \"\"\"\n        if self.listen:\n            if \"barrier\" not in self.basis:\n                self.basis.append(\"barrier\")\n            flatlist = map(self._map_qubit,\n                           [qubit for qubitlist in qubitlists\n                            for qubit in qubitlist])\n            self.circuit.barrier(*list(flatlist))\n\n    def reset(self, qubit):\n        \"\"\"Reset instruction.\n\n        qubit is a (regname, idx) tuple.\n        \"\"\"\n        if \"reset\" not in self.basis:\n            self.basis.append(\"reset\")\n        this_op = self.circuit.reset(self._map_qubit(qubit))\n        if self.creg is not None:\n            this_op.c_if(self._map_creg(self.creg), self.cval)\n\n    def set_condition(self, creg, cval):\n        \"\"\"Attach a current condition.\n\n        creg is a name string.\n        cval is the integer value for the test.\n        \"\"\"\n        self.creg = creg\n        self.cval = cval\n\n    def drop_condition(self):\n        \"\"\"Drop the current condition.\"\"\"\n        self.creg = None\n        self.cval = None\n\n    def start_gate(self, name, args, qubits, nested_scope=None):\n        \"\"\"Begin a custom gate.\n\n        name is name string.\n        args is list of Node expression objects.\n        qubits is list of (regname, idx) tuples.\n        nested_scope is a list of dictionaries mapping expression variables\n        to Node expression objects in order of increasing nesting depth.\n        \"\"\"\n        if self.listen and name not in self.basis \\\n           and self.gates[name][\"opaque\"]:\n            raise BackendError(\"opaque gate %s not in basis\" % name)\n        if self.listen and name in self.basis:\n            self.in_gate = name\n            self.listen = False\n            # Gate names mapped to number of arguments and qubits\n            # and method to invoke on [args, qubits]\n            lut = {\"ccx\": [(0, 3),\n                           lambda x: self.circuit.ccx(x[1][0], x[1][1],\n                                                      x[1][2])],\n                   \"ch\": [(0, 2),\n                          lambda x: self.circuit.ch(x[1][0], x[1][1])],\n                   \"crz\": [(1, 2),\n                           lambda x: self.circuit.crz(x[0][0], x[1][0],\n                                                      x[1][1])],\n                   \"cswap\": [(0, 3),\n                             lambda x: self.circuit.cswap(x[1][0],\n                                                          x[1][1],\n                                                          x[1][2])],\n                   \"cu1\": [(1, 2),\n                           lambda x: self.circuit.cu1(x[0][0], x[1][0],\n                                                      x[1][1])],\n                   \"cu3\": [(3, 2), lambda x: self.circuit.cu3(x[0][0],\n                                                              x[0][1],\n                                                              x[0][2],\n                                                              x[1][0],\n                                                              x[1][1])],\n                   \"cx\": [(0, 2), lambda x: self.circuit.cx(x[1][0], x[1][1])],\n                   \"cy\": [(0, 2), lambda x: self.circuit.cy(x[1][0], x[1][1])],\n                   \"cz\": [(0, 2), lambda x: self.circuit.cz(x[1][0], x[1][1])],\n                   \"swap\": [(0, 2), lambda x: self.circuit.swap(x[1][0], x[1][1])],\n                   \"h\": [(0, 1), lambda x: self.circuit.h(x[1][0])],\n                   \"id\": [(0, 1), lambda x: self.circuit.iden(x[1][0])],\n                   \"rx\": [(1, 1), lambda x: self.circuit.rx(x[0][0], x[1][0])],\n                   \"ry\": [(1, 1), lambda x: self.circuit.ry(x[0][0], x[1][0])],\n                   \"rz\": [(1, 1), lambda x: self.circuit.rz(x[0][0], x[1][0])],\n                   \"s\": [(0, 1), lambda x: self.circuit.s(x[1][0])],\n                   \"sdg\": [(0, 1), lambda x: self.circuit.s(x[1][0]).inverse()],\n                   \"t\": [(0, 1), lambda x: self.circuit.t(x[1][0])],\n                   \"tdg\": [(0, 1), lambda x: self.circuit.t(x[1][0]).inverse()],\n                   \"u1\": [(1, 1), lambda x: self.circuit.u1(x[0][0], x[1][0])],\n                   \"u2\": [(2, 1), lambda x: self.circuit.u2(x[0][0], x[0][1],\n                                                            x[1][0])],\n                   \"u3\": [(3, 1), lambda x: self.circuit.u3(x[0][0], x[0][1],\n                                                            x[0][2], x[1][0])],\n                   \"x\": [(0, 1), lambda x: self.circuit.x(x[1][0])],\n                   \"y\": [(0, 1), lambda x: self.circuit.y(x[1][0])],\n                   \"z\": [(0, 1), lambda x: self.circuit.z(x[1][0])]}\n            if name not in lut:\n                raise BackendError(\"gate %s not in standard extensions\" %\n                                   name)\n            gate_data = lut[name]\n            if gate_data[0] != (len(args), len(qubits)):\n                raise BackendError(\"gate %s signature (%d, %d) is \" %\n                                   (name, len(args), len(qubits)) +\n                                   \"incompatible with the standard \" +\n                                   \"extensions\")\n            this_gate = gate_data[1]([list(map(lambda x:\n                                               x.sym(nested_scope), args)),\n                                      list(map(self._map_qubit, qubits))])\n            if self.creg is not None:\n                this_gate.c_if(self._map_creg(self.creg), self.cval)\n\n    def end_gate(self, name, args, qubits, nested_scope=None):\n        \"\"\"End a custom gate.\n\n        name is name string.\n        args is list of Node expression objects.\n        qubits is list of (regname, idx) tuples.\n        nested_scope is a list of dictionaries mapping expression variables\n        to Node expression objects in order of increasing nesting depth.\n        \"\"\"\n        if name == self.in_gate:\n            self.in_gate = \"\"\n            self.listen = True\n\n    def get_output(self):\n        \"\"\"Return the QuantumCircuit object.\"\"\"\n        return self.circuit\n", "path": "qiskit/unroll/_circuitbackend.py"}]}