def stateEnergy(psi: List[tn.Node], H: HOp): E = 0 for i in range(len(psi)): psiCopy = bops.copyState(psi) single_i = bops.copyState([H.singles[i]])[0] psiCopy[i] = bops.permute(tn.contract(psiCopy[i][1] ^ single_i[0], name=('site' + str(i))), [0, 2, 1]) E += bops.getOverlap(psiCopy, psi) bops.removeState(psiCopy) tn.remove_node(single_i) for i in range(len(psi) - 1): psiCopy = bops.copyState(psi) r2l = bops.copyState([H.r2l[i+1]])[0] l2r = bops.copyState([H.l2r[i]])[0] psiCopy[i][2] ^ psiCopy[i+1][0] psiCopy[i][1] ^ l2r[0] r2l[0] ^ psiCopy[i+1][1] l2r[2] ^ r2l[2] M = tn.contract_between(psiCopy[i], \ tn.contract_between(l2r, tn.contract_between(r2l, psiCopy[i+1]))) if bops.multiContraction(M, M, '0123', '0123*').tensor != 0: [psiCopy, te] = bops.assignNewSiteTensors(psiCopy, i, M, '>>') E += bops.getOverlap(psiCopy, psi) bops.removeState(psiCopy) tn.remove_node(r2l) tn.remove_node(l2r) return E
def XXVar(statesDir: str, outDir: str, NA, theta, phi): maxBondDim = 100 res = np.zeros(2, dtype=complex) U = tn.Node( np.matmul(ru.getUPhi(np.pi * phi / 2, 2), ru.getUTheta(np.pi * theta / 2, 2))) with open(statesDir + 'psiXX_NA_' + str(NA) + '_NB_' + str(NA), 'rb') as f: psi = pickle.load(f) psiCopy = bops.relaxState(psi, 64) res[0] = bops.getOverlap(psi, psiCopy) bops.removeState(psi) for i in range(NA): psiCopy[i] = bops.permute( bops.multiContraction(psiCopy[i], U, '1', '0'), [0, 2, 1]) doublePsi = [None for j in range(len(psiCopy))] for j in range(len(psiCopy)): doublePsi[j] = doubleMPSSite(psiCopy[j]) doubleCopy = bops.copyState(doublePsi) for j in range(NA): doublePsi[j] = bops.permute( bops.multiContraction(doublePsi[j], E, '1', '0', cleanOr1=True), [0, 2, 1]) res[1] = bops.getOverlap(doublePsi, doubleCopy) with open( outDir + 'XXVar_NA_' + str(NA) + '_t_' + str(theta) + '_p_' + str(phi), 'wb') as f: pickle.dump(res, f) print(res)
def expectationValues(psi: List[tn.Node], vs: List[List[np.array]]): vs = np.round(vs, 10) n = len(vs) NA = len(vs[0]) result = 1 # result = np.eye(2, dtype=complex) for copy in range(n): psiCopy = bops.copyState(psi) for alpha in range(NA - 1, -1, -1): overlap = np.matmul(vs[copy][alpha], np.conj(vs[np.mod(copy + 1, n)][alpha])) toEstimate = np.outer(vs[copy][alpha], np.conj(vs[np.mod(copy + 1, n)][alpha])) if np.abs(np.round(overlap, 8)) == 2: toMeasure = toEstimate else: hermitianComponent = np.random.randint(2) if hermitianComponent: toMeasure = (toEstimate + np.conj(np.transpose(toEstimate))) else: toMeasure = (toEstimate - np.conj(np.transpose(toEstimate))) psiCopy[alpha] = bops.permute(bops.multiContraction(psiCopy[alpha], tn.Node(toMeasure), \ '1', '1'), [0, 2, 1]) psiCopy = bops.shiftWorkingSite(psiCopy, alpha, '<<') result *= bops.getOverlap(psiCopy, psi) # result = np.matmul(result, toMeasure) bops.removeState(psiCopy) return result
def trotterSweep(trotterGates, psi, startSite, endSite, maxBondDim=1024): psiCopy = bops.copyState(psi) truncErr = 0 N = len(psi) for k in [endSite - i for i in range(startSite, endSite)]: M = bops.multiContraction(psiCopy[k - 1], psiCopy[k], [2], [0]) M = bops.permute( bops.multiContraction(M, trotterGates[k - 1], [1, 2], [0, 1]), [0, 2, 3, 1]) [l, r, currTruncErr] = bops.svdTruncation(M, M[:2], M[2:], '<<', maxBondDim, leftName='site' + str(k - 1), rightName='site' + str(k), edgeName='v' + str(k)) if currTruncErr > truncErr: truncErr = currTruncErr psiCopy[k] = r psiCopy[k - 1] = l bops.multiContraction(r, r, [1, 2], [1, 2, '*']) for k in range(startSite, endSite): M = bops.multiContraction(psiCopy[k], psiCopy[k + 1], [2], [0]) M = bops.permute( bops.multiContraction(M, trotterGates[k], [1, 2], [0, 1]), [0, 2, 3, 1]) [l, r, currTruncErr] = bops.svdTruncation(M, M[:2], M[2:], '>>', maxBondDim, leftName='site' + str(k), rightName='site' + str(k + 1), edgeName='v' + str(k + 1)) if currTruncErr > truncErr: truncErr = currTruncErr psiCopy[k] = l psiCopy[k + 1] = r # For imaginary time propagation, renormalize state. norm = bops.getOverlap(psiCopy, psiCopy) psiCopy[N - 1].tensor = psiCopy[N - 1].tensor / math.sqrt(abs(norm)) return psiCopy, truncErr
def randomMeasurement(psi, startInd, endInd): d = psi[0].tensor.shape[1] res = [-1] * (endInd - startInd) psiCopy = bops.copyState(psi) for k in [len(psiCopy) - 1 - i for i in range(len(psiCopy) - startInd - 1)]: psiCopy = bops.shiftWorkingSite(psiCopy, k, '<<') for i in range(startInd, endInd): rho = bops.multiContraction(psiCopy[i], psiCopy[i], '02', '02*') measurement = np.random.uniform(low=0, high=1) covered = 0 for s in range(len(rho.tensor)): if covered < measurement < covered + rho.tensor[s, s]: res[i - startInd] = s break covered += rho.tensor[s, s] projectorTensor = np.zeros((d, d), dtype=complex) projectorTensor[res[i - startInd], res[i - startInd]] = 1 projector = tn.Node(projectorTensor, backend=None) bops.applySingleSiteOp(psiCopy, projector, i) psiCopy = bops.shiftWorkingSite(psiCopy, i, '>>') psiCopy[i + 1].tensor /= np.sqrt(bops.getOverlap(psiCopy, psiCopy)) tn.remove_node(rho) bops.removeState(psiCopy) return res
# print('<psi|hpsi> = ' + str(bops.getOverlap(psi, hpsi))) R2 = bops.getRenyiEntropy(psi, 2, int(len(psi) / 2 - 1)) NU = 200 etas = [1, 2, 3, N, int(N * 1.5), N * 2, int(N * 2.5), N * 3] results = [None] * len(etas) dt = J * 1e-2 for e in range(len(etas)): eta = etas[e] sum = 0 for n in range(NU): psiCopy = bops.copyState(psi) for j in range(eta): onsiteTermsA, neighborTermsA = getHAMatrices(N, C, Omega, delta) HA = dmrg.getDMRGH(N, onsiteTermsA, neighborTermsA) trotterGates = trotter.getTrotterGates(N, 2, onsiteTermsA, neighborTermsA, dt) for i in range(int(T / dt)): [psiCopy, truncErr] = trotter.trotterSweep(trotterGates, psiCopy, 0, int(len(psiCopy) / 2 - 1)) psiCopy2 = bops.copyState(psiCopy) projectS(psiCopy, int(len(psiCopy)/2 - 1)) p = bops.getOverlap(psiCopy, psiCopy2) sum += p * p bops.removeState(psiCopy) bops.removeState(psiCopy2) avg = sum / NU results[e] = avg * (2**int(len(psi)/2) * (2**int(len(psi)/2) + 1)) - 1 plt.plot(etas, [R2] * len(etas)) plt.plot(etas, results) plt.show()
def getTridiagonal(HL, HR, H, k, psi, psiCompare=None): accuracy = 1e-10 # 1e-12 v = bops.multiContraction(psi[k], psi[k + 1], '2', '0') # Small innaccuracies ruin everything! v.set_tensor(v.get_tensor() / bops.getNodeNorm(v)) psiCopy = bops.copyState(psi) base = [] base.append(v) Hv = applyHToM(HL, HR, H, v, k) alpha = bops.multiContraction(v, Hv, '0123', '0123*').get_tensor() if psiCompare is not None: copyV = bops.copyState([v])[0] psiCopy = bops.assignNewSiteTensors(psiCopy, k, copyV, '>>')[0] print('line 196, k = ' + str(k) + ', overlap = ' + str(bops.getOverlap(psiCopy, psiCompare))) E = stateEnergy(psi, H) w = bops.addNodes(Hv, bops.multNode(v, -alpha)) beta = bops.getNodeNorm(w) # Start with T as an array and turn into tridiagonal matrix at the end. Tarr = [[0, 0, 0]] Tarr[0][1] = alpha counter = 0 formBeta = 2 * beta # This is just some value to init formBeta > beta. while (beta > accuracy) and (counter <= 50) and (beta < formBeta): Tarr[counter][2] = beta Tarr.append([0, 0, 0]) Tarr[counter + 1][0] = beta counter += 1 v = bops.multNode(w, 1 / beta) base.append(v) if psiCompare is not None: copyV = bops.copyState([v])[0] psiCopy = bops.assignNewSiteTensors(psiCopy, k, copyV, '>>')[0] print('line 219, k = ' + str(k) + ', counter = ' + str(counter) + ', overlap = ' + str(bops.getOverlap(psiCopy, psiCompare))) Hv = applyHToM(HL, HR, H, v, k) alpha = bops.multiContraction(v, Hv, '0123', '0123*').get_tensor() Tarr[counter][1] = alpha w = bops.addNodes(bops.addNodes(Hv, bops.multNode(v, -alpha)), \ bops.multNode(base[counter-1], -beta)) formBeta = beta beta = bops.getNodeNorm(w) T = np.zeros((len(Tarr), len(Tarr))) T[0][0] = Tarr[0][1] if len(Tarr) > 1: T[0][1] = Tarr[0][2] for i in range(1, len(Tarr) - 1): T[i][i - 1] = Tarr[i][0] T[i][i] = Tarr[i][1] T[i][i + 1] = Tarr[i][2] T[len(Tarr) - 1][len(Tarr) - 2] = Tarr[len(Tarr) - 1][0] T[len(Tarr) - 1][len(Tarr) - 1] = Tarr[len(Tarr) - 1][1] return [T, base]
t = 0.5 onsiteTerm = np.zeros((2, 2)) onsiteTerm[1][1] = 0 onsiteTerm[0][0] = 0 neighborTerm = np.zeros((4, 4)) neighborTerm[1][2] = 1 neighborTerm[2][1] = 1 neighborTerm[0][0] = 0 neighborTerm[3][3] = 0 psi1 = bops.getStartupState(N) H = getDMRGH(N, onsiteTerm, neighborTerm) HLs, HRs = getHLRs(H, psi1) psi1, E0, truncErrs = getGroundState(H, HLs, HRs, psi1, None) print(stateEnergy(psi1, H)) print(len(truncErrs)) psi2 = bops.getOrthogonalState(psi1) print(bops.getOverlap(psi1, psi2)) HLs, HRs = getHLRs(H, psi2) psi2, E0, truncErrs = getGroundState(H, HLs, HRs, psi2, psi1) print(stateEnergy(psi2, H)) print(bops.getOverlap(psi1, psi2)) print(len(truncErrs)) # psi3 = bops.addStates(psi2, psi) # print(bops.getOverlap(psi, psi3)) # print(bops.getOverlap(psi2, psi3)) # psi3[0] = bops.multNode(psi3[0], 1 / math.sqrt(bops.getOverlap(psi3, psi3))) # H, HLs, HRs = getH(N, onsiteTerm, neighborTerm, psi3) # psi3, E0, truncErrs = getGroundState(H, HLs, HRs, N, psi3) # print(stateEnergy(psi3, H)) # print(bops.getOverlap(psi, psi3)) # print(bops.getOverlap(psi2, psi3))