def test_gate_split_gate(self, propagate_tags): p = MPS_rand_state(5, 7, tags={'PSI0'}) q = p.copy() G = qu.CNOT() p = p.gate_(G, where=[i for i in range(2, 4)], tags='G', contract='split-gate', propagate_tags=propagate_tags) TG = sorted(p['G'], key=lambda t: sorted(t.tags)) if propagate_tags is False: assert TG[0].tags == {'G'} assert TG[1].tags == {'G'} elif propagate_tags == 'register': assert TG[0].tags == {'G', 'I2'} assert TG[1].tags == {'G', 'I3'} elif propagate_tags == 'sites': assert TG[0].tags == {'G', 'I2', 'I3'} assert TG[1].tags == {'G', 'I2', 'I3'} elif propagate_tags is True: assert TG[0].tags == {'PSI0', 'G', 'I2', 'I3'} assert TG[1].tags == {'PSI0', 'G', 'I2', 'I3'} assert (p.H & p) ^ all == pytest.approx(1.0) assert abs((q.H & p) ^ all) < 1.0 assert len(p.tensors) == 7 assert set(p.outer_inds()) == {'k{}'.format(i) for i in range(5)}
def test_compress_trim_max_bond(self, method): p0 = MPS_rand_state(20, 20) p = p0.copy() p.compress(method=method) assert max(p['I4'].shape) == 20 p.compress(max_bond=13, method=method) assert max(p['I4'].shape) == 13 assert_allclose(p.H @ p, p0.H @ p0)
def test_gate_no_contract(self, bsz, propagate_tags, contract): p = MPS_rand_state(5, 7) q = p.copy() G = qu.rand_uni(2**bsz) p = p.gate(G, where=[i for i in range(2, 2 + bsz)], tags='G', contract=contract) TG = p['G'] if propagate_tags or contract: assert p.site_tag(2) in TG.tags assert p.H @ p == pytest.approx(1.0) assert abs(q.H @ p) < 1.0 assert len(p.tensors) == 6 - int(contract) * bsz assert set(p.outer_inds()) == {'k{}'.format(i) for i in range(5)}
def test_insert_gauge(self, dtype): k = MPS_rand_state(10, 7, dtype=dtype, normalize=False) kU = k.copy() U = rand_tensor((7, 7), dtype=dtype, inds='ab').data kU.insert_gauge(U, 4, 5) assert k[3].almost_equals(kU[3]) assert not k[4].almost_equals(kU[4]) assert not k[5].almost_equals(kU[5]) assert k[6].almost_equals(kU[6]) assert k[4].inds == kU[4].inds assert k[5].inds == kU[5].inds assert_allclose(k.H @ k, kU.H @ kU)
def test_gate_no_contract(self, bsz, propagate_tags, contract): p = MPS_rand_state(5, 7, tags={'PSI0'}) q = p.copy() G = qu.rand_uni(2**bsz) p = p.gate_(G, where=[i for i in range(2, 2 + bsz)], tags='G', contract=contract, propagate_tags=propagate_tags) TG = p['G'] if propagate_tags or contract: assert p.site_tag(2) in TG.tags assert ('PSI0' in TG.tags) == (propagate_tags is True) or contract assert (p.H & p) ^ all == pytest.approx(1.0) assert abs((q.H & p) ^ all) < 1.0 assert len(p.tensors) == 6 - int(contract) * bsz assert set(p.outer_inds()) == {f'k{i}' for i in range(5)}
def test_gate2split(self): psi = MPS_rand_state(10, 3) psi2 = psi.copy() G = qu.eye(2) & qu.eye(2) psi.gate2split(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.gate2split(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)