pytreenet.contractions package¶
Submodules¶
pytreenet.contractions.contraction_util module¶
Modul that contains utility functions for contractions.
The functions here are used in mutliple different kinds of contractions.
- pytreenet.contractions.contraction_util.contract_all_but_one_neighbour_block_to_hamiltonian(hamiltonian_tensor: ndarray, hamiltonian_node: Node, next_node_id: str, partial_tree_cache: PartialTreeCachDict) ndarray¶
Contract all neighbour blocks to the Hamiltonian tensor.
- Parameters:
hamiltonian_tensor (np.ndarray) – The tensor of the Hamiltonian node.
hamiltonian_node (Node) – The Hamiltonian node.
next_node_id (str) – The identifier of the node to which the remaining virtual leg points.
partial_tree_cache (PartialTreeCacheDict) – The dictionary containing the already contracted subtrees.
- Returns:
The resulting tensor:
_____ out | |____ | | 4 | | |1 | | ___|__ | | | | | |____| H |_____ | | | | 0 | | |______| | | | | | |2 | | | |_____ |_____| 3 in
- Return type:
np.ndarray
- pytreenet.contractions.contraction_util.contract_all_but_one_neighbour_block_to_ket(ket_tensor: ndarray, ket_node: Node, next_node_id: str, partial_tree_cache: PartialTreeCachDict) ndarray¶
Contract all neighbour blocks to the ket tensor.
- Parameters:
ket_tensor (np.ndarray) – The tensor of the ket node.
ket_node (Node) – The ket node.
next_node_id (str) – The identifier of the node to which the remaining virtual legs point.
partial_tree_cache (PartialTreeCacheDict) – The dictionary containing the already contracted subtrees.
- pytreenet.contractions.contraction_util.contract_all_neighbour_blocks_to_hamiltonian(hamiltonian_tensor: ndarray, hamiltonian_node: Node, partial_tree_cache: PartialTreeCachDict) ndarray¶
Contract all neighbour blocks to the hamiltonian tensor.
- Parameters:
hamiltonian_tensor (np.ndarray) – The tensor of the hamiltonian node.
hamiltonian_node (Node) – The hamiltonian node.
partial_tree_cache (PartialTreeCacheDict) – The dictionary containing the already contracted subtrees.
- Returns:
The resulting tensor:
_____ _____ | |____2 2_____| | | | | | | | | | | | | ___|__ | | | | | | | | | |____| |_____| | | | 1 | H | 1 | | | | |______| | | | | | | | | | | | | | | | | | |_____ _____| | |_____| 0 0 |_____|
- Return type:
np.ndarray
- pytreenet.contractions.contraction_util.contract_all_neighbour_blocks_to_ket(ket_tensor: ndarray, ket_node: Node, partial_tree_cache: PartialTreeCachDict) ndarray¶
Contract all neighbour blocks to the ket tensor.
- Parameters:
ket_tensor (np.ndarray) – The tensor of the ket node.
ket_node (Node) – The ket node.
partial_tree_cache (PartialTreeCacheDict) – The dictionary containing the already contracted subtrees.
- Returns:
The resulting tensor:
______ ______ | |____ ____| | | |n n| | | | : : | | | |____ ____| | | C1 |1 | 1| C2 | | | __|__ | | | |____| |____| | | |0 | A | 0| | |______| |_____| |______|
- Return type:
np.ndarray
- pytreenet.contractions.contraction_util.contract_neighbour_block_to_hamiltonian(hamiltonian_tensor: ndarray, hamiltonian_node: Node, neighbour_id: str, partial_tree_cache: PartialTreeCachDict, tensor_leg_to_neighbour: None | int = None) ndarray¶
- Contract the Hamiltonian tensor, i.e. H in the diagrams, with one neighbouring
block, C in the diagrams.
- Parameters:
hamiltonian_tensor (np.ndarray) – The tensor of the Hamiltonian node.
hamiltonian_node (Node) – The Hamiltonian node.
neighbour_id (str) – The identifier of the neighbour node which is the root node of the subtree that has already been contracted and is saved in the dictionary.
tensor_leg_to_neighbour (int) – The index of the leg of the Hamiltonian tensor that points to the neighbour block and is thus to be contracted.
partial_tree_cache (PartialTreeCacheDict) – The dictionary containing the already contracted subtrees. The tensors in here can have an arbitrary number of legs, but the first leg is the one that is contracted with the Hamiltonian tensor.
- Returns:
The resulting tensor:
_____ out | |____ | | 4 | | |1 | | ___|__ | | | | | |____| H |_____ | | | | 0 | | |______| | | | | | |2 | | | |_____ |_____| 3 in
- Return type:
np.ndarray
- pytreenet.contractions.contraction_util.contract_neighbour_block_to_hamiltonian_ignore_one_leg(hamiltonian_tensor: ndarray, hamiltonian_node: Node, neighbour_id: str, ignoring_node_id: str, partial_tree_cache: PartialTreeCachDict) ndarray¶
Contract all neighbour blocks to the Hamiltonian tensor.
- Parameters:
hamiltonian_tensor (np.ndarray) – The tensor of the Hamiltonian node.
hamiltonian_node (Node) – The Hamiltonian node.
neighbour_id (str) – The identifier of the neighbour node which is the root node of the subtree that has already been contracted and is saved in the dictionary.
ignoring_node_id (str) – The identifier of the node to which the virtual leg should not point.
partial_tree_cache (PartialTreeCacheDict) – The dictionary containing the already contracted subtrees.
- Returns:
The resulting tensor:
_____ out | |____ | | 4 | | |1 | | ___|__ | | | | | |____| H |_____ | | | | 0 | | |______| | | | | | |2 | | | |_____ |_____| 3 in
- Return type:
np.ndarray
- pytreenet.contractions.contraction_util.contract_neighbour_block_to_ket(ket_tensor: ndarray, ket_node: Node, neighbour_id: str, partial_tree_cache: PartialTreeCachDict, tensor_leg_to_neighbour: None | int = None) ndarray¶
Contracts the ket tensor with one neighbouring block.
This means A in the diagram is contracted with C in the diagram.
- Parameters:
ket_tensor (np.ndarray) – The tensor of the ket node.
ket_node (Node) – The ket node.
neighbour_id (str) – The identifier of the neighbour node which is the root node of the subtree that has already been contracted and is saved in the dictionary.
tensor_leg_to_neighbour (int) – The index of the leg of the ket tensor that points to the neighbour block and is thus to be contracted.
partial_tree_cache (PartialTreeCacheDict) – The dictionary containing the already contracted subtrees. The tensors in here can have an arbitrary number of legs, but the first leg is the one that is contracted with the ket tensor.
- Returns:
The resulting tensor:
______ ____| | . n | | : | | ____| | | 1 | C | __|__ | | ____| |____| | | A | 0 | | |_____| |______|
- Return type:
np.ndarray
- pytreenet.contractions.contraction_util.contract_neighbour_block_to_ket_ignore_one_leg(ket_tensor: ndarray, ket_node: Node, neighbour_id: str, ignoring_node_id: str, partial_tree_cache: PartialTreeCachDict) ndarray¶
Contracts the ket tensor with one neighbouring block, ignoring one leg.
This means the ket tensor, i.e. A in the diagrams, is contracted with one neighbouring block, C in the diagrams, ignoring one leg.
- Parameters:
ket_tensor (np.ndarray) – The tensor of the ket node.
ket_node (Node) – The ket node.
neighbour_id (str) – The identifier of the neighbour node which is the root node of the subtree that has already been contracted and is saved in the dictionary.
ignoring_node_id (str) – The identifier of the node to which the virtual leg should not point.
partial_tree_cache (PartialTreeCacheDict) – The dictionary containing the already contracted subtrees. The tensors in here can have an arbitrary number of legs, but the first leg is the one that is contracted with the ket tensor.
- Returns:
The resulting tensor:
______ ____| | . n | | : | | ____| | | 1 | C | __|__ | | ____| |____| | | A | 0 | | |_____| |______|
- Return type:
np.ndarray
- pytreenet.contractions.contraction_util.determine_index_with_ignored_leg(node: Node, neighbour_id: str, ignoring_node_id: str) int¶
Determine the index of neighbour legs, while ignoring one leg.
Sometimes when contracting all the neighbouring cached environments, we want to ignore the leg to a specific neighbour node. This means we do not want to contract that leg. This function determines the leg index of the current tensor, that should actually be contracted. This means earlier contractions are already taken into account.
- Parameters:
node (Node) – The node for which find the leg indices.
neighbour_id (str) – The identifier of the neighbour node.
ignoring_node_id (str) – The identifier of the neighbour leg to ignore.
- pytreenet.contractions.contraction_util.get_equivalent_legs(node1: Node, node2: Node, ignore_legs: None | List[str] | str = None) Tuple[List[int], List[int]]¶
- Get the equivalent legs of two nodes. This is useful when contracting
two nodes with equal neighbour identifiers, that may potentially be in different orders. Some neighbours may also be ignored.
- Parameters:
- Returns:
- The equivalent legs of the two nodes. This
means the indeces of legs to the same neighbour are at the same position in each list.
- Return type:
Tuple[List[int],List[int]]
pytreenet.contractions.state_operator_contraction module¶
This module provides functions to contract a TTNS with a TTNO
- pytreenet.contractions.state_operator_contraction.expectation_value(state: TreeTensorNetworkState, operator: TTNO) complex¶
Computes the Expecation value of a state with respect to an operator.
The operator is given as a TTNO and the state as a TTNS. The expectation is obtained by “sandwiching” the operator between the state and its complex conjugate: <psi|H|psi>.
- Parameters:
state (TreeTensorNetworkState) – The TTNS representing the state.
operator (TTNO) – The TTNO representing the Operator.
- Returns:
The expectation value.
- Return type:
complex
pytreenet.contractions.state_state_contraction module¶
This module provides the function to contract two TreeTensorNetworkStates.
The main function is contract_two_ttns(ttn1, ttn2), which contracts two TreeTensorNetworkStates with the same underlying tree structure. Therefore the contraction is basically the scalar product of the first state with the complex conjugate of the second state. <TTN2*|TTN1>
Using explicit imports from this module provides utility functions to contract subtrees and leaf nodes of two states.
- pytreenet.contractions.state_state_contraction.contract_two_ttns(ttn1: TreeTensorNetworkState, ttn2: TreeTensorNetworkState) complex¶
Contracts two TreeTensorNetworks.
- Parameters:
ttn1 (TreeTensorNetwork) – The first TreeTensorNetwork.
ttn2 (TreeTensorNetwork) – The second TreeTensorNetwork.
- Returns:
The resulting scalar product <TTN1|TTN2>
- Return type:
complex
pytreenet.contractions.tree_cach_dict module¶
This module provides the PartialTreeCachDict class.
This class is a dictionary that can in general be used to save tensors that depend on two nodes of a tree. The keys are the tuples of the two identifiers. The main use case is to save the contraction results of subtrees during larger contraction operations. For example during the TDVP algorithm or when computing the expecation value of a TTNO with respect to a given TTNS.
- class pytreenet.contractions.tree_cach_dict.PartialTreeCachDict(dictionary: Dict[Tuple[str, str]] | None = None)¶
Bases:
dictA dictionary to save the chached subtrees during contractions.
The main use case is to save the contraction results of subtrees during larger contraction operations. For example during the TDVP algorithm or when computing the expecation value of a TTNO with respect to a given TTNS.
The keys are tuples of size two. The first entry is usually the identifier of a node that acts as the root of a subtree that was contracted. The second identifier is the identifier of the node to which the open legs of the contracted subtree point.
- add_entry(node_id: str, next_node_id: str, cached_tensor: ndarray)¶
Saves a tensor in the dictionary.
Basically a wrapper for the __setitem__ method to ensure the correct order of keys.
- Parameters:
node_id (str) – The identifier where the subtree ends.
next_node_id (str) – The identifier to which the open legs point.
cached_tensor (np.ndarray) – The corresponding tensor.
- change_next_id_for_entry(node_id: str, old_next_id, new_next_id: str)¶
Canges the key for a given cached tensor.
For a given cached tensor the identifier to which the open legs of the chached tensor point are changed.
- Parameters:
node_id (str) – The node to which the entry tensor correpsonds.
old_next_id (str) – The old identifier of the node to which the open legs point.
new_next_id (str) – The new identifier of the node to which the open legs point.
- delete_entry(node_id: str, next_node_id: str)¶
Deletes an entry in the dictionary.
Basically a wrapper for the __delitem__ method to ensure the correct order of keys.
- Parameters:
node_id (str) – The identifier where the subtree ends.
next_node_id (str) – The identifier to which the open legs point.
- get_entry(node_id: str, next_node_id: str) ndarray¶
Returns the cached tensor saved.
Basically a wrapper for the __getitem__ method to ensure the correct order of keys.
- Parameters:
node_id (str) – The identifier where the subtree tree ends.
next_node_id (str) – The identifier to which the open legs point.
- Returns:
The corresponding contracted subtreetree tensor.
- Return type:
np.ndarray
pytreenet.contractions.tree_contraction module¶
This module provides a function to completely contract a tree tensor network.
This function is mainly used for debugging purposes, as it can get very costly in memory very quickly. The function contracts the complete tree into one high degree tensor and also provides the order in which the nodes were contracted. The order is then the order of the open legs in the final tensor.
- pytreenet.contractions.tree_contraction.completely_contract_tree(ttn: TreeTensorNetwork, to_copy: bool = False) Tuple[ndarray, List[str]]¶
Completely contracts the given tree tensor network by contracting (WARNING: Can get very costly very fast. Only use for debugging.)
- Parameters:
ttn (TreeTensorNetwork) – The TTN to be contracted.
to_copy (bool) – Wether or not the contraction should be perfomed on a deep copy. Default is False.
- Returns:
- The contracted TTN and the list of the
identifiers of the contracted nodes in the order they were contracted. The latter is very useful for debugging.
- Return type:
Tuple[np.ndarray, List[str]]