I was wondering if you could help me with how I can adopt the code sample you provided above to the spinful case?
For the spinful case, when I have both spin up and down particles, the code does not give correct correlation function. Please see a sample code below.
Thanks
===============================
    using ITensors
    N = 10
    Npart = 2
    t1 = 1.0
    sites = siteinds("Electron", N;
                   conserve_qns = true)
    ampo = AutoMPO()
    for b=1:N-1
    ampo += -t1, "Cdagup", b,    "Cup", b+1
    ampo += -t1, "Cdagup", b+1,  "Cup", b
    ampo += -t1, "Cdagdn", b,    "Cdn", b+1
    ampo += -t1, "Cdagdn", b+1,  "Cdn", b
    end
    H = MPO(ampo,sites)
    sweeps = Sweeps(6)
    maxdim!(sweeps,50,100,200,400,800,800)
    cutoff!(sweeps,1E-12)
    state = ["Emp" for n=1:N]
    for i=1: floor(Int,Npart/2)
        state[i] = "Up"
        state[floor(Int,Npart/2)+i] = "Dn"
    end
    psi0 = randomMPS(sites, state, min(10, floor(Int,N/2) ))
    # Check total number of particles:
    @show flux(psi0)
    # Start DMRG calculation:
    energy, psi = dmrg(H, psi0, sweeps)
    println("\nGround State Energy = $energy")
    #Below, correlation functions using the DMRG solution are calculated:
    cor_mat_mps = zeros(N,N);
    for i = 1:N
        orthogonalize!(psi,i)
        s_ind_i = siteind(psi,i);
        l_ind_i = linkind(psi,i);
        for j =1:N
            if j < i
                continue
            end
            if i == j
                cor_mat_mps[i,i] = scalar( 
                    psi[i] * prime( dag(psi[i]) , s_ind_i ) * op(s_ind_i,"Nup")
                    );
            else
                s_ind_j = siteind(psi,j);
                l_ind_j_left = linkind(psi,j-1);
                prod_aux = psi[i] * prime(dag(psi[i]), s_ind_i, l_ind_i);
                for n = i+1:j - 1
                    prod_aux *= psi[n] ;
                    prod_aux *= op(sites,"F",n) ;
                    prod_aux *= prime( dag( psi[n] ) ) ;
                end
                cor_mat_mps[i,j]=   scalar(
                    prod_aux * psi[j] * prime( dag( psi[j] ), s_ind_j, l_ind_j_left) *
                       op(s_ind_i,"Cdagup")*op(s_ind_j,"Cup")
                    ); 
                cor_mat_mps[j,i]= cor_mat_mps[i,j];
            end
        end
    end