def test_retagging(self): x = rand_tensor((2, 4), inds='ab', tags={'X', 'I0'}) y = rand_tensor((4, 2, 5), inds='bcd', tags={'Y', 'I1'}) z = rand_tensor((5, 3), inds='de', tags={'Z', 'I2'}) tn = TensorNetwork((x, y, z)) tn.retag_({"I0": "I1", "I1": "I2", "I2": "I3", "Z": "A"}) assert set(tn.tag_map.keys()) == {'X', 'I1', 'I2', 'I3', 'Y', 'A'}
def test_index_by_site(self): a_data = np.random.randn(2, 3, 4) b_data = np.random.randn(2, 3, 4) a = Tensor(a_data, inds='abc', tags={'I0'}) b = Tensor(b_data, inds='abc', tags={'I1'}) tn = TensorNetwork((a, b), structure="I{}") assert_allclose(tn.site[0].data, a_data) new_data = np.random.randn(2, 3, 4) tn.site[1] = Tensor(new_data, inds='abc', tags={'I1', 'red'}) assert_allclose(tn['I1'].data, new_data) assert 'red' in tn['I1'].tags
def test_copy_deep(self): a = rand_tensor((2, 3, 4), inds='abc', tags='t0') b = rand_tensor((2, 3, 4), inds='abd', tags='t1') tn1 = TensorNetwork((a, b)) tn2 = tn1.copy(deep=True) # check can modify tensor structure tn2['t1'].modify(inds=('a', 'b', 'X')) assert tn1['t1'] is not tn2['t1'] assert tn2['t1'].inds == ('a', 'b', 'X') assert tn1['t1'].inds == ('a', 'b', 'd') # and that data is not the same assert tn1['t1'].data is not tn2['t1'].data tn2['t1'].data[:] /= 2 assert_allclose(tn1['t1'].data / 2, tn2['t1'].data)
def test_copy(self): a = rand_tensor((2, 3, 4), inds='abc', tags='t0') b = rand_tensor((2, 3, 4), inds='abd', tags='t1') tn1 = TensorNetwork((a, b)) tn2 = tn1.copy() # check can modify tensor structure tn2['t1'].inds = ('a', 'b', 'X') assert tn1['t1'] is not tn2['t1'] assert tn2['t1'].inds == ('a', 'b', 'X') assert tn1['t1'].inds == ('a', 'b', 'd') # but that data remains the same assert tn1['t1'].data is tn2['t1'].data tn2['t1'].data /= 2 assert_allclose(tn1['t1'].data, tn2['t1'].data)
def test_combining_with_no_check_collisions(self): p1 = MPS_rand_state(5, 3, phys_dim=3) p2 = MPS_rand_state(5, 3, phys_dim=3) # shouldn't need to check any collisions tn = TensorNetwork((p1, p2), check_collisions=False) # test can contract assert 0 < abs(tn ^ ...) < 1
def test_TensorNetwork_init_checks(self): a = rand_tensor((2, 3, 4), inds=[0, 1, 2], tags={'red'}) b = rand_tensor((3, 4, 5), inds=[1, 2, 3], tags={'blue'}) c = rand_tensor((3, 4, 5), inds=[1, 2, 3], tags={'blue', 'c'}) with pytest.raises(TypeError): TensorNetwork(a, b) # missing brackets around ``a, b``. tn = a & b with pytest.raises(TypeError): tn['red'] = 1 tn.add_tag('foo') assert len(tn['foo']) == 2 with pytest.raises(KeyError): tn['foo'] = c tn[('foo', 'blue')] = c assert 'c' in tn.tags assert tn[('blue', 'c')] is c assert 'red' in tn.tags del tn['red'] assert 'red' not in tn.tags assert set(tn.tag_map.keys()) == {'blue', 'c'} tn.drop_tags('c') assert set(tn.tag_map.keys()) == {'blue'} tn.drop_tags(['blue']) assert set(tn.tag_map.keys()) == set()
def test_tensors_sorted(self): tn1, tn2 = TensorNetwork([]), TensorNetwork([]) A, B, C = (rand_tensor((1, 2, 3), 'abc', tags=['I0']), rand_tensor((2, 3, 4), 'bcd', tags=['I1']), rand_tensor((4, 1, 1), 'dae', tags=['I2'])) tn1 &= A tn1 &= B tn1 &= C tn2 &= C tn2 &= A tn2 &= B for t1, t2 in zip(tn1.tensors_sorted(), tn2.tensors_sorted()): assert t1.tags == t2.tags assert t1.almost_equals(t2)
def test_combining_tensors(self): a = rand_tensor((2, 3, 4), inds=[0, 1, 2], tags='red') b = rand_tensor((3, 4, 5), inds=[1, 2, 3], tags='blue') c = rand_tensor((5, 2, 6), inds=[3, 0, 4], tags='blue') with pytest.raises(TypeError): a & np.array([0, 0]) abc1 = (a & b & c).H.contract() abc2 = (a & (b & c)).H.contract() abc3 = (TensorNetwork([a, b, c])).H.contract() abc4 = (TensorNetwork([a, TensorNetwork([b, c])])).H.contract() abc5 = (TensorNetwork([a]) & TensorNetwork([b, c])).H.contract() assert_allclose(abc1.data, abc2.data) assert_allclose(abc1.data, abc3.data) assert_allclose(abc1.data, abc4.data) assert_allclose(abc1.data, abc5.data)
def test_set_data_in_tensor(self): a_data = np.random.randn(2, 3, 4) b_data = np.random.randn(2, 3, 4) a = Tensor(a_data, inds='abc', tags={'I0'}) b = Tensor(b_data, inds='abc', tags={'I1'}) tn = TensorNetwork((a, b), structure="I{}") assert_allclose(tn[0].data, a_data) new_data = np.random.randn(2, 3, 4) tn[1].modify(data=new_data) assert_allclose(tn['I1'].data, new_data)
def test_contract_with_slices(self): a = rand_tensor((2, 3, 4), inds=[0, 1, 2], tags='I0') b = rand_tensor((3, 4, 5), inds=[1, 2, 3], tags='I1') c = rand_tensor((5, 2, 6), inds=[3, 0, 4], tags='I2') d = rand_tensor((5, 2, 6), inds=[5, 6, 4], tags='I3') tn = TensorNetwork((a, b, c, d), structure="I{}") assert len((tn ^ slice(2)).tensors) == 3 assert len((tn ^ slice(..., 1, -1)).tensors) == 3 assert len((tn ^ slice(-1, 1)).tensors) == 3 assert len((tn ^ slice(None, -2, -1)).tensors) == 3 assert len((tn ^ slice(-2, 0)).tensors) == 3
def test_ownership(self): a = rand_tensor((2, 2), ('a', 'b'), tags={'X', 'Y'}) b = rand_tensor((2, 2), ('b', 'c'), tags={'X', 'Z'}) assert not a.check_owners() assert not b.check_owners() tn = TensorNetwork((a, b), virtual=True) assert a.check_owners() assert b.check_owners() assert a.owners[hash(tn)][0]() is tn assert b.owners[hash(tn)][0]() is tn assert all(map(tn.ind_map.__contains__, ('a', 'b', 'c'))) assert all(map(tn.tag_map.__contains__, ('X', 'Y', 'Z'))) a.reindex_({'a': 'd'}) assert 'a' not in tn.ind_map assert 'd' in tn.ind_map assert len(tn.tag_map['X']) == 2 b.retag_({'X': 'W'}) assert len(tn.tag_map['X']) == 1 assert 'W' in tn.tag_map del tn assert not a.check_owners() assert not b.check_owners()