Ejemplo n.º 1
0
 def test_flatten_consistent_result(self):
     net_noflat = tensornetwork.TensorNetwork()
     a_val = np.random.normal(size=(3, 5, 5, 6))
     b_val = np.random.normal(size=(5, 6, 4, 5))
     # Create non flattened example to compare against.
     a_noflat = net_noflat.add_node(a_val)
     b_noflat = net_noflat.add_node(b_val)
     e1 = net_noflat.connect(a_noflat[1], b_noflat[3])
     e2 = net_noflat.connect(a_noflat[3], b_noflat[1])
     e3 = net_noflat.connect(a_noflat[2], b_noflat[0])
     a_dangling_noflat = a_noflat[0]
     b_dangling_noflat = b_noflat[2]
     for edge in [e1, e2, e3]:
         net_noflat.contract(edge)
     noflat_result_node = net_noflat.get_final_node()
     noflat_result_node.reorder_edges(
         [a_dangling_noflat, b_dangling_noflat])
     noflat_result = noflat_result_node.get_tensor().numpy()
     # Create network with flattening
     net_flat = tensornetwork.TensorNetwork()
     a_flat = net_flat.add_node(a_val)
     b_flat = net_flat.add_node(b_val)
     e1 = net_flat.connect(a_flat[1], b_flat[3])
     e2 = net_flat.connect(a_flat[3], b_flat[1])
     e3 = net_flat.connect(a_flat[2], b_flat[0])
     a_dangling_flat = a_flat[0]
     b_dangling_flat = b_flat[2]
     final_edge = net_flat.flatten_edges([e1, e2, e3])
     flat_result_node = net_flat.contract(final_edge)
     flat_result_node.reorder_edges([a_dangling_flat, b_dangling_flat])
     flat_result = flat_result_node.get_tensor().numpy()
     self.assertAllClose(flat_result, noflat_result)
Ejemplo n.º 2
0
 def test_double_edge_contract(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.eye(2))
     e = net.connect(a[0], a[1], name="edge")
     net.contract(e)
     with self.assertRaises(ValueError):
         net.contract(e)
Ejemplo n.º 3
0
 def f(x, n):
     x_slice = x[:n]
     net = tensornetwork.TensorNetwork()
     n1 = net.add_node(x_slice)
     n2 = net.add_node(x_slice)
     e = net.connect(n1[0], n2[0])
     return net.contract(e).get_tensor()
Ejemplo n.º 4
0
def _build_network(
    tensors: Sequence[Union[np.ndarray,
                            tf.Tensor]], network: Sequence[Sequence]
) -> Tuple[tensornetwork.TensorNetwork, Dict[Any, tensornetwork.Edge]]:
    tn = tensornetwork.TensorNetwork()
    nodes = []
    edges = {}
    for (i, (tensor, edge_lbls)) in enumerate(zip(tensors, network)):
        if len(tensor.shape) != len(edge_lbls):
            raise ValueError(
                "Incorrect number of edge labels specified tensor {}".format(
                    i))

        node = tn.add_node(tensor, name="tensor_{}".format(i))
        nodes.append(node)

        for (axis_num, edge_lbl) in enumerate(edge_lbls):
            if edge_lbl not in edges:
                e = node[axis_num]
                e.set_name(str(edge_lbl))
                edges[edge_lbl] = e
            else:
                # This will raise an error if the edges are not dangling.
                e = tn.connect(edges[edge_lbl],
                               node[axis_num],
                               name=str(edge_lbl))
                edges[edge_lbl] = e
    return tn, edges
Ejemplo n.º 5
0
 def test_real_physics_with_tensors(self):
     # Calcuate the expected value in numpy
     a_vals = np.ones([2, 3, 4, 5])
     b_vals = np.ones([4, 6, 7])
     c_vals = np.ones([5, 6, 8])
     contract1 = np.tensordot(a_vals, b_vals, [[2], [0]])
     contract2 = np.tensordot(c_vals, contract1, [[0], [2]])
     final_result = np.trace(contract2, axis1=0, axis2=4)
     # Build the network
     net = tensornetwork.TensorNetwork()
     a = net.add_node(tf.ones([2, 3, 4, 5]), name="T")
     b = net.add_node(tf.ones([4, 6, 7]), name="A")
     c = net.add_node(tf.ones([5, 6, 8]), name="B")
     e1 = net.connect(a[2], b[0], "edge")
     e2 = net.connect(c[0], a[3], "edge2")
     e3 = net.connect(b[1], c[1], "edge3")
     net.check_correct()
     node_result = net.contract(e1)
     self.assertAllClose(node_result.get_tensor(), contract1)
     net.check_correct()
     node_result = net.contract(e2)
     self.assertAllClose(node_result.get_tensor(), contract2)
     net.check_correct()
     val = net.contract(e3)
     net.check_correct()
     self.assertAllClose(val.get_tensor(), final_result)
Ejemplo n.º 6
0
 def test_outer_product_final_nodes_not_contracted(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.ones(2))
     b = net.add_node(np.ones(2))
     e = net.connect(a[0], b[0])
     with self.assertRaises(ValueError):
         net.outer_product_final_nodes([e])
Ejemplo n.º 7
0
 def f(x, n):
     x_slice = x[..., :n]
     net = tensornetwork.TensorNetwork()
     n1 = net.add_node(x_slice)
     net.connect(n1[0], n1[2])
     net.connect(n1[1], n1[3])
     return net.contract(net.flatten_edges_between(n1, n1)).get_tensor()
Ejemplo n.º 8
0
 def test_large_nodes(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.zeros([5, 6, 7, 8, 9]), "a")
     b = net.add_node(np.zeros([5, 6, 7, 8, 9]), "b")
     for i in range(5):
         net.connect(a[i], b[i])
     net.check_correct()
Ejemplo n.º 9
0
 def test_edge_not_in_network(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.eye(2))
     b = net.add_node(np.eye(2))
     edge = net.connect(a[0], b[0])
     net.disconnect(edge)
     self.assertNotIn(edge, net)
Ejemplo n.º 10
0
 def test_has_nondangling_edge(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.ones(2))
     self.assertFalse(a.has_nondangling_edge())
     b = net.add_node(np.ones((2, 2)))
     net.connect(b[0], b[1])
     self.assertTrue(b.has_nondangling_edge())
Ejemplo n.º 11
0
 def test_trace_edge_ordering(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.zeros((2, 2, 3)))
     e2 = net.connect(a[1], a[0])
     e3 = a[2]
     with self.assertRaises(ValueError):
         a.reorder_edges([e2, e3])
Ejemplo n.º 12
0
 def test_double_edge_axis(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.array([2]), name="a")
     b = net.add_node(np.array([2]), name="b")
     net.connect(a[0], b[0])
     with self.assertRaises(ValueError):
         net.connect(a[0], b[0])
Ejemplo n.º 13
0
 def test_bad_trace_contract(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.array([2]))
     b = net.add_node(np.array([2]))
     e = net.connect(a[0], b[0])
     with self.assertRaises(ValueError):
         net._contract_trace(e)
Ejemplo n.º 14
0
 def test_outer_product_final_nodes(self):
     net = tensornetwork.TensorNetwork()
     edges = []
     for i in range(1, 5):
         edges.append(net.add_node(tf.ones(i))[0])
     final_node = net.outer_product_final_nodes(edges)
     self.assertAllClose(final_node.get_tensor(), np.ones([1, 2, 3, 4]))
     self.assertEqual(final_node.get_all_edges(), edges)
Ejemplo n.º 15
0
 def test_contract_between_no_outer_product(self):
     net = tensornetwork.TensorNetwork()
     a_val = np.random.normal(size=(2, 3, 4))
     b_val = np.random.normal(size=(5, 6, 7))
     a = net.add_node(a_val)
     b = net.add_node(b_val)
     with self.assertRaises(ValueError):
         net.contract_between(a, b)
Ejemplo n.º 16
0
 def test_contract_between_outer_product(self):
     net = tensornetwork.TensorNetwork()
     a_val = np.random.normal(size=(2, 3, 4))
     b_val = np.random.normal(size=(5, 6, 7))
     a = net.add_node(a_val)
     b = net.add_node(b_val)
     c = net.contract_between(a, b, allow_outer_product=True)
     self.assertEqual(c.get_tensor().shape, (2, 3, 4, 5, 6, 7))
Ejemplo n.º 17
0
 def test_merge_networks(self):
     net1 = tensornetwork.TensorNetwork()
     net2 = tensornetwork.TensorNetwork()
     a = net1.add_node(np.eye(2) * 2)
     b = net1.add_node(np.eye(2) * 3)
     e1 = net1.connect(a[0], b[0])
     c = net2.add_node(np.eye(2) * 4)
     net3 = tensornetwork.TensorNetwork.merge_networks([net1, net2])
     self.assertIn(a, net3.nodes_set)
     self.assertIn(b, net3.nodes_set)
     e2 = net3.connect(c[0], a[1])
     e3 = net3.connect(c[1], b[1])
     net3.check_correct()
     for edge in [e1, e2, e3]:
         net3.contract(edge)
     result = net3.get_final_node()
     self.assertAllClose(result.get_tensor().numpy(), 48.0)
Ejemplo n.º 18
0
 def test_node2_contract_trace(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.zeros([3, 3, 1]))
     b = net.add_node(np.zeros([1]))
     net.connect(b[0], a[2])
     trace_edge = net.connect(a[0], a[1])
     net._contract_trace(trace_edge)
     net.check_correct()
Ejemplo n.º 19
0
 def test_mismatch_edge_ordering(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.zeros((2, 3)))
     e2_a = a[0]
     b = net.add_node(np.zeros((2, )))
     e_b = b[0]
     with self.assertRaises(ValueError):
         a.reorder_edges([e2_a, e_b])
Ejemplo n.º 20
0
 def test_contract_fall_through_name(self):
     net = tensornetwork.TensorNetwork()
     node = net.add_node(np.eye(2), name="Identity Matrix")
     self.assertEqual(node.name, "Identity Matrix")
     edge = net.connect(node[0], node[1], name="Trace Edge")
     self.assertEqual(edge.name, "Trace Edge")
     final_result = net.contract(edge, name="Trace Of Identity")
     self.assertEqual(final_result.name, "Trace Of Identity")
Ejemplo n.º 21
0
 def test_direct_trace(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.ones([10, 10]), name="a")
     edge = net.connect(a[0], a[1], "edge")
     net.check_correct()
     result = net._contract_trace(edge)
     net.check_correct()
     self.assertAlmostEqual(result.get_tensor().numpy(), 10.0)
Ejemplo n.º 22
0
 def test_small_matmul(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.zeros([10, 10]), name="a")
     b = net.add_node(np.zeros([10, 10]), name="b")
     edge = net.connect(a[0], b[0], "edge")
     net.check_correct()
     c = net.contract(edge, name="a * b")
     self.assertEqual(c.get_tensor().shape, [10, 10])
     net.check_correct()
Ejemplo n.º 23
0
 def test_add_subnetwork(self):
     net1 = tensornetwork.TensorNetwork()
     net2 = tensornetwork.TensorNetwork()
     a = net1.add_node(np.eye(2) * 2)
     b = net1.add_node(np.eye(2) * 3)
     e1 = net1.connect(a[0], b[0])
     c = net2.add_node(np.eye(2) * 4)
     net2.add_subnetwork(net1)
     self.assertIn(a, net2.nodes_set)
     self.assertIn(b, net2.nodes_set)
     e2 = net2.connect(c[0], a[1])
     e3 = net2.connect(c[1], b[1])
     net2.check_correct()
     self.assertEqual(net2.edge_order, [e1, e2, e3])
     for edge in [e1, e2, e3]:
         net2.contract(edge)
     result = net2.get_final_node()
     self.assertAllClose(result.get_tensor().numpy(), 48.0)
Ejemplo n.º 24
0
 def test_contract_parallel(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.eye(2))
     b = net.add_node(np.eye(2))
     edge1 = net.connect(a[0], b[0])
     edge2 = net.connect(a[1], b[1])
     c = net.contract_parallel(edge1)
     self.assertNotIn(edge2, net)
     self.assertAllClose(c.get_tensor(), 2.0)
Ejemplo n.º 25
0
 def test_single_contract(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.array([1.0] * 5), "a")
     b = net.add_node(np.array([1.0] * 5), "b")
     e = net.connect(a[0], b[0])
     c = net.contract(e)
     net.check_correct()
     val = c.get_tensor().numpy()
     self.assertAlmostEqual(val, 5.0)
Ejemplo n.º 26
0
 def test_set_node2(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.eye(2))
     b = net.add_node(np.eye(2))
     e = net.connect(a[0], b[0])
     # You should never do this, but if you do, we should handle
     # it gracefully.
     e.node2 = None
     self.assertTrue(e.is_dangling())
Ejemplo n.º 27
0
 def test_contract_between_trace_edges(self):
     net = tensornetwork.TensorNetwork()
     a_val = np.random.normal(size=(3, 3))
     final_val = np.trace(a_val)
     a = net.add_node(a_val)
     net.connect(a[0], a[1])
     b = net.contract_between(a, a)
     net.check_correct()
     self.assertAllClose(b.get_tensor().numpy(), final_val)
Ejemplo n.º 28
0
 def test_get_all_nondangling(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.eye(2))
     b = net.add_node(np.eye(2))
     edge1 = net.connect(a[0], b[0])
     c = net.add_node(np.eye(2))
     d = net.add_node(np.eye(2))
     edge2 = net.connect(c[0], d[0])
     edge3 = net.connect(a[1], c[1])
     self.assertEqual({edge1, edge2, edge3}, net.get_all_nondangling())
Ejemplo n.º 29
0
 def test_flatten_edges_different_nodes(self):
     net = tensornetwork.TensorNetwork()
     a = net.add_node(np.eye(2))
     b = net.add_node(np.eye(2))
     c = net.add_node(np.eye(2))
     e1 = net.connect(a[0], b[0])
     e2 = net.connect(a[1], c[0])
     net.connect(b[1], c[1])
     with self.assertRaises(ValueError):
         net.flatten_edges([e1, e2])
Ejemplo n.º 30
0
 def test_flatten_trace_consistent_result(self):
     net_noflat = tensornetwork.TensorNetwork()
     a_val = np.random.normal(size=(5, 6, 6, 7, 5, 7))
     a_noflat = net_noflat.add_node(a_val)
     e1 = net_noflat.connect(a_noflat[0], a_noflat[4])
     e2 = net_noflat.connect(a_noflat[1], a_noflat[2])
     e3 = net_noflat.connect(a_noflat[3], a_noflat[5])
     for edge in [e1, e2, e3]:
         net_noflat.contract(edge)
     noflat_result = net_noflat.get_final_node().get_tensor().numpy()
     # Create network with flattening
     net_flat = tensornetwork.TensorNetwork()
     a_flat = net_flat.add_node(a_val)
     e1 = net_flat.connect(a_flat[0], a_flat[4])
     e2 = net_flat.connect(a_flat[1], a_flat[2])
     e3 = net_flat.connect(a_flat[3], a_flat[5])
     final_edge = net_flat.flatten_edges([e1, e2, e3])
     flat_result = net_flat.contract(final_edge).get_tensor().numpy()
     self.assertAllClose(flat_result, noflat_result)