Skip to content

Commit

Permalink
Rename typedefs (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
brownj85 authored Jun 1, 2021
1 parent f124ae2 commit e1b1774
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 296 deletions.
1 change: 1 addition & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

### Improvements

* Use camel case for type aliases [(#17)](https://github.com/XanaduAI/jet/pull/17)
* Exceptions are now favoured in place of `std::terminate` with `Exception` being the new base type for all exceptions thrown by Jet. [(#3)](https://github.com/XanaduAI/jet/pull/3)

* `TaskBasedCpuContractor` now stores `Tensor` results. [(#8)](https://github.com/XanaduAI/jet/pull/8)
Expand Down
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ include path and OpenBLAS is installed on your system, you can compile the
#include <Jet.hpp>
int main(){
using tensor_t = Jet::Tensor<std::complex<float>>;
using Tensor = Jet::Tensor<std::complex<float>>;
std::array<tensor_t, 3> tensors;
tensors[0] = tensor_t({"i", "j", "k"}, {2, 2, 2});
tensors[1] = tensor_t({"j", "k", "l"}, {2, 2, 2});
std::array<Tensor, 3> tensors;
tensors[0] = Tensor({"i", "j", "k"}, {2, 2, 2});
tensors[1] = Tensor({"j", "k", "l"}, {2, 2, 2});
tensors[0].FillRandom();
tensors[1].FillRandom();
Expand Down
22 changes: 11 additions & 11 deletions include/jet/PathInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ struct PathStepInfo {
class PathInfo {
public:
/// Type of a node ID.
using node_id_t = size_t;
using NodeID_t = size_t;

/// Type of a contraction path.
using path_t = std::vector<std::pair<node_id_t, node_id_t>>;
using Path = std::vector<std::pair<NodeID_t, NodeID_t>>;

/// Type of the index-to-size map.
using index_to_size_map_t = std::unordered_map<std::string, size_t>;
using IndexToSizeMap = std::unordered_map<std::string, size_t>;

/// Type of a `PathStepInfo` sequence.
using steps_t = std::vector<PathStepInfo>;
using Steps = std::vector<PathStepInfo>;

/**
* @brief Constructs an empty path.
Expand All @@ -78,7 +78,7 @@ class PathInfo {
* @param path Pairs of node IDs representing a raw contraction path.
*/
template <typename Tensor>
PathInfo(const TensorNetwork<Tensor> &tn, const path_t &path) : path_(path)
PathInfo(const TensorNetwork<Tensor> &tn, const Path &path) : path_(path)
{
const auto &nodes = tn.GetNodes();
num_leaves_ = nodes.size();
Expand Down Expand Up @@ -117,7 +117,7 @@ class PathInfo {
*
* @return Map which associates each index with a dimension size.
*/
const index_to_size_map_t &GetIndexSizes() const noexcept
const IndexToSizeMap &GetIndexSizes() const noexcept
{
return index_to_size_map_;
}
Expand All @@ -134,14 +134,14 @@ class PathInfo {
*
* @return Pairs of node IDs representing the contraction path.
*/
const path_t &GetPath() const noexcept { return path_; }
const Path &GetPath() const noexcept { return path_; }

/**
* @brief Returns the steps of this path.
*
* @return Collection of path steps.
*/
const steps_t &GetSteps() const noexcept { return steps_; }
const Steps &GetSteps() const noexcept { return steps_; }

/**
* @brief Computes the number of floating-point operations needed to execute
Expand Down Expand Up @@ -240,18 +240,18 @@ class PathInfo {

private:
/// Contraction path through the tensor network associated with this path.
path_t path_;
Path path_;

/// Contraction metadata of the tensor network associated with this path.
steps_t steps_;
Steps steps_;

/// Number of leaf nodes in the tensor network associated with this path.
size_t num_leaves_;

/// Map that associates each index with its corresponding dimension size.
/// This information is used to estimate memory requirements and floating-
/// point operation counts.
index_to_size_map_t index_to_size_map_;
IndexToSizeMap index_to_size_map_;

/**
* @brief Contracts two path steps.
Expand Down
24 changes: 12 additions & 12 deletions include/jet/TaskBasedCpuContractor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ namespace Jet {
template <typename Tensor> class TaskBasedCpuContractor {
public:
/// Type of the name-to-task map.
using name_to_task_map_t = std::unordered_map<std::string, tf::Task>;
using NameToTaskMap = std::unordered_map<std::string, tf::Task>;

/// Type of the name-to-tensor map.
using name_to_tensor_map_t =
using NameToTensorMap =
std::unordered_map<std::string, std::unique_ptr<Tensor>>;

/// Type of the name-to-parents map.
using name_to_parents_map_t =
using NameToParentsMap =
std::unordered_map<std::string, std::unordered_set<std::string>>;

/// Type of the task dependency graph.
using taskflow_t = tf::Taskflow;
using TaskFlow = tf::Taskflow;

/**
* @brief Constructs a new `%TaskBasedCpuContractor` object.
Expand All @@ -52,7 +52,7 @@ template <typename Tensor> class TaskBasedCpuContractor {
*
* @return Map which associates names to tasks.
*/
const name_to_task_map_t &GetNameToTaskMap() const noexcept
const NameToTaskMap &GetNameToTaskMap() const noexcept
{
return name_to_task_map_;
}
Expand All @@ -62,7 +62,7 @@ template <typename Tensor> class TaskBasedCpuContractor {
*
* @return Map which associates names to tensors.
*/
const name_to_tensor_map_t &GetNameToTensorMap() const noexcept
const NameToTensorMap &GetNameToTensorMap() const noexcept
{
return name_to_tensor_map_;
}
Expand All @@ -72,7 +72,7 @@ template <typename Tensor> class TaskBasedCpuContractor {
*
* @return Map which associates names to a vector of parent node IDs.
*/
const name_to_parents_map_t &GetNameToParentsMap() const noexcept
const NameToParentsMap &GetNameToParentsMap() const noexcept
{
return name_to_parents_map_;
}
Expand Down Expand Up @@ -112,7 +112,7 @@ template <typename Tensor> class TaskBasedCpuContractor {
*
* @return Taskflow instance representing the task dependency graph.
*/
const taskflow_t &GetTaskflow() const noexcept { return taskflow_; }
const TaskFlow &GetTaskflow() const noexcept { return taskflow_; }

/**
* @brief Returns the number of floating-point operations needed to perform
Expand Down Expand Up @@ -304,17 +304,17 @@ template <typename Tensor> class TaskBasedCpuContractor {

private:
/// Task graph to be executed during a contraction.
taskflow_t taskflow_;
TaskFlow taskflow_;

/// Map that associates a task name with its corresponding task.
name_to_task_map_t name_to_task_map_;
NameToTaskMap name_to_task_map_;

/// Map that associates a task name with its result tensor.
name_to_tensor_map_t name_to_tensor_map_;
NameToTensorMap name_to_tensor_map_;

/// Map that associates a task name with a list of parent task names.
/// Task `A` is a parent of task `B` if `A` immediately succeeds `B`.
name_to_parents_map_t name_to_parents_map_;
NameToParentsMap name_to_parents_map_;

/// Tasks that store the results of a contraction in `results_`.
std::vector<tf::Task> result_tasks_;
Expand Down
37 changes: 18 additions & 19 deletions include/jet/TensorNetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ namespace Jet {
template <class Tensor> class TensorNetwork {
public:
/// Type of a node ID.
using node_id_t = size_t;
using NodeID_t = size_t;

/**
* @brief `%Node` is a POD which wraps tensors in a `TensorNetwork`.
*/
struct Node {
/// Unique ID for this node.
node_id_t id;
NodeID_t id;

/// Name of this node.
std::string name;
Expand All @@ -71,7 +71,7 @@ template <class Tensor> class TensorNetwork {
size_t dim;

/// IDs of the nodes connected by this edge.
std::vector<node_id_t> node_ids;
std::vector<NodeID_t> node_ids;

/**
* @brief Reports whether two edges are the same.
Expand All @@ -92,31 +92,30 @@ template <class Tensor> class TensorNetwork {
};

/// Type of a `Node` collection.
using nodes_t = std::vector<Node>;
using Nodes = std::vector<Node>;

/// Type of the index-to-edge map.
using index_to_edge_map_t = std::unordered_map<std::string, Edge>;
using IndexToEdgeMap = std::unordered_map<std::string, Edge>;

/// Type of the tag-to-node-IDs map.
using tag_to_node_ids_map_t =
std::unordered_multimap<std::string, node_id_t>;
using TagToNodeIdsMap = std::unordered_multimap<std::string, NodeID_t>;

/// Type of a contraction path.
using path_t = std::vector<std::pair<node_id_t, node_id_t>>;
using Path = std::vector<std::pair<NodeID_t, NodeID_t>>;

/**
* @brief Returns the nodes in this `%TensorNetwork`.
*
* @return Collection of nodes.
*/
const nodes_t &GetNodes() const noexcept { return nodes_; }
const Nodes &GetNodes() const noexcept { return nodes_; }

/**
* @brief Returns the index-to-edge map of this `%TensorNetwork`.
*
* @return Map which associates indices with edges.
*/
const index_to_edge_map_t &GetIndexToEdgeMap() const noexcept
const IndexToEdgeMap &GetIndexToEdgeMap() const noexcept
{
return index_to_edge_map_;
}
Expand All @@ -126,7 +125,7 @@ template <class Tensor> class TensorNetwork {
*
* @return Map which associates tags with node IDs.
*/
const tag_to_node_ids_map_t &GetTagToNodesMap() const noexcept
const TagToNodeIdsMap &GetTagToNodesMap() const noexcept
{
return tag_to_nodes_map_;
}
Expand All @@ -138,7 +137,7 @@ template <class Tensor> class TensorNetwork {
*
* @return Pairs of node IDs representing the contraction path.
*/
const path_t &GetPath() noexcept { return path_; }
const Path &GetPath() noexcept { return path_; }

/**
* @brief Returns the number of indices in this `%TensorNetwork`.
Expand All @@ -165,10 +164,10 @@ template <class Tensor> class TensorNetwork {
*
* @return Node ID assigned to the tensor.
*/
node_id_t AddTensor(const Tensor &tensor,
const std::vector<std::string> &tags) noexcept
NodeID_t AddTensor(const Tensor &tensor,
const std::vector<std::string> &tags) noexcept
{
node_id_t id = nodes_.size();
NodeID_t id = nodes_.size();
nodes_.emplace_back(Node{
id, // id
DeriveNodeName_(tensor.GetIndices()), // name
Expand Down Expand Up @@ -295,7 +294,7 @@ template <class Tensor> class TensorNetwork {
* @param path Contraction path specified as a list of node ID pairs.
* @return Tensor associated with the result of the final contraction.
*/
const Tensor &Contract(const path_t &path = {})
const Tensor &Contract(const Path &path = {})
{
JET_ABORT_IF(nodes_.empty(),
"An empty tensor network cannot be contracted.");
Expand Down Expand Up @@ -326,15 +325,15 @@ template <class Tensor> class TensorNetwork {

private:
/// Nodes inside this tensor network.
nodes_t nodes_;
Nodes nodes_;

/// Map that associates each index with its corresponding edge.
/// Not used when a contraction path is specified.
index_to_edge_map_t index_to_edge_map_;
IndexToEdgeMap index_to_edge_map_;

/// Map that associates each tag with a list of nodes that contain that tag.
/// Not used when a contraction path is specified.
tag_to_node_ids_map_t tag_to_nodes_map_;
TagToNodeIdsMap tag_to_nodes_map_;

/// Contraction path representing pairs of nodes to be contracted.
std::vector<std::pair<size_t, size_t>> path_;
Expand Down
4 changes: 2 additions & 2 deletions python/src/PathInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ template <class T, class... Ts>
void bind_constructors(py::class_<Jet::PathInfo> &c)
{
c.def(py::init<const Jet::TensorNetwork<Jet::Tensor<T>> &,
const Jet::PathInfo::path_t &>(),
py::arg("tn"), py::arg("path") = Jet::PathInfo::path_t(), R"(
const Jet::PathInfo::Path &>(),
py::arg("tn"), py::arg("path") = Jet::PathInfo::Path(), R"(
Constructs a populated PathInfo for the given path
through a tensor network
Expand Down
2 changes: 1 addition & 1 deletion python/src/Tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,4 @@ template <class T> void AddBindingsForTensor(py::module_ &m)
Returns:
Transposed tensor object.
)");
}
}
4 changes: 2 additions & 2 deletions python/src/TensorNetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace py = pybind11;
template <class T> void AddBindingsForTensorNetwork(py::module_ &m)
{
using TensorNetwork = Jet::TensorNetwork<Jet::Tensor<T>>;
using node_id_t = typename Jet::TensorNetwork<Jet::Tensor<T>>::node_id_t;
using NodeID_t = typename Jet::TensorNetwork<Jet::Tensor<T>>::NodeID_t;
using Node = typename Jet::TensorNetwork<Jet::Tensor<T>>::Node;
using Edge = typename Jet::TensorNetwork<Jet::Tensor<T>>::Edge;

Expand Down Expand Up @@ -68,7 +68,7 @@ template <class T> void AddBindingsForTensorNetwork(py::module_ &m)
.def_property_readonly(
"tag_to_node_id_map",
[](const TensorNetwork &tn) {
std::unordered_map<std::string, std::vector<node_id_t>> map;
std::unordered_map<std::string, std::vector<NodeID_t>> map;

for (const auto &[tag, node_id] : tn.GetTagToNodesMap()) {
map[tag].emplace_back(node_id);
Expand Down
2 changes: 1 addition & 1 deletion python/src/TensorNetworkIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,4 @@ template <class T> void AddBindingsForTensorNetworkIO(py::module_ &m)
{
AddBindingsForTensorNetworkFile<T>(m);
AddBindingsForTensorNetworkSerializer<T>(m);
}
}
Loading

0 comments on commit e1b1774

Please sign in to comment.