# How to calculate onsite observerbles more efficiently?

**URL:** <https://itensor.discourse.group/t/how-to-calculate-onsite-observerbles-more-efficiently/1806>\
**Category:** ITensor Julia Questions\
**Tags:** julia, dmrg\
**Created:** [June 15, 2024, 6:33am UTC](https://itensor.discourse.group/t/how-to-calculate-onsite-observerbles-more-efficiently/1806 "2024-06-15T06:33:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucy](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/lucy/32/434_2.png) [@Lucy](https://itensor.discourse.group/u/Lucy)\
**Post date:** [June 15, 2024, 6:33am UTC](https://itensor.discourse.group/t/how-to-calculate-onsite-observerbles-more-efficiently/1806/1 "2024-06-15T06:33:15Z")

</div>

Hi there,

I find the function `measurelocalops!()` in the function `function measure!(obs::DMRGObserver; kwargs...)` in the source code `\src\ITensorMPS\observer.jl`.

I redefined this function as

```auto
function ITensors.measurelocalops!(obs::ObserverDemo, wf::ITensor, i::Int)
    for o in obs.onsite_ITensor_ops
        # Moves to GPU if needed
        # @show o
        oⱼ = ITensors.adapt(ITensors.datatype(wf), ITensors.op(obs.sites, o, i))
        m = ITensors.dot(wf, ITensors.apply(oⱼ, wf))
        imag(m) > 1e-8 && (@warn "encountered finite imaginary part when measuring $o")
        obs.measurements[o][end][i] = real(m)
    end
end

```

in my own code with the version of ITensors v0.3.35, and it works much more efficiently than `inner(psi', MPO(onsite_mpo, obs.sites), psi)`. However, when I update ITensors to version v0.3.68, this redefined function breaks down, while the source code does not change much.

How can I still use the more efficient way to calculate averages of the onsite operator with the updated version of ITensors?

Thank you so much for your time!

Best wishes.

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [June 15, 2024, 12:23pm UTC](https://itensor.discourse.group/t/how-to-calculate-onsite-observerbles-more-efficiently/1806/2 "2024-06-15T12:23:24Z")

</div>

Can you share a fully runnable minimal example that reproduces the issue you are seeing, along with the full error message (as instructed in [Please Read: Make It Easier to Help You](https://itensor.discourse.group/t/please-read-make-it-easier-to-help-you/7))? Additionally, can you update to the the latest version of ITensors.jl (v0.6.13) and try on that version?

---

<div class="post-metadata">

**Author:** ![Lucy](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/lucy/32/434_2.png) [@Lucy](https://itensor.discourse.group/u/Lucy)\
**Post date:** [September 25, 2024, 8:12am UTC](https://itensor.discourse.group/t/how-to-calculate-onsite-observerbles-more-efficiently/1806/3 "2024-09-25T08:12:30Z")

</div>

Thank you for your response! After running additional tests, I discovered a bug in one of my functions, so the issue isn’t connected to ITensors.
