# Fermionic operators

**URL:** <https://itensor.discourse.group/t/fermionic-operators/798>\
**Category:** ITensor Julia Questions\
**Created:** [April 23, 2023, 8:45am UTC](https://itensor.discourse.group/t/fermionic-operators/798 "2023-04-23T08:45:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jyotsna](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jyotsna](https://itensor.discourse.group/u/jyotsna)\
**Post date:** [April 23, 2023, 8:45am UTC](https://itensor.discourse.group/t/fermionic-operators/798/1 "2023-04-23T08:45:08Z")

</div>

Hi,

I am just starting out with ITensor. I was looking to write a code with some multiple site fermionic operators and wrote this for starters:

```auto
using ITensors

N_c=1 #number of unit cells

N=2*N_c

sites = siteinds("Fermion",N)

state0=["0","0"]

psi0 = productMPS(sites,state0)

state1=["1","1"]

psi1 = productMPS(sites,state1)

ampo = OpSum()

ampo+= ("A",1,"F",1,"A",2)

ampo+= ("A",1,"F",1,"A",2)

H= MPO(sites, ampo)

print(inner(psi0, H, psi1))

```

But this code is giving errors related to the line with MPO. Am I missing something?

Sorry, if this has already been answered elsewhere. I could not figure out where this was wrong.

---

<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:** [April 23, 2023, 11:53am UTC](https://itensor.discourse.group/t/fermionic-operators/798/2 "2023-04-23T11:53:07Z")

</div>

Glad you are checking out ITensor. Could you please paste below what error message you got from Julia, including the full stack trace?

---

<div class="post-metadata">

**Author:** ![jyotsna](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jyotsna](https://itensor.discourse.group/u/jyotsna)\
**Post date:** [April 23, 2023, 3:15pm UTC](https://itensor.discourse.group/t/fermionic-operators/798/3 "2023-04-23T15:15:40Z")

</div>

**ERROR:** LoadError: MethodError: Cannot `convert` an object of type Type{Float64} to an object of type Vector{ITensor}

Closest candidates are:

convert(::Type{T}, ::LinearAlgebra.Factorization) where T\<:AbstractArray at /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/factorization.jl:58

convert(::Type{T}, ::Strided.AbstractStridedView) where T\<:Array at ~/.julia/packages/Strided/pDrSx/src/abstractstridedview.jl:31

convert(::Type{Array{T, N}}, ::StaticArraysCore.SizedArray{S, T, N, N, Array{T, N}}) where {S, T, N} at ~/.julia/packages/StaticArrays/4uslg/src/SizedArray.jl:88

…

Stacktrace:

[1] **MPO(data::Type, llim::Vector{Index{Int64}}, rlim::Sum{Scaled{ComplexF64, Prod{Op}}})**

@ ITensors ~/.julia/packages/ITensors/nablm/src/mps/mpo.jl:9

[2] **MPO(sites::Vector{Index{Int64}}, ops::Sum{Scaled{ComplexF64, Prod{Op}}})**

@ ITensors ~/.julia/packages/ITensors/nablm/src/mps/mpo.jl:93

[3] top-level scope

@ ~/Documents/states\_in\_occ\_number\_basis.jl:25

---

<div class="post-metadata">

**Author:** ![jyotsna](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jyotsna](https://itensor.discourse.group/u/jyotsna)\
**Post date:** [April 23, 2023, 3:17pm UTC](https://itensor.discourse.group/t/fermionic-operators/798/4 "2023-04-23T15:17:40Z")

</div>

Also, thank you for the quick response!

---

<div class="post-metadata">

**Author:** ![jyotsna](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jyotsna](https://itensor.discourse.group/u/jyotsna)\
**Post date:** [April 24, 2023, 3:15am UTC](https://itensor.discourse.group/t/fermionic-operators/798/5 "2023-04-24T03:15:32Z")

</div>

It’s working now. I went through some other FAQs and understand how to do it now.

```auto
using ITensors

N_c=1 #number of unit cells

N=2*N_c

sites = siteinds("Fermion", N);

state0=["0","0"];

psi0 = productMPS(sites,state0);

state1=["1","1"];

psi1 = productMPS(sites,state1);

ampo = AutoMPO();

#ampo += (1, "c†", 1, "c†", 2);

ampo += (1, "c", 1, "c", 2);

H= MPO( ampo, sites);

psi1=Apply(H,psi1);

print(inner(psi0,psi1))

```

Thank you!

---

<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:** [April 25, 2023, 1:40pm UTC](https://itensor.discourse.group/t/fermionic-operators/798/6 "2023-04-25T13:40:52Z")

</div>

Good to know. What did you do differently that fixed the issue for you?

The other issue with the code above that was causing the error message is that you should call the MPO constructor as:

```auto
H = MPO(ampo,sites)

```

so with the arguments in the other order.

A possible “action item” for the ITensor team here would be to provide a clearer error message, hopefully. The original error message above is very unclear unfortunately!

---

<div class="post-metadata">

**Author:** ![jyotsna](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jyotsna](https://itensor.discourse.group/u/jyotsna)\
**Post date:** [April 26, 2023, 12:13am UTC](https://itensor.discourse.group/t/fermionic-operators/798/7 "2023-04-26T00:13:35Z")

</div>

Thank you!

Yes, that was one error. Also, I am using c’s and c^{\dagger}'s instead of the earlier version. Using “A” and “F” was giving me some error.  
I have a few more questions:

Also, I have another code now that looks like this for creating two states with occupation numbers on multiple sites:

```auto
sites = siteinds("Fermion", num_of_occupation_numbers);

```

and then

```auto
  for i in 1:number_of_basis_states

    for j in 1:i

        #state0=Array{Char,1}(num_of_occupation_numbers);

        num1=i
        num2=j

        for l in 1:num_of_occupation_numbers
            
            if num1%2==0
                state1[l]="0"

            else
                state1[l]="1"

            end
            
            num1=num1÷(2)

        end

        for l in 1:num_of_occupation_numbers
            
            if num2%2==0
                state2[l]="0"

            else
                state2[l]="1"

            end
            
            num2=num2÷(2)

        end

        psi1 = productMPS(sites,state1);

        psi2 = productMPS(sites,state2);

        
        

        psi2=Apply(op1_mpo,psi2);

        

        #print(inner(psi1,psi2));

        sigma_1_z_matrix[i,j]=(inner(psi1, psi2))*im;

        sigma_1_z_matrix[j,i]=-(conj(inner(psi1, psi2)))*im;

        #show("done")

    end

end

```

I am now getting the eigenvalues and eigenvectors of sigma\_1\_z\_matrix. **Is there a way I can just define the operator corresponding to this matrix and get its eigenvalues and eigenvectors in the occupation number basis?**  
**Similarly, once I get the eigenvectors, can I form linear combinations of states as shown here**

**[Physics (SiteType) System Examples · ITensors.jl](https://itensor.github.io/ITensors.jl/dev/examples/Physics.html)**

**of these multiple site states?**

I want to compute inner products involving these linear combinations.

But I am unable to use inner(psi1+psi2, psi3+psi4) like this for instance.

Does inner allow linear combinations to be formed? How would you suggest I use linear combinations of the basis states that I form using the occupation numbers (which can be 0 or 1) and use “apply” and “inner” on these linear combinations?

Thanks a lot for your help again.

---

<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:** [April 28, 2023, 1:57pm UTC](https://itensor.discourse.group/t/fermionic-operators/798/8 "2023-04-28T13:57:11Z")

</div>

Above you are asking many different questions. Could you please post the most important one or two of these questions as new posts to the forum? Then we will try to answer them for you, thanks!
