# Save MPS - error with GPU

**URL:** <https://itensor.discourse.group/t/save-mps-error-with-gpu/1801>\
**Category:** ITensor Julia Questions\
**Created:** [June 12, 2024, 10:24pm UTC](https://itensor.discourse.group/t/save-mps-error-with-gpu/1801 "2024-06-12T22:24:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joacop16](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/joacop16/32/560_2.png) [@Joacop16](https://itensor.discourse.group/u/Joacop16)\
**Post date:** [June 12, 2024, 10:24pm UTC](https://itensor.discourse.group/t/save-mps-error-with-gpu/1801/1 "2024-06-12T22:24:24Z")

</div>

Hi, ITensor Teams！

I am trying to use GPU to improve a code. During the code in some parts, I need to save an MPS to backup the information.

I found that there is a problem in general when I want to save the MPS if it was loaded to the GPU previously.

```julia
using ITensors
using CUDA
using HDF5

sites = siteinds("S=1/2",50)

psi = cu(randomMPS(sites))

f = h5open("myfile.h5","w")
write(f,"psi",psi)
close(f)

#Here I got: ArgumentError: Illegal conversion of a CUDA.DeviceMemory to a Ptr{Float32}

```

---

<div class="post-metadata">

**Author:** ![mtfishman](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/mtfishman/32/11_2.png) [@mtfishman](https://itensor.discourse.group/u/mtfishman)\
**Post date:** [June 12, 2024, 10:39pm UTC](https://itensor.discourse.group/t/save-mps-error-with-gpu/1801/2 "2024-06-12T22:39:06Z")

</div>

Hmm yeah we didn’t consider that, for now can you transfer to CPU, save to disk, and then when you load transfer back to GPU?

Alternatively you could try [JLD2.jl](https://github.com/JuliaIO/JLD2.jl) or [Serialization](https://docs.julialang.org/en/v1/stdlib/Serialization) for saving and loading to disk if you just need something more temporary. Those options aren’t great for long term storage since it may be difficult to read ITensor objects if we change the internal data structures in future versions of ITensor and you saved them using previous versions.

---

<div class="post-metadata">

**Author:** ![Joacop16](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/joacop16/32/560_2.png) [@Joacop16](https://itensor.discourse.group/u/Joacop16)\
**Post date:** [July 8, 2024, 9:29pm UTC](https://itensor.discourse.group/t/save-mps-error-with-gpu/1801/3 "2024-07-08T21:29:17Z")

</div>

Hi, I will review how to use JLD2.jl and Serialization because I just need to save it temporary (is for a backup just in case that the server where I am running the code is stopped), thank you for the advice.

---

<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:** [July 9, 2024, 7:53am UTC](https://itensor.discourse.group/t/save-mps-error-with-gpu/1801/4 "2024-07-09T07:53:43Z")

</div>

Hello Joaquin,

Can you please make another post if you are having issues with these packages? The JLD2.jl package is relatively straightforward to use here is an example of writing/reading an MPS from a DMRG calculations

```julia
using ITensors, ITensorMPS
using JLD2

N = 5
conserve_qns = false
nsweeps = 1
maxdim = [100]
cutoff = 1e-3
noise = 0.0

sites = siteinds("S=1", N; conserve_qns=conserve_qns)

os = OpSum()
for j in 1:(N - 1)
  os .+= 0.5, "S+", j, "S-", j + 1
  os .+= 0.5, "S-", j, "S+", j + 1
  os .+= "Sz", j, "Sz", j + 1
end
H = MPO(os, sites)

state = [isodd(n) ? "Up" : "Dn" for n in 1:N]
psi0 = randomMPS(sites, state, maxdim[1])

  # Run the DMRG algorithm, returning energy and optimized MPS'
energy, psi = dmrg(H, psi0; nsweeps, maxdim, cutoff)
filename = "save_state_example"
jldopen("$(filename).jld", "w") do fid
  fid["energy"] = energy
  fid["psi"] = psi
end

fetch_file = jldopen("$(filename).jld")
psi = fetch_file["psi"]
typeof(psi)
## MPS 

```

Its also important to remember that it is not possible to save tensors which are stored on GPU which is a limitation of the JLD2 library. If you would like to store such a tensor, please move the tensor from GPU to CPU.

Best,  
Karl

---

<div class="post-metadata">

**Author:** ![ImVictorCheng](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/imvictorcheng/32/643_2.png) [@ImVictorCheng](https://itensor.discourse.group/u/ImVictorCheng)\
**Post date:** [October 24, 2024, 2:40am UTC](https://itensor.discourse.group/t/save-mps-error-with-gpu/1801/5 "2024-10-24T02:40:47Z")

</div>

Hi, what’s the easiest way to transfer data (MPS for my case) from GPU to CPU?

---

<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:** [October 24, 2024, 3:05am UTC](https://itensor.discourse.group/t/save-mps-error-with-gpu/1801/6 "2024-10-24T03:05:15Z")

</div>

Could you please post this as a new Topic? Thanks.

---

<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:** [October 24, 2024, 3:05am UTC](https://itensor.discourse.group/t/save-mps-error-with-gpu/1801/7 "2024-10-24T03:05:35Z")

</div>


