46 std::vector<Eigen::Index> keys;
47 std::unordered_map<Eigen::Index, Eigen::Index> keysKeys;
52 Eigen::Index minId = std::numeric_limits<Eigen::Index>::max();
54 if (contractTheLowestTensorId) {
55 for (
const auto &tensor : tensors) {
56 if (tensor.first < minId)
64 while (tensors.size() > 1) {
65 Eigen::Index tensor1Id;
66 Eigen::Index tensor2Id;
68 if (contractTheLowestTensorId) {
70 auto it = tensors.find(tensor1Id);
72 if (it == tensors.end())
75 tensor2Id = it->first;
77 auto it = tensors.begin();
78 tensor1Id = it->first;
80 tensor2Id = it->first;
89 const auto &tensor = tensors[tensor1Id];
90 Eigen::Index resultRank = std::numeric_limits<Eigen::Index>::max();
91 for (Eigen::Index ti = 0;
92 ti < static_cast<Eigen::Index>(tensor->connections.size()); ++ti) {
93 const auto nextTensorId = tensor->connections[ti];
95 tensor2Id = nextTensorId;
97 const Eigen::Index newRank =
100 if (newRank <= resultRank) {
101 if (newRank < resultRank) {
102 tensor2Id = nextTensorId;
103 resultRank = newRank;
114 ContractNodes(qubit, tensors, tensor1Id, tensor2Id, resultRank);
116 if (resultRank == 0) {
117 if (tensors.size() == 1 || tensor->contractsTheNeededQubit)
118 return std::real(tensor->tensor->atOffset(0));
122 tensors.erase(tensor1Id);
124 if (contractTheLowestTensorId) {
125 minId = std::numeric_limits<Eigen::Index>::max();
126 for (
const auto &tensor : tensors) {
127 if (tensor.first < minId)
128 minId = tensor.first;
134 return std::real(tensors.begin()->second->tensor->atOffset(0));