def test_specify_sites(self, cyclic): sites = [12, 13, 15, 16, 17] k = MPS_rand_state(5, 7, cyclic=cyclic, sites=sites, nsites=20) assert set(k.tags) == {f'I{i}' for i in sites}
def test_known_bad_case(self, cyclic): k = MPS_rand_state(5, 10, cyclic=cyclic) rhoc_ab = k.partial_trace_compress(sysa=range(2), sysb=range(2, 4)) inds = ['kA', 'kB'], ['bA', 'bB'] x = rhoc_ab.trace(*inds) assert_allclose(1.0, x, rtol=1e-3)
def test_compress_site(self): psi = MPS_rand_state(10, 7) psi.compress_site(3, max_bond=1) assert psi.bond_sizes() == [2, 4, 1, 1, 7, 7, 7, 4, 2] assert psi.calc_current_orthog_center() == (3, 3) psi = MPS_rand_state(10, 7) psi.compress_site(0, max_bond=1) assert psi.bond_sizes() == [1, 7, 7, 7, 7, 7, 7, 4, 2] assert psi.calc_current_orthog_center() == (0, 0) psi = MPS_rand_state(10, 7) psi.compress_site(9, max_bond=1) assert psi.bond_sizes() == [2, 4, 7, 7, 7, 7, 7, 7, 1] assert psi.calc_current_orthog_center() == (9, 9)
def test_subtract(self): a, b, c = (MPS_rand_state(10, 7) for _ in 'abc') ab = a.H @ b ac = a.H @ c abmc = a.H @ (b - c) assert_allclose(ab - ac, abmc)
def test_can_change_data(self): p = MPS_rand_state(3, 10) assert_allclose(p.H @ p, 1) p[1].modify(data=np.random.randn(10, 10, 2)) assert abs(p.H @ p - 1) > 1e-13
def test_compress_form(self): p = MPS_rand_state(20, 20) p.compress('left') assert p.count_canonized() == (19, 0) p.compress('right') assert p.count_canonized() == (0, 19) p.compress(7) assert p.count_canonized() == (7, 12) p = MPS_rand_state(20, 20) p.compress('flat', absorb='left') assert p.count_canonized() == (0, 0)
def test_swap_gating(self): psi0 = MPS_rand_state(20, 5) CNOT = qu.controlled('not') psi0XX = psi0.gate(CNOT, (4, 13)) psi0XX_s = psi0.gate_with_auto_swap(CNOT, (4, 13)) assert psi0XX.H @ psi0XX_s == pytest.approx(1.0)
def test_sites_mpo_mps_product(self, cyclic): k = MPS_rand_state(13, 7, cyclic=cyclic) X = MPO_rand_herm(3, 5, sites=[3, 6, 7], nsites=13, cyclic=cyclic) b = k.H k.align_(X, b) assert (k & X & b) ^ ...
def test_gate_split(self): psi = MPS_rand_state(10, 3) psi2 = psi.copy() G = qu.eye(2) & qu.eye(2) psi.gate_split_(G, (2, 3), cutoff=0) assert psi.bond_size(2, 3) == 6 assert psi.H @ psi2 == pytest.approx(1.0) # check a unitary application G = qu.rand_uni(2**2) psi.gate_split_(G, (7, 8)) psi.compress() assert psi.bond_size(2, 3) == 3 assert psi.bond_size(7, 8) > 3 assert psi.H @ psi == pytest.approx(1.0) assert abs(psi2.H @ psi) < 1.0 # check matches dense application of gate psid = psi2.to_dense() Gd = qu.ikron(G, [2] * 10, (7, 8)) assert psi.to_dense().H @ (Gd @ psid) == pytest.approx(1.0)
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_construct_tridiag_works(self, sysa, sysb): p = MPS_rand_state(12, 7) pX = PTPTLazyMPS(p, sysa=sysa, sysb=sysb) for _ in construct_lanczos_tridiag_PTPTLazyMPS(pX, 5): pass
def test_graph(self): import matplotlib matplotlib.use('Template') k = MPS_rand_state(10, 7, normalize=False) k.graph(color=['I0', 'I2'])
def test_cut_iter(self): psi = MPS_rand_state(10, 7, cyclic=True) pp = psi.H & psi bnds = bonds(pp[0], pp[-1]) assert sum(tn ^ all for tn in pp.cut_iter(*bnds)) == pytest.approx(1.0) assert pp ^ all == pytest.approx(1.0)