Seg fault in sum

I am making a Lanczos type algorithm, and when I add two MPS' I am getting a segfault. The relevant code snippet is

    V = std::vector<MPS>(actual_maxiter+2);
auto W = std::vector<MPS>(actual_maxiter+2);
auto WP = std::vector<MPS>(actual_maxiter+2);
T = CMatrix(actual_maxiter+2,actual_maxiter+2);
for(auto& el : T) el = Cplx(0,0);
V[0] = psii;
WP[0] = applyMPO(A,V[0],args);
T(0,0) = innerC(WP[0],V[0]).real();
auto num = -1*T(0,0);
W[0] = num*V[0];
W[0].plusEq(WP[0]);


psii is an MPS passed into the function, V and T are members of my class. I am getting the segfault from the last line. I have also tried using sum instead of plusEq, and also there is a segfault. What is going wrong?

Also if I shift the positions to match for W[0] and WP[0], then I get the error "different index structure." Not sure if this last statement is useful information, but I figured I would share.

commented by (34k points)
Hi Nick, did you try running the code in debug mode to see if you get a more helpful error message? Also did you print out the two MPS you are summing to inspect their index structure? They have to have the same physical indices for the sum to work.

Debug mode instructions:
http://itensor.org/docs.cgi?vers=cppv3&page=formulas/debug_mode
commented by (250 points)
edited by
Hi Miles, in debug mode I get the error:
'SafePtr: pointer is null'
Not sure why, the MPS' that are used in sum have been set equal to something before the sum.

I checked the index structure, and they have the same physical indices, but WP[0] was all primed. So I added the line of code

WP[0] = noPrime(WP[0],siteInds(WP[0]));

Still a segfault.

The sum is basically a|v> + A|v>, where A is an MPO and a is a number. Is it normal for applyMPO to change the index structure? Should I be doing this some other way?

I solved this issue by adding

    V[0].position(c);
WP[0].position(c);
WP[0].replaceSiteInds(siteInds(V[0]));


To make sure the positions and the indices matched before adding. For some reason the sum function that takes a vector of MPS' does not work, but at least this does.

commented by (34k points)
Hi Nick, glad you were able to get it working. Yes, the indices have to match before adding. (The gauge positions shouldn't have to be the same for adding to work, so it would be interesting to comment out both of the .position(c) lines to see if it still works.)

I can't easily comment on what wasn't working in your earlier code without more context, such as knowing how the MPO "A" was defined. But if it was making WP[0] have different indices from V[0] then the addition does not work in that case.

Best regards,
Miles