Exemple #1
0
def test_named_axis(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  a = net.add_node(np.eye(2), axis_names=["alpha", "beta"])
  e = net.connect(a["alpha"], a["beta"])
  b = net.contract(e)
  np.testing.assert_allclose(b.tensor, 2.0)
Exemple #2
0
def test_remove_node_value_error(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    net2 = tensornetwork.TensorNetwork(backend=backend)
    a = net.add_node(np.ones((2, 2, 2)))
    with pytest.raises(ValueError):
        net2.remove_node(a)
Exemple #3
0
def apply_2site_schmidt_canonical(op,
                                  L0,
                                  G1,
                                  L1,
                                  G2,
                                  L2,
                                  max_bond_dim=None,
                                  auto_trunc_max_err=0.0):
    """
  Applies a two-site local operator to an MPS.
  Takes Lambda and Gamma tensors (Schmidt canonical form)
  and returns new ones, as well as the new norm of the state.
  """
    if tf.executing_eagerly():
        # FIXME: Not ideal, but these ops are very costly at compile time
        op_shp = tf.shape(op)
        tf.assert_equal(
            tf.shape(G1)[1],
            op_shp[2],
            message="Operator dimensions do not match MPS physical dimensions."
        )
        tf.assert_equal(
            tf.shape(G2)[1],
            op_shp[3],
            message="Operator dimensions do not match MPS physical dimensions."
        )

    # TODO(ash): Can we assume these are diagonal?
    L0_i = tf.matrix_inverse(L0)
    L2_i = tf.matrix_inverse(L2)

    net = tn.TensorNetwork()
    nL0_i = net.add_node(L0_i, axis_names=["L", "R"])
    nL0 = net.add_node(L0, axis_names=["L", "R"])
    nG1 = net.add_node(G1, axis_names=["L", "p", "R"])
    nL1 = net.add_node(L1, axis_names=["L", "R"])
    nG2 = net.add_node(G2, axis_names=["L", "p", "R"])
    nL2 = net.add_node(L2, axis_names=["L", "R"])
    nL2_i = net.add_node(L2_i, axis_names=["L", "R"])
    nop = net.add_node(op,
                       axis_names=["p_out_1", "p_out_2", "p_in_1", "p_in_2"])

    b0 = net.connect(nL0_i["R"], nL0["L"])
    b1 = net.connect(nL0["R"], nG1["L"])
    b2 = net.connect(nG1["R"], nL1["L"])
    b3 = net.connect(nL1["R"], nG2["L"])
    b4 = net.connect(nG2["R"], nL2["L"])
    b5 = net.connect(nL2["R"], nL2_i["L"])

    net.connect(nG1["p"], nop["p_in_1"])
    net.connect(nG2["p"], nop["p_in_2"])

    output_order = [nL0["L"], nop["p_out_1"], nop["p_out_2"], nL2["R"]]
    net.contract(b1)
    net.contract(b2)
    net.contract(b3)
    n_mps = net.contract(b4)
    n_block = net.contract_between(nop, n_mps)

    nu, ns, nvh, s_rest = net.split_node_full_svd(
        n_block,
        output_order[:2],
        output_order[2:],
        max_singular_values=max_bond_dim,
        max_truncation_err=auto_trunc_max_err)

    trunc_err = tf.norm(s_rest)
    nrm = tf.norm(ns.tensor)
    ns.tensor = tf.divide(ns.tensor, nrm)
    L1_new = ns.tensor

    #output_order = [nL0_i["L"], nu["p_out_1"], es1]
    output_order = [nL0_i["L"], nu[1], ns[0]]
    nG1_new = net.contract(b0)
    nG1_new.reorder_edges(output_order)
    G1_new = nG1_new.tensor

    #output_order = [es2, nvh["p_out_2"], nL2_i["R"]]
    output_order = [ns[1], nvh[1], nL2_i["R"]]
    nG2_new = net.contract(b5)
    nG2_new.reorder_edges(output_order)
    G2_new = nG2_new.tensor

    return G1_new, L1_new, G2_new, nrm, trunc_err
Exemple #4
0
def test_network_init(backend, dtype):
    net = tensornetwork.TensorNetwork(backend=backend, dtype=dtype)
    assert net.nodes_set == set()
    assert net.dtype == dtype
    assert net.node_increment == 0
    assert net.edge_increment == 0
Exemple #5
0
def test_flatten_edges_between_no_edges(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    a = net.add_node(np.ones((3)))
    b = net.add_node(np.ones((3)))
    assert net.flatten_edges_between(a, b) is None
Exemple #6
0
def test_add_node_twice_raise_value_error(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    a = net.add_node(tensornetwork.CopyNode(3, 3))
    with pytest.raises(ValueError):
        net.add_node(a)
Exemple #7
0
def test_contract_dangling_edge_value_error(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    a = net.add_node(np.array([1.0]))
    e = a[0]
    with pytest.raises(ValueError):
        net.contract(e)
Exemple #8
0
def test_join_dangling(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  a = net.add_node(np.ones((3,)))
  b = net.add_node(np.ones((3,)))
  net.connect(a[0], b[0])
  net.check_correct()
Exemple #9
0
def test_set_tensor(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  a = net.add_node(np.ones(2))
  np.testing.assert_allclose(a.tensor, np.ones(2))
  a.tensor = np.zeros(2)
  np.testing.assert_allclose(a.tensor, np.zeros(2))
Exemple #10
0
def test_add_axis_names(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  a = net.add_node(np.eye(2), name="A", axis_names=["ignore1", "ignore2"])
  a.add_axis_names(["a", "b"])
  assertEqual(a.axis_names, ["a", "b"])
Exemple #11
0
def test_node_names(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  a = net.add_node(np.eye(2), "a", axis_names=["e0", "e1"])
  assertEqual(a.name, "a")
  assertEqual(a[0].name, "e0")
  assertEqual(a[1].name, "e1")
Exemple #12
0
def test_bad_axis_name_connect(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  a = net.add_node(np.eye(2), axis_names=["test", "names"])
  with pytest.raises(ValueError):
    a.get_edge("bad_name")
Exemple #13
0
def test_bad_axis_name_length(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  with pytest.raises(ValueError):
    # This should have 2 names, not 1.
    net.add_node(np.eye(2), axis_names=["need_2_names"])
Exemple #14
0
def test_duplicate_name(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  with pytest.raises(ValueError):
    net.add_node(np.eye(2), axis_names=["test", "test"])
Exemple #15
0
def test_add_node_sanity_check(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    net.add_node(np.eye(2), "a")
    net.check_correct()
Exemple #16
0
def test_edge_in_network(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  a = net.add_node(np.eye(2))
  b = net.add_node(np.eye(2))
  edge = net.connect(a[0], b[0])
  assertIn(edge, net)
Exemple #17
0
def test_add_node_in_network(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    a = net.add_node(np.eye(2))
    assert a in net
Exemple #18
0
def test_node_not_in_network(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  a = net.add_node(np.ones((2, 2)))
  e = net.connect(a[0], a[1])
  net.contract(e)
  assertNotIn(a, net)
Exemple #19
0
def test_disconnect_dangling_edge_value_error(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    a = net.add_node(np.eye(2))
    with pytest.raises(ValueError):
        net.disconnect(a[0])
Exemple #20
0
def test_set_default(backend):
  tensornetwork.set_default_backend(backend)
  assert tensornetwork.config.default_backend == backend
  net = tensornetwork.TensorNetwork()
  assert net.backend.name == backend
Exemple #21
0
def test_get_final_node_dangling_edge_value_error(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    a = net.add_node(np.ones((3, 3)), "a")
    net.connect(a[0], a[1])
    with pytest.raises(ValueError):
        net.get_final_node()
Exemple #22
0
def test_bad_backend():
  with pytest.raises(ValueError):
    tensornetwork.TensorNetwork("NOT_A_BACKEND")
Exemple #23
0
def test_get_all_edges(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    a = net.add_node(np.eye(2))
    b = net.add_node(np.eye(2))
    assert {a[0], a[1], b[0], b[1]} == net.get_all_edges()
Exemple #24
0
def test_add_subnetwork_incompatible_backends():
    net1 = tensornetwork.TensorNetwork(backend="numpy")
    net2 = tensornetwork.TensorNetwork(backend="tensorflow")
    with pytest.raises(ValueError):
        net1.add_subnetwork(net2)
Exemple #25
0
def test_new_node_name(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    net.node_increment = 7
    assert net._new_node_name(None) == "__Node_8"
    assert net._new_node_name("new_name") == "new_name"
    assert net._new_node_name(None) == "__Node_10"
Exemple #26
0
def test_merge_networks_incompatible_backends():
    net1 = tensornetwork.TensorNetwork(backend="numpy")
    net2 = tensornetwork.TensorNetwork(backend="tensorflow")
    with pytest.raises(ValueError):
        tensornetwork.TensorNetwork.merge_networks([net1, net2])
Exemple #27
0
def test_dtype(backend):
    net = tensornetwork.TensorNetwork(backend=backend)
    assert net.dtype is None
Exemple #28
0
def test_switch_backend_to_tensorflow():
    net = tensornetwork.TensorNetwork(backend="numpy")
    a = net.add_node(np.eye(2))
    net.switch_backend(new_backend="tensorflow")
    assert isinstance(a.tensor, tf.Tensor)
 def build_tensornetwork(tensors):
     net = tensornetwork.TensorNetwork(backend="tensorflow")
     a = net.add_node(tensors[0])
     b = net.add_node(tensors[1])
     e = net.connect(a[0], b[0])
     return net.contract(e).get_tensor()
Exemple #30
0
def test_node_get_dim_bad_axis(backend):
  net = tensornetwork.TensorNetwork(backend=backend)
  node = net.add_node(np.eye(2), name="a", axis_names=["1", "2"])
  with pytest.raises(ValueError):
    node.get_dimension(10)