# Issues with Nf parity conservation

**URL:** <https://itensor.discourse.group/t/issues-with-nf-parity-conservation/1176>\
**Category:** ITensor Julia Questions\
**Tags:** dmrg\
**Created:** [October 2, 2023, 5:27pm UTC](https://itensor.discourse.group/t/issues-with-nf-parity-conservation/1176 "2023-10-02T17:27:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vernek](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/vernek/32/381_2.png) [@vernek](https://itensor.discourse.group/u/vernek)\
**Post date:** [October 2, 2023, 5:27pm UTC](https://itensor.discourse.group/t/issues-with-nf-parity-conservation/1176/1 "2023-10-02T17:27:17Z")

</div>

Dear Miles,

I’m experiencing an issue with number of fermion conservation when I include a term of p-wave pairing term. To be more precise, a minimum Hamiltonian contains a term like \Delta(c^\dagger\_{i,\downarrow}c^\dagger\_{i+1,\downarrow}+c\_{i+1,\downarrow}c\_{i,\downarrow}) which conserves Nf parity. I’m using Electron site and setting conserve\_nfparity=true but it seems that DMRG finds a lowest energy state with different parity as compared to the initial one. Below I include a minimum conde where I note the problem.

```julia

using ITensors

let

    N=4
    t1=1.0
    t2=1.0
    Delta=1.0
    E1=0.0
    E2=0.0
   
    sites = siteinds("Electron",N,conserve_qns=true,conserve_nf=false,
                     conserve_sz=false,conserve_nfparity=true)

    ########## Set Hamiltonian ###########
    ampo = OpSum()

    ampo += E1,"Nup",1
    ampo += E2,"Nup",2
    ampo += E1,"Ndn",1
    ampo += E2,"Ndn",2
    
    ampo += t1,"Cdagup",1,"Cup",3
    ampo += t1,"Cdagup",3,"Cup",1
    ampo += t1,"Cdagdn",1,"Cdn",3
    ampo += t1,"Cdagdn",3,"Cdn",1
    
    ampo += t2,"Cdagup",2,"Cup",4
    ampo += t2,"Cdagup",4,"Cup",2
    ampo += t2,"Cdagdn",2,"Cdn",4
    ampo += t2,"Cdagdn",4,"Cdn",2
    
    ampo += Delta,"Cdagdn",4,"Cdagdn",3
    ampo += Delta,"Cdn",3,"Cdn",4    
    H= MPO(ampo,sites)
    ####################################
    
    ####### Set Initial state ############
    state = [isodd(n) ? "Up" : "Dn" for n=1:N]
    state[3]="Dn"
    state[4]="Emp"
    
    psi0 = randomMPS(sites,state)

    println("Initial occupation")
    Nup=expect(psi0,"Nup")
    Ndn=expect(psi0,"Ndn")
    @show(sum(Nup))
    @show(sum(Ndn))
    @show(sum(Nup+Ndn))
    ############# DMRG ##########
    sweeps = Sweeps(10)
    maxdim!(sweeps,50,100,100,300,500,500,600)
    noise!(sweeps,1E-4,1E-6,1E-8,1E-10,1E-12,1E-12)
    mindim!(sweeps,10)
    cutoff!(sweeps,1E-8)

    energy,psi = dmrg(H,psi0,sweeps)
    #############################
    println("Final occupation")   
    Nup=expect(psi,"Nup")
    Ndn=expect(psi,"Ndn")
    @show(sum(Nup))
    @show(sum(Ndn))
    @show(sum(Nup+Ndn))
    
end

```

In this example I have just electron 4 sites and the pairing takes place between sites 3 and 4. If I start with an state with 3 electrons the ground state will appear with 4 electrons. Could you try to figure out if there is a problem?

Kind regards,  
Edson.

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [October 2, 2023, 10:55pm UTC](https://itensor.discourse.group/t/issues-with-nf-parity-conservation/1176/2 "2023-10-02T22:55:35Z")

</div>

Dear Edson,  
Thanks for the really nice code which was helpful for me to run while thinking about your question. I think I realized the answer to your question is actually a conceptual one and not a code one. But the code helped me because I was printing out samples from the wavefunction (using the ITensor `sample!` function if you want to try it) and they were always coming out with odd parity. But I was getting the same Nup+Ndn total as you were.

So here’s the answer: having odd parity, as your state does, does not mean that the total density needs to be an odd number, or even an integer. Here’s an example state that shows this:

\ket{\psi} = \frac{1}{\sqrt{3}}( \ket{\uparrow 0 0} + \ket{0 0 \uparrow} + \ket{\uparrow\uparrow\uparrow})

If you take the expected value of \hat{N}\_\uparrow in that state, it should give some value between 1 and 3 whereas it does have odd parity because each ket has an odd number of fermions in it.

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [October 2, 2023, 10:56pm UTC](https://itensor.discourse.group/t/issues-with-nf-parity-conservation/1176/3 "2023-10-02T22:56:52Z")

</div>

If you do want to check the overall parity or overall quantum-number properties of your state after DMRG, you can call

```auto
flux(psi)

```

and get the parity that way. Or you could call `sample!(psi)` as I mentioned to see representative samples from the state. The samples are obtained [using this algorithm](http://tensornetwork.org/mps/algorithms/sampling/).

---

<div class="post-metadata">

**Author:** ![vernek](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/vernek/32/381_2.png) [@vernek](https://itensor.discourse.group/u/vernek)\
**Post date:** [October 3, 2023, 11:19am UTC](https://itensor.discourse.group/t/issues-with-nf-parity-conservation/1176/4 "2023-10-03T11:19:11Z")

</div>

Dear Miles,

Thank you very much for realizing my naive mistake. I completely agree with you. I wanted to check for the symmetry of the ground state and stated by looking at the expectation value of the total number of electrons which indeed is not useful for this purpose. The best way is indeed using the function flux().

Kind regards,

Edson.

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [October 3, 2023, 2:24pm UTC](https://itensor.discourse.group/t/issues-with-nf-parity-conservation/1176/5 "2023-10-03T14:24:46Z")

</div>

Hi Edson, no problem. I was confused by it at first too, and thought there was a bug, but then when I checked the flux and samples of the state itself those looked ok, so then I realized the issue must be with the densities.
