# Calculating the Log Negativity

**URL:** <https://itensor.discourse.group/t/calculating-the-log-negativity/1797>\
**Category:** DMRG and Numerical Methods\
**Created:** [June 11, 2024, 8:04am UTC](https://itensor.discourse.group/t/calculating-the-log-negativity/1797 "2024-06-11T08:04:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hank](https://avatars.discourse-cdn.com/v4/letter/h/f475e1/32.png) [@Hank](https://itensor.discourse.group/u/Hank)\
**Post date:** [June 11, 2024, 8:04am UTC](https://itensor.discourse.group/t/calculating-the-log-negativity/1797/1 "2024-06-11T08:04:03Z")

</div>

Hello everyone.

Hope you are doing well.

Below, I provide with the code of the spin chain of mixed spins (1,1/2) with 14 sites. I should specify that my interest is in obtaining the reduced density matrix as well as some log negativity based on matrix product states.

In the code below, my problem is at the level of obtaining the logarithm of the trace of partial transpose since the states are tensors of order 3 in general.

```auto
using ITensors, ITensorMPS

let
    c=1
  N = 14

  # Make an array of N Index objects with alternating
  # "S=1/2" and "S=1" tags on odd versus even sites
  # (The first argument n->isodd(n) ... is an
  # on-the-fly function mapping integers to strings)
  sites = siteinds(n->isodd(n) ? "S=1/2" : "S=1",N)

 # Couplings between spin-half and
  # spin-one sites:
  Jho = 1.0 # half-one coupling
  Jhh = 0.5 # half-half coupling
  Joo = 0.5 # one-one coupling

  os = OpSum()
  for j=1:N-1
    os += 0.5*Jho,"S+",j,"S-",j+1
    os += 0.5*Jho,"S-",j,"S+",j+1
    os += Jho,"Sz",j,"Sz",j+1
  end
  for j=1:2:N-2
    os += 0.5*Jhh,"S+",j,"S-",j+2
    os += 0.5*Jhh,"S-",j,"S+",j+2
    os += Jhh,"Sz",j,"Sz",j+2
  end
  for j=2:2:N-2
    os += 0.5*Joo,"S+",j,"S-",j+2
    os += 0.5*Joo,"S-",j,"S+",j+2
    os += Joo,"Sz",j,"Sz",j+2
  end
  H = MPO(os,sites)

  nsweeps = 10
  maxdim = [10,20,40,80,100,140,180,200]
    cutoff = [1E-8]

  psi0 = random_mps(sites;linkdims=4)

  energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff)

   orthogonalize!(psi,c+6)
    ket1 = psi[c]*psi[c+1]
    bra1 = dag(ket1)
    ket2 = psi[c+4]#*psi[c+5]
    bra2 = dag(ket2)
    rho1 = prime(ket1,"Site")*bra1
    rho2 = prime(ket2,"Site")*bra2
    N1=partial_transpose_per(rho1, 2, (2,2))
    N2=partial_transpose_per(rho2, 2, (2,2))
    Ns1=log(tr(N1))
    Ns2=log(tr(N2)) 
    N=(Ns1*Ns2)^(1/7)
    
    return @show psi[10];
end
   

```

Therefore, my question would be: Please, is there an alternative way to compute the logarithm negativity once I obtain the reduced density matrix since the approach I used seems not useful for that purpose?

Also, I used as code for partial transpose the following:

```auto
function partial_transpose_per(x, sys, dims)
    n = length(dims)
    d = prod(dims)
    s = n - sys + 1
    p = collect(1:2n)
    p[s] = n + s
    p[n + s] = s
    rdims = reverse(dims)
    r = reshape(x, (rdims..., rdims...))
    return reshape(permutedims(r,p),(d,d))
end

```

Thank you in advance for your reaction and suggestion(s).

Idriss

---

<div class="post-metadata">

**Author:** ![adityab999](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@adityab999](https://itensor.discourse.group/u/adityab999)\
**Post date:** [June 13, 2024, 5:05pm UTC](https://itensor.discourse.group/t/calculating-the-log-negativity/1797/2 "2024-06-13T17:05:18Z")

</div>

Hi,

There are a few issues with your code even before coming to the point of computing the negativity. I ran it and found the following :

1 - Firstly, the sum of eigenvalues of your reduced density matrices is \neq 1, which means you are not constructing density matrices. This is because, in the example code you posted, you are orthogonalizing at c+6 but then making ket1 at (c,c+1). You must either orthogonalize at c, or create ket1 at (c+6,c+7).

2 - In the partial transpose function, there are a few incorrect operations. One can’t reshape an itensor object. You have to first convert the itensor object to an Array object, but even doing that produces dimension mismatch, which is understandable presumably because one has to figure out which indices of the itensor object you want for your Array objects. I didn’t dig much into this as this approach is confusing to me, so i am writing below an alternative that I have used in similar calculations.

```auto
orthogonalize!(psi,c+6)
ket1 = psi[c+6]*psi[c+7]    
bra1 = dag(ket1)
rho1 = prime(ket1,"Site")*bra1 # if the two sites are not adjacent, something else needs to be added to the construction of the red. density matrix.

#@show inds(rho1) # this shows that inds(rho1)[2] and inds(rho1)[4] belong to the second site c+7. Suppose you now want to partial-transpose with respect to the basis at this site, then do 

rho1_PT = swapinds(rho1,inds(rho1)[2],inds(rho1)[4])

d,u = eigen(rho1_PT)
#@show diag(real(d)) # if this reduced density matrix fails the PPT criterion, it will show some negative eigenvalues signaling mixed state entanglement.

```

Now one way to define the entanglement negativity is \mathcal{N} = \frac{1}{2} \sum\_i (|\lambda\_i| - \lambda\_i ) and logarithmic negativity being = log(1+2\mathcal{N}), where \lambda\_i are the eigenvalues of the partial-transposed reduced density matrix (rho1\_PT above).

---

<div class="post-metadata">

**Author:** ![Hank](https://avatars.discourse-cdn.com/v4/letter/h/f475e1/32.png) [@Hank](https://itensor.discourse.group/u/Hank)\
**Post date:** [June 24, 2024, 8:28am UTC](https://itensor.discourse.group/t/calculating-the-log-negativity/1797/3 "2024-06-24T08:28:39Z")

</div>

Hi @adityab999,

Thank you very much for your reaction.

Please do allow me to address another concern of mine.

In fact, currently I am trying to compute a 4-partite log negativity on my spin-1/2 chain. To do so, I have been told to select 4 different sites that I will call A, B,C, and, D.

One approach consists of grouping the sites as follows:  
A|BCD  
B|ACD  
C|ABD  
D|ABC  
AB  
AC  
AD

computing the negativity of each configuration and later find the seventh-root of the product of each negativity.

My problem is at the level of computing the negativity of each configuration because the sanity check (reduced density matrix having its diagonal summing up to 1) seems to fail.

Therefore, I would like to know if you have some recommendations to address the problem.

Thanks in advance for your reaction.

Sincerely,  
Idriss

---

<div class="post-metadata">

**Author:** ![adityab999](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@adityab999](https://itensor.discourse.group/u/adityab999)\
**Post date:** [July 1, 2024, 5:14am UTC](https://itensor.discourse.group/t/calculating-the-log-negativity/1797/4 "2024-07-01T05:14:12Z")

</div>

Hi, your specific questions are doable with the approach mentioned previously in this thread and/or with the one mentioned here ([Is this a correct way to calculate one-site and two-site entanglement spectrum and entropy?](https://itensor.discourse.group/t/is-this-a-correct-way-to-calculate-one-site-and-two-site-entanglement-spectrum-and-entropy/1263)). Please look into them and experiment around to find a way that works correctly.
