def _map_bit(self, bit): """Map bit tuple (regname, index) to (ClassicalRegister, index).""" cregs = self.circuit.cregs if bit[0] not in cregs: raise _backenderror.BackendError( "creg %s does not exist" % bit[0]) return (cregs[bit[0]], bit[1])
def _map_qubit(self, qubit): """Map qubit tuple (regname, index) to (QuantumRegister, index).""" qregs = self.circuit.qregs if qubit[0] not in qregs: raise _backenderror.BackendError( "qreg %s does not exist" % qubit[0]) return (qregs[qubit[0]], qubit[1])
def _map_creg(self, creg): """Map creg name to ClassicalRegister.""" cregs = self.circuit.cregs cregs_names = [element.name for element in cregs] if creg not in cregs_names: raise _backenderror.BackendError("creg %s does not exist" % creg) index = cregs_names.index(creg) return cregs[index]
def _map_bit(self, bit): """Map bit tuple (regname, index) to (ClassicalRegister, index).""" cregs = self.circuit.cregs regname = bit[0] cregs_names = [element.name for element in cregs] if regname not in cregs_names: raise _backenderror.BackendError( "creg %s does not exist" % bit[0]) index = cregs_names.index(regname) return (cregs[index], bit[1])
def _map_qubit(self, qubit): """Map qubit tuple (regname, index) to (QuantumRegister, index).""" qregs = self.circuit.qregs regname = qubit[0] qregs_names = [element.name for element in qregs] if regname not in qregs_names: raise _backenderror.BackendError( "qreg %s does not exist" % qubit[0]) index = qregs_names.index(regname) return (qregs[index], qubit[1])
def start_gate(self, name, args, qubits, nested_scope=None, extra_fields=None): """Begin a custom gate. name is name string. args is list of Node expression objects. qubits is list of (regname, idx) tuples. nested_scope is a list of dictionaries mapping expression variables to Node expression objects in order of increasing nesting depth. """ if self.listen and name not in self.basis \ and self.gates[name]["opaque"]: raise _backenderror.BackendError( "opaque gate %s not in basis" % name) if self.listen and name in self.basis: self.in_gate = name self.listen = False # Gate names mapped to number of arguments and qubits # and method to invoke on [args, qubits] lut = {"ccx": [(0, 3), lambda x: self.circuit.ccx(x[1][0], x[1][1], x[1][2])], "ch": [(0, 2), lambda x: self.circuit.ch(x[1][0], x[1][1])], "crz": [(1, 2), lambda x: self.circuit.crz(x[0][0], x[1][0], x[1][1])], "cswap": [(0, 3), lambda x: self.circuit.cswap(x[1][0], x[1][1], x[1][2])], "cu1": [(1, 2), lambda x: self.circuit.cu1(x[0][0], x[1][0], x[1][1])], "cu3": [(3, 2), lambda x: self.circuit.cu3(x[0][0], x[0][1], x[0][2], x[1][0], x[1][1])], "cx": [(0, 2), lambda x: self.circuit.cx(x[1][0], x[1][1])], "cy": [(0, 2), lambda x: self.circuit.cy(x[1][0], x[1][1])], "cz": [(0, 2), lambda x: self.circuit.cz(x[1][0], x[1][1])], "swap": [(0, 2), lambda x: self.circuit.swap(x[1][0], x[1][1])], "h": [(0, 1), lambda x: self.circuit.h(x[1][0])], "id": [(0, 1), lambda x: self.circuit.iden(x[1][0])], "rx": [(1, 1), lambda x: self.circuit.rx(x[0][0], x[1][0])], "ry": [(1, 1), lambda x: self.circuit.ry(x[0][0], x[1][0])], "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])], "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], x[1][0])], "u3": [(3, 1), lambda x: self.circuit.u3(x[0][0], x[0][1], x[0][2], x[1][0])], "x": [(0, 1), lambda x: self.circuit.x(x[1][0])], "y": [(0, 1), lambda x: self.circuit.y(x[1][0])], "z": [(0, 1), lambda x: self.circuit.z(x[1][0])]} if name not in lut: raise _backenderror.BackendError( "gate %s not in standard extensions" % name) gate_data = lut[name] if gate_data[0] != (len(args), len(qubits)): raise _backenderror.BackendError( "gate %s signature (%d, %d) is " % (name, len(args), len(qubits)) + "incompatible with the standard " + "extensions") this_gate = gate_data[1]([list(map(lambda x: x.sym(nested_scope), args)), list(map(self._map_qubit, qubits))]) if self.creg is not None: this_gate.c_if(self._map_creg(self.creg), self.cval)
def _map_creg(self, creg): """Map creg name to ClassicalRegister.""" cregs = self.circuit.cregs if creg not in cregs: raise _backenderror.BackendError("creg %s does not exist" % creg) return cregs[creg]