| | |
| | from baseGate import * |
| |
|
| | |
| | elementGate = { |
| | "X":"CNOT cq-0,tq-0;", |
| | "Y":"Sd tq-0;CNOT cq-0,tq-0;S tq-0;", |
| | "Z":"H tq-0;CNOT cq-0,tq-0;H tq-0;", |
| | "H":"H tq-0;Sd tq-0;CNOT cq-0,tq-0;H tq-0;T tq-0;CNOT cq-0,tq-0;T tq-0;H tq-0;S tq-0;X tq-0;S cq-0;", |
| | "S":"T tq-0;CNOT cq-0,tq-0;Td tq-0;CNOT cq-0,tq-0;", |
| | "Sd":"Td tq-0;CNOT cq-0,tq-0;T tq-0;CNOT cq-0,tq-0;", |
| | "T":"Rz(math.pi/8) tq-0;CNOT cq-0,tq-0;Rz(-math.pi/8) tq-0;CNOT cq-0,tq-0;", |
| | "Td":"Rz(-math.pi/8) tq-0;CNOT cq-0,tq-0;Rz(math.pi/8) tq-0;CNOT cq-0,tq-0;", |
| | "Rz":"Rz(alpha) tq-0;CNOT cq-0,tq-0;Rz(beta) tq-0;CNOT cq-0,tq-0;", |
| | "Ry":"Ry(alpha) tq-0;CNOT cq-0,tq-0;Ry(beta) tq-0;CNOT cq-0,tq-0;", |
| | |
| | |
| | } |
| |
|
| | |
| | class SplitGate: |
| | def __init__(self): |
| | self.allowSet = [] |
| | for key in allowGate: |
| | if key == 'M': |
| | continue |
| | self.allowSet.append(key) |
| |
|
| | |
| | def get_curl_info(self): |
| | try: |
| | raise Exception |
| | except: |
| | f = sys.exc_info()[2].tb_frame.f_back |
| | return [f.f_code.co_name, f.f_lineno] |
| |
|
| | |
| | |
| | def __convert0to1(self,cql:list,vl:list): |
| | QASM = "" |
| | for i in range(0,len(cql)): |
| | if len(vl) == 1: |
| | j = 0 |
| | else: |
| | j = i |
| | if vl[j] == 0: |
| | QASM += "X cq-" + str(i) + ";" |
| | |
| | return QASM |
| |
|
| | |
| | |
| | def CU(self,gateName:str,cq:Qubit,tq:Qubit,vl:list,angle = None,executeStatus = False): |
| | QASM = "" |
| | QASM += self.__convert0to1([cq],vl) |
| | |
| | singleGate = gateName.split("-")[1] |
| | |
| | tmpQASM = "" |
| | |
| | try: |
| | tmpQASM = elementGate[singleGate] |
| | if angle != None: |
| | |
| | angleN = angle/2 |
| | tmpQASM = tmpQASM.replace("alpha",str(angleN),1) |
| | tmpQASM = tmpQASM.replace("beta",str(-angleN),1) |
| | except KeyError: |
| | info = get_curl_info() |
| | writeErrorMsg("Gate: "+ singleGate + " hasn't been definded in Dict:elementGate in defines.py!",info[0],info[1]) |
| | QASM += tmpQASM |
| | QASM += self.__convert0to1([cq],vl) |
| | if executeStatus: |
| | return self.execute(QASM,[cq],[tq],gateName,angle) |
| | return QASM |
| |
|
| | |
| | |
| | |
| | def MCU(self,gateName:str,cql:list,tq:Qubit,vl:list,angle = None,executeStatus = False): |
| | if gateName == "c1-c1-X" or gateName == "Toffoli": |
| | QASM = self.__Toffoli(["cq-0","cq-1"],"tq-0") |
| | else: |
| | |
| | |
| | N = len(cql) + 1 |
| | actualN = 2 * N -3 |
| | for i in range(0,actualN-1): |
| | if i % 2 == 0 and i > 0: |
| | |
| | cql.insert(i,Qubit(True)) |
| | |
| | vl.insert(i,1) |
| | QASM = "" |
| | QASM += self.__convert0to1(cql,vl) |
| | for j in range(2,actualN-1,2): |
| | QASM += self.__Toffoli(["cq-"+str(j-2),"cq-"+str(j-1)],"cq-"+str(j)) |
| | |
| | |
| | singleG = gateName.split("-")[-1] |
| | if singleG == "X": |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | else: |
| | |
| | try: |
| | if angle != None: |
| | |
| | angleN = angle/2 |
| | if singleG == "Rz": |
| | QASM += "Rz("+ str(angleN) +") tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "Rz("+ str(-angleN) +") tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | elif singleG == "Ry": |
| | QASM += "Ry("+ str(angleN) +") tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "Ry("+ str(-angleN) +") tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | else: |
| | raise GateNameError(singleGate) |
| | else: |
| | if singleG == "Y": |
| | QASM += "Sd tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "S tq-0;" |
| | elif singleG == "Z": |
| | QASM += "H tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "H tq-0;" |
| | elif singleG == "H": |
| | QASM += "H tq-0;Sd tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "H tq-0;T tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "T tq-0;H tq-0;S tq-0;X tq-0;" |
| | QASM += "S cq-"+str(actualN-3)+";S cq-"+str(actualN-2)+";" |
| | elif singleG == "S": |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "Td tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "T tq-0;T cq-"+str(actualN-3)+";T cq-"+str(actualN-2)+";" |
| | elif singleG == "Sd": |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "T tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "Td tq-0;Td cq-"+str(actualN-3)+";Td cq-"+str(actualN-2)+";" |
| | elif singleG == "T": |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "Rz(-math.pi/8) tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "Rz(math.pi/8) tq-0;Rz(math.pi/8) cq-"+str(actualN-3)+";Rz(math.pi/8) cq-"+str(actualN-2)+";" |
| | elif singleG == "Td": |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "Rz(math.pi/8) tq-0;" |
| | QASM += self.__Toffoli(["cq-"+str(actualN-3),"cq-"+str(actualN-2)],"tq-0") |
| | QASM += "Rz(-math.pi/8) tq-0;Rz(-math.pi/8) cq-"+str(actualN-3)+";Rz(-math.pi/8) cq-"+str(actualN-2)+";" |
| | else: |
| | raise GateNameError(singleG) |
| | except GateNameError as gne: |
| | info = get_curl_info() |
| | writeErrorMsg(gne,info[0],info[1]) |
| | pass |
| | QASM += self.__convert0to1(cql,vl) |
| | |
| | |
| | if executeStatus: |
| | return self.execute(QASM,cql,[tq],gateName,angle) |
| | return QASM |
| |
|
| | |
| | |
| | |
| | def __Toffoli(self,cqIndexL:list,id_tq:str): |
| | id_cq0 = cqIndexL[0] |
| | id_cq1 = cqIndexL[1] |
| | QASM = "H "+id_tq+";CNOT "+id_cq1 |
| | QASM += ","+id_tq+";Td "+id_tq+";CNOT "+id_cq0 |
| | QASM += ","+id_tq+";T "+id_tq+";CNOT "+id_cq1 |
| | QASM += ","+id_tq+";Td "+id_tq+";CNOT "+id_cq0 |
| | QASM += ","+id_tq+";Td "+id_cq1+";T "+id_tq |
| | QASM += ";CNOT "+id_cq0+","+id_cq1+";H "+id_tq |
| | QASM += ";Td "+id_cq1+";CNOT "+id_cq0+","+id_cq1 |
| | QASM += ";T "+id_cq0+";S "+id_cq1 +";" |
| | |
| | return QASM |
| |
|
| | |
| | def __recordEG(self,gateName:str,cql:list,tql:list,angle = None): |
| | resQL = cql.copy() |
| | for tq in tql: |
| | resQL.append(tq) |
| |
|
| | |
| |
|
| | i = 0 |
| | while i < len(resQL): |
| | if resQL[i].tag == "AX": |
| | |
| | del resQL[i] |
| | continue |
| | i += 1 |
| |
|
| | cG = [[0] * (2**len(resQL))] * (2**len(resQL)) |
| | |
| | |
| | |
| |
|
| | |
| | gate = Gate(resQL,cG,gateName) |
| | if len(resQL) == 1: |
| | c = gate.recordSingleExecution(True,angle) |
| | |
| | else: |
| | c = gate.recordmultiExecution(True,angle) |
| | |
| |
|
| | |
| | |
| | def execute(self,er,cqL:list,tqL:list,gateName:str,angle = None): |
| | self.__recordEG(gateName,cqL,tqL,angle) |
| | |
| | erL = er.split(";") |
| | for item in erL: |
| | if item == "": |
| | continue |
| | tmpStr = item.split(" ") |
| | gate = tmpStr[0] |
| | import re |
| | |
| | m = re.match("(.*)\((.*)\)(.*)", gate) |
| | if m != None: |
| | gate = m.group(1) |
| | parameter = m.group(2) |
| | exeStr = gate + "(" + parameter + "," |
| | else: |
| | exeStr = gate + "(" |
| | q = tmpStr[1].split(",") |
| | for i in range(0,len(q)): |
| | qType = q[i].split("-")[0] |
| | index = q[i].split("-")[1] |
| | try: |
| | if qType == "cq": |
| | exeStr += "cqL[" + index + "]" |
| | elif qType == "tq": |
| | exeStr += "tqL[" + index + "]" |
| | else: |
| | raise ValueError |
| | except ValueError: |
| | info = self.get_curl_info() |
| | writeErrorMsg("Qubit List: "+qtype+" isn't defined in Class SplitGate!",info[0],info[1]) |
| | except IndexError: |
| | info = self.get_curl_info() |
| | writeErrorMsg("The index of the target element is our of range!",info[0],info[1]) |
| | if i != len(q)-1: |
| | exeStr += "," |
| | exeStr += ",False)" |
| | exec(exeStr) |
| | |
| | resQL = cqL.copy() |
| | for tq in tqL: |
| | resQL.append(tq) |
| | return resQL |
| |
|
| | |
| | |
| | def X(q:Qubit,record = True,forceQuit = False): |
| | X = [[0,1],[1,0]] |
| | gate = Gate([q],X,"X") |
| | return gate.singleOperator(record,forceQuit = forceQuit) |
| |
|
| | def Y(q:Qubit,record = True,forceQuit = False): |
| | Y = [[0,-1j],[1j,0]] |
| | gate = Gate([q],Y,"Y") |
| | return gate.singleOperator(record,forceQuit = forceQuit) |
| |
|
| | def Z(q:Qubit,record = True,forceQuit = False): |
| | Z = [[1,0],[0,-1]] |
| | gate = Gate([q],Z,"Z") |
| | return gate.singleOperator(record,forceQuit = forceQuit) |
| |
|
| | def I(q:Qubit,record = True,forceQuit = False): |
| | I = [[1,0],[0,1]] |
| | gate = Gate([q],I,"I") |
| | return gate.singleOperator(record,forceQuit = forceQuit) |
| |
|
| |
|
| | def H(q:Qubit,record = True,forceQuit = False): |
| | H = [[1/math.sqrt(2),1/math.sqrt(2)],[1/math.sqrt(2),-1/math.sqrt(2)]] |
| | gate = Gate([q],H,"H") |
| | return gate.singleOperator(record,forceQuit = forceQuit) |
| |
|
| | def S(q:Qubit,record = True,forceQuit = False): |
| | S = [[1,0],[0,1j]] |
| | gate = Gate([q],S,"S") |
| | return gate.singleOperator(record,forceQuit = forceQuit) |
| |
|
| | def Sd(q:Qubit,record = True,forceQuit = False): |
| | Sd = [[1,0],[0,-1j]] |
| | gate = Gate([q],Sd,"Sd") |
| | return gate.singleOperator(record,forceQuit = forceQuit) |
| |
|
| |
|
| | def T(q:Qubit,record = True,forceQuit = False): |
| | T = [[1,0],[0,(1+1j)/math.sqrt(2)]] |
| | gate = Gate([q],T,"T") |
| | return gate.singleOperator(record,forceQuit = forceQuit) |
| |
|
| | def Td(q:Qubit,record = True,forceQuit = False): |
| | Td = [[1,0],[0,(1-1j)/math.sqrt(2)]] |
| | gate = Gate([q],Td,"Td") |
| | return gate.singleOperator(record,forceQuit = forceQuit) |
| |
|
| | |
| | |
| | def Rz(phi,q:Qubit,record = True,forceQuit = False): |
| | pows = 1j*phi / 2 |
| | Rz = [[cmath.exp(-pows),0],[0,cmath.exp(pows)]] |
| | gate = Gate([q],Rz,"Rz") |
| | return gate.singleOperator(record,phi,forceQuit = forceQuit) |
| |
|
| | def Ry(theta,q:Qubit,record = True,forceQuit = False): |
| | Ry = [[math.cos(theta/2),-math.sin(theta/2)],[math.sin(theta/2),math.cos(theta/2)]] |
| | gate = Gate([q],Ry,"Ry") |
| | return gate.singleOperator(record,theta,forceQuit = forceQuit) |
| |
|
| | |
| | def Rx(phi,q:Qubit,record = True,forceQuit = False): |
| | PI = math.pi |
| | I = [[1,0],[0,1]] |
| | q = Rz(PI/2,q,False) |
| | q = Ry(-phi,q,False) |
| | q = Rz(-PI/2,q,False) |
| | gate = Gate([q],I,"Rx") |
| | gate.recordSingleExecution(True,phi,forceQuit = forceQuit) |
| | return q |
| |
|
| |
|
| | |
| | |
| | |
| | def CNOT(q1:Qubit,q2:Qubit,record = True,forceQuit = False): |
| | CNOT = [[1,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,1,0]] |
| | gate = Gate([q1,q2],CNOT,"CNOT") |
| | return gate.CNOTOperator(record,forceQuit = forceQuit) |
| |
|
| |
|
| | def ControlledZ(q1:Qubit,q2:Qubit,record = True,forceQuit = False): |
| | H(q2,record,forceQuit) |
| | CNOT(q1,q2,record,forceQuit) |
| | H(q2,record,forceQuit) |
| | return q1.entanglement |
| |
|
| | |
| | |
| | def M(q:Qubit,result = True): |
| | I = [[1,0],[0,1]] |
| | |
| | gate = Gate([q],I,"M") |
| | |
| | |
| | return gate.MOperator(result) |
| |
|
| |
|
| | |
| | def Toffoli(q1:Qubit,q2:Qubit,q3:Qubit): |
| | sg = SplitGate() |
| | qL = sg.MCU("Toffoli",[q1,q2],q3,[1,1],None,True) |
| | return qL |
| |
|