Skip to content

Commit

Permalink
Refactor op::concat
Browse files Browse the repository at this point in the history
  • Loading branch information
ajay-mk committed Feb 15, 2025
1 parent c70044d commit 27422c3
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions SeQuant/domain/mbpt/vac_av.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
//
using namespace sequant::mbpt;

template <typename T>
using OpConnectVec = std::vector<std::pair<T, T>>;

/// defines the default op connections
inline std::vector<std::pair<mbpt::OpType, mbpt::OpType>>
default_op_connections() {
inline OpConnectVec<mbpt::OpType> default_op_connections() {
using mbpt::OpType;
static const std::vector<std::pair<mbpt::OpType, mbpt::OpType>> defaults = {
static const OpConnectVec<mbpt::OpType> defaults = {
{OpType::h, OpType::t},
{OpType::f, OpType::t},
{OpType::f̃, OpType::t},
Expand All @@ -29,20 +31,22 @@ default_op_connections() {
}

/// concat 2 sets of op connections
inline std::vector<std::pair<mbpt::OpType, mbpt::OpType>> concat(
const std::vector<std::pair<mbpt::OpType, mbpt::OpType>> connections1,
const std::vector<std::pair<mbpt::OpType, mbpt::OpType>> connections2) {
template <typename T,
typename = std::enable_if_t<std::is_same_v<T, mbpt::OpType> ||
std::is_same_v<T, std::wstring>>>
inline OpConnectVec<T> concat(const OpConnectVec<T>& connections1,
const OpConnectVec<T>& connections2) {
return ranges::concat_view(connections1, connections2) | ranges::to_vector;
}

/// lowers representation of op connections from mbpt::OpType to labels
/// @param[in] op_connections vector of pairs of operators to be connected
/// @return vector of pairs of operator labels to be connected
inline std::vector<std::pair<std::wstring, std::wstring>> to_label_connections(
const std::vector<std::pair<mbpt::OpType, mbpt::OpType>>& op_connections) {
inline OpConnectVec<std::wstring> to_label_connections(
const OpConnectVec<mbpt::OpType>& op_connections) {
// convert mbpt::OpType to std::wstring
using mbpt::optype2label;
std::vector<std::pair<std::wstring, std::wstring>> op_connect_wstr;
OpConnectVec<std::wstring> op_connect_wstr;
for (const auto& [op1, op2] : op_connections) {
op_connect_wstr.emplace_back(optype2label.at(op1), optype2label.at(op2));
}
Expand Down Expand Up @@ -83,10 +87,10 @@ inline ExprPtr lower_to_tensor_form(const ExprPtr& expr_inp) {
/// by default_op_connections() .
/// @param[in] skip_clone if true, will not clone the input expression
/// @return the VEV
ExprPtr vac_av(ExprPtr expr,
std::vector<std::pair<mbpt::OpType, mbpt::OpType>>
op_connections = default_op_connections(),
bool skip_clone = false);
ExprPtr vac_av(
ExprPtr expr,
OpConnectVec<mbpt::OpType> op_connections = default_op_connections(),
bool skip_clone = false);

/// computes the vacuum expectation value (VEV)

Expand All @@ -97,7 +101,5 @@ ExprPtr vac_av(ExprPtr expr,
/// when `opR` precedes `opL`, i.e. `opL` is to the left of `opR`
/// @param[in] skip_clone if true, will not clone the input expression
/// @return the VEV
ExprPtr vac_av(
ExprPtr expr,
std::vector<std::pair<std::wstring, std::wstring>> op_connections,
bool skip_clone = false);
ExprPtr vac_av(ExprPtr expr, OpConnectVec<std::wstring> op_connections,
bool skip_clone = false);

0 comments on commit 27422c3

Please sign in to comment.