# Forcing hermitianity of an operator

**URL:** <https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062>\
**Category:** ITensor Julia Questions\
**Created:** [July 28, 2023, 1:23pm UTC](https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062 "2023-07-28T13:23:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![EmilioRui](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/emiliorui/32/208_2.png) [@EmilioRui](https://itensor.discourse.group/u/EmilioRui)\
**Post date:** [July 28, 2023, 1:23pm UTC](https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062/1 "2023-07-28T13:23:34Z")

</div>

Hi everyone!

I am trying to force an operator to be hermitian.

I would essentially like to sum an operator by its dagger operator.

B = A + A^\dagger

Where A is a complex MPO.

Should I do it with ITensors.dag() or with ITensors.adjoint()

Thank you in advance.

---

<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:** [July 30, 2023, 1:02pm UTC](https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062/2 "2023-07-30T13:02:43Z")

</div>

You can use `swapprime(dag(A), 0 => 1)`. It is a bit confusing, but `dag` essentially just does the complex conjugation (as well as swapping arrow directions, when QN conservation is enabled), and `swapprime` swaps the prime levels of the MPO, which effectively does a transposition. So together they do a [conjugate transpose/Hermitian transpose](https://en.wikipedia.org/wiki/Conjugate_transpose).

---

<div class="post-metadata">

**Author:** ![EmilioRui](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/emiliorui/32/208_2.png) [@EmilioRui](https://itensor.discourse.group/u/EmilioRui)\
**Post date:** [July 30, 2023, 3:30pm UTC](https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062/3 "2023-07-30T15:30:16Z")

</div>

Thank you so much for the quick answer!

---

<div class="post-metadata">

**Author:** ![TakisAngelides](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/takisangelides/32/133_2.png) [@TakisAngelides](https://itensor.discourse.group/u/TakisAngelides)\
**Post date:** [January 12, 2024, 5:02pm UTC](https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062/4 "2024-01-12T17:02:59Z")

</div>

Hi Matt,

I think there is an issue here with your statement:

" and `swapprime` swaps the prime levels of the MPO, which effectively does a transposition "

because if this is done with conserved quantum numbers one would eventually run into the error that the primed indices are pointing in whereas the convention is that they are pointing out. E.g. an error can occur if you try to apply this MPO to an object like:

```auto
N = 1
sites = siteinds("S=1/2", N, conserve_qns = true)
state = [isodd(n) ? 1 : 2 for n=1:N]
mps = randomMPS(sites, state)

```

Example code:

```auto
sites = siteinds("S=1/2", N, conserve_qns = true)
mpo = randomMPO(sites)
println(inds(mpo))
println("------------------------------------------------------------")
mpoT = swapprime(mpo, 0 => 1)
println(inds(mpoT))

```

Output:

```auto
Base.Fix1{typeof(filter), MPO}(filter, MPO
[1] ((dim=2|id=380|"S=1/2,Site,n=1")' <Out>
 1: QN("Sz",1) => 1
 2: QN("Sz",-1) => 1, (dim=2|id=380|"S=1/2,Site,n=1") <In>
 1: QN("Sz",1) => 1
 2: QN("Sz",-1) => 1)
)
------------------------------------------------------------
Base.Fix1{typeof(filter), MPO}(filter, MPO
[1] ((dim=2|id=380|"S=1/2,Site,n=1") <Out>
 1: QN("Sz",1) => 1
 2: QN("Sz",-1) => 1, (dim=2|id=380|"S=1/2,Site,n=1")' <In>
 1: QN("Sz",1) => 1
 2: QN("Sz",-1) => 1)

```

Let me know if I am looking at this the wrong way or if indeed this is problematic, then I am curious what would be the correct way to transpose so that it works with conserved QN as well. One way I see is to reverse the direction of the site indices after the swapprime but I did not find a way except from dag which would (in this case unwantedly) also complex conjugate the elements. One possible ugly solution maybe can be this:

```auto
let

    sites = siteinds("S=1/2", 1, conserve_qns = true)
    mpo = randomMPO(sites)
    println(inds(mpo))
    println("------------------------------------------------------------")
    mpoT = swapprime(mpo, 0 => 1)
    for (idx, element) in enumerate(mpoT)
        s, sp = inds(element; :tags => "Site", :plev => 0)[1], inds(element; :tags => "Site", :plev => 1)[1]
        mpoT[idx] = setprime(setprime(element*delta(dag(s), dag(s''))*delta(dag(sp), dag(sp'')), 0; :plev => 2), 1; :plev => 3)
    end
    println(inds(mpoT))

end

```

Output:

```auto
Base.Fix1{typeof(filter), MPO}(filter, MPO
[1] ((dim=2|id=267|"S=1/2,Site,n=1")' <Out>
 1: QN("Sz",1) => 1
 2: QN("Sz",-1) => 1, (dim=2|id=267|"S=1/2,Site,n=1") <In>
 1: QN("Sz",1) => 1
 2: QN("Sz",-1) => 1)
)
------------------------------------------------------------
Base.Fix1{typeof(filter), MPO}(filter, MPO
[1] ((dim=2|id=267|"S=1/2,Site,n=1") <In>
 1: QN("Sz",1) => 1
 2: QN("Sz",-1) => 1, (dim=2|id=267|"S=1/2,Site,n=1")' <Out>
 1: QN("Sz",1) => 1
 2: QN("Sz",-1) => 1)
)

```

Thanks in advance for the help!

---

<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:** [January 12, 2024, 5:14pm UTC](https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062/5 "2024-01-12T17:14:20Z")

</div>

You should use `swapprime(dag(mpo), 0 => 1)` or `noprime(linkinds, swapprime(dag(mpo), 0 => 1))` since some functions like `dmrg` don’t work properly with primed link indices.

See also [DMRG for non Hermitian MPO](https://itensor.discourse.group/t/dmrg-for-non-hermitian-mpo/1287).

---

<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:** [January 12, 2024, 5:15pm UTC](https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062/6 "2024-01-12T17:15:14Z")

</div>

Also you should use `siteinds` or `linkinds` to print indices of MPOs.

---

<div class="post-metadata">

**Author:** ![TakisAngelides](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/takisangelides/32/133_2.png) [@TakisAngelides](https://itensor.discourse.group/u/TakisAngelides)\
**Post date:** [January 12, 2024, 5:17pm UTC](https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062/7 "2024-01-12T17:17:28Z")

</div>

But this will complex conjugate the elements of the mpo whereas I was referring to only the transpose action on an MPO.

---

<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:** [January 12, 2024, 5:19pm UTC](https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062/8 "2024-01-12T17:19:35Z")

</div>

You could use `swapprime(dag(conj(mpo)), 0 => 1)`.

---

<div class="post-metadata">

**Author:** ![TakisAngelides](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/takisangelides/32/133_2.png) [@TakisAngelides](https://itensor.discourse.group/u/TakisAngelides)\
**Post date:** [January 12, 2024, 5:21pm UTC](https://itensor.discourse.group/t/forcing-hermitianity-of-an-operator/1062/9 "2024-01-12T17:21:26Z")

</div>

This should do the job, did not know of the existence of the conj function, thanks!
