|
| 1 | +// |
| 2 | +// Created by Oliver Backhouse on 22/11/2025. |
| 3 | +// |
| 4 | + |
| 5 | +#include <SeQuant/domain/mbpt/rules/thc.hpp> |
| 6 | + |
| 7 | +#include <SeQuant/core/expr.hpp> |
| 8 | +#include <SeQuant/core/space.hpp> |
| 9 | +#include <SeQuant/core/utility/macros.hpp> |
| 10 | + |
| 11 | +#include <range/v3/view.hpp> |
| 12 | + |
| 13 | +#include <string_view> |
| 14 | + |
| 15 | +namespace sequant::mbpt { |
| 16 | + |
| 17 | +ExprPtr tensor_hypercontract_impl(Tensor const& tnsr, Index const& aux_idx_1, |
| 18 | + Index const& aux_idx_2, |
| 19 | + std::wstring_view factor_label, |
| 20 | + std::wstring_view aux_label) { |
| 21 | + SEQUANT_ASSERT(tnsr.bra_rank() == 2 // |
| 22 | + && tnsr.ket_rank() == 2 // |
| 23 | + && tnsr.aux_rank() == 0); |
| 24 | + |
| 25 | + auto t1 = ex<Tensor>(factor_label, bra({ranges::front(tnsr.bra())}), ket(), |
| 26 | + aux({aux_idx_1})); |
| 27 | + auto t2 = ex<Tensor>(factor_label, bra(), ket({ranges::front(tnsr.ket())}), |
| 28 | + aux({aux_idx_1})); |
| 29 | + auto t3 = ex<Tensor>(factor_label, bra({ranges::back(tnsr.bra())}), ket(), |
| 30 | + aux({aux_idx_2})); |
| 31 | + auto t4 = ex<Tensor>(factor_label, bra(), ket({ranges::back(tnsr.ket())}), |
| 32 | + aux({aux_idx_2})); |
| 33 | + auto z = ex<Tensor>(aux_label, bra(), ket(), aux({aux_idx_1, aux_idx_2})); |
| 34 | + |
| 35 | + if (tnsr.symmetry() == Symmetry::Antisymm) { |
| 36 | + auto t1a = ex<Tensor>(factor_label, bra({ranges::back(tnsr.bra())}), ket(), |
| 37 | + aux({aux_idx_1})); |
| 38 | + auto t3a = ex<Tensor>(factor_label, bra({ranges::front(tnsr.bra())}), ket(), |
| 39 | + aux({aux_idx_2})); |
| 40 | + |
| 41 | + return (t1 * t2 * z * t3 * t4) - (t1a * t2 * z * t3a * t4); |
| 42 | + } |
| 43 | + |
| 44 | + return t1 * t2 * z * t3 * t4; |
| 45 | +} |
| 46 | + |
| 47 | +ExprPtr tensor_hypercontract(ExprPtr const& expr, IndexSpace aux_space, |
| 48 | + std::wstring_view tensor_label, |
| 49 | + std::wstring_view outer_factor_label, |
| 50 | + std::wstring_view core_tensor_label) { |
| 51 | + using ranges::views::transform; |
| 52 | + |
| 53 | + if (expr->is<Sum>()) |
| 54 | + return ex<Sum>(*expr | transform([&](auto&& x) { |
| 55 | + return tensor_hypercontract(x, aux_space, tensor_label, |
| 56 | + outer_factor_label, core_tensor_label); |
| 57 | + })); |
| 58 | + |
| 59 | + else if (expr->is<Tensor>()) { |
| 60 | + auto const& tensor = expr->as<Tensor>(); |
| 61 | + if (tensor.label() == tensor_label // |
| 62 | + && tensor.bra_rank() == 2 // |
| 63 | + && tensor.ket_rank() == 2) |
| 64 | + return tensor_hypercontract_impl(tensor, Index(aux_space, 1), |
| 65 | + Index(aux_space, 2), outer_factor_label, |
| 66 | + core_tensor_label); |
| 67 | + else |
| 68 | + return expr; |
| 69 | + } else if (expr->is<Product>()) { |
| 70 | + auto const& prod = expr->as<Product>(); |
| 71 | + |
| 72 | + Product result; |
| 73 | + result.scale(prod.scalar()); |
| 74 | + size_t aux_ix = 0; |
| 75 | + for (auto&& f : prod.factors()) { |
| 76 | + if (f->is<Tensor>() && f->as<Tensor>().label() == tensor_label) { |
| 77 | + auto const& g = f->as<Tensor>(); |
| 78 | + auto index1 = Index(aux_space, ++aux_ix); |
| 79 | + auto index2 = Index(aux_space, ++aux_ix); |
| 80 | + auto g_thc = tensor_hypercontract_impl( |
| 81 | + g, index1, index2, outer_factor_label, core_tensor_label); |
| 82 | + result.append(1, std::move(g_thc), Product::Flatten::Yes); |
| 83 | + } else { |
| 84 | + result.append(1, f, Product::Flatten::No); |
| 85 | + } |
| 86 | + } |
| 87 | + return ex<Product>(std::move(result)); |
| 88 | + } else { |
| 89 | + return expr; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +} // namespace sequant::mbpt |
0 commit comments