# Obtain elements of a tensor represented by an MPS when the quantum number is conserved

**URL:** <https://itensor.discourse.group/t/obtain-elements-of-a-tensor-represented-by-an-mps-when-the-quantum-number-is-conserved/71>\
**Category:** ITensor Julia Questions\
**Tags:** julia, mps\
**Created:** [April 23, 2022, 3:22am UTC](https://itensor.discourse.group/t/obtain-elements-of-a-tensor-represented-by-an-mps-when-the-quantum-number-is-conserved/71 "2022-04-23T03:22:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xenon-zhao](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/xenon-zhao/32/27_2.png) [@Xenon-zhao](https://itensor.discourse.group/u/Xenon-zhao)\
**Post date:** [April 23, 2022, 3:22am UTC](https://itensor.discourse.group/t/obtain-elements-of-a-tensor-represented-by-an-mps-when-the-quantum-number-is-conserved/71/1 "2022-04-23T03:22:06Z")

</div>

Hi, I want to obtain elements of a tensor represented by an MPS when the quantum number is conserved.

```julia
using ITensors

sites3=siteinds("S=1/2",3; conserve_qns=true)
state3 = ["Dn" for i in 1:3]
state3[2] = "Up"
state3
psi4 = productMPS(sites3,state3)

for i in 1:2
    for j in 1:2
        for k in 1:2
            el3 = [i,j,k]
            V3 = ITensor(1.)
            for l=1:3
            V3 *= (psi4[l]*state(sites3[l],el3[l]))
            end
            v3 = scalar(V3)
            print(i,j,k)
            # v is the element we wanted to obtain:
            @show v3
        end
    end
end

```

It works well without `conserve_qns=true` in `siteinds` function.

 ![Itensor Bug](https://global.discourse-cdn.com/free1/uploads/itensor/original/1X/c05ba7c3cad9d1b59f571af4aeb46c8f4ee918af.png)

---

<div class="post-metadata">

**Author:** ![Xenon-zhao](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/xenon-zhao/32/27_2.png) [@Xenon-zhao](https://itensor.discourse.group/u/Xenon-zhao)\
**Post date:** [April 23, 2022, 6:18am UTC](https://itensor.discourse.group/t/obtain-elements-of-a-tensor-represented-by-an-mps-when-the-quantum-number-is-conserved/71/2 "2022-04-23T06:18:55Z")

</div>

This problem have been solved.

Reason of problem:  
A pair of QN indices must have opposite arrow directions to be contracted. A physicist might view an Out arrow as denoting a “ket” index and an In arrow as a “bra” index[1].

> [1] Matthew Fishman et al. The ITensor Software Library for Tensor Network Calculations. SciPost Physics Codebases. 2021

`psi4[l]` and `state(sites3[l],el3[l])` have the same arrow directions.  
Solving of problem:  
The function `dag()` is defined to reverse all of the arrows of tensor’s indices. So the `psi4[l]*state(sites3[l],el3[l])` need to be changed to `psi4[l]*dag(state(sites3[l],el3[l]))`.  
Then it works well.

---

<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:** [April 23, 2022, 2:59pm UTC](https://itensor.discourse.group/t/obtain-elements-of-a-tensor-represented-by-an-mps-when-the-quantum-number-is-conserved/71/3 "2022-04-23T14:59:57Z")

</div>

Glad you caught it! Thanks for posting your question.
