# Computing correlation functions with mixed site types: unsure how to overload operator definitions

**URL:** <https://itensor.discourse.group/t/computing-correlation-functions-with-mixed-site-types-unsure-how-to-overload-operator-definitions/2419>\
**Category:** ITensor Julia Questions\
**Created:** [June 11, 2025, 9:42am UTC](https://itensor.discourse.group/t/computing-correlation-functions-with-mixed-site-types-unsure-how-to-overload-operator-definitions/2419 "2025-06-11T09:42:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amoghanakru](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@amoghanakru](https://itensor.discourse.group/u/amoghanakru)\
**Post date:** [June 11, 2025, 9:42am UTC](https://itensor.discourse.group/t/computing-correlation-functions-with-mixed-site-types-unsure-how-to-overload-operator-definitions/2419/1 "2025-06-11T09:42:40Z")

</div>

Hi,

I am currently working with a mixed site set consisting of spin-1/2 electrons and a bosonic spin-1/2. The implementation is as follows (copied from [here](https://itensor.discourse.group/t/mixed-site-set-with-quantum-number-conservation/470), see also [here](https://itensor.discourse.group/t/mixed-site-set-with-u-1-symmetry/947)), where the number of sites in the MPS is doubled and the electrons and spins are on neighboring sites.

```julia
using ITensors
using ITensors, ITensorMPS, ITensorInfiniteMPS

Nx=4
Ny=4
sites = Vector{Index}(undef,2*Nx*Ny)
for n=1:2*Nx*Ny
  nx=div(n - 1, Ny) + 1
  if iseven(nx) #This just places the spins on sites with even x
    sites[n] = Index([QN() => 2],"S=1/2,Site,n=$n")
  else
    sites[n] = siteind("Electron"; addtags="n=$n",conserve_qns=true,conserve_sz=false)
  end
end

```

Now, I wish to compute the charge-charge correlator, which in an ordinary spin-1/2 system is efficiently done with `charge_corr=correlation_matrix(psi, "Ntot", "Ntot")`. However, in the present case, “Ntot” is not defined for the site type “S=1/2”. My initial guess was to redefine the “S=1/2” site type by making a new file `include("spinhalfkondo.jl")`, identical to [the one from ITensors](https://github.com/ITensor/ITensors.jl/blob/main/src/lib/SiteTypes/src/sitetypes/spinhalf.jl) save for this extra line at the very end:

```auto
function op(::OpName"Ntot", ::SiteType"S=1/2")
    return [
      0.0 0.0;
      0.0 0.0
    ]
  end

```

However, this solution does not work, throwing the error

```auto
ERROR: ArgumentError: Overload of "op" or "op!" functions not found for operator name "Ntot" and Index tags: ("S=1/2,Site,n=4",).

```

I’m not sure how to perform the requisite overload, given that I have constructed the site indices in an unusual manner. Any help would be appreciated.

For convenience, this code fragment generates a wavefunction compatible with the site set defined above.

```auto
  # Create starting state, for testing purposes
  state = map(CartesianIndices((Ny, 2*Nx))) do I
    y=I[1]
    x=I[2]
    if iseven(x) #i.e. if this is a spin index
      return iseven(div(x,2)) ⊻ iseven(y) ? "↓" : "↑"
    else #i.e. if this is a fermion index
      x_f = div(x,2)+1
      return (mod(x_f+y,filling)==0) ? "Up" : "0"
    end
  end
  display(state)

  psi = MPS(sites, state)

```

---

<div class="post-metadata">

**Author:** ![ryanlevy](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/ryanlevy/32/150_2.png) [@ryanlevy](https://itensor.discourse.group/u/ryanlevy)\
**Post date:** [June 11, 2025, 10:17am UTC](https://itensor.discourse.group/t/computing-correlation-functions-with-mixed-site-types-unsure-how-to-overload-operator-definitions/2419/2 "2025-06-11T10:17:26Z")

</div>

> [@amoghanakru](#):
>
> ```auto
> function op(::OpName"Ntot", ::SiteType"S=1/2")
> return [
> 0.0 0.0;
> 0.0 0.0
> ]
> end
> 
> ```

You need to have `function ITensors.op(` and it will work

Some other small comments:

- `state` can be a bad variable name to use as it is also a function

- Recall you can pass what sites you want in the `correlation_matrix`, so you could avoid this with annoying post processing with

- Your `state` is a matrix, and when your `state` is converted inside `MPS` [the code](https://github.com/ITensor/ITensorMPS.jl/blob/main/src/mps.jl#L421) is doing

---

<div class="post-metadata">

**Author:** ![amoghanakru](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@amoghanakru](https://itensor.discourse.group/u/amoghanakru)\
**Post date:** [June 12, 2025, 9:40pm UTC](https://itensor.discourse.group/t/computing-correlation-functions-with-mixed-site-types-unsure-how-to-overload-operator-definitions/2419/3 "2025-06-12T21:40:29Z")

</div>

Thanks very much, the solution worked perfectly!

Just one quick question regarding the last point: I adapted that particular segment (with defining a state and then constructing the MPS) from the [2-D Hubbard DMRG example](https://github.com/ITensor/ITensorMPS.jl/blob/main/examples/dmrg/2d_hubbard_conserve_momentum.jl). Was there any concern with the implementation here? (I have benchmarked this code against exactly solvable things like a band insulator and things look OK, but if there was any potential problem that you had anticipated I would want to address it.)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/d3072b13e047cd06df7f3981547c0917d940dfa4.png) [@system](https://itensor.discourse.group/u/system)\
**Post date:** [June 22, 2025, 9:40pm UTC](https://itensor.discourse.group/t/computing-correlation-functions-with-mixed-site-types-unsure-how-to-overload-operator-definitions/2419/4 "2025-06-22T21:40:59Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
