Template Class TensorNetwork

Class Documentation

template<class Tensor>
class Jet::TensorNetwork

TensorNetwork represents a tensor network.

Internally, the nodes (tensors) of the network are stored in nodes_ and the tags are stored in tag_to_node_ids_map. Furthermore, when a contraction path is not specified, the edges (contractions) are stored in index_to_edge_map_. The contraction path followed by a call to Contract() is stored in the path_ member.

tparam Tensor

Type of the tensor in the tensor network. The only requirement for this type is that the following member functions exist:

std::vector<std::string> GetIndices();
std::vector<std::size_t> GetShape();

void InitIndicesAndShape(const std::vector<std::string>&,
                         const std::vector<std::size_t>&);

static Tensor Reshape(const Tensor&, const std::vector<std::string>&);
static Tensor SliceIndex(const Tensor&, const std::string&, size_t);
static Tensor ContractTensors(const Tensor&, const Tensor&);

Public Types

using NodeID_t = size_t

Type of a node ID.

using Nodes = std::vector<Node>

Type of a Node collection.

using IndexToEdgeMap = std::unordered_map<std::string, Edge>

Type of the index-to-edge map.

using TagToNodeIdsMap = std::unordered_multimap<std::string, NodeID_t>

Type of the tag-to-node-IDs map.

using Path = std::vector<std::pair<NodeID_t, NodeID_t>>

Type of a contraction path.

Public Functions

inline const Nodes &GetNodes() const noexcept

Returns the nodes in this TensorNetwork.

Returns

Collection of nodes.

inline const IndexToEdgeMap &GetIndexToEdgeMap() const noexcept

Returns the index-to-edge map of this TensorNetwork.

Returns

Map which associates indices with edges.

inline const TagToNodeIdsMap &GetTagToNodesMap() const noexcept

Returns the tag-to-node-IDs map of this TensorNetwork.

Returns

Map which associates tags with node IDs.

inline const Path &GetPath() noexcept

Returns the contaction path of this TensorNetwork.

Note

The contraction path is always empty before Contract() is called.

Returns

Pairs of node IDs representing the contraction path.

inline size_t NumIndices() const noexcept

Returns the number of indices in this TensorNetwork.

Returns

Number of indices.

inline size_t NumTensors() const noexcept

Returns the number of tensors in this TensorNetwork.

Returns

Number of tensors.

inline NodeID_t AddTensor(const Tensor &tensor, const std::vector<std::string> &tags) noexcept

Adds a tensor with the specified tags and returns its assigned ID.

Warning

This function is not safe for concurrent execution.

Parameters
  • tensorTensor to be added to this tensor network.

  • tags – Tags to be associated with the tensor.

Returns

Node ID assigned to the tensor.

inline void SliceIndices(const std::vector<std::string> &indices, unsigned long long value)

Slices a set of indices.

The value taken along each axis is derived from the provided linear index.

Example: Suppose this tensor network contains an index “A0” of dimension 2 and another index “B1” of dimension 3. To slice these indices at values 0 and 1 respectively, use

tensor_network.SliceIndices({"A0", "B1"}, 0 + 1 * 2);

See

Jet::Utilities::UnravelIndex()

Warning

The program is aborted if a sliced index does not exist in this tensor network.

Parameters
  • indices – Indices to be sliced.

  • value – Raveled value representing the element to take along each of the given indices. See UnravelIndex() for details on how raveled values are interpreted.

inline const Tensor &Contract(const Path &path = {})

Contracts this tensor network.

If the given contraction path is empty, the first two nodes belonging to each edge of the tensor network are contracted. Otherwise, the specified contraction path is used to guide the contraction of nodes.

Note

After this function is invoked, the path_ member of this tensor network will reflect the contractions performed in this function.

Warning

The program is aborted if the tensor network is empty.

Parameters

path – Contraction path specified as a list of node ID pairs.

Returns

Tensor associated with the result of the final contraction.

struct Edge

Edge is a POD which connects Nodes in a TensorNetwork.

Public Functions

inline bool operator==(const Edge &other) const noexcept

Reports whether two edges are the same.

Two edges are the same if they have the same dimension and contain the same set of node IDs.

Parameters

other – Edge to compare to this edge.

Returns

True if this edge and the given edge are the same.

Public Members

size_t dim

Dimensionality of this edge.

std::vector<NodeID_t> node_ids

IDs of the nodes connected by this edge.

struct Node

Node is a POD which wraps tensors in a TensorNetwork.

Public Members

NodeID_t id

Unique ID for this node.

std::string name

Name of this node.

std::vector<std::string> indices

Indices of this node.

std::vector<std::string> tags

Tags of this node.

bool contracted

Reports whether this node has been contracted.

Tensor tensor

Tensor of this node.