def test_canonize_cyclic(self, dtype, block): k = MPS_rand_state(40, 10, dtype=dtype, cyclic=True) b = k.H k.add_tag('KET') b.add_tag('BRA') kb = (b | k) assert not np.allclose(k[block].H @ k[block], 1.0) assert not np.allclose(b[block].H @ b[block], 1.0) k.canonize_cyclic(block, bra=b) assert_allclose(k[block].H @ k[block], 1.0, rtol=2e-4) assert_allclose(b[block].H @ b[block], 1.0, rtol=2e-4) ii = kb.select(block, which='!any') ^ all if isinstance(block, slice): start, stop = block.start, block.stop else: start, stop = block, block + 1 assert len(kb.select_tensors(block, 'any')) == 2 * (stop - start) ul, = bonds(kb[k.site_tag(start - 1), 'BRA'], kb[k.site_tag(start), 'BRA']) ur, = bonds(kb[k.site_tag(stop - 1), 'BRA'], kb[k.site_tag(stop), 'BRA']) ll, = bonds(kb[k.site_tag(start - 1), 'KET'], kb[k.site_tag(start), 'KET']) lr, = bonds(kb[k.site_tag(stop - 1), 'KET'], kb[k.site_tag(stop), 'KET']) ii = ii.to_dense((ul, ur), (ll, lr)) assert_allclose(ii, np.eye(ii.shape[0]), rtol=0.001, atol=0.001)
def test_replace_with_svd_using_linear_operator(self, dtype, method): k = MPS_rand_state(100, 10, dtype=dtype, cyclic=True) b = k.H b.expand_bond_dimension(11) k.add_tag('_KET') b.add_tag('_BRA') tn = b & k x1 = tn ^ ... ul, = tn['_KET', 'I1'].bonds(tn['_KET', 'I2']) ll, = tn['_BRA', 'I1'].bonds(tn['_BRA', 'I2']) where = ['I{}'.format(i) for i in range(2, 40)] tn.replace_with_svd(where, left_inds=(ul, ll), eps=1e-3, method=method, inplace=True, ltags='_U', rtags='_V') tn.structure = None x2 = tn ^ ... # check ltags and rtags have gone in assert isinstance(tn['_U'], Tensor) assert isinstance(tn['_V'], Tensor) assert_allclose(x1, x2, rtol=1e-4)
def test_can_change_data_using_subnetwork(self): p = MPS_rand_state(3, 10) pH = p.H p.add_tag('__ket__') pH.add_tag('__bra__') tn = p | pH assert_allclose((tn ^ ...), 1) assert_allclose(tn[('__ket__', 'I1')].data, tn[('__bra__', 'I1')].data.conj()) p[1].modify(data=np.random.randn(10, 10, 2)) assert abs((tn ^ ...) - 1) > 1e-13 assert not np.allclose(tn[('__ket__', 'I1')].data, tn[('__bra__', 'I1')].data.conj())
def test_partition(self): k = MPS_rand_state(10, 7, site_tag_id='Q{}', structure_bsz=4) where = ['Q{}'.format(i) for i in range(10) if i % 2 == 1] k.add_tag('odd', where=where, which='any') tn_even, tn_odd = k.partition('odd') assert len(tn_even.tensors) == len(tn_odd.tensors) == 5 assert tn_even.structure == 'Q{}' assert tn_even.structure_bsz == 4 assert tn_odd.structure == 'Q{}' assert tn_odd.structure_bsz == 4 assert (tn_even & tn_odd).sites == range(10)