# Implementation of DMRG using Julia and Periodic Boundary Conditions

**URL:** <https://itensor.discourse.group/t/implementation-of-dmrg-using-julia-and-periodic-boundary-conditions/1747>\
**Category:** ITensor Julia Questions\
**Tags:** dmrg\
**Created:** [May 22, 2024, 2:35pm UTC](https://itensor.discourse.group/t/implementation-of-dmrg-using-julia-and-periodic-boundary-conditions/1747 "2024-05-22T14:35:21Z")\
**Posts on this page:** 1\
**Showing post:** 6

<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:** [May 24, 2024, 10:10pm UTC](https://itensor.discourse.group/t/implementation-of-dmrg-using-julia-and-periodic-boundary-conditions/1747/6 "2024-05-24T22:10:18Z")

</div>

Let’s say you want to measure the reduced density matrix of the sites c and c+1 of an MPS. Then you can do that with the following code:

```julia
  orthogonalize!(psi,c)

  ket = psi[c]*psi[c+1]
  bra = dag(ket)
  rho = prime(ket,"Site")*bra

```

I would strongly suggest you write a tensor diagram and think about the “orthogonality conditions” of an MPS to understand what the code above is doing. Also I give a longer answer in [this post](https://itensor.discourse.group/t/retrieve-bipartite-reduced-density-matrix/940/6) about how to get a reduced density matrix for the entire left half of a finite system that could be helpful background for you.

To do more sites, you can generalize the above code to make `ket` include more MPS tensors, such as `ket = psi[c]*psi[c+1]*psi[c+2]` for the three-site case. Note that this calculation scales exponentially in the number of sites, but should work well for a small number of sites.

---

_[View the full topic](https://itensor.discourse.group/t/implementation-of-dmrg-using-julia-and-periodic-boundary-conditions/1747)._
