def __init__(self, lattice, chi, state_type='pure', spin='half', ini_way='random', operators=None, is_symme_env=True, is_debug=False): PepsBasic.__init__(self) """ Order of bonds: (phys, virtual, virtual, ...) Lattices: 'honeycomb0' lattice: n-th virtual bond, n-th lambda 0,lm0 1,lm1 \ / - 2,lm2 - / \ 1,lm1 0,lm0 """ self.lattice = lattice self.stateType = state_type # pure or mixed self.spin = spin self.d0 = from_spin2phys_dim(spin) self.chi = chi self.nTensor = 0 self.nLm = 0 self.nVirtual = 0 self.pos_lm = list() self.lm_ten_bond = None self.iniWay = ini_way self._is_debug = is_debug if self.stateType is 'mixed': self.d = self.d0**2 else: self.d = self.d0 self.initial_lattice() self.tensors = bf.empty_list(self.nTensor) self.lm = bf.empty_list(self.nLm) self.initial_ipeps() self.initial_lm() if operators is None: op_half = spin_operators(spin) self.operators = [ op_half['id'], op_half['sx'], op_half['sy'], op_half['sz'], op_half['su'], op_half['sd'] ] else: self.operators = operators # Below is for tree DMRG self.is_symme_env = is_symme_env self.env = None self.model_related = dict()
def rho_two_body_simple(self, which_lm): if which_lm is 'all': rho = bf.empty_list(self.nLm) for n_lm in range(0, self.nLm): rho[n_lm] = self.rho_two_body_nlm_simple(n_lm) elif type(which_lm) is int: rho = self.rho_two_body_nlm_simple(which_lm) else: rho = bf.empty_list(len(which_lm)) n = 0 for n_lm in which_lm: rho[n] = self.rho_two_body_nlm_simple(n_lm) n += 1 return rho
def rho_one_body_simple(self, which_t): if which_t is 'all': rho = bf.empty_list(self.nTensor) for nt in range(0, self.nTensor): rho[nt] = self.rho_one_body_simple_nt_tensor(nt) elif type(which_t) is int: rho = self.rho_one_body_simple_nt_tensor(which_t) else: rho = bf.empty_list(len(which_t)) n = 0 for nt in which_t: rho[n] = self.rho_one_body_simple_nt_tensor(nt) n += 1 return rho
def gtnc(para_tot=None): print('Preparing parameters') if para_tot is None: para_tot = pm.parameters_gcmpm() n_class = len(para_tot['classes']) paras = bf.empty_list(n_class) for n in range(0, n_class): paras[n] = copy.deepcopy(para_tot) paras[n]['class'] = int(para_tot['classes'][n]) paras[n]['chi'] = para_tot['chi'][n] paras[n]['theta'] = para_tot['theta'] paras[n]['save_exp'] = save_exp_gtn_one_class(paras[n]) classifiers = bf.empty_list(n_class) for n in range(0, n_class): print_dict(paras[n]) data = para_tot['data_path'] + paras[n]['save_exp'] if para_tot['if_load'] and os.path.isfile(data): print('The classifier already exists. Load directly') classifiers[n] = load_pr(data, 'a') else: print('Training the MPS of ' + str(para_tot['classes'][n])) classifiers[n] = gtn_one_class(paras[n])[0] # if para_tot['if_save']: # save_pr('../data_tnml/gcmpm/', paras[n]['save_exp'], # [classifiers[n]], ['classifier']) # classifiers[n].mps.check_orthogonality_by_tensors(tol=1e-12) # ==================== Testing accuracy ==================== print('Calculating the testing accuracy') b = TNmachineLearning.MachineLearningFeatureMap(para_tot['d']) b.load_data(data_path='..\\..\\..\\MNIST\\', file_sample='t10k-images.idx3-ubyte', file_label='t10k-labels.idx1-ubyte', is_normalize=True) b.select_samples(para_tot['classes']) if classifiers[0].is_dct: b.dct(shift=para_tot['shift'], factor=para_tot['factor']) b.images2vecs(para_tot['theta'] * np.pi / 2) fid = bf.empty_list(n_class) for n in range(0, n_class): fid[n] = b.compute_fidelities(classifiers[n].mps.mps) max_fid = np.argmin(np.hstack(fid), axis=1) predict = np.zeros(max_fid.shape, dtype=int) for n in range(0, n_class): predict += (max_fid == n) * int(para_tot['classes'][n]) # plot(predict) # plot(b.labels) accuracy = np.sum(predict == b.labels, dtype=float) / b.numVecSample print(accuracy)
def initialize_env(self): self.env = bf.empty_list(self.nVirtual) tmp = np.random.randn(self.chi, self.d0**2, self.chi) tmp = (tmp + tmp.transpose([2, 1, 0])) / 2 tmp /= np.linalg.norm(tmp.reshape(-1, )) for n in range(0, self.nVirtual): self.env[n] = tmp.copy()
def get_model_related_tree_dmrg(self, jx, jy, jz, hx, hz, tau=1e-6): if 'h2phys' not in self.model_related: self.model_related['h2phys'] = hamiltonian_heisenberg( self.spin, jx, jy, jz, hx, hz) self.model_related['h2_gate'] = expm(-tau / 2 * self.model_related['h2phys']) if 'tensor_gate' not in self.model_related: self.model_related['tensor_gate'] = hamiltonian2gate_tensors( self.model_related['h2phys'], tau) if 'hbath' not in self.model_related: self.model_related['hbath'] = bf.empty_list(self.nVirtual)
def update_by_given_effective_ops(psi, ops, bonds): indexes = bf.empty_list(1 + bonds.__len__()) indexes[0] = list(range(psi.ndim)) x = 1 for n in range(psi.ndim): if n in bonds: indexes[0][n] = x indexes[bonds.index(n) + 1] = [-n - 1, x] x += 1 else: indexes[0][n] = -n - 1 return tm.cont([psi] + ops, indexes)
def gcmpm(para_tot=None): print('Preparing parameters') if para_tot is None: para_tot = pm.parameters_gcmpm() n_class = len(para_tot['classes']) paras = bf.empty_list(n_class) for n in range(0, n_class): paras[n] = para_tot.copy() paras[n]['class'] = para_tot['classes'][n] paras[n]['chi'] = para_tot['chi'][n] paras[n]['save_exp'] = save_exp_gcmpm_one_class(paras[n]) classifiers = bf.empty_list(n_class) for n in range(0, n_class): data = '../data_tnml/gcmpm/' + paras[n]['save_exp'] if para_tot['if_load'] and os.path.isfile(data): print('The classifier already exists. Load directly') classifiers[n] = load_pr(data, 'classifier') else: print('Training the MPS of ' + str(para_tot['classes'][n])) classifiers[n] = gcmpm_one_class(paras[n])[0] if para_tot['if_save']: save_pr('../data_tnml/gcmpm/', paras[n]['save_exp'], [classifiers[n]], ['classifier']) # Testing accuracy print('Calculating the testing accuracy') labels = para_tot['classes'] b = TNmachineLearning.MachineLearningFeatureMap('MNIST', para_tot['d'], file_sample='t10k-images.idx3-ubyte', file_label='t10k-labels.idx1-ubyte') b.images2vecs(para_tot['classes'], ['all', 'all']) fid = np.zeros((n_class, )) num_wrong = 0 for ni in range(0, b.numVecSample): for n in range(0, n_class): fid[n] = b.fidelity_mps_image(classifiers[n].mps.mps, ni) n_max = int(np.argmax(fid)) if labels[n_max] != b.LabelNow[ni]: num_wrong += 1 accuracy = num_wrong/b.numVecSample print(accuracy)
def initialize_virtual_vecs_train(self, way='contract'): self.norms = np.ones((self.length, self.numVecSample)) self.vecsLeft = bf.empty_list(self.length) self.vecsRight = bf.empty_list(self.length) if way is 'random': for n in range(0, self.length): self.vecsLeft[n] = np.random.randn(self.mps.virtual_dim[n], self.numVecSample) self.vecsRight[n] = np.random.randn( self.mps.virtual_dim[n + 1], self.numVecSample) elif way is 'ones': for n in range(0, self.length): self.vecsLeft[n] = np.ones( (self.mps.virtual_dim[n], self.numVecSample)) self.vecsRight[n] = np.ones( (self.mps.virtual_dim[n + 1], self.numVecSample)) else: self.vecsRight[self.length - 1] = np.ones( (self.mps.virtual_dim[self.length], self.numVecSample)) self.update_virtual_vecs_train_all_tensors('right') self.vecsLeft[0] = np.ones( (self.mps.virtual_dim[0], self.numVecSample))
def initialize_virtual_vecs_train(self): self.vecsLeft = bf.empty_list(self.length, list()) self.vecsRight = bf.empty_list(self.length, list()) for n in range(0, self.length): self.vecsLeft[n] = np.ones((self.mps.virtual_dim[n], self.numVecSample)) self.vecsRight[n] = np.ones((self.mps.virtual_dim[n+1], self.numVecSample))