# Partial Transpose and Log negativity

**URL:** <https://itensor.discourse.group/t/partial-transpose-and-log-negativity/1779>\
**Category:** ITensor Julia Questions\
**Created:** [June 3, 2024, 11:46pm UTC](https://itensor.discourse.group/t/partial-transpose-and-log-negativity/1779 "2024-06-03T23:46:04Z")\
**Posts on this page:** 3\
**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 3, 2024, 11:46pm UTC](https://itensor.discourse.group/t/partial-transpose-and-log-negativity/1779/1 "2024-06-03T23:46:04Z")

</div>

Hi all.

Hope you are doing well.

With this message, I would like to address a problem I am currently encountering in computing the partial transpose and the log negativity of a spin ring with N=14 sites.

To start with a simple case, I used a linear chain of say 14 sites for mixed spin chain (1, 1/2), and with some help (from here mainly) I got some results in finding the reduced density matrix (RDM) of the chain.

However, another important step would be to compute the partial transpose of let say some chosen sites via:

ket = psi[1]\*psi[2]  
bra= dag(ket)  
rho = prime(ket,“Site”)\*bra  
;

N=partial\_transpose\_per(rho, 2, (6,6))

where for the partial transpose I use the following code:

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  
;

My problem is at the level of the partial transpose here since it seems there is a mismatch although rho has the dimension 2_3_2\*3.

Please, do someone know how to solve the problem of dimension mismatch here since I’ve tried couple of things but so far none seems successful?

Sincerely,  
Idriss

---

<div class="post-metadata">

**Author:** ![kmp5](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/kmp5/32/251_2.png) [@kmp5](https://itensor.discourse.group/u/kmp5)\
**Post date:** [June 5, 2024, 12:15pm UTC](https://itensor.discourse.group/t/partial-transpose-and-log-negativity/1779/2 "2024-06-05T12:15:44Z")

</div>

Hi Idriss,

It would be helpful if you could make a small example code which is fully runnable and throws the same errors as the problem you describe.  
Also it would be very helpful if you could format your code in the markdown here. You can do that using this command ``` as a pre and post fix to your code block. for example

```julia
julia> a = 2
julia> f(x) = x * 3
julia> f(a)
6

```

Thank you!  
Karl

---

<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 6, 2024, 10:21am UTC](https://itensor.discourse.group/t/partial-transpose-and-log-negativity/1779/3 "2024-06-06T10:21:16Z")

</div>

Hi Mr Karl. Thank you very much for your suggestion. 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).
