0 votes
asked by (300 points)
edited by

I was basing on this tutorial:


to implement my own Hamiltonian on a given graph. My code looks as follows:

#include "itensor/all.h"

using namespace itensor;

main(int argc, char* argv[])
int Nx = 8; // Nx can be understood as the size of an elementary cell
auto N = atoi(argv[1])*Nx;

auto sites = SpinOne(N,{"ConserveQNs=",true});

auto ampo = AutoMPO(sites);

// [Definition of my Hamiltonian]

auto H = toMPO(ampo);

auto state = InitState(sites);

for (int i = 1; i <= N; i++) {

auto sweeps = Sweeps(10);
sweeps.maxm() = 50,100,200,300,400;
sweeps.cutoff() = 1E-10;

// Begin the DMRG calculation
// for the ground state
auto [en0,psi0] = dmrg(H,randomMPS(state),sweeps,{"Quiet=",true});

// Print the final energies reported by DMRG
printfln("\n      Ground State Energy = %.10f",en0);

// Measuring Sx, Sy & Sz

println("Ground state");
println("\nj | Sx |: ");
for( auto j : range1(N) ) {
    // Re-gauge psi0 to get ready to measure at position j

    auto ket = psi0(j);
    auto bra = dag(prime(ket,"Site"));

    auto Sxjop = op(sites,"Sx",j);

    //take an inner product
    auto sxj = eltC(bra*Sxjop*ket);
    println(j, " | ", sxj, " | ");

return 0;


When I run this code I get the following error message:

From line 173, file itdata/qdense.cc
Setting Tensor element non-zero would violate its symmetry.

I know that this problem was mentioned (e.g. here: http://itensor.org/support/1127/error-message-regarding-the-operator-sx2-spintwo-siteset), but the previous posts didn't mention problems while trying to use Sx instead of Sx2.

Moreover, when I modify the following lines:

auto sites = SpinOne(N,{"ConserveQNs=",true});
auto [en0,psi0] = dmrg(H,randomMPS(state),sweeps,{"Quiet=",true});


auto sites = SpinOne(N,{"ConserveQNs=",false});
auto [en0,psi0] = dmrg(H,randomMPS(sites),sweeps,{"Quiet=",true});

everything works fine (but much slower). The change from state to sites in the second line of the example is necessary, or otherwise I get the error:

randomMPS(SiteSet) with QN conservation is ambiguous, use randomMPS(InitState) instead.

Do you have any idea, how to solve this problem?

1 Answer

+1 vote
answered by (14.1k points)
selected by
Best answer


An immediate thing to note is that since the state you are measuring has a well-defined Sz quantum number, measuring Sx will always be zero (since the Sx operator will change the total spin of your state, so Sx|psi> will have a different total Sz from |psi> so <psi|Sx|psi> must be zero).

However, this question brought up the fact that this operation is currently not as easy as it should be to do right now in ITensor. Currently, this is the easiest way I can think of to measure a non-QN conserving operator of a QN-conserving MPS:

auto N = 10;
auto sites = SpinOne(N);

auto state = InitState(sites,"Up");
auto psi = MPS(state);

// Measure Sx on site j
auto j = 3;

// Get the site index without QNs
auto sj = removeQNs(sites(j));

// Create a SpinOneSite from the Index
// without QNs to use in the op function
auto Sxj = op(SpinOneSite(sj),"Sx");

auto sxj = elt(psi(j)*Sxj*dag(prime(psi(j),"Site")));


We are discussing alternative solutions to be able to do this more generally (without manually modifying the Index and then creating the correct Site type), but unfortunately they require fairly technical solutions right now given the current system.


Welcome to ITensor Support Q&A, where you can ask questions and receive answers from other members of the community.

Formatting Tips:
  • To format code, indent by four spaces
  • To format inline LaTeX, surround it by @@ on both sides
  • To format LaTeX on its own line, surround it by $$ above and below
  • For LaTeX, it may be necessary to backslash-escape underscore characters to obtain proper formatting. So for example writing \sum\_i to represent a sum over i.
If you cannot register due to firewall issues (e.g. you cannot see the capcha box) please email Miles Stoudenmire to ask for an account.

To report ITensor bugs, please use the issue tracker.