Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Args:
nodes: A collection of connected nodes.
algorithm: `opt_einsum` contraction method to use.
output_edge_order: An optional list of edges. Edges of the
final node in `nodes_set`
are reordered into `output_edge_order`;
if final node has more than one edge,
`output_edge_order` must be pronvided.
ignore_edge_order: An option to ignore the output edge
order.
Returns:
Final node after full contraction.
"""
nodes_set = set(nodes)
edges = get_all_edges(nodes_set)
#output edge order has to be determinded before any contraction
#(edges are refreshed after contractions)
if not ignore_edge_order:
if output_edge_order is None:
output_edge_order = list(get_subgraph_dangling(nodes))
if len(output_edge_order) > 1:
raise ValueError("The final node after contraction has more than "
"one remaining edge. In this case `output_edge_order` "
"has to be provided.")
if set(output_edge_order) != get_subgraph_dangling(nodes):
raise ValueError(
"output edges are not equal to the remaining "
"non-contracted edges of the final node."
)
def _get_path_nodes(nodes: Iterable[BaseNode], algorithm: Algorithm
) -> Tuple[List[Tuple[int, int]], List[BaseNode]]:
"""Calculates the contraction paths using `opt_einsum` methods.
Args:
nodes: An iterable of nodes.
algorithm: `opt_einsum` method to use for calculating the contraction path.
Returns:
The optimal contraction path as returned by `opt_einsum`.
"""
sorted_nodes = sorted(nodes, key=lambda n: n.signature)
input_sets = [set(node.edges) for node in sorted_nodes]
output_set = get_subgraph_dangling(nodes)
size_dict = {edge: edge.dimension for edge in get_all_edges(nodes)}
return algorithm(input_sets, output_set, size_dict), sorted_nodes
memory_limit: Maximum number of elements in an array during contractions.
ignore_edge_order: An option to ignore the output edge order.
Returns:
Final node after full contraction.
"""
n = len(list(nodes)) #pytype thing
_nodes = nodes
if n <= 0:
raise ValueError("Cannot contract empty tensor network.")
if n == 1:
if not ignore_edge_order:
if output_edge_order is None:
output_edge_order = list(
(get_all_edges(_nodes) - get_all_nondangling(_nodes)))
if len(output_edge_order) > 1:
raise ValueError("The final node after contraction has more than "
"one dangling edge. In this case `output_edge_order` "
"has to be provided.")
edges = get_all_nondangling(_nodes)
if edges:
final_node = contract_parallel(edges.pop())
else:
final_node = list(_nodes)[0]
final_node.reorder_edges(output_edge_order)
if not ignore_edge_order:
final_node.reorder_edges(output_edge_order)
return final_node
if n < 5:
"""
Save an iterable of nodes into hdf5 format.
Args:
nodes: An iterable of connected nodes. All nodes have to connect within
`nodes`.
path: path to file where network is saved.
"""
if reachable(nodes) > set(nodes):
raise ValueError(
"Some nodes in `nodes` are connected to nodes not contained in `nodes`."
" Saving not possible.")
if len(set(nodes)) < len(list(nodes)):
raise ValueError(
'Some nodes in `nodes` appear more than once. This is not supported')
#we need to iterate twice and order matters
edges = list(get_all_edges(nodes))
nodes = list(nodes)
old_edge_names = {n: edge.name for n, edge in enumerate(edges)}
old_node_names = {n: node.name for n, node in enumerate(nodes)}
#generate unique names for nodes and edges
#for saving them
for n, node in enumerate(nodes):
node.set_name('node{}'.format(n))
for e, edge in enumerate(edges):
edge.set_name('edge{}'.format(e))
with h5py.File(path, 'w') as net_file:
nodes_group = net_file.create_group('nodes')
node_names_group = net_file.create_group('node_names')