pytreenet.random package¶
Submodules¶
pytreenet.random.random_hamiltonian module¶
This module supplies all functions to generate random Hamiltonians.
The Hamiltonians can be both symbolic and numeric.
- pytreenet.random.random_hamiltonian.random_hamiltonian(num_of_terms: int, possible_operators: List[str] | List[ndarray], tree: TreeStructure, strength: Tuple[float, float] = (-1, 1), num_sites: Tuple[int, int] = (2, 2), conversion_dict: Dict[str, ndarray] | None = None, seed: None | int | Generator = None) Hamiltonian¶
Generates a random Hamiltonian.
The function generates a Hamiltonian with a given number of terms. The operators acting in each term are randomly chosen.
- Parameters:
num_of_terms (int) – The number of terms in the Hamiltonian.
possible_operators (Union[List[str],List[ndarray]]) – A list of all possible single site operators. The operators can be symbolic or numerical.
tree (TreeStructure) – The tree structure that the Hamiltonian should be compatible with.
strength (Tuple[float,float], optional) – The range of strengths that the interaction terms can have. Defaults to (-1,1).
num_sites (Tuple[int,int], optional) – The range of the number of sites that can partake in an interaction term. This means the number of sites that have one of the possible operators applied to them. Defaults to (2,2).
conversion_dict (Union[None,Dict[str,ndarray]], optional) – A dictionary that maps the symbolic operators to numerical matrices. Defaults to None.
- Returns:
A random Hamiltonian.
- Return type:
- pytreenet.random.random_hamiltonian.random_hamiltonian_compatible() Hamiltonian¶
Generates a Hamiltonian that is compatible with the TTNS produced by ptn.ttns.random_big_ttns_two_root_children. It is already padded with identities.
- Returns:
A Hamiltonian to use for testing.
- Return type:
- pytreenet.random.random_hamiltonian.random_numeric_term(possible_operators: List[ndarray], sites: List[str], min_strength: float = -1, max_strength: float = 1, min_num_sites: int = 2, max_num_sites: int = 2, seed: None | int | Generator = None) TensorProduct¶
Generate a single random numeric interaction term.
- Parameters:
possible_operators (Union[List[str],List[ndarray]]) – A list of all possible single site operators.
sites (List[str]) – A list containing the possible identifiers of sites/nodes.
min_strength (float, optional) – Minimum strength an interaction term can have. The strength is multiplied to the first operator of the term. Defaults to -1 and is ignored for symbolic operators.
max_strength (float, optional) – Maximum strength an interaction term can have. The strength is multiplied to the first operator of the term. Defaults to 1 and is ignored for symbolic operators.
min_num_sites (int, optional) – The minimum number of sites that can partake in an interaction term, i.e. have one of the possible operators applied to them. Defaults to 2.
max_num_sites (int, optional) – The maximum number of sites that can partake in an interaction term, i.e. have one of the possible operators applied to them. Defaults to 2.
seed (Union[None,int,Generator], optional) – A seed for the random number generator or a generator itself. Defaults to None.
- Returns:
- A random term in the form of a tensor product with
matrices as single site operators.
- Return type:
- pytreenet.random.random_hamiltonian.random_numeric_terms(num_of_terms: int, possible_operators: List[ndarray], sites: List[str], min_strength: float = -1, max_strength: float = 1, min_num_sites: int = 2, max_num_sites: int = 2, seed: None | int | Generator = None) List[TensorProduct]¶
Generates random numeric interaction terms.
The function generates a given number of interaction terms from a list of possible matrices. A random strength can be assigned to each term.
- Parameters:
num_of_terms (int) – The number of random terms to be generated.
possible_operators (Union[List[str],List[ndarray]]) – A list of all possible single site operators.
sites (List[str]) – A list containing the possible identifiers of sites/nodes.
min_strength (float, optional) – Minimum strength an interaction term can have. The strength is multiplied to the first operator of the term. Defaults to -1 and is ignored for symbolic operators.
max_strength (float, optional) – Maximum strength an interaction term can have. The strength is multiplied to the first operator of the term. Defaults to 1 and is ignored for symbolic operators.
min_num_sites (int, optional) – The minimum number of sites that can partake in an interaction term, i.e. have one of the possible operators applied to them. Defaults to 2.
max_num_sites (int, optional) – The maximum number of sites that can partake in an interaction term, i.e. have one of the possible operators applied to them. Defaults to 2.
seed (Union[None,int,Generator], optional) – A seed for the random number generator or a generator itself. Defaults to None.
- Returns:
A list containing all the random terms.
- Return type:
List[TensorProduct]
- pytreenet.random.random_hamiltonian.random_symbolic_term(possible_operators: List[str], sites: List[str], min_num_sites: int = 2, max_num_sites: int = 2, seed: None | int | Generator = None) TensorProduct¶
Generates a random symbolic interaction term.
- Parameters:
possible_operators (list[ndarray]) – Symbolic operators to choose from.
sites (list[str]) – Identifiers of the nodes to which they may be applied.
num_sites (int, optional) – Number of non-trivial sites in a term. Defaults to 2.
seed (Union[int, None], optional) – A seed for the random number generator. Defaults to None.
- Returns:
A random term in the form of a tensor product
- Return type:
- pytreenet.random.random_hamiltonian.random_symbolic_terms(num_of_terms: int, possible_operators: List[str], sites: List[str], min_num_sites: int = 2, max_num_sites: int = 2, seed=None) List[TensorProduct]¶
Creates random symbolic interaction terms.
- Parameters:
num_of_terms (int) – The number of random terms to be generated.
possible_operators (List[str]) – A list of all possible single site operators.
sites (List[str]) – A list containing the possible identifiers of sites/nodes.
min_num_sites (int, optional) – The minimum number of sites that can partake in an interaction term, i.e. have one of the possible operators applied to them. Defaults to 2.
max_num_sites (int, optional) – The maximum number of sites that can partake in an interaction term, i.e. have one of the possible operators applied to them. Defaults to 2.
seed (Union[None,int,Generator], optional) – A seed for the random number generator or a generator itself. Defaults to None.
- Returns:
A list containing all the random terms.
- Return type:
List[TensorProduct]
- pytreenet.random.random_hamiltonian.random_terms(num_of_terms: int, possible_operators: List[str] | List[ndarray], sites: List[str], min_strength: float = -1, max_strength: float = 1, min_num_sites: int = 2, max_num_sites: int = 2, seed: None | int | Generator = None) List[TensorProduct]¶
Generates random interaction terms.
The function generates a given number of interaction terms from a list of possible operators. The operators can be symbolic or numerical and a random strength can be assigned to each term.
- Parameters:
num_of_terms (int) – The number of random terms to be generated.
possible_operators (Union[List[str],List[ndarray]]) – A list of all possible single site operators.
sites (List[str]) – A list containing the possible identifiers of sites/nodes.
min_strength (float, optional) – Minimum strength an interaction term can have. The strength is multiplied to the first operator of the term. Defaults to -1 and is ignored for symbolic operators.
max_strength (float, optional) – Maximum strength an interaction term can have. The strength is multiplied to the first operator of the term. Defaults to 1 and is ignored for symbolic operators.
min_num_sites (int, optional) – The minimum number of sites that can partake in an interaction term, i.e. have one of the possible operators applied to them. Defaults to 2.
max_num_sites (int, optional) – The maximum number of sites that can partake in an interaction term, i.e. have one of the possible operators applied to them. Defaults to 2.
seed (Union[None,int,Generator], optional) – A seed for the random number generator or a generator itself. Defaults to None.
- Returns:
A list containing all the random terms.
- Return type:
List[TensorProduct]
pytreenet.random.random_matrices module¶
This module contains functions to generate random matrices.
- pytreenet.random.random_matrices.crandn(size: Tuple[int, ...]) ndarray¶
- Draw random samples from the standard complex normal (Gaussian)
distribution.
- Parameters:
size (Tuple[int,...]) – The size/shape of the output array.
- Returns:
The array of random complex numbers.
- Return type:
np.ndarray
- pytreenet.random.random_matrices.random_hermitian_matrix(size: int = 2) ndarray¶
Creates a random hermitian matrix H^dagger = H
- Parameters:
size (int, optional) – Size of the matrix. Defaults to 2.
- Returns:
The hermitian matrix.
- Return type:
np.ndarray
- pytreenet.random.random_matrices.random_matrix(size: int = 2) ndarray¶
Creates a random matrix of given size.
- Parameters:
size (int, optional) – Size of the matrix. Defaults to 2.
- Returns:
The random matrix.
- Return type:
np.ndarray
- pytreenet.random.random_matrices.random_unitary_matrix(size: int = 2) ndarray¶
Creates a random unitary matrix U^dagger U = I
- Parameters:
size (int, optional) – Size of the matrix. Defaults to 2.
- Returns:
The unitary matrix.
- Return type:
np.ndarray
pytreenet.random.random_node module¶
Module to generate a random node.
- pytreenet.random.random_node.random_graph_node() GraphNode¶
Create a graph node with a random identifier.
- pytreenet.random.random_node.random_tensor_node(shape, identifier: str = '')¶
Creates a tensor node with an a random associated tensor with shape=shape.
pytreenet.random.random_tensorproduct module¶
This module allows to generate random tensor products.
The randomly generated tensor products can be either symbolic or numeric. They can also be generated from a reference tree or by providing a list of possible operators.
- pytreenet.random.random_tensorproduct.random_numeric_tensor_product(ref_tree: TreeTensorNetwork, num_operators: int = 1, possible_operators: List[ndarray] | None = None, factor: float = 1.0, seed: None | int | Generator = None) TensorProduct¶
Generates a random numeric tensor product from a reference tree.
- Parameters:
ref_tree (TreeTensorNetwork) – A reference TreeTensorNetwork. It provides the identifiers and potentially the operators for the tensor product.
num_operators (int, optional) – The number of operators in the tensor product. These are the non-identity operators. Defaults to 1.
possible_operators (Union[List[np.ndarray],None], optional) – A list of possible operators that can be chosen as non-identity operators. If none are provided, the function will generate a numeric operator, infering the dimensions from the reference tree. Defaults to None.
factor (float, optional) – A factor that is multiplied to the operators. Defaults to 1.0.
seed (Union[int,Generator,None], optional) – A seed for the random number generator or a generator itself. Defaults to None.
- Returns:
The generated random tensor product.
- Return type:
- pytreenet.random.random_tensorproduct.random_numeric_tensor_product_from_list(identifiers: List[str], possible_operators: List[ndarray], num_operators: int = 1, factor: float = 1.0, seed: None | int | Generator = None) TensorProduct¶
Generates a random numeric tensor product.
- Parameters:
identifiers (List[str]) – A list of identifiers for the operators in the tensor product.
possible_operators (List[np.ndarray]) – A list of possible operators that can be chosen as non-identity operators.
num_operators (int, optional) – The number of operators in the tensor product. These are the non-identity operators. Defaults to 1.
factor (float, optional) – A factor that is multiplied to the operators. Defaults to 1.0.
seed (Union[int,Generator,None], optional) – A seed for the random number generator or a generator itself. Defaults to None.
- Returns:
The generated random tensor product.
- Return type:
- pytreenet.random.random_tensorproduct.random_numeric_tensor_product_from_tree(ref_tree: TreeTensorNetwork, num_operators: int = 1, factor: float = 1.0, seed: None | int | Generator = None) TensorProduct¶
Generates a random numeric tensor product from a reference tree.
- Parameters:
ref_tree (TreeTensorNetwork) – A reference TreeTensorNetwork. It provides the identifiers and dimensions for the operators in the tensor product.
num_operators (int, optional) – The number of operators in the tensor product. These are the non-identity operators. Defaults to 1.
factor (float, optional) – A factor that is multiplied to the operators. Defaults to 1.0.
seed (Union[int,Generator,None], optional) – A seed for the random number generator or a generator itself. Defaults to None.
- pytreenet.random.random_tensorproduct.random_symbolic_tensor_product(identifiers: List[str], possible_operators: List[str], num_operators: int = 1, seed: None | int | Generator = None) TensorProduct¶
Generates a random symbolic tensor product.
- Parameters:
identifiers (List[str]) – A list of identifiers for the operators in the tensor product.
possible_operators (List[str]) – A list of possible operators that can be chosen as non-identity operators.
num_operators (int, optional) – The number of operators in the tensor product. These are the non-identity operators. Defaults to 1.
seed (Union[int,Generator,None], optional) – A seed for the random number generator or a generator itself. Defaults to None.
- Returns:
The generated random tensor product.
- Return type:
- pytreenet.random.random_tensorproduct.random_tensor_product(ref_tree: TreeTensorNetwork, num_operators: int = 1, possible_operators: List[str] | List[ndarray] | None = None, factor: float = 1.0, seed: None | int | Generator = None) TensorProduct¶
- Generates a random tensor product that is compatible with the reference
TreeTensorNetwork.
- Parameters:
ref_tree (TreeTensorNetwork) – A reference TreeTensorNetwork. It provides the identifiers and dimensions for the operators in the tensor product.
num_operators (int, optional) – The number of operators in the tensor product. These are the non-identity operators. Defaults to 1.
possible_operators (Union[List[str],List[np.ndarray],None]) – A list of possible operators that can be chosen as non-identity operators. These can be either strings or numpy arrays, defining a symbolic or numeric operator. If none are provided, the function will generate a numeric operator, infering the dimensions from the reference tree. Defaults to None.
factor (float, optional) – A factor that is multiplied to the operators. Ignored for symbolic operators. Defaults to 1.0.
seed (Union[int,Generator,None], optional) – A seed for the random number generator or a generator itself. Defaults to None.
- Returns:
The generated random tensor product.
- Return type:
pytreenet.random.random_ttns module¶
This module contains functions generating random Tree Tensor Network States.
There are a variety of given tree topologies, which can be filled with random tensors.
- class pytreenet.random.random_ttns.RandomTTNSMode(value)¶
Bases:
EnumAn enumeration for the different modes of random generation of TTNS.
The modes are usually concerned with the different ways to choose the virtual bond dimensions.
SAME: All bond dimensions are chosen equal
- DIFFVIRT: Virtual dimensions are chosen different. The exact size depends
on the topology used.
SAMEPHYS: Forces the same physical dimensions for the TTNS.
- DIFFVIRT = 'different_virt_dimensions'¶
- SAME = 'same_dimension'¶
- SAMEPHYS = 'same_phys_dim'¶
- pytreenet.random.random_ttns.random_big_ttns(mode: RandomTTNSMode = RandomTTNSMode.SAME) TreeTensorNetworkState¶
Generates a big TTNS
The node identifiers of the form “site” + int. The identifiers and dimensions are set, but the associated tensors are random.
- Parameters:
mode (RandomTTNSMode) – The mode of random generation of the TTNS. Currently the only mode supported is SAME.
- Returns:
A random TTNS with the following topology:
1------6-----7 / \ \ / \ \ / \ 8 2 4 | | | | 3 5
- Return type:
- pytreenet.random.random_ttns.random_big_ttns_two_root_children(mode: RandomTTNSMode = RandomTTNSMode.SAME) TreeTensorNetworkState¶
Returns a random big TTNS where the root has only two children.
For testing it is important to know that the children of 1 will be in a different order, if the TTNS is orthogonalised.
- Parameters:
mode (RandomTTNSMode) – The mode of random generation of the TTNS. If it is SAME all legs will be chosen as 2. For DIFFVIRT the virtual bond dimensons are all different.
- Returns:
A random TTNS with the topology:
0 / \ / \ 1 6 / \ \ / \ \ 2 3 7 / \ / \ 4 5
- Return type:
- pytreenet.random.random_ttns.random_small_ttns(mode: RandomTTNSMode = RandomTTNSMode.DIFFVIRT) TreeTensorNetworkState¶
Generates a small TreeTensorNetworkState of three nodes: The root (“root”) and its two children (“c1” and “c2”). The associated tensors are random, but their dimensions are set.
- Parameters:
mode (RandomTTNSMode) –
The mode of random generation of the TTNS. If mode is DIFFVIRT, the virtual bond dimensions are as follows:
|2 | r / \ 3| 5/ 6\ |4 | / \ | c1 c2
Otherwise all virtual bond dimensions default to 2. If the mode is SAMEPHYS all phyiscal dimensions will default to 2.
- Returns:
- A tree tensor network with the above topology and
randomly filled tensors.
- Return type: