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.
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}
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 or 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.
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.
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
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.