Hi, so based on the sample code you posted above (thanks for that) the issue is that to use the .set method, you need to pass indices that are the actual indices the ITensor has.
So the correct code depends on a few things. If you constructed the ITensor dPhi like this:
auto dPhi = ITensor(k,l);
then it is those k and l indices (the ones used to construct dPhi) which you need to use in the .set method inside your function. The new k and l indices you create inside of your function will be different in this case from the ones used to construct dPhi even though they have the same variable names and perhaps other similar properties.
This can be a confusing aspect of indices in ITensor: the main thing that distinguishes is an internal id number which is automatically fixed when an index is constructed and which can't be changed. The id number is made by the constructor and any copies of that index will have the same id.
I'd recommend printing out some indices using the command Print(k); and Print(l); You'll see the id numbers in the output as the last number inside the parenthesis.
On the other hand if you didn't construct dPhi outside of your function, you'll need to do so in order to call the .set method the way you're doing.
So most likely you need to expand the definition of your function to allow you to pass the indices of dPhi as additional function parameters.