No problem! This is a common question since the way the total quantum numbers (QNs), such as particle number, is fixed is kind of subtle in ITensor. It is fixed by your choice of initial state. So if you prepare the MPS you are using as an initial guess for DMRG (I assume you are planning to do a DMRG calculation) to be a state with a certain particle number, then that particle number will remain the same as long as you construct your MPS using site indices that carry QN information.
The line of code
sites = siteinds("Electron",N)
is actually creating an array of N objects of type Index
, so N electron sites, not N electrons.
To make these sites carry QN information, you need to change this line to:
sites = siteinds("Electron",N,conserve_qns=true)
Then, to prepare an initial state with two electrons, say one being an up-spin electron and the other being a down-spin electron, you can use code such as:
states = ["0" for n=1:N]
states[1] = "Up"
states[2] = "Dn"
psi = productMPS(sites,states)
You can also call psi = randomMPS(sites,states,10)
to create a randomized MPS which has the site indices sites
, the same total QN as given by the product state states
, and a bond dimension of 10.
Then if you plug this psi
MPS into DMRG as an initial state together with a Hamiltonian that conserves particle number, the way ITensor works is that the particle number is guaranteed not to change. This is because when you use Index objects that have QNs in them, only operations which change these QNs by a definite amount are allowed, and your Hamiltonian will change them by an amount which is zero if it conserves them.
energy,psi = dmrg(H,psi,sweeps)
Please post a comment if you have any follow-up questions.
There is a full example of doing DMRG for the extended Hubbard model with QN conservation here:
https://github.com/ITensor/ITensors.jl/blob/master/examples/dmrg/exthubbard.jl
Best,
Miles