Esempio n. 1
0
 def test_from_dense(self):
     psi = qu.rand_ket(2**8)
     mps = MatrixProductState.from_dense(psi, dims=[2] * 8)
     assert mps.tags == {'I{}'.format(i) for i in range(8)}
     assert mps.site_inds == tuple('k{}'.format(i) for i in range(8))
     assert mps.nsites == 8
     mpod = mps.to_dense()
     assert qu.expec(mpod, psi) == pytest.approx(1)
Esempio n. 2
0
 def test_matrix_product_state(self):
     tensors = ([np.random.rand(5, 2)] +
                [np.random.rand(5, 5, 2)
                 for _ in range(3)] + [np.random.rand(5, 2)])
     mps = MatrixProductState(tensors)
     assert len(mps.tensors) == 5
     nmps = mps.reindex_sites('foo{}', inplace=False, where=slice(0, 3))
     assert nmps.site_ind_id == "k{}"
     assert isinstance(nmps, MatrixProductState)
     assert set(nmps.outer_inds()) == {'foo0', 'foo1', 'foo2', 'k3', 'k4'}
     assert set(mps.outer_inds()) == {'k0', 'k1', 'k2', 'k3', 'k4'}
     mps.site_ind_id = 'foo{}'
     assert set(
         mps.outer_inds()) == {'foo0', 'foo1', 'foo2', 'foo3', 'foo4'}
     assert mps.site_inds == ('foo0', 'foo1', 'foo2', 'foo3', 'foo4')
     assert mps.site_ind_id == 'foo{}'
     mps.show()
Esempio n. 3
0
    def test_right_canonize_site(self):
        a = np.random.randn(7, 2) + 1.0j * np.random.randn(7, 2)
        b = np.random.randn(7, 7, 2) + 1.0j * np.random.randn(7, 7, 2)
        c = np.random.randn(7, 2) + 1.0j * np.random.randn(7, 2)
        mps = MatrixProductState([a, b, c], site_tag_id="I{}")

        mps.right_canonize_site(2)
        assert mps['I2'].shape == (2, 2)
        assert mps['I2'].tags == {'I2'}
        assert mps['I1'].tags == {'I1'}

        U = (mps['I2'].data)
        assert_allclose(U.conj().T @ U, np.eye(2), atol=1e-13)
        assert_allclose(U @ U.conj().T, np.eye(2), atol=1e-13)

        # combined two site contraction is identity also
        mps.right_canonize_site(1)
        ptn = (mps.H & mps) ^ ['I1', 'I2']
        assert_allclose(ptn['I1'].data, np.eye(4), atol=1e-13)

        # try normalizing the state
        mps['I0'] /= mps['I0'].norm()

        assert_allclose(mps.H @ mps, 1)