コード例 #1
0
 def test_multiply_spread(self):
     a = rand_tensor([2, 2], inds=['a', 'b'], tags='A')
     b = Tensor(a.data, ['b', 'c'], tags='B')
     c = Tensor(a.data, ['c', 'd'], tags='C')
     tn = (a | b | c)
     tn.multiply_(-8j + 1 / 3, spread_over=3)
     assert_allclose(tn['A'].data, tn['B'].data)
     assert_allclose(tn['B'].data, tn['C'].data)
コード例 #2
0
 def test_contract_with_wild_mix(self):
     a = Tensor(np.random.randn(2, 3, 4), inds=['-1', 'a', 'foo'],
                tags='red')
     b = Tensor(np.random.randn(3, 4, 5), inds=['a', 'foo', '42.42'],
                tags='blue')
     c = a @ b
     assert c.shape == (2, 5)
     assert c.inds == ('-1', '42.42')
コード例 #3
0
 def test_tensor_deep_copy(self):
     a = Tensor(np.random.randn(2, 3, 4), inds=[0, 1, 2], tags='blue')
     b = a.copy(deep=True)
     b.add_tag('foo')
     assert 'foo' not in a.tags
     b.data[:] = b.data / 2
     # still reference the same underlying array
     assert_allclose(a.data / 2, b.data)
コード例 #4
0
 def test_contract_with_out_of_range_inds(self):
     a = Tensor(np.random.randn(2, 3, 4), inds=[-1, 100, 2200],
                tags='red')
     b = Tensor(np.random.randn(3, 4, 5), inds=[100, 2200, -3],
                tags='blue')
     c = a @ b
     assert c.shape == (2, 5)
     assert c.inds == (-1, -3)
コード例 #5
0
 def test_contract_with_legal_characters(self):
     a = Tensor(np.random.randn(2, 3, 4), inds='abc',
                tags='red')
     b = Tensor(np.random.randn(3, 4, 5), inds='bcd',
                tags='blue')
     c = a @ b
     assert c.shape == (2, 5)
     assert c.inds == ('a', 'd')
コード例 #6
0
    def test_tensor_construct(self):
        x = np.random.randn(2, 3, 4)
        a = Tensor(x, inds=[0, 1, 2], tags='blue')
        assert_allclose(a.H.data, x.conj())
        assert a.size == 24

        with pytest.raises(ValueError):
            Tensor(x, inds=[0, 2], tags='blue')
コード例 #7
0
    def test_tensor_transpose(self):
        a = Tensor(np.random.rand(2, 3, 4, 5, 2, 2), 'abcdef', tags={'blue'})
        at = a.transpose(*'cdfeba')
        assert at.shape == (4, 5, 2, 2, 3, 2)
        assert at.inds == ('c', 'd', 'f', 'e', 'b', 'a')

        with pytest.raises(ValueError):
            a.transpose(*'cdfebz')
コード例 #8
0
ファイル: test_tensor_core.py プロジェクト: caidish/quimb
 def test_tensor_copy(self):
     a = Tensor(np.random.randn(2, 3, 4), inds=[0, 1, 2], tags='blue')
     b = a.copy()
     b.tags.add('foo')
     assert 'foo' not in a.tags
     b.data /= 2
     # still reference the same underlying array
     assert_allclose(a.data, b.data)
コード例 #9
0
 def test_multi_contract(self):
     a = Tensor(np.random.randn(2, 3, 4), inds=[0, 1, 2], tags='red')
     b = Tensor(np.random.randn(3, 4, 5), inds=[1, 2, 3], tags='blue')
     c = Tensor(np.random.randn(5, 2, 6), inds=[3, 0, 4], tags='blue')
     d = tensor_contract(a, b, c)
     assert isinstance(d, Tensor)
     assert d.shape == (6, )
     assert d.inds == (4, )
     assert d.tags == {'red', 'blue'}
コード例 #10
0
 def test_multiply_spread_neg_stays_real(self):
     a = rand_tensor([2, 2], inds=['a', 'b'], tags='A', dtype='float32')
     b = Tensor(a.data, ['b', 'c'], tags='B')
     c = Tensor(a.data, ['c', 'd'], tags='C')
     tn = (a | b | c)
     tn.multiply_(-1000)
     assert a.dtype == b.dtype == c.dtype == 'float32'
     assert_allclose(abs(tn['A'].data), abs(tn['B'].data))
     assert_allclose(abs(tn['B'].data), abs(tn['C'].data))
コード例 #11
0
 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)
コード例 #12
0
 def test_tensor_tensor_arithmetic(self, op, mismatch):
     a = Tensor(np.random.rand(2, 3, 4), inds=[0, 1, 2], tags='blue')
     b = Tensor(np.random.rand(2, 3, 4), inds=[0, 1, 2], tags='red')
     if mismatch:
         b.modify(inds=(0, 1, 3))
         with pytest.raises(ValueError):
             op(a, b)
     else:
         c = op(a, b)
         assert_allclose(c.data, op(a.data, b.data))
コード例 #13
0
    def test_fuse(self):
        a = Tensor(np.random.rand(2, 3, 4, 5), 'abcd', tags={'blue'})
        b = a.fuse({'bra': ['a', 'c'], 'ket': 'bd'})
        assert set(b.shape) == {8, 15}
        assert set(b.inds) == {'bra', 'ket'}
        assert b.tags == {'blue'}

        b = a.fuse({'ket': 'bd', 'bra': 'ac'})
        assert set(b.shape) == {15, 8}
        assert set(b.inds) == {'ket', 'bra'}
        assert b.tags == {'blue'}
コード例 #14
0
 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[0].data, a_data)
     new_data = np.random.randn(2, 3, 4)
     tn[1] = Tensor(new_data, inds='abc', tags={'I1', 'red'})
     assert_allclose(tn['I1'].data, new_data)
     assert 'red' in tn['I1'].tags
コード例 #15
0
    def test_contract_some(self):
        a = Tensor(np.random.randn(2, 3, 4), inds=[0, 1, 2])
        b = Tensor(np.random.randn(3, 4, 5), inds=[1, 2, 3])

        assert a.shared_bond_size(b) == 12

        c = a @ b

        assert isinstance(c, Tensor)
        assert c.shape == (2, 5)
        assert c.inds == (0, 3)
コード例 #16
0
    def test_contract_None(self):
        a = Tensor(np.random.randn(2, 3, 4), inds=[0, 1, 2])
        b = Tensor(np.random.randn(3, 4, 5), inds=[3, 4, 5])
        c = a @ b
        assert c.shape == (2, 3, 4, 3, 4, 5)
        assert c.inds == (0, 1, 2, 3, 4, 5)

        a = Tensor(np.random.randn(2, 3, 4), inds=[0, 1, 2])
        b = Tensor(np.random.randn(3, 4, 5), inds=[5, 4, 3])
        c = a @ b

        assert c.shape == (2, 3, 4, 3, 4, 5)
        assert c.inds == (0, 1, 2, 5, 4, 3)
コード例 #17
0
    def test_conj_inplace(self):
        a_data = np.random.randn(2, 3, 4) + 1.0j * np.random.randn(2, 3, 4)
        b_data = np.random.randn(3, 4, 5) + 1.0j * np.random.randn(3, 4, 5)
        c_data = np.random.randn(5, 2, 6) + 1.0j * np.random.randn(5, 2, 6)

        a = Tensor(a_data, inds=[0, 1, 2], tags={'red', 'I0'})
        b = Tensor(b_data, inds=[1, 2, 3], tags={'blue', 'I1'})
        c = Tensor(c_data, inds=[3, 0, 4], tags={'blue', 'I2'})

        tn = a & b & c
        tn.conj_()

        for i, arr in enumerate((a_data, b_data, c_data)):
            assert_allclose(tn["I{}".format(i)].data, arr.conj())
コード例 #18
0
    def test_with_alpha_construct(self):
        x = np.random.randn(2, 3, 4)
        a = Tensor(x, inds='ijk', tags='blue')
        assert_allclose(a.H.data, x.conj())
        assert a.size == 24

        with pytest.raises(ValueError):
            Tensor(x, inds='ij', tags='blue')

        x = np.random.randn(2, 3, 4)
        a = Tensor(x, inds=['a1', 'b2', 'c3'], tags='blue')
        assert_allclose(a.H.data, x.conj())
        assert a.size == 24

        with pytest.raises(ValueError):
            Tensor(x, inds=['ijk'], tags='blue')
コード例 #19
0
def gate_split_mpo_like(TG: qtn.Tensor, up_inds, down_inds):
    '''Split a 3-body gate into MPO-like chain.

    -TG: qtn.Tensor
        Tensor operator to split
    -up_inds: ia, ib, ic
        (Ordered) upper indices for sites a,b,c
    -down_inds: ja, jb, jc
        (Ordered) lower indices on sites a, b, c
    
    Returns:
    -(T_a, T_b, T_c): sequence of tensors, 1 for each site
    
    #   
    #        ia ib ic       ia    ib   ic
    #          \│╱           │    │    │  
    #    G  =   ●     ==>    ●────●────●
    #          ╱|\           │    │    │     
    #        ja jb jc       ja   jb    jc  
    # 
    '''
    ia, ib, ic = up_inds
    ja, jb, jc = down_inds

    T_a, V = TG.split(left_inds=(ia, ja), method='qr', get='tensors')
    T_b, T_c = V.split(left_inds=(ib, jb),
                       right_inds=(ic, jc),
                       method='qr',
                       get='tensors')
    return T_a, T_b, T_c
コード例 #20
0
    def test_conj(self):
        a_data = np.random.randn(2, 3, 4) + 1.0j * np.random.randn(2, 3, 4)
        b_data = np.random.randn(3, 4, 5) + 1.0j * np.random.randn(3, 4, 5)
        c_data = np.random.randn(5, 2, 6) + 1.0j * np.random.randn(5, 2, 6)

        a = Tensor(a_data, inds=[0, 1, 2], tags={'red', '0'})
        b = Tensor(b_data, inds=[1, 2, 3], tags={'blue', '1'})
        c = Tensor(c_data, inds=[3, 0, 4], tags={'blue', '2'})

        tn = a & b & c
        new_tn = tn.conj()

        for i, arr in enumerate((a_data, b_data, c_data)):
            assert_allclose(new_tn[str(i)].data, arr.conj())

        # make sure original network unchanged
        for i, arr in enumerate((a_data, b_data, c_data)):
            assert_allclose(tn[str(i)].data, arr)
コード例 #21
0
ファイル: test_tensor_core.py プロジェクト: zeta1999/quimb
 def test_diagonal_reduce(self):
     A = rand_tensor([2, 2], 'ab', dtype=complex)
     B = Tensor([[3j, 0.], [0., 4j]], 'bc')
     C = rand_tensor([2, 2], 'ca', dtype=complex)
     tn = A & B & C
     tn_s = tn.diagonal_reduce()
     assert tn.num_indices == 3
     assert tn_s.num_indices == 2
     assert tn ^ all == pytest.approx(tn_s.contract(all, output_inds=[]))
コード例 #22
0
    def test_reindex(self):
        a = Tensor(np.random.randn(2, 3, 4), inds=[0, 1, 2], tags='red')
        b = Tensor(np.random.randn(3, 4, 5), inds=[1, 2, 3], tags='blue')
        c = Tensor(np.random.randn(5, 2, 6), inds=[3, 0, 4], tags='green')

        a_b_c = (a & b & c)

        d = a_b_c.reindex({4: 'foo', 2: 'bar'})

        assert a_b_c.outer_inds() == (4, )
        assert d.outer_inds() == ('foo', )
        assert set(a_b_c.inner_inds()) == {0, 1, 2, 3}
        assert set(d.inner_inds()) == {0, 1, 'bar', 3}
        assert d.tensors[0].inds == (0, 1, 'bar')

        d = a_b_c.reindex_({4: 'foo', 2: 'bar'})

        assert a_b_c.outer_inds() == ('foo', )
        assert set(d.inner_inds()) == {0, 1, 'bar', 3}
        assert d.tensors[0].inds == (0, 1, 'bar')
コード例 #23
0
 def test_arithmetic_scalar(self):
     x = np.random.randn(2, 3, 4)
     a = Tensor(x, inds=[0, 1, 2], tags='blue')
     assert_allclose((a + 2).data, x + 2)
     assert_allclose((a - 3).data, x - 3)
     assert_allclose((a * 4).data, x * 4)
     assert_allclose((a / 5).data, x / 5)
     assert_allclose((a**2).data, x**2)
     assert_allclose((2 + a).data, 2 + x)
     assert_allclose((3 - a).data, 3 - x)
     assert_allclose((4 * a).data, 4 * x)
     assert_allclose((5 / a).data, 5 / x)
     assert_allclose((5**a).data, 5**x)
コード例 #24
0
ファイル: test_tensor_core.py プロジェクト: zeta1999/quimb
 def test_antidiag_gauge(self):
     A = rand_tensor([2, 2], 'ab', dtype=complex)
     B = Tensor([[0., 3j], [4j, 0.]], 'bc')
     C = rand_tensor([2, 2], 'ca', dtype=complex)
     tn = A & B & C
     assert tn.num_indices == 3
     # can't use diagonal reduction yet
     assert tn.diagonal_reduce().num_indices == 3
     # initial gauge doesn't change indices
     tn_a = tn.antidiag_gauge()
     assert tn_a.num_indices == 3
     # but allows the diagonal reduction
     tn_ad = tn_a.diagonal_reduce()
     assert tn_ad.num_indices == 2
     assert tn ^ all == pytest.approx(tn_ad.contract(all, output_inds=[]))
コード例 #25
0
 def test_fuse_leftover(self):
     a = Tensor(np.random.rand(2, 3, 4, 5, 2, 2), 'abcdef', tags={'blue'})
     b = a.fuse({'bra': 'ac', 'ket': 'bd'})
     assert b.shape == (8, 15, 2, 2)
     assert b.inds == ('bra', 'ket', 'e', 'f')
     assert b.tags == {'blue'}
コード例 #26
0
 def test_tensor_conj_inplace(self):
     data = np.random.rand(2, 3, 4) + 1.0j * np.random.rand(2, 3, 4)
     a = Tensor(data, inds=[0, 1, 2], tags='blue')
     a.conj_()
     assert_allclose(data.conj(), a.data)
コード例 #27
0
 def test_contract_all(self):
     a = Tensor(np.random.randn(2, 3, 4), inds=[0, 1, 2])
     b = Tensor(np.random.randn(3, 4, 2), inds=[1, 2, 0])
     c = a @ b
     assert isinstance(c, float)
     assert not isinstance(c, Tensor)
コード例 #28
0
 def test_raise_on_triple_inds(self):
     a = Tensor(np.random.randn(2, 3, 4), inds=[0, 1, 2])
     b = Tensor(np.random.randn(3, 4, 5), inds=[1, 1, 2])
     with pytest.raises(ValueError):
         a @ b
コード例 #29
0
 def test_singular_values(self, method):
     psim = Tensor(np.eye(2) * 2**-0.5, inds='ab')
     assert_allclose(psim.H @ psim, 1.0)
     assert_allclose(
         psim.singular_values('a', method=method)**2, [0.5, 0.5])
コード例 #30
0
 def test_entropy(self, method):
     psim = Tensor(np.eye(2) * 2**-0.5, inds='ab')
     assert_allclose(psim.H @ psim, 1.0)
     assert_allclose(psim.entropy('a', method=method)**2, 1)