def test_split_edge_different_backend_raises_value_error(single_node_edge): if single_node_edge.node.backend.name == "numpy": pytest.skip("numpy comparing to all the others") node1 = single_node_edge.node node2 = tn.Node(np.random.rand(2, 2, 2), backend="numpy") edge = tn.connect(node1[1], node2[1]) with pytest.raises(ValueError, match="Not all backends are the same."): tn.split_edge(edge, (2, 1))
def test_split_edges_standard_contract_between(backend): a = tn.Node(np.random.randn(6, 3, 5), name="A", backend=backend) b = tn.Node(np.random.randn(2, 4, 6, 3), name="B", backend=backend) e1 = tn.connect(a[0], b[2], "Edge_1_1") # to be split tn.connect(a[1], b[3], "Edge_1_2") # background standard edge node_dict, _ = tn.copy({a, b}) c_prior = node_dict[a] @ node_dict[b] shape = (2, 1, 3) tn.split_edge(e1, shape) tn.check_correct({a, b}) c_post = tn.contract_between(a, b) np.testing.assert_allclose(c_prior.tensor, c_post.tensor)
def test_split_edges_dangling(backend): a = tn.Node(np.zeros((2, 10, 4, 5)), name="A", backend=backend) e1 = a[0] e2 = a[1] e3 = a[2] e4 = a[3] shape = (2, 5) new_edge_names = ["New Edge 2", "New Edge 5"] new_edges = tn.split_edge(e2, shape, new_edge_names) assert a.shape == (2, 4, 5, 2, 5) assert a.edges == [e1, e3, e4, *new_edges] for new_edge, dim in zip(new_edges, shape): assert new_edge.dimension == dim for new_edge, new_name in zip(new_edges, new_edge_names): assert new_edge.name == new_name tn.check_correct({a})
def test_split_trace_edge(backend): a = tn.Node(np.zeros((2, 6, 4, 6, 5, 5)), backend=backend) c = tn.Node(np.zeros((2, 4)), backend=backend) e1 = tn.connect(a[1], a[3]) e2 = tn.connect(a[4], a[5]) external_1 = tn.connect(a[0], c[0]) external_2 = tn.connect(c[1], a[2]) shape = (2, 1, 3) new_edge_names = ["New Edge 2", "New Edge 1", "New Edge 3"] new_edges = tn.split_edge(e1, shape, new_edge_names) assert a.shape == (2, 4, 5, 5) + shape + shape assert a.edges == [external_1, external_2, e2, e2, *new_edges, *new_edges] for new_edge, dim in zip(new_edges, shape): assert new_edge.dimension == dim for new_edge, new_name in zip(new_edges, new_edge_names): assert new_edge.name == new_name tn.check_correct({a, c})
def test_split_edges_standard(backend): a = tn.Node(np.zeros((6, 3, 5)), name="A", backend=backend) b = tn.Node(np.zeros((2, 4, 6, 3)), name="B", backend=backend) e1 = tn.connect(a[0], b[2], "Edge_1_1") # to be split e2 = tn.connect(a[1], b[3], "Edge_1_2") # background standard edge edge_a_2 = a[2] # dangling edge_b_0 = b[0] # dangling edge_b_1 = b[1] # dangling shape = (2, 1, 3) new_edge_names = ["New Edge 2", "New Edge 1", "New Edge 3"] new_edges = tn.split_edge(e1, shape, new_edge_names) assert a.shape == (3, 5) + shape assert b.shape == (2, 4, 3) + shape assert a.edges == [e2, edge_a_2, *new_edges] assert b.edges == [edge_b_0, edge_b_1, e2, *new_edges] for new_edge, dim in zip(new_edges, shape): assert new_edge.dimension == dim for new_edge, new_name in zip(new_edges, new_edge_names): assert new_edge.name == new_name tn.check_correct({a, b})
def test_split_edges_dimension_mismatch_value_error(backend): a = tn.Node(np.eye(5), backend=backend) e1 = tn.connect(a[0], a[1]) with pytest.raises(ValueError): tn.split_edge(e1, (2, 2))
def test_split_edge_trivial(single_node_edge): edge = single_node_edge.edge assert tn.split_edge(edge, (1,)) == [edge]