def eps_l_noop_inplace(x, A1, A2, out): """Implements the left epsilon map for a pre-exisiting output matrix. The output must have shape (A1.shape[2], A2.shape[2]). See eps_l_noop(). Parameters ---------- x : ndarray The argument matrix. A1: ndarray The MPS ket tensor for the current site. A2: ndarray The MPS bra tensor for the current site. out: ndarray The output matrix (must have correct dimensions). Returns ------- res : ndarray The resulting matrix. """ out.fill(0) #Two extra temporaries required because scipy doesn't bother to provide full gemm functionality. tmp_A1sh = np.empty((A1[0].shape[1], A1[0].shape[0]), dtype=A1[0].dtype, order='F') tmp_xA2s = np.empty((x.shape[0], A2[0].shape[1]), dtype=np.promote_types(x.dtype, A2[0].dtype)) out_s = np.empty_like(out, order='C') for s in xrange(A1.shape[0]): tmp_A1sh[:] = A1[s].T np.conjugate(tmp_A1sh, out=tmp_A1sh) tmp_xA2s = mm.dot_inplace(x, A2[s], tmp_xA2s) out_s = np.dot(tmp_A1sh, tmp_xA2s, out=out_s) #dot expects a C-ordered output array out += out_s return out
def eps_r_noop_inplace(x, A1, A2, out): """Implements the right epsilon map for a pre-exisiting output matrix. The output must have shape (A1.shape[1], A2.shape[1]). See eps_r_noop(). Parameters ---------- x : ndarray The argument matrix. A1: ndarray The MPS ket tensor for the current site. A2: ndarray The MPS bra tensor for the current site. out: ndarray The output matrix (must have correct dimensions). Returns ------- res : ndarray The resulting matrix. """ out.fill(0) tmp_A1xs = np.empty((A1[0].shape[0], x.shape[1]), dtype=np.promote_types(A1.dtype, x.dtype)) tmp_A2sh = np.empty((A2[0].shape[1], A2[0].shape[0]), dtype=A2[0].dtype, order='F') out_s = np.empty_like(out, order='C') for s in xrange(A1.shape[0]): tmp_A1xs = mm.dot_inplace(A1[s], x, tmp_A1xs) tmp_A2sh[:] = A2[s].T np.conjugate(tmp_A2sh, out=tmp_A2sh) out_s = np.dot(tmp_A1xs, tmp_A2sh, out=out_s) out += out_s return out
def calc_BHB(self, x, p, tdvp, tdvp2, prereq, M_prev=None, y_pi_prev=None, pinv_solver=None): if pinv_solver is None: pinv_solver = las.gmres if self.ham_sites == 3: return else: V_, AhlAo1, A_o2c, Ao1, Ao1c, A_Vr_ho2, A_A_o12c, rhs10, _ham_tp = prereq A = tdvp.A[0] A_ = tdvp2.A[0] l = tdvp.l[0] r_ = tdvp2.r[0] l_sqrt = tdvp.l_sqrt[0] l_sqrt_i = tdvp.l_sqrt_i[0] r__sqrt = tdvp2.r_sqrt[0] r__sqrt_i = tdvp2.r_sqrt_i[0] K__r = tdvp2.K[0] K_l = tdvp.K_left[0] pseudo = tdvp2 is tdvp B = tdvp2.get_B_from_x(x, tdvp2.Vsh[0], l_sqrt_i, r__sqrt_i) #Skip zeros due to rank-deficiency if la.norm(B) == 0: return sp.zeros_like(x), M_prev, y_pi_prev if self.sanity_checks: tst = tm.eps_r_noop(r_, B, A_) if not la.norm(tst) > self.sanity_tol: log.warning("Sanity check failed: Gauge-fixing violation! " + str(la.norm(tst))) if self.sanity_checks: B2 = np.zeros_like(B) for s in xrange(self.q): B2[s] = l_sqrt_i.dot(x.dot(m.mmul(V_[s], r__sqrt_i))) if la.norm(B - B2) / la.norm(B) > self.sanity_tol: log.warning("Sanity Fail in calc_BHB! Bad Vri!") y = tm.eps_l_noop(l, B, A) # if pseudo: # y = y - m.adot(r_, y) * l #should just = y due to gauge-fixing M = pinv_1mE(y, [A_], [A], l, r_, p=-p, left=True, pseudo=pseudo, out=M_prev, tol=self.pinv_tol, solver=pinv_solver, use_CUDA=self.pinv_CUDA, CUDA_use_batch=self.pinv_CUDA_batch, sanity_checks=self.sanity_checks, sc_data='M') #print m.adot(r, M) if self.sanity_checks: y2 = M - sp.exp(+1.j * p) * tm.eps_l_noop(M, A_, A) norm = la.norm(y.ravel()) if norm == 0: norm = 1 tst = la.norm(y - y2) / norm if tst > self.sanity_tol: log.warning("Sanity Fail in calc_BHB! Bad M. Off by: %g", tst) # if pseudo: # M = M - l * m.adot(r_, M) Mh = M.conj().T.copy(order='C') res_ls = 0 res_lsi = 0 exp = sp.exp if self.ham_sites == 3: pass else: Bo1 = get_Aop(B, _ham_tp, 2, conj=False) tmp = sp.empty((B.shape[1], V_.shape[1]), dtype=A.dtype, order='C') tmp2 = sp.empty((A_.shape[1], A_o2c[0].shape[1]), dtype=A.dtype, order='C') tmp3 = sp.empty_like(tmp2, order='C') for al in xrange(len(Bo1)): tmp3 = m.dot_inplace(tm.eps_r_noop_inplace(r_, A_, A_o2c[al], tmp2), r__sqrt_i, tmp3) res_ls += tm.eps_r_noop_inplace(tmp3, Bo1[al], V_, tmp) #1 tmp3 = m.dot_inplace(tm.eps_r_noop_inplace(r_, B, A_o2c[al], tmp2), r__sqrt_i, tmp3) tmp = tm.eps_r_noop_inplace(tmp3, Ao1[al], V_, tmp) #3 tmp *= exp(+1.j * p) res_ls += tmp del(tmp) del(tmp2) del(tmp3) res_lsi += sp.exp(-1.j * p) * Mh.dot(rhs10) #10 if self.ham_sites == 3: pass else: Bo2 = get_Aop(B, _ham_tp, 3, conj=False) for al in xrange(len(AhlAo1)): res_lsi += AhlAo1[al].dot(tm.eps_r_noop(r__sqrt, Bo2[al], V_)) #2 res_lsi += exp(-1.j * p) * tm.eps_l_noop(l, Ao1c[al], B).dot(A_Vr_ho2[al]) #4 res_lsi += exp(-2.j * p) * tm.eps_l_noop(Mh, Ao1c[al], A_).dot(A_Vr_ho2[al]) #12 K__rri = m.mmul(K__r, r__sqrt_i) res_ls += tm.eps_r_noop(K__rri, B, V_) #5 res_lsi += K_l.dot(tm.eps_r_noop(r__sqrt, B, V_)) #6 res_lsi += sp.exp(-1.j * p) * Mh.dot(tm.eps_r_noop(K__rri, A_, V_)) #8 y1 = sp.exp(+1.j * p) * tm.eps_r_noop(K__r, B, A_) #7 if self.ham_sites == 3: pass elif self.ham_sites == 2: tmp = 0 for al in xrange(len(A_A_o12c)): tmp += sp.exp(+1.j * p) * tm.eps_r_noop(tm.eps_r_noop(r_, A_, A_A_o12c[al][1]), B, A_A_o12c[al][0]) #9 tmp += sp.exp(+2.j * p) * tm.eps_r_noop(tm.eps_r_noop(r_, B, A_A_o12c[al][1]), A, A_A_o12c[al][0]) #11 y = y1 + tmp #7, 9, 11 del(tmp) if pseudo: y = y - m.adot(l, y) * r_ y_pi = pinv_1mE(y, [A], [A_], l, r_, p=p, left=False, pseudo=pseudo, out=y_pi_prev, tol=self.pinv_tol, solver=pinv_solver, use_CUDA=self.pinv_CUDA, CUDA_use_batch=self.pinv_CUDA_batch, sanity_checks=self.sanity_checks, sc_data='y_pi') #print m.adot(l, y_pi) if self.sanity_checks: z = y_pi - sp.exp(+1.j * p) * tm.eps_r_noop(y_pi, A, A_) tst = la.norm((y - z).ravel()) / la.norm(y.ravel()) if tst > self.sanity_tol: log.warning("Sanity Fail in calc_BHB! Bad x_pi. Off by: %g", tst) res_ls += tm.eps_r_noop(m.mmul(y_pi, r__sqrt_i), A, V_) res = l_sqrt.dot(res_ls) res += l_sqrt_i.dot(res_lsi) if self.sanity_checks: expval = m.adot(x, res) / m.adot(x, x) #print "expval = " + str(expval) if expval < -self.sanity_tol: log.warning("Sanity Fail in calc_BHB! H is not pos. semi-definite (%s)", expval) if abs(expval.imag) > self.sanity_tol: log.warning("Sanity Fail in calc_BHB! H is not Hermitian (%s)", expval) return res, M, y_pi
def calc_BHB(self, x, p, tdvp, tdvp2, prereq, M_prev=None, y_pi_prev=None, pinv_solver=None): if pinv_solver is None: pinv_solver = las.gmres if self.ham_sites == 3: return else: V_, AhlAo1, A_o2c, Ao1, Ao1c, A_Vr_ho2, A_A_o12c, rhs10, _ham_tp = prereq A = tdvp.A[0] A_ = tdvp2.A[0] l = tdvp.l[0] r_ = tdvp2.r[0] l_sqrt = tdvp.l_sqrt[0] l_sqrt_i = tdvp.l_sqrt_i[0] r__sqrt = tdvp2.r_sqrt[0] r__sqrt_i = tdvp2.r_sqrt_i[0] K__r = tdvp2.K[0] K_l = tdvp.K_left[0] pseudo = tdvp2 is tdvp B = tdvp2.get_B_from_x(x, tdvp2.Vsh[0], l_sqrt_i, r__sqrt_i) #Skip zeros due to rank-deficiency if la.norm(B) == 0: return sp.zeros_like(x), M_prev, y_pi_prev if self.sanity_checks: tst = tm.eps_r_noop(r_, B, A_) if not la.norm(tst) > self.sanity_tol: log.warning("Sanity check failed: Gauge-fixing violation! " + str(la.norm(tst))) if self.sanity_checks: B2 = np.zeros_like(B) for s in xrange(self.q): B2[s] = l_sqrt_i.dot(x.dot(m.mmul(V_[s], r__sqrt_i))) if la.norm(B - B2) / la.norm(B) > self.sanity_tol: log.warning("Sanity Fail in calc_BHB! Bad Vri!") y = tm.eps_l_noop(l, B, A) # if pseudo: # y = y - m.adot(r_, y) * l #should just = y due to gauge-fixing M = pinv_1mE(y, [A_], [A], l, r_, p=-p, left=True, pseudo=pseudo, out=M_prev, tol=self.pinv_tol, solver=pinv_solver, use_CUDA=self.pinv_CUDA, CUDA_use_batch=self.pinv_CUDA_batch, sanity_checks=self.sanity_checks, sc_data='M') #print m.adot(r, M) if self.sanity_checks: y2 = M - sp.exp(+1.j * p) * tm.eps_l_noop(M, A_, A) norm = la.norm(y.ravel()) if norm == 0: norm = 1 tst = la.norm(y - y2) / norm if tst > self.sanity_tol: log.warning("Sanity Fail in calc_BHB! Bad M. Off by: %g", tst) # if pseudo: # M = M - l * m.adot(r_, M) Mh = M.conj().T.copy(order='C') res_ls = 0 res_lsi = 0 exp = sp.exp if self.ham_sites == 3: pass else: Bo1 = get_Aop(B, _ham_tp, 2, conj=False) tmp = sp.empty((B.shape[1], V_.shape[1]), dtype=A.dtype, order='C') tmp2 = sp.empty((A_.shape[1], A_o2c[0].shape[1]), dtype=A.dtype, order='C') tmp3 = sp.empty_like(tmp2, order='C') for al in xrange(len(Bo1)): tmp3 = m.dot_inplace( tm.eps_r_noop_inplace(r_, A_, A_o2c[al], tmp2), r__sqrt_i, tmp3) res_ls += tm.eps_r_noop_inplace(tmp3, Bo1[al], V_, tmp) #1 tmp3 = m.dot_inplace( tm.eps_r_noop_inplace(r_, B, A_o2c[al], tmp2), r__sqrt_i, tmp3) tmp = tm.eps_r_noop_inplace(tmp3, Ao1[al], V_, tmp) #3 tmp *= exp(+1.j * p) res_ls += tmp del (tmp) del (tmp2) del (tmp3) res_lsi += sp.exp(-1.j * p) * Mh.dot(rhs10) #10 if self.ham_sites == 3: pass else: Bo2 = get_Aop(B, _ham_tp, 3, conj=False) for al in xrange(len(AhlAo1)): res_lsi += AhlAo1[al].dot(tm.eps_r_noop(r__sqrt, Bo2[al], V_)) #2 res_lsi += exp(-1.j * p) * tm.eps_l_noop(l, Ao1c[al], B).dot( A_Vr_ho2[al]) #4 res_lsi += exp(-2.j * p) * tm.eps_l_noop(Mh, Ao1c[al], A_).dot( A_Vr_ho2[al]) #12 K__rri = m.mmul(K__r, r__sqrt_i) res_ls += tm.eps_r_noop(K__rri, B, V_) #5 res_lsi += K_l.dot(tm.eps_r_noop(r__sqrt, B, V_)) #6 res_lsi += sp.exp(-1.j * p) * Mh.dot(tm.eps_r_noop(K__rri, A_, V_)) #8 y1 = sp.exp(+1.j * p) * tm.eps_r_noop(K__r, B, A_) #7 if self.ham_sites == 3: pass elif self.ham_sites == 2: tmp = 0 for al in xrange(len(A_A_o12c)): tmp += sp.exp(+1.j * p) * tm.eps_r_noop( tm.eps_r_noop(r_, A_, A_A_o12c[al][1]), B, A_A_o12c[al][0]) #9 tmp += sp.exp(+2.j * p) * tm.eps_r_noop( tm.eps_r_noop(r_, B, A_A_o12c[al][1]), A, A_A_o12c[al][0]) #11 y = y1 + tmp #7, 9, 11 del (tmp) if pseudo: y = y - m.adot(l, y) * r_ y_pi = pinv_1mE(y, [A], [A_], l, r_, p=p, left=False, pseudo=pseudo, out=y_pi_prev, tol=self.pinv_tol, solver=pinv_solver, use_CUDA=self.pinv_CUDA, CUDA_use_batch=self.pinv_CUDA_batch, sanity_checks=self.sanity_checks, sc_data='y_pi') #print m.adot(l, y_pi) if self.sanity_checks: z = y_pi - sp.exp(+1.j * p) * tm.eps_r_noop(y_pi, A, A_) tst = la.norm((y - z).ravel()) / la.norm(y.ravel()) if tst > self.sanity_tol: log.warning("Sanity Fail in calc_BHB! Bad x_pi. Off by: %g", tst) res_ls += tm.eps_r_noop(m.mmul(y_pi, r__sqrt_i), A, V_) res = l_sqrt.dot(res_ls) res += l_sqrt_i.dot(res_lsi) if self.sanity_checks: expval = m.adot(x, res) / m.adot(x, x) #print "expval = " + str(expval) if expval < -self.sanity_tol: log.warning( "Sanity Fail in calc_BHB! H is not pos. semi-definite (%s)", expval) if abs(expval.imag) > self.sanity_tol: log.warning("Sanity Fail in calc_BHB! H is not Hermitian (%s)", expval) return res, M, y_pi