pytreenet.operators package¶
This submodule provides all kinds of operators.
They do not have to have a tree form.
Submodules¶
pytreenet.operators.common_operators module¶
This module provides commonly used operators as numpy arrays.
The operators provided are * Pauli matrices X, Y, and Z * Bosonic creation, annihilation, and number operator * SWAP gates of arbitrary dimension
- pytreenet.operators.common_operators.bosonic_operators(dimension: int = 2) Tuple[ndarray, ndarray, ndarray]¶
Supplies the common bosonic operators.
The common operators are the creation, annihilation, and number operator.
- Parameters:
dimension (int, optional) – The dimension of the bosonic space to be considered. This determines the size of all the operators. Defaults to 2.
- Returns:
creation_op: Bosonic creation operator.
annihilation_op: Bosonic anihilation operator.
number_op: The bosonic number operator, i.e. a diagonal matrix with increasing integers on the diagonal from 0 to dimension-1.
- Return type:
Tuple[np.ndarray,np.ndarray,np.ndarray]
- pytreenet.operators.common_operators.pauli_matrices() Tuple[ndarray, ndarray, ndarray]¶
Returns the three Pauli matrices X, Y, and Z in Z-basis as ndarray.
- pytreenet.operators.common_operators.projector(dimension: int, index: int) ndarray¶
A projector on a specific index of a system with a given dimension.
- Parameters:
dimension (int) – The dimension of the system.
index (int) – The index of the state to project to.
- Returns:
The projector on the index of the system.
- Return type:
np.ndarray
- pytreenet.operators.common_operators.swap_gate(dimension: int = 2) ndarray¶
A SWAP gate acts on two systems with the same pysical dimension and swaps their states.
- Parameters:
dimension (int, optional) – Physical dimension of the two sites, which has to be the same for both. Defaults to 2.
- Returns:
A SWAP-gate for two dimension-dimensional systems.
- Return type:
np.ndarray
pytreenet.operators.hamiltonian module¶
This module provides a class to represent the Hamiltonian of a system.
The Hamiltonian of a quantum mechanical system can be interpreted as the a representation of the energy or potential of the system. It is usually used to define a model in the first place. For quantum systems made up of multiple smaller systems the Hamiltonian is defined as a sum over tensor products. These products define one operator per subsystem and fully define the Hamiltonian.
where \(A_{i}^{[j]}\) is the operator acting on the j-th subsystem of the as part of the i-th term of the Hamiltonian.
- class pytreenet.operators.hamiltonian.Hamiltonian(terms: List[TensorProduct] | TensorProduct | None = None, conversion_dictionary: Dict[str, ndarray] | None = None)¶
Bases:
objectRepresents the Hamiltonian on a TTN.
The entries of the main list should represent the terms of the Hamiltonian and be a dictionary. The key is an identifier which could be matched to a TensorNode in the TTN and the value is an operator that is to be applied to that node/site.
- terms¶
A list of tensor products, each representing one term of the Hamiltonian. Summin over the list would theoretically yield the total Hamiltonian.
- Type:
List[TensorProduct]
- conversion_dictionary¶
A dictionary that can be used to convert symbolically given terms of the Hamiltonian into actual numeric arrays.
- Type:
Dict[str,ndarray]
- add_hamiltonian(other: Hamiltonian)¶
Adds one Hamiltonian to this Hamiltonian.
- Parameters:
other (Hamiltonian) – Hamiltonian to be added. It will not be modified
- add_multiple_terms(terms: list[TensorProduct])¶
Add multiple terms to this Hamiltonian
- Parameters:
terms (list[TensorProduct]) – Terms to be added.
- add_term(term: TensorProduct)¶
Adds a term to the Hamiltonian.
- Parameters:
term (TensorProduct) – The term to be added in the form of a TensorProduct.
- contains_duplicates() bool¶
Checks, if there are duplicates of terms.
Can be especially important after padding.
- Returns:
True if there are duplicates, False otherwise
- Return type:
bool
- is_compatible_with(ttn: TreeTensorNetwork) bool¶
Returns wether the Hamiltonian is compatible with the provided TTN.
Compatibility means that all node identifiers that appear any term of this Hamiltonian are identifiers of nodes in the TTN.
- Parameters:
ttn (TreeTensorNetwork) – The TTN to check against.
- Returns:
Whether the two are compatible or not.
- Return type:
bool
- pad_with_identities(reference_ttn: TreeTensorNetwork, mode: PadMode = PadMode.safe, symbolic: bool = True) Hamiltonian¶
Pads a Hamiltonian with identities.
Returns a Hamiltonian, where all terms are padded with an identity according to the given reference tree tensor network. Make sure to update the resulting Hamiltonian’s conversion dictionary to include any new identity.
- Parameters:
reference_ttn (TreeTensorNetwork) – Provides the structure on which padding should occur. Furthermore the dimension of the open legs of each provide the new identities’ dimensions.
mode (PadMode, optional) – ‘safe’ performs a compatability check with the reference TTN. Risky will not run this check, which might be time consuming for large TTN. Defaults to PadMode.safe.
symbolic (bool, optional) – Whether the terms should be padded with a symbolic identity or an actual array. Defaults to True.
- Returns:
A new Hamiltonian with all terms being padded.
- Return type:
- perform_compatibility_checks(mode: PadMode, reference_ttn: TreeTensorNetwork)¶
Performs the check of the mode and the check of compatibility, if desired.
- Parameters:
mode (PadMode, optional) – ‘safe’ performs a compatability check with the reference ttn. Risky will not run this check, which might be time consuming for large TTN. Defaults to PadMode.safe.
- Raises:
NotCompatibleException – If the Hamiltonian and TTN are not compatible
- to_matrix(ref_ttn: TreeTensorNetwork, use_padding: bool = True, mode: PadMode = PadMode.safe) NumericOperator¶
Creates a numeric operator that is equivalent to the Hamiltonian.
The resulting operator can get very large very fast, so this should only be used for debugging. The result is a matrix valued operator. The resulting operator is a matrix where the dimensions are ordered according to the open dimensions of the nodes in the reference TTN in the order the nodes identifiers are saved in the TTN.
- Parameters:
ref_ttn (TreeTensorNetwork) – TTN giving the tree structure which the Hamiltonian should respect.
use_padding (bool, optional) – Enable, if the Hamiltonian requires padding with respect to the reference TTN. Defaults to True.
mode (PadMode, optional) – ‘safe’ performs a compatability check with the reference TTN. Risky will not run this check, which might be time consuming for large TTN. Defaults to PadMode.safe.
- Returns:
- Operator corresponding to the Hamiltonian. The
actual array is matrix valued.
- Return type:
- to_tensor(ref_ttn: TreeTensorNetwork, use_padding: bool = True, mode: PadMode = PadMode.safe) NumericOperator¶
Creates a NumericOperator that is equivalent to the Hamiltonian.
The resulting operator can get very large very fast, so this should only be used for debugging. The result is a tensor with multiple legs. The legs are ordered according to the open dimensions of the nodes in the reference TTN in the order the nodes identifiers are saved in the TTN.
- Parameters:
ref_ttn (TreeTensorNetwork) – TTN giving the tree structure which the Hamiltonian should respect.
use_padding (bool, optional) – Enable, if the Hamiltonian requires padding with respect to the reference TTN. Defaults to True.
mode (PadMode, optional) – ‘safe’ performs a compatability check with the reference TTN. Risky will not run this check, which might be time consuming for large TTN. Defaults to PadMode.safe.
- Returns:
- Operator corresponding to the Hamiltonian. The
actual array is tensor valued.
- Return type:
- class pytreenet.operators.hamiltonian.PadMode(value)¶
Bases:
EnumWhen padding a Hamiltonian, we can decide to check its compatabilty with a given tree tensor network or not.
- risky = 1¶
- safe = 2¶
- pytreenet.operators.hamiltonian.create_nearest_neighbour_hamiltonian(structure: TreeStructure, local_operator: ndarray | str, conversion_dict: Dict[str, ndarray] | None = None) Hamiltonian¶
- Creates a nearest neighbour Hamiltonian for a given tree structure and
local operator.
- So for every nearest neighbour pair (i,j) the Hamiltonian will contain a
term A_i (x) A_j, where A_i is the local operator at site i.
- Parameters:
structure (TreeStructure) – The tree structure for which the Hamiltonian should be created.
local_operator (Union[ndarray, str]) – The local operator to be used to generate the nearest neighbour interaction, i.e. A. Which is equal for all i.
conversion_dict (Union[Dict[str,ndarray],None]) – A conversion that can be used, if symbolic operators were used. Defaults to None.
- Returns:
The Hamiltonian for the given structure.
- Return type:
- pytreenet.operators.hamiltonian.create_single_site_hamiltonian(structure: TreeStructure, local_operator: str | ndarray, conversion_dict: Dict[str, ndarray] | None = None) Hamiltonian¶
Creates a Hamiltonian for a given tree structure and local operators. The Hamiltonian will contain a term A for every site i
- Parameters:
structure (TreeStructure) – The tree structure for which the Hamiltonian should be created.
local_operator (Union[str, ndarray]) – The local operators to be used to generate the single site interaction, i.e. A_i for every i.
conversion_dict (Union[Dict[str,ndarray],None]) – A conversion that can be used, if symbolic operators were used. Defaults to None.
- Returns:
The Hamiltonian for the given structure.
- Return type:
pytreenet.operators.operator module¶
Provides an operator class.
This class can be used to store the numeric value of an operator in conjuction with the identifiers of the node it is applied. It also contains utility functions that allow treatment of a high dimensional array as one would a matrix. For example by testing the unitarity or exponentiating the operator.
- class pytreenet.operators.operator.NumericOperator(operator: ndarray, node_identifiers: List[str] | str)¶
Bases:
objectAn operator that hold the operator and identifiers it is applied to.
- Atrributes:
- operator (np.ndarray): The numeric value of this operator. If this is
matrix valued, the convention of the leg order is
(output_leg, input_leg). If it is instead a higher dimensional tensor the leg order is defined as(output_leg1, ..., output_legn, input_leg1, input_leg2, ..., input_legn)- identifiers (List[str]): The identifiers of the nodes this operator is
applied to. Should be in the same order as the legs of the operator.
- is_unitary() bool¶
Returns whether this operator is unitary or not.
- to_matrix() NumericOperator¶
Turns an operator into a matrix.
- Returns:
- A new NumericOperator whose operator is the
current operator but in matrix form, i.e. all input legs are combined and all output legs are combined.
- Return type:
- to_tensor(dim: None | int = None, ttn: TreeTensorNetwork | None = None) NumericOperator¶
Turns an operator in matrix form into a high dimensional tensor.
At least one of dim or ttn has to be given.
- Parameters:
dim (Union[int, None], optional) – If all nodes have the same open dimension it can be given here. Defaults to None.
ttn (Union[TreeTensorNetwork, None], optional) – If not all nodes have the same open dimension a TTN is needed as a reference. Defaults to None.
- Returns:
- A NumericOperator equivalent to the original
matrix valued operator but in tensor form.
- Return type:
pytreenet.operators.tensorproduct module¶
The tensor product class is used to represent the tensor product of multiple single site operators. It is basically a dictionary mapping a node to an operator that should be applied to it. This works for both symbolic and numeric operators. In general tensor products form the basis of many general operators and are used frequently in conjuction with tensor networks. In general the tensor product is defined as:
where \(A^{[i]}\) is an operator acting on the i-th site. The operator can be any appropriately sized operator and does not need to be unitary or hermitian. In many cases sites on which only the identity is applied are left out of the tensor product. For example for three nodes we would have:
where \(I^{[3]}\) is the identity operator acting on the third site.
- class pytreenet.operators.tensorproduct.TensorProduct(matrix_dict: Dict[str, ndarray | str] | None = None)¶
Bases:
UserDictA class representing a tensor product of multiple single site operators.
\[\bigotimes_{i} A^{[i]}\]where \(A^{[i]}\) is an operator acting on the i-th site.
The tensor product is represented as a dictionary mapping a node to an operator.
- allclose(other: TensorProduct) bool¶
Returns, whether the two tensor products are close to each other.
This means that the operators corresponding to each identifier are close.
- exp(factor: Number = 1) NumericOperator¶
Compute the exponential of a tensor product.
Notably it will not be a tensor product anymore but a general operator.
- Parameters:
factor (Number, optional) – A factor that is multiplied to the exponent. Defaults to 1.
- Returns:
- The exponentiated term. The identifiers in the
operator are in the same order as the tensor product keys.
- Return type:
- classmethod from_operators(operators: List[NumericOperator]) TensorProduct¶
Obtain a tensor_product from multiple single site operators.
- Parameters:
operators (List[NumericOperator]) – A list of single site operators.
- Returns:
The tensor product of the operators.
- Return type:
- into_operator(conversion_dict: Dict[str, ndarray] | None = None, order: List[str] | None = None) NumericOperator¶
Computes the numeric value of a tensor product.
The actual numeric operator is obtained by performing the tensor product of all single site operators. If the tensor product contains symbolic operators, a conversion dictionary has to be provided.
- Parameters:
conversion_dict (Union[Dict[str, np.ndarray], None], optional) – A dictionary that contains the numeric values of all symbolic operators in this tensor_product. Defaults to None.
order (Union[List[str],None], optional) – Give a specific order in which the factors should be multiplied. This can make a difference, since the tensor product is not commutative. Defaults to None.
- Returns:
- Numeric operator with the value of the computed
tensor product of all contained tensor_products.
- Return type:
- pad_with_identities(ttn: TreeTensorNetwork, symbolic: bool = False) TensorProduct¶
Pads this tensor product with identities.
All sites that are in the TTN but not in the tensor product are padded with an appropriately sized identity operator.
- Parameters:
ttn (TreeTensorNetworkState) – The TTN to be considered for the padding.
symbolic (bool) – True adds “I{size}” instead of the appropriately sized numpy array.
- Returns:
The padded tensor product.
- Return type:
- Raises:
KeyError – Raised if this tensor product contains single site operators acting on sites that do not exist in the TTN.