Esempio n. 1
0
def load_nodes(path: str) -> List[BaseNode]:
    """
  Load a set of nodes from disk.

  Args:
    path: path to file where network is saved.
  Returns:
    An iterable of `Node` objects
  """
    nodes_list = []
    edges_list = []
    with h5py.File(path, 'r') as net_file:
        nodes = list(net_file["nodes"].keys())
        node_names = {
            'node{}'.format(n): v
            for n, v in enumerate(net_file["node_names"]['names'][()])
        }

        edge_names = {
            'edge{}'.format(n): v
            for n, v in enumerate(net_file["edge_names"]['names'][()])
        }
        edges = list(net_file["edges"].keys())
        for node_name in nodes:
            node_data = net_file["nodes/" + node_name]
            node_type = get_component(node_data['type'][()])
            nodes_list.append(
                node_type._load_node(net=None, node_data=node_data))
        nodes_dict = {node.name: node for node in nodes_list}
        for edge in edges:
            edge_data = net_file["edges/" + edge]
            edges_list.append(Edge._load_edge(edge_data, nodes_dict))

    for edge in edges_list:
        edge.set_name(edge_names[edge.name])
    for node in nodes_list:
        node.set_name(node_names[node.name])

    return nodes_list
Esempio n. 2
0
def load(path: str):
    """Load a tensor network from disk.

  Args:
    path: path to file where network is saved.
  """
    with h5py.File(path, 'r') as net_file:
        net = TensorNetwork(backend=net_file["backend"][()])
        node_names = list(net_file["nodes"].keys())
        edge_names = list(net_file["edges"].keys())

        for node_name in node_names:
            node_data = net_file["nodes/" + node_name]
            node_type = get_component(node_data['type'][()])
            node_type._load_node(net, node_data)

        nodes_dict = {node.name: node for node in net.nodes_set}

        for edge in edge_names:
            edge_data = net_file["edges/" + edge]
            Edge._load_edge(edge_data, nodes_dict)
    return net