def test_apply_two_site_gate(backend_dtype_values): backend = backend_dtype_values[0] dtype = backend_dtype_values[1] D, d, N = 10, 2, 10 tensors = [get_random_np((1, d, D), dtype) ] + [get_random_np((D, d, D), dtype) for _ in range(N - 2)] + [get_random_np((D, d, 1), dtype)] mps = BaseMPS(tensors, center_position=0, backend=backend) gate = get_random_np((2, 2, 2, 2), dtype) tensor1 = mps.tensors[5] tensor2 = mps.tensors[6] mps.apply_two_site_gate(gate, 5, 6) tmp = np.tensordot(tensor1, tensor2, ([2], [0])) actual = np.transpose(np.tensordot(tmp, gate, ([1, 2], [2, 3])), (0, 2, 3, 1)) node1 = tn.Node(mps.tensors[5], backend=backend) node2 = tn.Node(mps.tensors[6], backend=backend) node1[2] ^ node2[0] order = [node1[0], node1[1], node2[1], node2[2]] res = tn.contract_between(node1, node2) res.reorder_edges(order) np.testing.assert_allclose(res.tensor, actual)
def test_apply_two_site_wrong_site1_site2_raises_error(backend): backend = backend_factory.get_backend(backend) tensor = np.array([[[1., 2., 1.], [1., -2., 1.]], [[-1., 1., -1.], [-1., 1., -1.]], [[1., 2, 3], [3, 2, 1]]], dtype=np.float64) tensors = 6 * [backend.convert_to_tensor(tensor)] mps = BaseMPS(tensors, backend=backend, center_position=2) gate = backend.convert_to_tensor(np.ones((2, 2, 2, 2), dtype=np.float64)) with pytest.raises(ValueError): mps.apply_two_site_gate(gate=gate, site1=2, site2=2) with pytest.raises(ValueError): mps.apply_two_site_gate(gate=gate, site1=2, site2=4)
def test_apply_two_site_max_singular_value_not_center_raises_error(backend): backend = backend_factory.get_backend(backend) tensor = np.array([[[1., 2., 1.], [1., -2., 1.]], [[-1., 1., -1.], [-1., 1., -1.]], [[1., 2, 3], [3, 2, 1]]], dtype=np.float64) tensors = 6 * [backend.convert_to_tensor(tensor)] mps = BaseMPS(tensors, backend=backend, center_position=2) gate = backend.convert_to_tensor(np.ones((2, 2, 2, 2), dtype=np.float64)) with pytest.raises(ValueError): mps.apply_two_site_gate(gate=gate, site1=3, site2=4, max_singular_values=1) with pytest.raises(ValueError): mps.apply_two_site_gate(gate=gate, site1=3, site2=4, max_truncation_err=.1)
def test_apply_two_site_gate_2(backend): backend = backend_factory.get_backend(backend) tensor = np.array([[[1., 2., 1.], [1., -2., 1.]], [[-1., 1., -1.], [-1., 1., -1.]], [[1., 2, 3], [3, 2, 1]]], dtype=np.float64) tensors = 6 * [backend.convert_to_tensor(tensor)] mps = BaseMPS(tensors, backend=backend, center_position=2) gate = backend.convert_to_tensor( np.array([[[[0., 1.], [0., 0.]], [[1., 0.], [0., 0.]]], [[[0., 0.], [0., 1.]], [[0., 0.], [1., 0.]]]], dtype=np.float64)) actual = mps.apply_two_site_gate( gate=gate, site1=1, site2=2, max_singular_values=1) np.testing.assert_allclose(actual[0], 9.133530) expected = np.array([[5.817886], [9.039142]]) np.testing.assert_allclose(np.abs(mps.tensors[1][0]), expected, rtol=1e-04) expected = np.array([[0.516264, 0.080136, 0.225841], [0.225841, 0.59876, 0.516264]]) np.testing.assert_allclose(np.abs(mps.tensors[2][0]), expected, rtol=1e-04)