# Site dependent Customized Operator

**URL:** <https://itensor.discourse.group/t/site-dependent-customized-operator/2078>\
**Category:** ITensor Julia Questions\
**Created:** [October 22, 2024, 1:41pm UTC](https://itensor.discourse.group/t/site-dependent-customized-operator/2078 "2024-10-22T13:41:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jyoti\_Bisht](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jyoti\_Bisht](https://itensor.discourse.group/u/Jyoti_Bisht)\
**Post date:** [October 22, 2024, 1:41pm UTC](https://itensor.discourse.group/t/site-dependent-customized-operator/2078/1 "2024-10-22T13:41:33Z")

</div>

Hi,

1. I want to introduce new operators in sitetype=“Electron” to find out the two point correlation. But the operator is different on alternate site. The question is how to define site dependent operators?

2. How to take product of two operators?

3. How to see the matrix form of the inbuilt operators (let’s say “Cup” or “Cdn”). I can check them elementwise but if at once I want see the whole matrix (4\*4), how to do that?

Thank you…

---

<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:** [October 22, 2024, 2:44pm UTC](https://itensor.discourse.group/t/site-dependent-customized-operator/2078/2 "2024-10-22T14:44:26Z")

</div>

1. I would recommend just specifying the operator and then choosing the correct sites it runs over. For example if using `correlation_matrix` has a keyword argument `sites`.
2. There are lots of ways to this, can you be more specific on what kinds of operators and what you want to use them for?
3. If you want to see the matrix form, you can see the source code here [ITensors.jl/src/lib/SiteTypes/src/sitetypes/electron.jl at main · ITensor/ITensors.jl · GitHub](https://github.com/ITensor/ITensors.jl/blob/main/src/lib/SiteTypes/src/sitetypes/electron.jl)  
Or you can use `@show`

```julia
julia> @show op("Cup",sites[1])
op("Cup", sites[1]) = ITensor ord=2
Dim 1: (dim=4|id=964|"Electron,Site,n=1")'
Dim 2: (dim=4|id=964|"Electron,Site,n=1")
NDTensors.Dense{Float64, Vector{Float64}}
 4×4
 0.0 1.0 0.0 0.0
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 1.0
 0.0 0.0 0.0 0.0
ITensor ord=2 (dim=4|id=964|"Electron,Site,n=1")' (dim=4|id=964|"Electron,Site,n=1")
NDTensors.Dense{Float64, Vector{Float64}}

```

The arrangement of the data might not be how you like it though, so you can specify the index ordering and convert the ITensor to an array as well

```auto
julia> array(op("Cup",sites[1]),sites[1],sites[1]')
4×4 Matrix{Float64}:
 0.0 0.0 0.0 0.0
 1.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0
 0.0 0.0 1.0 0.0

```

---

<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 22, 2024, 2:57pm UTC](https://itensor.discourse.group/t/site-dependent-customized-operator/2078/3 "2024-10-22T14:57:44Z")

</div>

Regarding 1, about making totally site-dependent operators, you can always just make them “by hand” by doing something like this:

```julia
matrix_elements = [a b; c d]
operator = ITensor(matrix_elements, sites[j] , sites[j]')

```

That would make an ITensor with whichever entries you like that is like the operators returned by our `op` function. The downside is that it does not, however, let you use such an operator in our OpSum system.

If you do need site-dependent operators in OpSum or something like `correlation_matrix` or `expect`, can you give a small example of what you are trying to do?

---

<div class="post-metadata">

**Author:** ![Jyoti\_Bisht](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jyoti\_Bisht](https://itensor.discourse.group/u/Jyoti_Bisht)\
**Post date:** [October 22, 2024, 3:05pm UTC](https://itensor.discourse.group/t/site-dependent-customized-operator/2078/4 "2024-10-22T15:05:24Z")

</div>

Thank you very much ryanlevy for your replies.

1. I quite did not understand your answer because of my lack of information may be . Let me rephrase my question.  
Lets say, I have a chain with customized operator like AAAAAAA… where A=Ao(at odd site) and A=Ae(at even site).  
So is there a way like simple condition problem.  
(if site = odd  
A = Ao  
else  
A= Ae  
end). to define the operators ??

The suggestion you have recommended, can you please provide simple code over it because I did not use “sites” argument for “correlation\_matrix” before??

1. In case of “Electron site type”, if I want to see the multiplication of lets say cup\*cup , how to do that?

2. I understood it.

Thank you very much

---

<div class="post-metadata">

**Author:** ![Jyoti\_Bisht](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jyoti\_Bisht](https://itensor.discourse.group/u/Jyoti_Bisht)\
**Post date:** [October 22, 2024, 3:25pm UTC](https://itensor.discourse.group/t/site-dependent-customized-operator/2078/5 "2024-10-22T15:25:30Z")

</div>

Thanks Miles for your reply.

I am working with sitetype = “Electron”. I am adding one operator(A) in it. I want to find out correlation\_matrix(psi,“A”,“A”). In this A=Ao at odd site and A=Ae at even site. So how do I do that?

---

<div class="post-metadata">

**Author:** ![sandipanmanna](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@sandipanmanna](https://itensor.discourse.group/u/sandipanmanna)\
**Post date:** [October 23, 2024, 6:07am UTC](https://itensor.discourse.group/t/site-dependent-customized-operator/2078/6 "2024-10-23T06:07:57Z")

</div>

Hi!  
1)You can define a custom operator that differ between even and odd site. Maybe something like this will work.

```auto
function ITensors.op(::OpName"c_custom", ::SiteType"Electron", s1::Index)

    # Get the site location
    loc=parse(Int, match(r"n=(\d+)", string(tags(s1)))[1])
    if(loc%2==0)
        return op("Cdagup", s1)
    else
        return op("Cup", s1)
    end

end

let 
    N = 10
    sites = siteinds("Electron",N)
    psi=randomMPS(sites)

    opM=op("c_custom",sites[6])
    @show(opM)

    corr_mat = correlation_matrix(psi, "c_custom", "c_custom"; sites=4:7);
    @show(stdout,"text/plain",corr_mat)

    
end

```

Here, sites tell you for which sites the correlation matrix need to be calculated.  
I am not sure if there is an inbuilt function to get the location from the siteindex object. This uses some regex to obtain site location from tag. Also, I am not exactly sure whether the inbuilt correlation\_matrix will handle the JW strings for this custom operator properly. Please do a check with ED.

Did you mean to ask that you have some custom operator in place of Cup? You can define that operator too in a similar fashion by setting the appropriate elements in an itensor.

1. It may depend on your purpose. I guess by multiplication you meant on the same site (matrix multiplication) and not Kronecker Product. If you want to simply see the elements, you can just convert the operators to a matrix and take their product.

```auto
matrix(op("Cup",sites[6]))*matrix(op("Cup",sites[6]))

```

Note the exact representation depends on basis ordering.

---

<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:** [December 4, 2024, 8:01pm UTC](https://itensor.discourse.group/t/site-dependent-customized-operator/2078/7 "2024-12-04T20:01:38Z")

</div>

The approach by @sandipanmanna should work. Though it is not an ideal approach, but definitely makes sense.

I think this is just a case we had not planned for with the `correlation_matrix` function, and in the future we may want to offer an interface where the user can provide a function that the code will call to construct an operator which can be custom for each site.

Another approach that could work well for you is simply to open up the source code for the `correlation_matrix` function, make a new function based on it, and modify it to suit your purpose.

---

<div class="post-metadata">

**Author:** ![Jyoti\_Bisht](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jyoti\_Bisht](https://itensor.discourse.group/u/Jyoti_Bisht)\
**Post date:** [January 15, 2025, 12:07pm UTC](https://itensor.discourse.group/t/site-dependent-customized-operator/2078/8 "2025-01-15T12:07:06Z")

</div>

sorry for the late reply.  
thank you very much Sandipanmanna, your method worked.

thank you Miles for your suggestions.

---

<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:** [January 25, 2025, 12:07pm UTC](https://itensor.discourse.group/t/site-dependent-customized-operator/2078/9 "2025-01-25T12:07:19Z")

</div>

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