Skip to content

Commit 4cbc6eb

Browse files
authored
add and iadd for VectorSpaceList (#446)
* implement add and iadd for sparseoperatorlist * review response
1 parent d60f228 commit 4cbc6eb

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

forte/api/sparse_operator_list_api.cc

+17-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,23 @@ void export_SparseOperatorList(py::module& m) {
164164
auto sop = op.to_operator();
165165
return apply_operator_lin(sop, st);
166166
},
167-
"Multiply a SparseOperator and a SparseState");
167+
"Multiply a SparseOperator and a SparseState")
168+
.def(
169+
"__add__",
170+
[](const SparseOperatorList& op1, const SparseOperatorList& op2) {
171+
SparseOperatorList result = op1;
172+
result += op2;
173+
return result;
174+
},
175+
"Add (concatenate) two SparseOperatorList objects")
176+
.def(
177+
"__iadd__",
178+
[](SparseOperatorList& op1, const SparseOperatorList& op2) {
179+
op1 += op2;
180+
return op1;
181+
},
182+
"Add (concatenate) a SparseOperatorList object to this SparseOperatorList object");
183+
168184

169185
// Wrapper class that holds a SparseOperator
170186
// and overloads operator* to apply forte::SparseExp.

forte/helpers/math_structures.h

+13
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,19 @@ template <typename Derived, typename T, typename F> class VectorSpaceList {
419419
/// @brief Check if two vectors lists are equal
420420
bool operator==(const VectorSpaceList& rhs) const { return elements_ == rhs.elements_; }
421421

422+
/// @brief Concatenate two vectors
423+
Derived operator+=(const Derived& rhs) {
424+
elements_.insert(elements_.end(), rhs.elements_.begin(), rhs.elements_.end());
425+
return static_cast<Derived&>(*this);
426+
}
427+
428+
/// @brief Concatenate two vectors
429+
Derived operator+(const Derived& rhs) const {
430+
Derived result = static_cast<Derived&>(*this);
431+
result += rhs;
432+
return result;
433+
}
434+
422435
/// @brief Get the adjoint of the vector
423436
VectorSpaceList adjoint() const {
424437
VectorSpaceList result;

tests/pytest/sparse_ci/test_sparse_operator.py

+12
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,22 @@ def test_sparse_operator_list_remove():
517517
assert len(sopl) == 1
518518

519519

520+
def test_sparse_operator_list_add():
521+
sop1 = forte.SparseOperatorList()
522+
sop1.add("[1a+ 1a-]", 1.0)
523+
sop2 = forte.SparseOperatorList()
524+
sop2.add("[0a+ 0a-]", 1.0)
525+
sop3 = sop1 + sop2
526+
assert len(sop3) == 2
527+
assert sop3(0)[0].str() == "[1a+ 1a-]"
528+
assert sop3(1)[0].str() == "[0a+ 0a-]"
529+
530+
520531
if __name__ == "__main__":
521532
test_sparse_operator_creation()
522533
test_sparse_operator()
523534
test_sparse_operator_api()
524535
test_sparse_operator_product()
525536
test_sparse_operator_list_reverse()
526537
test_sparse_operator_list_remove()
538+
test_sparse_operator_list_add()

0 commit comments

Comments
 (0)