def get_eigen_pot(self, fin_nd_list): """ This function first marginalizes self to fin_nd_list. Then it reshapes that marginal to a square array, and calculates the eigenvalues of that square array. Finally, it returns a Potential pot such that pot.pot_arr contains the eigenvalues as an array with shape = [x.size for x in fin_nd_list]. Parameters ---------- fin_nd_list : list[BayesNode] Returns ------- Potential """ assert self.nodes >= set(fin_nd_list) sub_dmat = DensityMatrix.new_from_tr_of_mixed_st(self, fin_nd_list) arr = sub_dmat.dmat_arr shp = sub_dmat.nd_sizes num_rows = np.prod(np.array(shp)) arr = np.reshape(arr, (num_rows, num_rows)) evals = np.linalg.eigvalsh(arr) evals = np.reshape(evals, shp) return Potential(False, fin_nd_list, pot_arr=evals)
def set_pot_to_one(self, is_quantum): """ Sets self.potential to one. Needs to know is_quantum to decide whether to use a numpy array with float64 or complex128 entries. Sets pot to one over all states, not just the active ones. Parameters ---------- is_quantum : bool Returns ------- None """ # insert subnodes into pot in arbitrary order self.potential = Potential(is_quantum, list(self.subnodes), bias=1)
def main(): # define some nodes a_nd = BayesNode(0, name="A", size=2) b_nd = BayesNode(1, name="B", size=3) c_nd = BayesNode(2, name="C", size=2) d_nd = BayesNode(3, name="D", size=3) print('----------------------classical case') pot = Potential(False, [a_nd, b_nd, c_nd, d_nd]) pot.set_to_random() # this normalizes so all entries sum to 1 pot /= pot.get_new_marginal([]) assert pot.is_joint_prob_dist() Entropy.ent_c(pot, [a_nd], verbose=True) Entropy.ent_c(pot, [a_nd, c_nd], verbose=True) Entropy.ent_c(pot, [c_nd, a_nd], verbose=True) Entropy.cond_info_c(pot, [a_nd, b_nd], [c_nd], verbose=True) Entropy.mut_info_c(pot, [a_nd, b_nd], [c_nd], verbose=True) Entropy.cond_mut_info_c(pot, [a_nd], [b_nd, d_nd], [c_nd], verbose=True) print('----------------------quantum case') dmat = DensityMatrix([a_nd, b_nd, c_nd, d_nd]) # dmat = DensityMatrix([a_nd, b_nd]) dmat.set_to_random(normalize=True) # print(dmat) assert dmat.is_legal_dmat() Entropy.ent_q(dmat, [a_nd], verbose=True) Entropy.ent_q(dmat, [a_nd, c_nd], verbose=True) Entropy.ent_q(dmat, [c_nd, a_nd], verbose=True) Entropy.cond_info_q(dmat, [a_nd, b_nd], [c_nd], verbose=True) Entropy.mut_info_q(dmat, [a_nd, b_nd], [c_nd], verbose=True) Entropy.cond_mut_info_q(dmat, [a_nd], [b_nd, d_nd], [c_nd], verbose=True)
new_abc = cp.deepcopy(rho_abc) new_abc.set_to_transpose([b_node, a_node, c_node]) new_abc -= rho_bc assert new_abc == rho_abc - rho_bc print("\n-----------------try tracing ops and normalize") new_abc = cp.deepcopy(rho_abc) new_abc.tr_normalize_self() # print('new_abc', new_abc) print('tr new_abc', new_abc.trace()) fin_nd_list = [b_node, a_node] new_ba = DensityMatrix.new_from_tr_of_mixed_st(new_abc, fin_nd_list) print("new_ba", new_ba) new_a = DensityMatrix.new_from_tr_of_mixed_st(new_ba, [a_node]) print('new_a', new_a) print("\n**************************") pot = Potential(True, [a_node, b_node, c_node]) pot.set_to_random() new_bc = DensityMatrix.new_from_tr_of_pure_st(pot, [b_node, c_node]) new_bc.tr_normalize_self() print("from this pot", pot) print("_________") print("new_bc", new_bc) new_c = DensityMatrix.new_from_tr_of_mixed_st(new_bc, [c_node]) print('new_c', new_c)
def main(): # define some nodes a_node = BayesNode(0, name="A", size=2) b_node = BayesNode(1, name="B", size=3) c_node = BayesNode(2, name="C", size=2) d_node = BayesNode(3, name="D", size=3) e_node = BayesNode(4, name="E", size=2) print("\n-----------------define some density matrices") rho_ab = DensityMatrix([a_node, b_node]) print("rho_ab:", rho_ab) rho_ab.set_to_random(max_int=10) print("rho_ab:", rho_ab) rho_ab2 = DensityMatrix([a_node, b_node]) rho_bc = DensityMatrix([b_node, c_node]) rho_abc = DensityMatrix([a_node, b_node, c_node]) print("rho_abc:", rho_abc) rho_abc.set_all_entries_to(2 + 5j) print("rho_abc:", rho_abc) rho_abc.set_to_random() rho_dbc = DensityMatrix([d_node, b_node, c_node]) rho_dbc.set_to_random() print("\n-----------------try transpose, distance and ==") new_abc = cp.deepcopy(rho_abc) new_abc.set_to_transpose([b_node, a_node, c_node]) assert rho_abc == new_abc assert rho_abc != (new_abc + 5) print("distance(rho_abc, new_abc)=", DensityMatrix.distance(new_abc, rho_abc)) print("rho_abc == new_abc?", rho_abc == new_abc) print("distance(rho_abc, rho_bc)=", DensityMatrix.distance(rho_abc, rho_bc)) print("rho_abc == rho_bc?", rho_abc == rho_bc) print("\n-----------------try add, sub, mult") print("rho_ab:", rho_ab) print("rho_ab2:", rho_ab2) print("rho_ab + 5:", rho_ab + 5) print("rho_ab - 5:", rho_ab - 5) print("rho_ab * 5:", rho_ab * 5) print("rho_ab + rho_ab2:", rho_ab + rho_ab2) print("rho_ab - rho_ab2:", rho_ab - rho_ab2) print("rho_ab * rho_ab2:", rho_ab * rho_ab2) print("rho_ab + rho_bc:", rho_ab + rho_bc) print("rho_ab - rho_bc:", rho_ab - rho_bc) print("rho_ab * rho_bc:", rho_ab * rho_bc) print("\n-----------------try iadd, isub") new_abc = cp.deepcopy(rho_abc) new_abc.set_to_transpose([b_node, a_node, c_node]) new_abc += 5 assert new_abc == rho_abc + 5 new_abc = cp.deepcopy(rho_abc) new_abc.set_to_transpose([b_node, a_node, c_node]) new_abc += rho_bc assert new_abc == rho_abc + rho_bc new_abc = cp.deepcopy(rho_abc) new_abc.set_to_transpose([b_node, a_node, c_node]) new_abc -= rho_bc assert new_abc == rho_abc - rho_bc print("\n-----------------try tracing ops and normalize") new_abc = cp.deepcopy(rho_abc) new_abc.tr_normalize_self() # print('new_abc', new_abc) print('tr new_abc', new_abc.trace()) fin_nd_list = [b_node, a_node] new_ba = DensityMatrix.new_from_tr_of_mixed_st(new_abc, fin_nd_list) print("new_ba", new_ba) new_a = DensityMatrix.new_from_tr_of_mixed_st(new_ba, [a_node]) print('new_a', new_a) print("\n**************************") pot = Potential(True, [a_node, b_node, c_node]) pot.set_to_random() new_bc = DensityMatrix.new_from_tr_of_pure_st(pot, [b_node, c_node]) new_bc.tr_normalize_self() print("from this pot", pot) print("_________") print("new_bc", new_bc) new_c = DensityMatrix.new_from_tr_of_mixed_st(new_bc, [c_node]) print('new_c', new_c)
""" xyz_nds = x_nds + y_nds + z_nds pot = dmat.get_eigen_pot(xyz_nds) return Entropy.cond_mut_info_c(pot, x_nds, y_nds, z_nds, verbose) if __name__ == "__main__": # define some nodes a_nd = BayesNode(0, name="A", size=2) b_nd = BayesNode(1, name="B", size=3) c_nd = BayesNode(2, name="C", size=2) d_nd = BayesNode(3, name="D", size=3) print('----------------------classical case') pot = Potential(False, [a_nd, b_nd, c_nd, d_nd]) pot.set_to_random() pot /= pot.get_new_marginal([]) # this normalizes so all entries sum to 1 assert pot.is_joint_prob_dist() Entropy.ent_c(pot, [a_nd], verbose=True) Entropy.ent_c(pot, [a_nd, c_nd], verbose=True) Entropy.ent_c(pot, [c_nd, a_nd], verbose=True) Entropy.cond_info_c(pot, [a_nd, b_nd], [c_nd], verbose=True) Entropy.mut_info_c(pot, [a_nd, b_nd], [c_nd], verbose=True) Entropy.cond_mut_info_c(pot, [a_nd], [b_nd, d_nd], [c_nd], verbose=True) print('----------------------quantum case') dmat = DensityMatrix([a_nd, b_nd, c_nd, d_nd]) # dmat = DensityMatrix([a_nd, b_nd]) dmat.set_to_random(normalize=True) # print(dmat)