def test_apply_one_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], [1, 0]], dtype=np.float64)) mps.apply_one_site_gate(gate=gate, site=1) expected = np.array([[1., -2., 1.], [1., 2., 1.]]) np.testing.assert_allclose(mps.tensors[1][0], expected)
def test_apply_one_site_gate_invalid_site_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), dtype=np.float64)) with pytest.raises(ValueError): mps.apply_one_site_gate(gate=gate, site=-1) with pytest.raises(ValueError): mps.apply_one_site_gate(gate=gate, site=6)
def test_apply_one_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) tensor = mps.nodes[5].tensor gate = get_random_np((2, 2), dtype) mps.apply_one_site_gate(gate, 5) actual = np.transpose(np.tensordot(tensor, gate, ([1], [1])), (0, 2, 1)) np.testing.assert_allclose(mps.nodes[5].tensor, actual)