def test_mt_vectorize(): from representability.tensor import index_tuple_basis from representability.tensor import Bijection a = np.arange(16).reshape((4, 4), order='C') print(a) basis = [(0, 0), (0, 1), (1, 0), (1, 1)] basis = index_tuple_basis(basis) ta = Tensor(a, basis=basis, name='a') assert np.allclose(ta.data, a) assert ta.size == 16 assert isinstance(ta.basis, Bijection) b = np.arange(16, 16 + 36).reshape((6, 6), order='C') print(b) basis = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 2), (2, 3)] basis = index_tuple_basis(basis) tb = Tensor(b, basis=basis, name='b') mt = MultiTensor([ta, tb]) dbe = DualBasisElement() dbe.add_element('a', (0, 0, 0, 1), 1.0) # 1 dbe.add_element('b', (0, 1, 0, 2), 1.0) # 24 assert mt.off_set_map == {'a': 0, 'b': 16} mt_vec_idx, _ = mt.synthesize_element(dbe) assert mt_vec_idx[0] == 1 assert mt_vec_idx[1] == 24
def test_tensor_index(): """ Testing the index_vectorized which is mapped through the basis :return: """ a = np.arange(16).reshape((4, 4), order='C') print(a) basis = [(0, 0), (0, 1), (1, 0), (1, 1)] basis = index_tuple_basis(basis) tt = Tensor(a, basis=basis) assert np.allclose(tt.data, a) assert tt.size == 16 assert isinstance(tt.basis, Bijection) assert np.isclose(tt.index_vectorized(0, 0, 0, 0), 0) assert np.isclose(tt.index_vectorized(0, 0, 0, 1), 1) assert np.isclose(tt.index_vectorized(0, 0, 1, 0), 2) assert np.isclose(tt.index_vectorized(0, 0, 1, 1), 3) assert np.isclose(tt.index_vectorized(0, 1, 0, 0), 4) assert np.isclose(tt.index_vectorized(0, 1, 0, 1), 5) assert np.isclose(tt.index_vectorized(0, 1, 1, 0), 6) assert np.isclose(tt.index_vectorized(0, 1, 1, 1), 7) assert np.isclose(tt.index_vectorized(1, 0, 0, 0), 8) assert np.isclose(tt.index_vectorized(1, 0, 0, 1), 9) assert np.isclose(tt.index_vectorized(1, 0, 1, 0), 10) assert np.isclose(tt.index_vectorized(1, 0, 1, 1), 11) assert np.isclose(tt.index_vectorized(1, 1, 0, 0), 12) assert np.isclose(tt.index_vectorized(1, 1, 0, 1), 13) assert np.isclose(tt.index_vectorized(1, 1, 1, 0), 14) assert np.isclose(tt.index_vectorized(1, 1, 1, 1), 15) # etc... a = np.arange(16).reshape((4, 4), order='C') tt = Tensor(a) # the canonical basis assert np.isclose(tt.index_vectorized(0, 0), 0) assert np.isclose(tt.index_vectorized(0, 1), 1) assert np.isclose(tt.index_vectorized(0, 2), 2) assert np.isclose(tt.index_vectorized(0, 3), 3) assert np.isclose(tt.index_vectorized(1, 0), 4) assert np.isclose(tt.index_vectorized(1, 1), 5) assert np.isclose(tt.index_vectorized(1, 2), 6) assert np.isclose(tt.index_vectorized(1, 3), 7) b = np.arange(16, 16 + 36).reshape((6, 6), order='C') print(b) basis = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 2), (2, 3)] basis = index_tuple_basis(basis) tb = Tensor(b, basis=basis)
def test_geminal_basis(): gems = list(product(range(5), repeat=2)) b = index_tuple_basis(gems) assert b.fwd(4) == (0, 4) assert b.rev((0, 4)) == 4 assert b.rev(b.fwd(4)) == 4 assert b.domain_element_sizes() == (1, 2)
def geminal_spin_basis(m_dim): """ Construct the geminal basis blocked by Sz D2 and Q2 have the same aa, bb, ab basis structure. :param Int m_dim: rank of the single particle position space basis (NOT SPIN ORBITAL RANK) :return: Basis Bijections :rtype: representability.tensor.Bijection """ # basis construction for canonical matrices gem_aa_bas = [] gem_ab_bas = [] for i in range(m_dim): for j in range(m_dim): if i < j: gem_aa_bas.append((i, j)) gem_ab_bas.append((i, j)) return index_tuple_basis(gem_aa_bas), index_tuple_basis(gem_ab_bas)
def triples_spin_orbital_antisymm_basis(m_dim): """ Construct the triples spin basis :param Int m_dim: :return: representbaility.tensor.Bijection """ bas = [] for p, q, r in product(range(m_dim), repeat=3): if p < q < r: bas.append((p, q, r)) return index_tuple_basis(bas)
def test_tensor_basis(): """ make a matrix that has a different basis than indexing """ n = 4 dim = int(n * (n - 1) / 2) geminals = [] bas = {} cnt = 0 for i in range(4): for j in range(i + 1, 4): bas[cnt] = (i, j) geminals.append((i, j)) cnt += 1 rev_bas = dict(zip(bas.values(), bas.keys())) rand_mat = np.random.random((dim, dim)) basis_bijection = index_tuple_basis(geminals) test_tensor = Tensor(rand_mat, basis=basis_bijection) assert test_tensor.basis.fwd(0) == (0, 1) assert test_tensor.basis.fwd(2) == (0, 3) assert test_tensor.basis.rev(test_tensor.basis.fwd(5)) == 5 assert test_tensor.ndim == 2 assert test_tensor.dim == dim # index into data directly assert test_tensor[2, 3] == rand_mat[2, 3] # index into data via basis indexing assert test_tensor(0, 1, 0, 1) == rand_mat[0, 0] assert test_tensor(1, 2, 0, 1) == rand_mat[rev_bas[(1, 2)], rev_bas[(0, 1)]] assert test_tensor.index_vectorized(1, 2, 0, 1) == rev_bas[(1, 2)] * dim + \ rev_bas[(0, 1)] # testing iteration over the upper triangle for iter_vals in test_tensor.utri_iterator(): val, [i, j] = iter_vals assert val == rand_mat[test_tensor.basis.rev(i), test_tensor.basis.rev(j)]
def d2_q2_mapping(dim): """ Map each d2 block to the q2 block :param dim: rank of spatial single-particle basis :return: """ krond = np.eye(dim) def d2q2element(p, q, r, s, factor, tname_d1_1, tname_d2, tname_q2): # , spin_string): """ # ( 1.00000) cre(r) cre(s) des(q) des(p) # ( 1.00000) kdelta(p,s) cre(r) des(q) # ( -1.00000) kdelta(p,r) cre(s) des(q) # ( -1.00000) kdelta(q,s) cre(r) des(p) # ( 1.00000) kdelta(q,r) cre(s) des(p) # ( -1.00000) kdelta(p,s) kdelta(q,r) # ( 1.00000) kdelta(p,r) kdelta(q,s) """ dbe = DualBasisElement() dbe.add_element(tname_q2, (p, q, r, s), -factor) dbe.add_element(tname_d2, (r, s, p, q), factor) if p == s: dbe.add_element(tname_d1_1, (r, q), factor) if p == r: dbe.add_element(tname_d1_1, (s, q), -factor) if q == s: dbe.add_element(tname_d1_1, (r, p), -factor) if q == r: dbe.add_element(tname_d1_1, (s, p), factor) # remember the negative sign because AX = b dbe.dual_scalar = -factor * (krond[p, r] * krond[q, s] - krond[p, s] * krond[q, r]) # dbe.add_element('kkcc_' + spin_string + spin_string, (r, s, p, q), factor) # if q == s: # dbe.add_element('ck_' + spin_string, (p, r), factor) # if p == s: # dbe.add_element('ck_' + spin_string, (q, r), -factor) # if q == r: # dbe.add_element('kc_' + spin_string, (s, p), factor) # if p == r: # dbe.add_element('kc_' + spin_string, (s, q), -factor) # dbe.add_element('cckk_' + spin_string + spin_string, (p, q, r, s), -factor) # dbe.dual_scalar = 0 return dbe def d2q2element_ab(p, q, r, s, factor): dbe = DualBasisElement() if q == s: dbe.add_element('ck_a', (p, r), factor) if p == r: dbe.add_element('kc_b', (s, q), -factor) dbe.add_element('kkcc_ab', (r, s, p, q), factor) dbe.add_element('cckk_ab', (p, q, r, s), -factor) # dbe.dual_scalar = -krond[q, s]*krond[p, r] * factor dbe.dual_scalar = 0 return dbe dual_basis_list = [] # for p, q, r, s in product(range(dim), repeat=4): from representability.tensor import index_tuple_basis gem_aa = [] gem_ab = [] for p, q in product(range(dim), repeat=2): if p < q: gem_aa.append((p, q)) gem_ab.append((p, q)) bas_aa = index_tuple_basis(gem_aa) bas_ab = index_tuple_basis(gem_ab) for i, j in product(range(dim * (dim - 1) // 2), repeat=2): if i >= j: p, q = bas_aa.fwd(i) r, s = bas_aa.fwd(j) # if p < q and r < s and p * dim + q < r * dim + s: dbe1 = d2q2element(p, q, r, s, 0.5, 'ck_a', 'cckk_aa', 'kkcc_aa') dbe2 = d2q2element(r, s, p, q, 0.5, 'ck_a', 'cckk_aa', 'kkcc_aa') # dbe1 = d2q2element(p, q, r, s, 0.5, 'a') # dbe2 = d2q2element(r, s, p, q, 0.5, 'a') dual_basis_list.append(dbe1.join_elements(dbe2)) # dbe1 = d2q2element(p, q, r, s, 0.5, 'b') # dbe2 = d2q2element(r, s, p, q, 0.5, 'b') dbe1 = d2q2element(p, q, r, s, 0.5, 'ck_b', 'cckk_bb', 'kkcc_bb') dbe2 = d2q2element(r, s, p, q, 0.5, 'ck_b', 'cckk_bb', 'kkcc_bb') dual_basis_list.append(dbe1.join_elements(dbe2)) # # if p < q and r < s and p == r and q == s: # # dbe1 = d2q2element(p, q, r, s, 1., 'a') # dbe = d2q2element(p, q, r, s, 1., 'ck_a', 'cckk_aa', 'kkcc_aa') # dual_basis_list.append(dbe) # # # dbe1 = d2q2element(p, q, r, s, 1., 'b') # dbe = d2q2element(p, q, r, s, 1., 'ck_b', 'cckk_bb', 'kkcc_bb') # dual_basis_list.append(dbe) for i, j in product(range(dim * dim), repeat=2): if i >= j: p, q = bas_ab.fwd(i) r, s = bas_ab.fwd(j) # if p * dim + q <= r * dim + s: dbe1 = d2q2element_ab(p, q, r, s, 0.5) dbe2 = d2q2element_ab(r, s, p, q, 0.5) dual_basis_list.append(dbe1.join_elements(dbe2)) return DualBasis(elements=dual_basis_list)