# Issues using linsolve (cont'd discussion)

**URL:** https://itensor.discourse.group/t/issues-using-linsolve-contd-discussion/410
**Category:** DMRG and Numerical Methods
**Created:** [September 21, 2022, 6:01pm UTC](https://itensor.discourse.group/t/issues-using-linsolve-contd-discussion/410 "2022-09-21T18:01:26Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ray-l](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/ray-l/32/76_2.png) [@ray-l](https://itensor.discourse.group/u/ray-l)
#### Post date: [September 21, 2022, 6:01pm UTC](https://itensor.discourse.group/t/issues-using-linsolve-contd-discussion/410/1 "2022-09-21T18:01:26Z")

</div>

Continuing the discussion from [Constant shift in MPS shift-and-invert algo](https://itensor.discourse.group/t/constant-shift-in-mps-shift-and-invert-algo/174/6):

I finally have time to come back to this problem. In short, I’m trying to variationally solve the following linear system:  
(H - \lambda) |\psi\rangle = |\phi\rangle

Or that

```auto
o--o--o- -o--o--o--o--o--o <psi|
| | | | | | | | | | |
o--o--o--o--o--o--o--o--o--o--o H
| | | | | | | | | | |                                
o--o--o--o--o--o--o--o--o--o--o H
| | | | | | | | | | |
o--o--o- -o--o--o--o--o--o |psi>

=
o--o--o- -o--o--o--o--o--o <psi|
| | | | | | | | | | |
o--o--o--o--o--o--o--o--o--o--o H
| | | | | | | | | | |
o--o--o--o--o--o--o--o--o--o--o |phi>

```

(For now let’s assume \lambda=0, since I believe you can always add back the cross terms ( \lambda \langle \psi | H | \psi\rangle , \lambda^2\langle \psi|\psi\rangle, \lambda\langle \psi|\phi\rangle ) which doesn’t change the problem)  
(Or there’s a way to add constant terms?)

The way I approach this, is that I defined a new position! function:

```julia
function position!(P::AbstractProjMPO, psi::MPS, phi::MPS, pos::Int)
  L = makeL!(P, psi, phi, pos - 1)
  R = makeR!(P, psi, phi, pos + nsite(P))

  # start of the left end
  if L == nothing
    return phi[pos] * phi[pos + 1] * R

  # start of the right end
  elseif R == nothing
    return L * phi[pos] * phi[pos + 1]
  
  # in the middle
  else
    return L * phi[pos] * phi[pos + 1] * R

  end 
end

```

with appropriate modification to makeL! and makeR!

```julia
function _makeR!(P::AbstractProjMPO, psi::MPS, phi::MPS, k::Int)
"""everything were kept the same except"""
R = R * phi[rl - 1] * P.H[rl - 1] * dag(prime(psi[rl - 1]))
"""instead of R = R * psi[rl - 1] * P.H[rl - 1] * dag(prime(psi[rl - 1]))"""
end 

```

Eventually I defined a hamiltonian ‘squared’:

```julia
  H2 = contract(H', H; cutoff=1e-12)
  H2 = replaceprime(H2, 2 => 1)

```

And I calculate the LHS and RHS and a guess

```julia
RHS = position!(PH, psi, phi, b)
position!(PH2, psi, b)
guess = psi[b] * psi[b + 1]

```

Put it through linsolve

```julia
          vecs, conv = linsolve(
            PH2,
            RHS,
            guess, 0, 1;
            ishermitian=ishermitian,
            tol=linsolve_tol,
            krylovdim=linsolve_krylovdim,
            maxiter=linsolve_maxiter,
          )

```

This error initially occurred  
`LoadError: DimensionMismatch("In scalar(T) or T[], ITensor T is not a scalar`

After I removed ‘guess’ from the linsolve arguments, another error

````julia
LoadError: The order of the ProjMPO-ITensor product P*v is not equal to the order of the ITensor v, this is probably due to an index mismatch.
Common reasons for this error:
(1) You are trying to multiply the ProjMPO with the 2-site wave-function at the wrong position.
(2) `orthogonalize!` was called, changing the MPS without updating the ProjMPO.```
````

---

<div class="post-metadata">

### Author: ![ray-l](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/ray-l/32/76_2.png) [@ray-l](https://itensor.discourse.group/u/ray-l)
#### Post date: [September 26, 2022, 12:03am UTC](https://itensor.discourse.group/t/issues-using-linsolve-contd-discussion/410/2 "2022-09-26T00:03:23Z")

</div>

Essentially, the LHS (The PH2 here) uses all the unmodified methods of ProjMPS, it’s just that I used a different Hamiltonian, which I believe is done correctly. I can run the LHS Hamiltonian through eigsolve as well.

I guess there’s something wrong in the way I modified the RHS methods, from contraction between the same state \langle \psi | H|\psi\rangle to \langle \psi | H |\phi\rangle, but it’s not immediately clear to me what was the issue.
