From 235974c5664e39af9bf5b0bbcd40cb2f82c17776 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 16 May 2024 15:14:08 -0400 Subject: [PATCH 1/9] [ITensorMPS] Change randomMPS(siteinds, ..., linkdims) to randomMPS(siteinds, ...; linkdims) --- docs/src/Observer.md | 2 +- docs/src/examples/DMRG.md | 40 ++--- docs/src/examples/MPSandMPO.md | 2 +- docs/src/tutorials/DMRG.md | 8 +- docs/src/tutorials/QN_DMRG.md | 2 +- .../dmrg/1d_heisenberg_conserve_spin.jl | 8 +- .../examples/dmrg/1d_hubbard_extended.jl | 2 +- .../dmrg/2d_heisenberg_conserve_spin.jl | 8 +- .../ArgParse/1d_hubbard_extended.jl | 155 ------------------ .../dmrg/input_files/ArgParse/input.jl | 56 ------- .../argsdict/1d_hubbard_extended.jl | 155 ------------------ .../dmrg/input_files/argsdict/input.jl | 16 -- .../input_files/script/1d_hubbard_extended.jl | 98 ----------- .../examples/dmrg/input_files/script/input.jl | 16 -- .../dmrg/write_to_disk/1d_heisenberg.jl | 8 +- src/lib/ITensorMPS/examples/src/hubbard.jl | 40 ++--- src/lib/ITensorMPS/src/abstractmps.jl | 10 +- 17 files changed, 65 insertions(+), 561 deletions(-) delete mode 100644 src/lib/ITensorMPS/examples/dmrg/input_files/ArgParse/1d_hubbard_extended.jl delete mode 100644 src/lib/ITensorMPS/examples/dmrg/input_files/ArgParse/input.jl delete mode 100644 src/lib/ITensorMPS/examples/dmrg/input_files/argsdict/1d_hubbard_extended.jl delete mode 100644 src/lib/ITensorMPS/examples/dmrg/input_files/argsdict/input.jl delete mode 100644 src/lib/ITensorMPS/examples/dmrg/input_files/script/1d_hubbard_extended.jl delete mode 100644 src/lib/ITensorMPS/examples/dmrg/input_files/script/input.jl diff --git a/docs/src/Observer.md b/docs/src/Observer.md index 57bce78631..f2f8e88b73 100644 --- a/docs/src/Observer.md +++ b/docs/src/Observer.md @@ -184,7 +184,7 @@ let a += 0.5,"S-",n,"S+",n+1 end H = MPO(a,s) - psi0 = randomMPS(s,4) + psi0 = randomMPS(s;linkdims=4) nsweeps = 5 cutoff = 1E-8 diff --git a/docs/src/examples/DMRG.md b/docs/src/examples/DMRG.md index 44a6227705..45f9f425cd 100644 --- a/docs/src/examples/DMRG.md +++ b/docs/src/examples/DMRG.md @@ -47,7 +47,7 @@ The random starting wavefunction `psi0` must be defined in the same Hilbert spac as the Hamiltonian, so we construct it using the same collection of site indices: ```julia -psi0 = randomMPS(sites,2) +psi0 = randomMPS(sites;linkdims=2) ``` Here we have made a random MPS of bond dimension 2. We could have used a random product @@ -58,7 +58,7 @@ stuck in local minima. We could also set psi to some specific initial state usin Finally, we are ready to call DMRG: ```julia -energy,psi = dmrg(H,psi0; nsweeps, maxdim, cutoff) +energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff) ``` When the algorithm is done, it returns the ground state energy as the variable `energy` and an MPS @@ -85,9 +85,9 @@ let maxdim = [10,20,100,100,200] # gradually increase states kept cutoff = [1E-10] # desired truncation error - psi0 = randomMPS(sites,2) + psi0 = randomMPS(sites;linkdims=2) - energy,psi = dmrg(H,psi0; nsweeps, maxdim, cutoff) + energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff) return end @@ -158,9 +158,9 @@ let maxdim = [10,10,20,40,80,100,140,180,200] cutoff = [1E-8] - psi0 = randomMPS(sites,4) + psi0 = randomMPS(sites;linkdims=4) - energy,psi = dmrg(H,psi0; nsweeps, maxdim, cutoff) + energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff) return end @@ -178,7 +178,7 @@ more efficient than if the MPOs had been summed together into a single MPO. To use this version of DMRG, say you have MPOs `H1`, `H2`, and `H3`. Then call DMRG like this: ```julia -energy,psi = dmrg([H1,H2,H3],psi0; nsweeps, maxdim, cutoff) +energy,psi = dmrg([H1,H2,H3],psi0;nsweeps,maxdim,cutoff) ``` ## Make a 2D Hamiltonian for DMRG @@ -238,13 +238,13 @@ let # Initialize wavefunction to a random MPS # of bond-dimension 10 with same quantum # numbers as `state` - psi0 = randomMPS(sites,state,20) + psi0 = randomMPS(sites,state;linkdims=20) nsweeps = 10 maxdim = [20,60,100,100,200,400,800] cutoff = [1E-8] - energy,psi = dmrg(H,psi0; nsweeps, maxdim, cutoff) + energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff) return end @@ -257,7 +257,7 @@ These additional 'penalty states' are provided as an array of MPS just after the Hamiltonian, like this: ```julia -energy,psi3 = dmrg(H,[psi0,psi1,psi2],psi3_init; nsweeps, maxdim, cutoff) +energy,psi3 = dmrg(H,[psi0,psi1,psi2],psi3_init;nsweeps,maxdim,cutoff) ``` Here the penalty states are `[psi0,psi1,psi2]`. @@ -328,16 +328,16 @@ let # # Compute the ground state psi0 # - psi0_init = randomMPS(sites,linkdims=2) - energy0,psi0 = dmrg(H,psi0_init; nsweeps, maxdim, cutoff, noise) + psi0_init = randomMPS(sites;linkdims=2) + energy0,psi0 = dmrg(H,psi0_init;nsweeps,maxdim,cutoff,noise) println() # # Compute the first excited state psi1 # - psi1_init = randomMPS(sites,linkdims=2) - energy1,psi1 = dmrg(H,[psi0],psi1_init; nsweeps, maxdim, cutoff, noise, weight) + psi1_init = randomMPS(sites;linkdims=2) + energy1,psi1 = dmrg(H,[psi0],psi1_init;nsweeps,maxdim,cutoff,noise,weight) # Check psi1 is orthogonal to psi0 @show inner(psi1,psi0) @@ -357,8 +357,8 @@ let # # Compute the second excited state psi2 # - psi2_init = randomMPS(sites,linkdims=2) - energy2,psi2 = dmrg(H,[psi0,psi1],psi2_init; nsweeps, maxdim, cutoff, noise, weight) + psi2_init = randomMPS(sites;linkdims=2) + energy2,psi2 = dmrg(H,[psi0,psi1],psi2_init;nsweeps,maxdim,cutoff,noise,weight) # Check psi2 is orthogonal to psi0 and psi1 @show inner(psi2,psi0) @@ -429,7 +429,7 @@ let a += 0.5,"S-",n,"S+",n+1 end H = MPO(a,s) - psi0 = randomMPS(s,linkdims=4) + psi0 = randomMPS(s;linkdims=4) nsweeps = 5 maxdim = [10,20,80,160] @@ -437,7 +437,7 @@ let observer = EntanglementObserver() - energy, psi = dmrg(H,psi0; nsweeps, maxdim, cutoff, observer, outputlevel=2) + energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff,observer,outputlevel=2) return end @@ -523,7 +523,7 @@ let a += 0.5,"S-",n,"S+",n+1 end H = MPO(a,s) - psi0 = randomMPS(s,linkdims=4) + psi0 = randomMPS(s;linkdims=4) nsweeps = 5 maxdim = [10,20,80,160] @@ -531,7 +531,7 @@ let obs = SizeObserver() - energy, psi = dmrg(H,psi0; nsweeps, maxdim, cutoff, observer=obs) + energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff,observer=obs) return end diff --git a/docs/src/examples/MPSandMPO.md b/docs/src/examples/MPSandMPO.md index d886a97bdf..509e723e5b 100644 --- a/docs/src/examples/MPSandMPO.md +++ b/docs/src/examples/MPSandMPO.md @@ -143,7 +143,7 @@ using ITensors, ITensorMPS N = 10 chi = 4 sites = siteinds("S=1/2",N) -psi = randomMPS(sites,chi) +psi = randomMPS(sites;linkdims=chi) magz = expect(psi,"Sz") for (j,mz) in enumerate(magz) println("$j $mz") diff --git a/docs/src/tutorials/DMRG.md b/docs/src/tutorials/DMRG.md index 428274ff1f..99b18f05c7 100644 --- a/docs/src/tutorials/DMRG.md +++ b/docs/src/tutorials/DMRG.md @@ -38,13 +38,13 @@ let end H = MPO(os,sites) - psi0 = randomMPS(sites,10) + psi0 = randomMPS(sites;linkdims=10) nsweeps = 5 maxdim = [10,20,100,100,200] cutoff = [1E-10] - energy, psi = dmrg(H,psi0; nsweeps, maxdim, cutoff) + energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff) return end @@ -92,7 +92,7 @@ physical indices given by the array `sites`. The line ```julia -psi0 = randomMPS(sites,10) +psi0 = randomMPS(sites;linkdims=10) ``` constructs an MPS `psi0` which has the physical indices `sites` and a bond dimension of 10. @@ -116,7 +116,7 @@ specified than sweeps, the last value is used for all remaining sweeps). Finally the call ```julia -energy, psi = dmrg(H,psi0; nsweeps, maxdim, cutoff) +energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff) ``` runs the DMRG algorithm included in ITensor, using `psi0` as an diff --git a/docs/src/tutorials/QN_DMRG.md b/docs/src/tutorials/QN_DMRG.md index 390e3a04ba..56bd33ddce 100644 --- a/docs/src/tutorials/QN_DMRG.md +++ b/docs/src/tutorials/QN_DMRG.md @@ -81,7 +81,7 @@ To make change (2), instead of constructing the initial MPS `psi0` to be an arbi So we will replace the line ```julia -psi0 = randomMPS(sites,10) +psi0 = randomMPS(sites;linkdims=10) ``` by the lines diff --git a/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg_conserve_spin.jl b/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg_conserve_spin.jl index e9b44b2be4..fb4de478b4 100644 --- a/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg_conserve_spin.jl +++ b/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg_conserve_spin.jl @@ -17,14 +17,14 @@ let 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 + 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, 10) + psi0 = randomMPS(sites, state; linkdims=10) # Plan to do 5 DMRG sweeps: nsweeps = 5 diff --git a/src/lib/ITensorMPS/examples/dmrg/1d_hubbard_extended.jl b/src/lib/ITensorMPS/examples/dmrg/1d_hubbard_extended.jl index 9c2deaa730..571a81fc52 100644 --- a/src/lib/ITensorMPS/examples/dmrg/1d_hubbard_extended.jl +++ b/src/lib/ITensorMPS/examples/dmrg/1d_hubbard_extended.jl @@ -54,7 +54,7 @@ let # Initialize wavefunction to be bond # dimension 10 random MPS with number # of particles the same as `state` - psi0 = randomMPS(sites, state, 10) + psi0 = randomMPS(sites, state; linkdims=10) # Check total number of particles: @show flux(psi0) diff --git a/src/lib/ITensorMPS/examples/dmrg/2d_heisenberg_conserve_spin.jl b/src/lib/ITensorMPS/examples/dmrg/2d_heisenberg_conserve_spin.jl index 6278658ffe..259d21c7b0 100644 --- a/src/lib/ITensorMPS/examples/dmrg/2d_heisenberg_conserve_spin.jl +++ b/src/lib/ITensorMPS/examples/dmrg/2d_heisenberg_conserve_spin.jl @@ -12,9 +12,9 @@ let os = OpSum() for b in lattice - os .+= 0.5, "S+", b.s1, "S-", b.s2 - os .+= 0.5, "S-", b.s1, "S+", b.s2 - os .+= "Sz", b.s1, "Sz", b.s2 + os += 0.5, "S+", b.s1, "S-", b.s2 + os += 0.5, "S-", b.s1, "S+", b.s2 + os += "Sz", b.s1, "Sz", b.s2 end H = MPO(os, sites) @@ -22,7 +22,7 @@ let # Initialize wavefunction to a random MPS # of bond-dimension 10 with same quantum # numbers as `state` - psi0 = randomMPS(sites, state, 20) + psi0 = randomMPS(sites, state; linkdims=20) nsweeps = 10 maxdim = [20, 60, 100, 100, 200, 400, 800] diff --git a/src/lib/ITensorMPS/examples/dmrg/input_files/ArgParse/1d_hubbard_extended.jl b/src/lib/ITensorMPS/examples/dmrg/input_files/ArgParse/1d_hubbard_extended.jl deleted file mode 100644 index ebe26b9180..0000000000 --- a/src/lib/ITensorMPS/examples/dmrg/input_files/ArgParse/1d_hubbard_extended.jl +++ /dev/null @@ -1,155 +0,0 @@ -using ITensors, ITensorMPS - -# -# DMRG calculation of the extended Hubbard model -# ground state wavefunction, and spin densities -# - -# Usage: -# -# First install `ArgParse.jl`: -# -# julia>] add ArgParse -# -# Then run: -# -# $ julia exthubbard.jl input.jl -# -# or with options, like -# -# $ julia exthubbard.jl input.jl --N=100 --Npart=20 --nsweep=4 --cutoff=1e-11 --maxdim=20 40 80 100 --noise=1e-12 1e-13 -# -# In the REPL (if you just type `julia`), you can call it as follows: -# -# julia> push!(ARGS, "input.jl"); -# -# julia> push!(ARGS, "--N=100", "--Npart=20"); -# -# julia> include("exthubbard.jl"); -# - -# Include the specified input file -# from the first input. -# Searches for the default file input.jl -# if nothing is specified. -filename = get(ARGS, 1, "input.jl") -if isfile(filename) - include(filename) -else - include("input.jl") -end - -# -# Parse the arguments and store -# them in a dictionary. -# Settings are defined in the input file. -# - -args = parse_args(settings) - -# -# Get the values from the dictionary -# - -N = args["N"] -Npart = args["Npart"] -t1 = args["t1"] -t2 = args["t2"] -U = args["U"] -V1 = args["V1"] -nsweep = args["nsweep"] -maxdim = args["maxdim"] -mindim = args["mindim"] -cutoff = args["cutoff"] -noise = args["noise"] - -# -# Alternatively, define all of the -# variables for all of the inputs -# programatically. You can use the -# keyword argument `as_symbols = true` -# in the `parse_args` function to -# automatically turn the keys of -# the dictionary into symbols. -# - -#for (arg, val) in args -# arg == "input_file" && continue -# @eval $(Symbol(arg)) = $val -#end - -@show N, Npart - -sites = siteinds("Electron", N; conserve_qns=true) - -os = OpSum() -for i in 1:N - os .+= U, "Nupdn", i -end -for b in 1:(N - 1) - os .+= -t1, "Cdagup", b, "Cup", b + 1 - os .+= -t1, "Cdagup", b + 1, "Cup", b - os .+= -t1, "Cdagdn", b, "Cdn", b + 1 - os .+= -t1, "Cdagdn", b + 1, "Cdn", b - os .+= V1, "Ntot", b, "Ntot", b + 1 -end -for b in 1:(N - 2) - os .+= -t2, "Cdagup", b, "Cup", b + 2 - os .+= -t2, "Cdagup", b + 2, "Cup", b - os .+= -t2, "Cdagdn", b, "Cdn", b + 2 - os .+= -t2, "Cdagdn", b + 2, "Cdn", b -end -H = MPO(os, sites) - -state = ["Emp" for n in 1:N] -p = Ref(Npart) -for i in N:-1:1 - if p[] > i - println("Doubly occupying site $i") - state[i] = "UpDn" - p[] -= 2 - elseif p[] > 0 - println("Singly occupying site $i") - state[i] = (isodd(i) ? "Up" : "Dn") - p[] -= 1 - end -end -# Initialize wavefunction to be bond -# dimension 10 random MPS with number -# of particles the same as `state` -psi0 = randomMPS(sites, state, 10) - -# Check total number of particles: -@show flux(psi0) - -# Start DMRG calculation: -energy, psi = dmrg(H, psi0; nsweeps=nsweep, maxdim, mindim, cutoff, noise) - -upd = fill(0.0, N) -dnd = fill(0.0, N) -for j in 1:N - orthogonalize!(psi, j) - psidag_j = dag(prime(psi[j], "Site")) - upd[j] = scalar(psidag_j * op(sites, "Nup", j) * psi[j]) - dnd[j] = scalar(psidag_j * op(sites, "Ndn", j) * psi[j]) -end - -println("Up Density:") -for j in 1:N - println("$j $(upd[j])") -end -println() - -println("Dn Density:") -for j in 1:N - println("$j $(dnd[j])") -end -println() - -println("Total Density:") -for j in 1:N - println("$j $(upd[j]+dnd[j])") -end -println() - -println("\nGround State Energy = $energy") diff --git a/src/lib/ITensorMPS/examples/dmrg/input_files/ArgParse/input.jl b/src/lib/ITensorMPS/examples/dmrg/input_files/ArgParse/input.jl deleted file mode 100644 index 10dafaaefe..0000000000 --- a/src/lib/ITensorMPS/examples/dmrg/input_files/ArgParse/input.jl +++ /dev/null @@ -1,56 +0,0 @@ -using ArgParse - -settings = ArgParseSettings() -@add_arg_table! settings begin - "--N", "-N" - help = "Number of sites" - arg_type = Int - default = 20 - "--Npart" - help = "Number of particles" - arg_type = Int - default = 10 - "--t1" - help = "Nearest neighbor hopping" - arg_type = Float64 - default = 1.0 - "--t2" - help = "Next-nearest neighbor hopping" - arg_type = Float64 - default = 0.2 - "--U", "-U" - help = "On-site potential" - arg_type = Float64 - default = 1.0 - "--V1", "-V" - help = "Nearest neighbor potential" - arg_type = Float64 - default = 0.5 - "--nsweep", "-n" - help = "Number of sweeps of DMRG" - arg_type = Int64 - default = 6 - "--maxdim" - help = "Maximum bond dimension in DMRG" - nargs = '+' - arg_type = Int64 - default = [50, 100, 200, 400, 800] - "--mindim" - help = "Minimum bond dimension in DMRG" - nargs = '+' - arg_type = Int64 - default = [10, 20] - "--cutoff", "-c" - help = "Truncation cutoff in DMRG" - nargs = '+' - arg_type = Float64 - default = [1e-12] - "--noise" - help = "Magnitude of noise to add in DMRG" - nargs = '+' - arg_type = Float64 - default = [1e-7, 1e-8, 1e-10, 0, 1e-11, 0] - "input_file" - help = "Input file" - arg_type = String -end diff --git a/src/lib/ITensorMPS/examples/dmrg/input_files/argsdict/1d_hubbard_extended.jl b/src/lib/ITensorMPS/examples/dmrg/input_files/argsdict/1d_hubbard_extended.jl deleted file mode 100644 index 26681cf346..0000000000 --- a/src/lib/ITensorMPS/examples/dmrg/input_files/argsdict/1d_hubbard_extended.jl +++ /dev/null @@ -1,155 +0,0 @@ -using ITensors, ITensorMPS - -# -# DMRG calculation of the extended Hubbard model -# ground state wavefunction, and spin densities -# - -# Usage: -# -# Run: -# -# $ julia exthubbard.jl input.jl -# -# or with options, like -# -# $ julia exthubbard.jl input.jl N=100 Npart=20 nsweep=4 cutoff=1e-11 maxdim=20,40,80,100 noise=1e-12,1e-13 -# -# In the REPL (if you just type `julia`), you can modify the command line options as follows (or just modify "input.jl" and reinclude it): -# -# julia> push!(ARGS, "input.jl"); -# -# julia> push!(ARGS, "N=100", "Npart=20"); -# -# julia> include("exthubbard.jl"); -# - -# Parse arguments to overide defaults -args = argsdict(; prefix="file") - -# Include the file from the first positional -# argument. If none is specified, use -# input.jl by default. -include(get(args, "file1", "input.jl")) - -# Create the sweeps object from the input -# file -sweeps = Sweeps(nsw, sweeps_args) -# Extract the original (default) values -maxdim = get_maxdims(sweeps) -mindim = get_mindims(sweeps) -cutoff = get_cutoffs(sweeps) -noise = get_noises(sweeps) - -# Extract the argument values from -# the dictionary - -N = get(args, "N", N) -Npart = get(args, "Npart", Npart) -t1 = get(args, "t1", t1) -t2 = get(args, "t2", t2) -U = get(args, "U", U) -V1 = get(args, "V1", V1) -nsw = get(args, "nsweep", nsw) -maxdim = get(args, "maxdim", maxdim) -mindim = get(args, "mindim", mindim) -cutoff = get(args, "cutoff", cutoff) -noise = get(args, "noise", noise) - -# -# Alternatively, define all of the -# variables for all of the inputs -# programatically. You can use the -# keyword argument `as_symbols = true` -# in the `parse_args` function to -# automatically turn the keys of -# the dictionary into symbols. -# - -#for (arg, val) in args -# arg == "input_file" && continue -# @eval $(Symbol(arg)) = $val -#end - -sweeps = Sweeps(nsw) -maxdim!(sweeps, maxdim...) -mindim!(sweeps, mindim...) -cutoff!(sweeps, cutoff...) -noise!(sweeps, noise...) - -@show sweeps -@show N, Npart - -sites = siteinds("Electron", N; conserve_qns=true) - -os = OpSum() -for i in 1:N - os .+= U, "Nupdn", i -end -for b in 1:(N - 1) - os .+= -t1, "Cdagup", b, "Cup", b + 1 - os .+= -t1, "Cdagup", b + 1, "Cup", b - os .+= -t1, "Cdagdn", b, "Cdn", b + 1 - os .+= -t1, "Cdagdn", b + 1, "Cdn", b - os .+= V1, "Ntot", b, "Ntot", b + 1 -end -for b in 1:(N - 2) - os .+= -t2, "Cdagup", b, "Cup", b + 2 - os .+= -t2, "Cdagup", b + 2, "Cup", b - os .+= -t2, "Cdagdn", b, "Cdn", b + 2 - os .+= -t2, "Cdagdn", b + 2, "Cdn", b -end -H = MPO(os, sites) - -state = ["Emp" for n in 1:N] -p = Ref(Npart) -for i in N:-1:1 - if p[] > i - println("Doubly occupying site $i") - state[i] = "UpDn" - p[] -= 2 - elseif p[] > 0 - println("Singly occupying site $i") - state[i] = (isodd(i) ? "Up" : "Dn") - p[] -= 1 - end -end -# Initialize wavefunction to be bond -# dimension 10 random MPS with number -# of particles the same as `state` -psi0 = randomMPS(sites, state, 10) - -# Check total number of particles: -@show flux(psi0) - -# Start DMRG calculation: -energy, psi = dmrg(H, psi0, sweeps) - -upd = fill(0.0, N) -dnd = fill(0.0, N) -for j in 1:N - psi = orthogonalize(psi, j) - psidag_j = dag(prime(psi[j], "Site")) - upd[j] = scalar(psidag_j * op(sites, "Nup", j) * psi[j]) - dnd[j] = scalar(psidag_j * op(sites, "Ndn", j) * psi[j]) -end - -println("Up Density:") -for j in 1:N - println("$j $(upd[j])") -end -println() - -println("Dn Density:") -for j in 1:N - println("$j $(dnd[j])") -end -println() - -println("Total Density:") -for j in 1:N - println("$j $(upd[j]+dnd[j])") -end -println() - -println("\nGround State Energy = $energy") diff --git a/src/lib/ITensorMPS/examples/dmrg/input_files/argsdict/input.jl b/src/lib/ITensorMPS/examples/dmrg/input_files/argsdict/input.jl deleted file mode 100644 index 3036e4e0a8..0000000000 --- a/src/lib/ITensorMPS/examples/dmrg/input_files/argsdict/input.jl +++ /dev/null @@ -1,16 +0,0 @@ -N = 20 -Npart = 10 -t1 = 1.0 -t2 = 0.2 -U = 1.0 -V1 = 0.5 -nsw = 6 -sweeps_args = [ - "maxdim" "mindim" "cutoff" "noise" - 50 10 1e-12 1e-7 - 100 20 1e-12 1e-8 - 200 20 1e-12 1e-10 - 400 20 1e-12 0 - 800 20 1e-12 1e-11 - 800 20 1e-12 0 -] diff --git a/src/lib/ITensorMPS/examples/dmrg/input_files/script/1d_hubbard_extended.jl b/src/lib/ITensorMPS/examples/dmrg/input_files/script/1d_hubbard_extended.jl deleted file mode 100644 index 51ff4f99c5..0000000000 --- a/src/lib/ITensorMPS/examples/dmrg/input_files/script/1d_hubbard_extended.jl +++ /dev/null @@ -1,98 +0,0 @@ -using ITensors, ITensorMPS - -# -# DMRG calculation of the extended Hubbard model -# ground state wavefunction, and spin densities -# - -# Usage: -# -# Run: -# -# $ julia exthubbard.jl input.jl -# - -# Include the specified input file -# Include the file from the first positional -# argument. If none is specified, use -# input.jl by default. -include(get(ARGS, 1, "input.jl")) - -sweeps = Sweeps(nsweep, sweeps_args) - -@show sweeps -@show N, Npart - -sites = siteinds("Electron", N; conserve_qns=true) - -os = OpSum() -for i in 1:N - os .+= U, "Nupdn", i -end -for b in 1:(N - 1) - os .+= -t1, "Cdagup", b, "Cup", b + 1 - os .+= -t1, "Cdagup", b + 1, "Cup", b - os .+= -t1, "Cdagdn", b, "Cdn", b + 1 - os .+= -t1, "Cdagdn", b + 1, "Cdn", b - os .+= V1, "Ntot", b, "Ntot", b + 1 -end -for b in 1:(N - 2) - os .+= -t2, "Cdagup", b, "Cup", b + 2 - os .+= -t2, "Cdagup", b + 2, "Cup", b - os .+= -t2, "Cdagdn", b, "Cdn", b + 2 - os .+= -t2, "Cdagdn", b + 2, "Cdn", b -end -H = MPO(os, sites) - -state = ["Emp" for n in 1:N] -p = Ref(Npart) -for i in N:-1:1 - if p[] > i - println("Doubly occupying site $i") - state[i] = "UpDn" - p[] -= 2 - elseif p[] > 0 - println("Singly occupying site $i") - state[i] = (isodd(i) ? "Up" : "Dn") - p[] -= 1 - end -end -# Initialize wavefunction to be bond -# dimension 10 random MPS with number -# of particles the same as `state` -psi0 = randomMPS(sites, state, 10) - -# Check total number of particles: -@show flux(psi0) - -# Start DMRG calculation: -energy, psi = dmrg(H, psi0, sweeps) - -upd = fill(0.0, N) -dnd = fill(0.0, N) -for j in 1:N - orthogonalize!(psi, j) - psidag_j = dag(prime(psi[j], "Site")) - upd[j] = scalar(psidag_j * op(sites, "Nup", j) * psi[j]) - dnd[j] = scalar(psidag_j * op(sites, "Ndn", j) * psi[j]) -end - -println("Up Density:") -for j in 1:N - println("$j $(upd[j])") -end -println() - -println("Dn Density:") -for j in 1:N - println("$j $(dnd[j])") -end -println() - -println("Total Density:") -for j in 1:N - println("$j $(upd[j]+dnd[j])") -end -println() - -println("\nGround State Energy = $energy") diff --git a/src/lib/ITensorMPS/examples/dmrg/input_files/script/input.jl b/src/lib/ITensorMPS/examples/dmrg/input_files/script/input.jl deleted file mode 100644 index f0191a6e53..0000000000 --- a/src/lib/ITensorMPS/examples/dmrg/input_files/script/input.jl +++ /dev/null @@ -1,16 +0,0 @@ -N = 20 -Npart = 10 -t1 = 1.0 -t2 = 0.2 -U = 1.0 -V1 = 0.5 -nsweep = 6 -sweeps_args = [ - "maxdim" "mindim" "cutoff" "noise" - 50 10 1e-12 1e-7 - 100 20 1e-12 1e-8 - 200 20 1e-12 1e-10 - 400 20 1e-12 0 - 800 20 1e-12 1e-11 - 800 20 1e-12 0 -] diff --git a/src/lib/ITensorMPS/examples/dmrg/write_to_disk/1d_heisenberg.jl b/src/lib/ITensorMPS/examples/dmrg/write_to_disk/1d_heisenberg.jl index ff0373cc9d..8492a10771 100644 --- a/src/lib/ITensorMPS/examples/dmrg/write_to_disk/1d_heisenberg.jl +++ b/src/lib/ITensorMPS/examples/dmrg/write_to_disk/1d_heisenberg.jl @@ -14,14 +14,14 @@ let 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 + 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, 10) + psi0 = randomMPS(sites, state; linkdims=10) # Plan to do 5 DMRG sweeps: nsweeps = 5 diff --git a/src/lib/ITensorMPS/examples/src/hubbard.jl b/src/lib/ITensorMPS/examples/src/hubbard.jl index 00a3877420..30bf2ef71e 100644 --- a/src/lib/ITensorMPS/examples/src/hubbard.jl +++ b/src/lib/ITensorMPS/examples/src/hubbard.jl @@ -2,14 +2,14 @@ function hubbard_1d(; N::Int, t=1.0, U=0.0) opsum = OpSum() for b in 1:(N - 1) - opsum .-= t, "Cdagup", b, "Cup", b + 1 - opsum .-= t, "Cdagup", b + 1, "Cup", b - opsum .-= t, "Cdagdn", b, "Cdn", b + 1 - opsum .-= t, "Cdagdn", b + 1, "Cdn", b + opsum -= t, "Cdagup", b, "Cup", b + 1 + opsum -= t, "Cdagup", b + 1, "Cup", b + opsum -= t, "Cdagdn", b, "Cdn", b + 1 + opsum -= t, "Cdagdn", b + 1, "Cdn", b end if U ≠ 0 for n in 1:N - opsum .+= U, "Nupdn", n + opsum += U, "Nupdn", n end end return opsum @@ -20,14 +20,14 @@ function hubbard_2d(; Nx::Int, Ny::Int, t=1.0, U=0.0, yperiodic::Bool=true) lattice = square_lattice(Nx, Ny; yperiodic=yperiodic) opsum = OpSum() for b in lattice - opsum .-= t, "Cdagup", b.s1, "Cup", b.s2 - opsum .-= t, "Cdagup", b.s2, "Cup", b.s1 - opsum .-= t, "Cdagdn", b.s1, "Cdn", b.s2 - opsum .-= t, "Cdagdn", b.s2, "Cdn", b.s1 + opsum -= t, "Cdagup", b.s1, "Cup", b.s2 + opsum -= t, "Cdagup", b.s2, "Cup", b.s1 + opsum -= t, "Cdagdn", b.s1, "Cdn", b.s2 + opsum -= t, "Cdagdn", b.s2, "Cdn", b.s1 end if U ≠ 0 for n in 1:N - opsum .+= U, "Nupdn", n + opsum += U, "Nupdn", n end end return opsum @@ -40,8 +40,8 @@ function hubbard_2d_ky(; Nx::Int, Ny::Int, t=1.0, U=0.0) s = x * Ny + ky + 1 disp = -2 * t * cos((2 * π / Ny) * ky) if abs(disp) > 1e-12 - opsum .+= disp, "Nup", s - opsum .+= disp, "Ndn", s + opsum += disp, "Nup", s + opsum += disp, "Ndn", s end end end @@ -49,10 +49,10 @@ function hubbard_2d_ky(; Nx::Int, Ny::Int, t=1.0, U=0.0) for ky in 0:(Ny - 1) s1 = x * Ny + ky + 1 s2 = (x + 1) * Ny + ky + 1 - opsum .-= t, "Cdagup", s1, "Cup", s2 - opsum .-= t, "Cdagup", s2, "Cup", s1 - opsum .-= t, "Cdagdn", s1, "Cdn", s2 - opsum .-= t, "Cdagdn", s2, "Cdn", s1 + opsum -= t, "Cdagup", s1, "Cup", s2 + opsum -= t, "Cdagup", s2, "Cup", s1 + opsum -= t, "Cdagdn", s1, "Cdn", s2 + opsum -= t, "Cdagdn", s2, "Cdn", s1 end end if U ≠ 0 @@ -64,7 +64,7 @@ function hubbard_2d_ky(; Nx::Int, Ny::Int, t=1.0, U=0.0) s2 = x * Ny + (py - qy + Ny) % Ny + 1 s3 = x * Ny + py + 1 s4 = x * Ny + ky + 1 - opsum .+= (U / Ny), "Cdagdn", s1, "Cdagup", s2, "Cup", s3, "Cdn", s4 + opsum += (U / Ny), "Cdagdn", s1, "Cdagup", s2, "Cup", s3, "Cdn", s4 end end end @@ -75,10 +75,10 @@ end function hubbard(; Nx::Int, Ny::Int=1, t=1.0, U=0.0, yperiodic::Bool=true, ky::Bool=false) return opsum = if Ny == 1 - hubbard_1d(; N=Nx, t=t, U=U) + hubbard_1d(; N=Nx, t, U) elseif ky - hubbard_2d_ky(; Nx=Nx, Ny=Ny, t=t, U=U) + hubbard_2d_ky(; Nx, Ny, t, U) else - hubbard_2d(; Nx=Nx, Ny=Ny, yperiodic=yperiodic, t=t, U=U) + hubbard_2d(; Nx, Ny, yperiodic, t, U) end end diff --git a/src/lib/ITensorMPS/src/abstractmps.jl b/src/lib/ITensorMPS/src/abstractmps.jl index 1e614dccd0..abfe33512e 100644 --- a/src/lib/ITensorMPS/src/abstractmps.jl +++ b/src/lib/ITensorMPS/src/abstractmps.jl @@ -179,8 +179,8 @@ should be preserved. s = siteinds("S=1/2", 4) # Make random MPS with bond dimension 2 -ψ₁ = randomMPS(s, "↑", 2) -ψ₂ = randomMPS(s, "↑", 2) +ψ₁ = randomMPS(s, "↑"; linkdims=2) +ψ₂ = randomMPS(s, "↑"; linkdims=2) ψ₁ = orthogonalize(ψ₁, 1) ψ₂ = orthogonalize(ψ₂, 1) @@ -1518,9 +1518,9 @@ N = 10 s = siteinds("S=1/2", N; conserve_qns = true) state = n -> isodd(n) ? "↑" : "↓" -ψ₁ = randomMPS(s, state, 2) -ψ₂ = randomMPS(s, state, 2) -ψ₃ = randomMPS(s, state, 2) +ψ₁ = randomMPS(s, state; linkdims=2) +ψ₂ = randomMPS(s, state; linkdims=2) +ψ₃ = randomMPS(s, state; linkdims=2) ψ = +(ψ₁, ψ₂; cutoff = 1e-8) From 3858b641ea3f73dd836bf12403a6959fbcf910bd Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 16 May 2024 15:20:10 -0400 Subject: [PATCH 2/9] More syntax updates --- docs/src/examples/DMRG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/examples/DMRG.md b/docs/src/examples/DMRG.md index 45f9f425cd..5ec31b2c22 100644 --- a/docs/src/examples/DMRG.md +++ b/docs/src/examples/DMRG.md @@ -228,9 +228,9 @@ let # Define the Heisenberg spin Hamiltonian on this lattice os = OpSum() for b in lattice - os .+= 0.5, "S+", b.s1, "S-", b.s2 - os .+= 0.5, "S-", b.s1, "S+", b.s2 - os .+= "Sz", b.s1, "Sz", b.s2 + os += 0.5, "S+", b.s1, "S-", b.s2 + os += 0.5, "S-", b.s1, "S+", b.s2 + os += "Sz", b.s1, "Sz", b.s2 end H = MPO(os,sites) From 6f490b6e507f69977ef8ea65c413e22c6ff4422d Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 16 May 2024 15:20:45 -0400 Subject: [PATCH 3/9] Bump to v0.6.6 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 279ae138a4..752f59dd25 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ITensors" uuid = "9136182c-28ba-11e9-034c-db9fb085ebd5" authors = ["Matthew Fishman ", "Miles Stoudenmire "] -version = "0.6.5" +version = "0.6.6" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" From e9af94dabb9ae02652cfe14400091e21fd92e5fb Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 16 May 2024 15:41:28 -0400 Subject: [PATCH 4/9] [ITensorMPS] Rename randomMPS to random_mps --- .../test/ITensors/TestITensorDMRG/dmrg.jl | 4 +- docs/src/MPSandMPO.md | 6 +- docs/src/Observer.md | 2 +- docs/src/examples/DMRG.md | 18 +-- docs/src/examples/MPSandMPO.md | 6 +- docs/src/faq/DMRG.md | 2 +- docs/src/index.md | 2 +- docs/src/tutorials/DMRG.md | 4 +- docs/src/tutorials/QN_DMRG.md | 2 +- .../precompile_itensors.jl | 4 +- .../examples/autodiff/mps_autodiff.jl | 2 +- .../ITensorMPS/examples/dmrg/1d_heisenberg.jl | 2 +- .../dmrg/1d_heisenberg_conserve_spin.jl | 2 +- .../examples/dmrg/1d_hubbard_extended.jl | 2 +- .../examples/dmrg/1d_ising_with_observer.jl | 2 +- .../dmrg/2d_heisenberg_conserve_spin.jl | 2 +- .../dmrg/2d_hubbard_conserve_momentum.jl | 2 +- .../dmrg/2d_hubbard_conserve_particles.jl | 2 +- .../2d_hubbard_conserve_momentum.jl | 2 +- .../dmrg/write_to_disk/1d_heisenberg.jl | 2 +- .../exact_diagonalization.jl | 2 +- .../examples/finite_temperature/metts.jl | 2 +- .../mps_mpo_algebra/mps_density_matrix.jl | 2 +- src/lib/ITensorMPS/src/abstractmps.jl | 26 +-- src/lib/ITensorMPS/src/deprecated.jl | 10 +- src/lib/ITensorMPS/src/exports.jl | 2 +- src/lib/ITensorMPS/src/mpo.jl | 6 +- src/lib/ITensorMPS/src/mps.jl | 50 +++--- .../test/base/backup/test_arraystorage.jl | 2 +- .../ITensorMPS/test/base/test_algorithm.jl | 2 +- src/lib/ITensorMPS/test/base/test_autompo.jl | 4 +- src/lib/ITensorMPS/test/base/test_dmrg.jl | 32 ++-- .../ITensorMPS/test/base/test_inference.jl | 2 +- src/lib/ITensorMPS/test/base/test_mpo.jl | 60 +++---- src/lib/ITensorMPS/test/base/test_mps.jl | 150 +++++++++--------- src/lib/ITensorMPS/test/base/test_qnmpo.jl | 10 +- src/lib/ITensorMPS/test/base/test_readme.jl | 2 +- src/lib/ITensorMPS/test/base/utils/util.jl | 2 +- .../test_chainrules.jl | 24 +-- .../test_optimization.jl | 4 +- .../examples/example_dmrg.jl | 4 +- .../TestITensorsExportedNames.jl | 2 +- 42 files changed, 231 insertions(+), 239 deletions(-) diff --git a/NDTensors/test/ITensors/TestITensorDMRG/dmrg.jl b/NDTensors/test/ITensors/TestITensorDMRG/dmrg.jl index d997c088db..90146dbc91 100644 --- a/NDTensors/test/ITensors/TestITensorDMRG/dmrg.jl +++ b/NDTensors/test/ITensors/TestITensorDMRG/dmrg.jl @@ -1,4 +1,4 @@ -using ITensors: MPO, OpSum, dmrg, randomMPS, siteinds +using ITensors: MPO, OpSum, dmrg, random_mps, siteinds using Random: Random using Test: @test include("../../NDTensorsTestUtils/NDTensorsTestUtils.jl") @@ -19,7 +19,7 @@ function test_dmrg( Random.seed!(1234) init = j -> isodd(j) ? "↑" : "↓" - psi0 = dev(randomMPS(elt, sites, init; linkdims=4)) + psi0 = dev(random_mps(elt, sites, init; linkdims=4)) H = dev(MPO(elt, os, sites)) nsweeps = 3 diff --git a/docs/src/MPSandMPO.md b/docs/src/MPSandMPO.md index e4efc2a78f..d658d3795e 100644 --- a/docs/src/MPSandMPO.md +++ b/docs/src/MPSandMPO.md @@ -12,9 +12,9 @@ MPO ```@docs MPS(::Int) MPS(::Type{<:Number}, ::Vector{<:Index}) -randomMPS(sites::Vector{<:Index}) -randomMPS(::Type{<:Number}, sites::Vector{<:Index}) -randomMPS(::Vector{<:Index}, ::Any) +random_mps(sites::Vector{<:Index}) +random_mps(::Type{<:Number}, sites::Vector{<:Index}) +random_mps(::Vector{<:Index}, ::Any) MPS(::Vector{<:Index}, ::Any) MPS(::Type{<:Number}, ::Vector{<:Index}, ::Any) MPS(::Vector{<:Pair{<:Index}}) diff --git a/docs/src/Observer.md b/docs/src/Observer.md index f2f8e88b73..fd50d96817 100644 --- a/docs/src/Observer.md +++ b/docs/src/Observer.md @@ -184,7 +184,7 @@ let a += 0.5,"S-",n,"S+",n+1 end H = MPO(a,s) - psi0 = randomMPS(s;linkdims=4) + psi0 = random_mps(s;linkdims=4) nsweeps = 5 cutoff = 1E-8 diff --git a/docs/src/examples/DMRG.md b/docs/src/examples/DMRG.md index 5ec31b2c22..77c6e0a601 100644 --- a/docs/src/examples/DMRG.md +++ b/docs/src/examples/DMRG.md @@ -47,7 +47,7 @@ The random starting wavefunction `psi0` must be defined in the same Hilbert spac as the Hamiltonian, so we construct it using the same collection of site indices: ```julia -psi0 = randomMPS(sites;linkdims=2) +psi0 = random_mps(sites;linkdims=2) ``` Here we have made a random MPS of bond dimension 2. We could have used a random product @@ -85,7 +85,7 @@ let maxdim = [10,20,100,100,200] # gradually increase states kept cutoff = [1E-10] # desired truncation error - psi0 = randomMPS(sites;linkdims=2) + psi0 = random_mps(sites;linkdims=2) energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff) @@ -158,7 +158,7 @@ let maxdim = [10,10,20,40,80,100,140,180,200] cutoff = [1E-8] - psi0 = randomMPS(sites;linkdims=4) + psi0 = random_mps(sites;linkdims=4) energy,psi = dmrg(H,psi0;nsweeps,maxdim,cutoff) @@ -238,7 +238,7 @@ let # Initialize wavefunction to a random MPS # of bond-dimension 10 with same quantum # numbers as `state` - psi0 = randomMPS(sites,state;linkdims=20) + psi0 = random_mps(sites,state;linkdims=20) nsweeps = 10 maxdim = [20,60,100,100,200,400,800] @@ -328,7 +328,7 @@ let # # Compute the ground state psi0 # - psi0_init = randomMPS(sites;linkdims=2) + psi0_init = random_mps(sites;linkdims=2) energy0,psi0 = dmrg(H,psi0_init;nsweeps,maxdim,cutoff,noise) println() @@ -336,7 +336,7 @@ let # # Compute the first excited state psi1 # - psi1_init = randomMPS(sites;linkdims=2) + psi1_init = random_mps(sites;linkdims=2) energy1,psi1 = dmrg(H,[psi0],psi1_init;nsweeps,maxdim,cutoff,noise,weight) # Check psi1 is orthogonal to psi0 @@ -357,7 +357,7 @@ let # # Compute the second excited state psi2 # - psi2_init = randomMPS(sites;linkdims=2) + psi2_init = random_mps(sites;linkdims=2) energy2,psi2 = dmrg(H,[psi0,psi1],psi2_init;nsweeps,maxdim,cutoff,noise,weight) # Check psi2 is orthogonal to psi0 and psi1 @@ -429,7 +429,7 @@ let a += 0.5,"S-",n,"S+",n+1 end H = MPO(a,s) - psi0 = randomMPS(s;linkdims=4) + psi0 = random_mps(s;linkdims=4) nsweeps = 5 maxdim = [10,20,80,160] @@ -523,7 +523,7 @@ let a += 0.5,"S-",n,"S+",n+1 end H = MPO(a,s) - psi0 = randomMPS(s;linkdims=4) + psi0 = random_mps(s;linkdims=4) nsweeps = 5 maxdim = [10,20,80,160] diff --git a/docs/src/examples/MPSandMPO.md b/docs/src/examples/MPSandMPO.md index 509e723e5b..34a336ebfe 100644 --- a/docs/src/examples/MPSandMPO.md +++ b/docs/src/examples/MPSandMPO.md @@ -88,7 +88,7 @@ let # hide N = 10 s = siteinds(2,N) chi = 4 -psi = randomMPS(s;linkdims=chi) +psi = random_mps(s;linkdims=chi) # Make an array of integers of the element we # want to obtain @@ -143,7 +143,7 @@ using ITensors, ITensorMPS N = 10 chi = 4 sites = siteinds("S=1/2",N) -psi = randomMPS(sites;linkdims=chi) +psi = random_mps(sites;linkdims=chi) magz = expect(psi,"Sz") for (j,mz) in enumerate(magz) println("$j $mz") @@ -362,7 +362,7 @@ N = 10 # number of sites d = 3 # dimension of each site chi = 16 # bond dimension of the MPS s = siteinds(d,N) -psi = randomMPS(s;linkdims=chi) +psi = random_mps(s;linkdims=chi) ``` We can now draw some samples from this MPS as diff --git a/docs/src/faq/DMRG.md b/docs/src/faq/DMRG.md index a502a0c3c2..fa32ae4892 100644 --- a/docs/src/faq/DMRG.md +++ b/docs/src/faq/DMRG.md @@ -91,7 +91,7 @@ When DMRG is failing to converge, here are some of the steps you can take to imp up on the left-hand side only, it can take DMRG a very long time to converge. * Try using a random MPS with a modestly large bond dimension. ITensor offers a function - called [`randomMPS`](@ref) which can be used to make random MPS in both the quantum number (QN) + called [`random_mps`](@ref) which can be used to make random MPS in both the quantum number (QN) conserving and non-QN conserving cases. Because random MPS have properties which are "typical" of most ground states, they can be good initial states for DMRG. diff --git a/docs/src/index.md b/docs/src/index.md index 24d11288b8..e7ab55e400 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -294,7 +294,7 @@ let H = MPO(os,sites) # Create an initial random matrix product state - psi0 = randomMPS(sites) + psi0 = random_mps(sites) # Plan to do 5 passes or 'sweeps' of DMRG, # setting maximum MPS internal dimensions diff --git a/docs/src/tutorials/DMRG.md b/docs/src/tutorials/DMRG.md index 99b18f05c7..9ff95b823d 100644 --- a/docs/src/tutorials/DMRG.md +++ b/docs/src/tutorials/DMRG.md @@ -38,7 +38,7 @@ let end H = MPO(os,sites) - psi0 = randomMPS(sites;linkdims=10) + psi0 = random_mps(sites;linkdims=10) nsweeps = 5 maxdim = [10,20,100,100,200] @@ -92,7 +92,7 @@ physical indices given by the array `sites`. The line ```julia -psi0 = randomMPS(sites;linkdims=10) +psi0 = random_mps(sites;linkdims=10) ``` constructs an MPS `psi0` which has the physical indices `sites` and a bond dimension of 10. diff --git a/docs/src/tutorials/QN_DMRG.md b/docs/src/tutorials/QN_DMRG.md index 56bd33ddce..3d3f593372 100644 --- a/docs/src/tutorials/QN_DMRG.md +++ b/docs/src/tutorials/QN_DMRG.md @@ -81,7 +81,7 @@ To make change (2), instead of constructing the initial MPS `psi0` to be an arbi So we will replace the line ```julia -psi0 = randomMPS(sites;linkdims=10) +psi0 = random_mps(sites;linkdims=10) ``` by the lines diff --git a/ext/ITensorsPackageCompilerExt/precompile_itensors.jl b/ext/ITensorsPackageCompilerExt/precompile_itensors.jl index 04c904d288..4d5d639359 100644 --- a/ext/ITensorsPackageCompilerExt/precompile_itensors.jl +++ b/ext/ITensorsPackageCompilerExt/precompile_itensors.jl @@ -1,4 +1,4 @@ -using ITensors.ITensorMPS: MPO, OpSum, dmrg, randomMPS, siteinds +using ITensors.ITensorMPS: MPO, OpSum, dmrg, random_mps, siteinds # TODO: This uses all of the tests to make # precompile statements, but takes a long time @@ -19,7 +19,7 @@ function main(; N, dmrg_kwargs) for conserve_qns in (false, true) sites = siteinds("S=1", N; conserve_qns) H = MPO(opsum, sites) - ψ0 = randomMPS(sites, j -> isodd(j) ? "↑" : "↓"; linkdims=2) + ψ0 = random_mps(sites, j -> isodd(j) ? "↑" : "↓"; linkdims=2) dmrg(H, ψ0; outputlevel=0, dmrg_kwargs...) end return nothing diff --git a/src/lib/ITensorMPS/examples/autodiff/mps_autodiff.jl b/src/lib/ITensorMPS/examples/autodiff/mps_autodiff.jl index 41a16f05ec..5a9c123585 100644 --- a/src/lib/ITensorMPS/examples/autodiff/mps_autodiff.jl +++ b/src/lib/ITensorMPS/examples/autodiff/mps_autodiff.jl @@ -31,7 +31,7 @@ h = 0.5 # Loss function only works with `Vector{ITensor}`, # extract with `ITensors.data`. -ψ0 = ITensors.data(randomMPS(s; linkdims=10)) +ψ0 = ITensors.data(random_mps(s; linkdims=10)) H = ITensors.data(MPO(ising(n; J, h), s)) loss(ψ) = loss(H, ψ) diff --git a/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg.jl b/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg.jl index 1b9111b2f3..4718721151 100644 --- a/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg.jl +++ b/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg.jl @@ -23,7 +23,7 @@ let H = MPO(os, sites) # Create an initial random matrix product state - psi0 = randomMPS(sites; linkdims=10) + psi0 = random_mps(sites; linkdims=10) # Plan to do 5 DMRG sweeps: nsweeps = 5 diff --git a/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg_conserve_spin.jl b/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg_conserve_spin.jl index fb4de478b4..f8ec1fb23d 100644 --- a/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg_conserve_spin.jl +++ b/src/lib/ITensorMPS/examples/dmrg/1d_heisenberg_conserve_spin.jl @@ -24,7 +24,7 @@ let H = MPO(os, sites) state = [isodd(n) ? "Up" : "Dn" for n in 1:N] - psi0 = randomMPS(sites, state; linkdims=10) + psi0 = random_mps(sites, state; linkdims=10) # Plan to do 5 DMRG sweeps: nsweeps = 5 diff --git a/src/lib/ITensorMPS/examples/dmrg/1d_hubbard_extended.jl b/src/lib/ITensorMPS/examples/dmrg/1d_hubbard_extended.jl index 571a81fc52..d4139bedb4 100644 --- a/src/lib/ITensorMPS/examples/dmrg/1d_hubbard_extended.jl +++ b/src/lib/ITensorMPS/examples/dmrg/1d_hubbard_extended.jl @@ -54,7 +54,7 @@ let # Initialize wavefunction to be bond # dimension 10 random MPS with number # of particles the same as `state` - psi0 = randomMPS(sites, state; linkdims=10) + psi0 = random_mps(sites, state; linkdims=10) # Check total number of particles: @show flux(psi0) diff --git a/src/lib/ITensorMPS/examples/dmrg/1d_ising_with_observer.jl b/src/lib/ITensorMPS/examples/dmrg/1d_ising_with_observer.jl index f4b9847e2c..d3dbd7c75b 100644 --- a/src/lib/ITensorMPS/examples/dmrg/1d_ising_with_observer.jl +++ b/src/lib/ITensorMPS/examples/dmrg/1d_ising_with_observer.jl @@ -23,7 +23,7 @@ end let N = 100 sites = siteinds("S=1/2", N) - psi0 = randomMPS(sites; linkdims=10) + psi0 = random_mps(sites; linkdims=10) # define parameters for DMRG sweeps nsweeps = 15 diff --git a/src/lib/ITensorMPS/examples/dmrg/2d_heisenberg_conserve_spin.jl b/src/lib/ITensorMPS/examples/dmrg/2d_heisenberg_conserve_spin.jl index 259d21c7b0..87887843e7 100644 --- a/src/lib/ITensorMPS/examples/dmrg/2d_heisenberg_conserve_spin.jl +++ b/src/lib/ITensorMPS/examples/dmrg/2d_heisenberg_conserve_spin.jl @@ -22,7 +22,7 @@ let # Initialize wavefunction to a random MPS # of bond-dimension 10 with same quantum # numbers as `state` - psi0 = randomMPS(sites, state; linkdims=20) + psi0 = random_mps(sites, state; linkdims=20) nsweeps = 10 maxdim = [20, 60, 100, 100, 200, 400, 800] diff --git a/src/lib/ITensorMPS/examples/dmrg/2d_hubbard_conserve_momentum.jl b/src/lib/ITensorMPS/examples/dmrg/2d_hubbard_conserve_momentum.jl index 2ed348c7f9..27c8535352 100644 --- a/src/lib/ITensorMPS/examples/dmrg/2d_hubbard_conserve_momentum.jl +++ b/src/lib/ITensorMPS/examples/dmrg/2d_hubbard_conserve_momentum.jl @@ -79,7 +79,7 @@ function main(; display(state) psi0 = if random_init - randomMPS(itensor_rng, sites, state; linkdims=2) + random_mps(itensor_rng, sites, state; linkdims=2) else MPS(sites, state) end diff --git a/src/lib/ITensorMPS/examples/dmrg/2d_hubbard_conserve_particles.jl b/src/lib/ITensorMPS/examples/dmrg/2d_hubbard_conserve_particles.jl index 928d21f7f0..f1887341da 100644 --- a/src/lib/ITensorMPS/examples/dmrg/2d_hubbard_conserve_particles.jl +++ b/src/lib/ITensorMPS/examples/dmrg/2d_hubbard_conserve_particles.jl @@ -30,7 +30,7 @@ function main(; Nx=6, Ny=3, U=4.0, t=1.0) # Initialize wavefunction to a random MPS # of bond-dimension 10 with same quantum # numbers as `state` - psi0 = randomMPS(sites, state) + psi0 = random_mps(sites, state) energy, psi = dmrg(H, psi0; nsweeps, maxdim, cutoff, noise) @show t, U diff --git a/src/lib/ITensorMPS/examples/dmrg/threaded_blocksparse/2d_hubbard_conserve_momentum.jl b/src/lib/ITensorMPS/examples/dmrg/threaded_blocksparse/2d_hubbard_conserve_momentum.jl index 2df178da49..9905e96cb6 100644 --- a/src/lib/ITensorMPS/examples/dmrg/threaded_blocksparse/2d_hubbard_conserve_momentum.jl +++ b/src/lib/ITensorMPS/examples/dmrg/threaded_blocksparse/2d_hubbard_conserve_momentum.jl @@ -58,7 +58,7 @@ function main(; end display(state) - psi0 = randomMPS(sites, state; linkdims=10) + psi0 = random_mps(sites, state; linkdims=10) energy, psi = @time dmrg(H, psi0; nsweeps, maxdim, cutoff, noise, outputlevel) diff --git a/src/lib/ITensorMPS/examples/dmrg/write_to_disk/1d_heisenberg.jl b/src/lib/ITensorMPS/examples/dmrg/write_to_disk/1d_heisenberg.jl index 8492a10771..6391e862ab 100644 --- a/src/lib/ITensorMPS/examples/dmrg/write_to_disk/1d_heisenberg.jl +++ b/src/lib/ITensorMPS/examples/dmrg/write_to_disk/1d_heisenberg.jl @@ -21,7 +21,7 @@ let H = MPO(os, sites) state = [isodd(n) ? "Up" : "Dn" for n in 1:N] - psi0 = randomMPS(sites, state; linkdims=10) + psi0 = random_mps(sites, state; linkdims=10) # Plan to do 5 DMRG sweeps: nsweeps = 5 diff --git a/src/lib/ITensorMPS/examples/exact_diagonalization/exact_diagonalization.jl b/src/lib/ITensorMPS/examples/exact_diagonalization/exact_diagonalization.jl index 389809e4c9..2016ab813f 100644 --- a/src/lib/ITensorMPS/examples/exact_diagonalization/exact_diagonalization.jl +++ b/src/lib/ITensorMPS/examples/exact_diagonalization/exact_diagonalization.jl @@ -30,7 +30,7 @@ function main(n; blas_num_threads=Sys.CPU_THREADS, fuse=true, binary=true) s = siteinds("S=1/2", n; conserve_qns=true) H = MPO(heisenberg(n), s) initstate(j) = isodd(j) ? "↑" : "↓" - ψ0 = randomMPS(s, initstate; linkdims=10) + ψ0 = random_mps(s, initstate; linkdims=10) edmrg, ψdmrg = dmrg(H, ψ0; nsweeps=10, cutoff=1e-6) diff --git a/src/lib/ITensorMPS/examples/finite_temperature/metts.jl b/src/lib/ITensorMPS/examples/finite_temperature/metts.jl index ec53c5da5d..17016b1de1 100644 --- a/src/lib/ITensorMPS/examples/finite_temperature/metts.jl +++ b/src/lib/ITensorMPS/examples/finite_temperature/metts.jl @@ -51,7 +51,7 @@ function main(; N=10, cutoff=1E-8, δτ=0.1, beta=2.0, NMETTS=3000, Nwarm=10) Ry_gates = ops([("Ry", n, (θ=π / 2,)) for n in 1:N], s) # Arbitrary initial state - psi = randomMPS(s) + psi = random_mps(s) # Make H for measuring the energy terms = OpSum() diff --git a/src/lib/ITensorMPS/examples/mps_mpo_algebra/mps_density_matrix.jl b/src/lib/ITensorMPS/examples/mps_mpo_algebra/mps_density_matrix.jl index bea370842d..49abf397e8 100644 --- a/src/lib/ITensorMPS/examples/mps_mpo_algebra/mps_density_matrix.jl +++ b/src/lib/ITensorMPS/examples/mps_mpo_algebra/mps_density_matrix.jl @@ -4,7 +4,7 @@ N = 4 nmps = 3 cutoff = 1e-8 s = siteinds("S=1/2", N) -ψs = [randomMPS(s; linkdims=2) for _ in 1:nmps] +ψs = [random_mps(s; linkdims=2) for _ in 1:nmps] ρs = [outer(ψ, ψ; cutoff) for ψ in ψs] ρ = sum(ρs; cutoff) diff --git a/src/lib/ITensorMPS/src/abstractmps.jl b/src/lib/ITensorMPS/src/abstractmps.jl index abfe33512e..2ec1fffba0 100644 --- a/src/lib/ITensorMPS/src/abstractmps.jl +++ b/src/lib/ITensorMPS/src/abstractmps.jl @@ -109,7 +109,7 @@ Returns the range of sites of the orthogonality center of the MPS/MPO. ```julia s = siteinds("S=½", 5) -ψ = randomMPS(s) +ψ = random_mps(s) ψ = orthogonalize(ψ, 3) # ortho_lims(ψ) = 3:3 @@ -179,8 +179,8 @@ should be preserved. s = siteinds("S=1/2", 4) # Make random MPS with bond dimension 2 -ψ₁ = randomMPS(s, "↑"; linkdims=2) -ψ₂ = randomMPS(s, "↑"; linkdims=2) +ψ₁ = random_mps(s, "↑"; linkdims=2) +ψ₂ = random_mps(s, "↑"; linkdims=2) ψ₁ = orthogonalize(ψ₁, 1) ψ₂ = orthogonalize(ψ₂, 1) @@ -256,7 +256,7 @@ julia> using ITensors, ITensorMPS julia> s = siteinds("S=1/2", 3); -julia> M1 = randomMPS(s; linkdims=3); +julia> M1 = random_mps(s; linkdims=3); julia> norm(M1) 0.9999999999999999 @@ -304,7 +304,7 @@ julia> using ITensors, ITensorMPS julia> s = siteinds("S=1/2", 3); -julia> M1 = randomMPS(s; linkdims=3); +julia> M1 = random_mps(s; linkdims=3); julia> norm(M1) 1.0 @@ -541,7 +541,7 @@ To find all sites with common indices with `is`, use the # Examples ```julia s = siteinds("S=1/2", 5) -ψ = randomMPS(s) +ψ = random_mps(s) findsite(ψ, s[3]) == 3 findsite(ψ, (s[3], s[4])) == 3 @@ -572,7 +572,7 @@ indices in common with the collection of site indices # Examples ```julia s = siteinds("S=1/2", 5) -ψ = randomMPS(s) +ψ = random_mps(s) findsites(ψ, s[3]) == [3] findsites(ψ, (s[4], s[1])) == [1, 4] @@ -998,7 +998,7 @@ with mismatched prime levels. The most common cause of this is something like th ```julia s = siteinds("S=1/2") - psi = randomMPS(s) + psi = random_mps(s) H = MPO(s, "Id") Hpsi = contract(H, psi; cutoff=1e-8) # or `Hpsi = *(H, psi; cutoff=1e-8)` inner(psi, Hpsi) @@ -1323,7 +1323,7 @@ case, we just return the original MPS or MPO. You can check for this case as fol ```julia s = siteinds("S=1/2", 4) -ψ = 0 * randomMPS(s) +ψ = 0 * random_mps(s) lognorm_ψ = [] normalize!(ψ; (lognorm!)=lognorm_ψ) lognorm_ψ[1] == -Inf # There was an infinite norm @@ -1518,9 +1518,9 @@ N = 10 s = siteinds("S=1/2", N; conserve_qns = true) state = n -> isodd(n) ? "↑" : "↓" -ψ₁ = randomMPS(s, state; linkdims=2) -ψ₂ = randomMPS(s, state; linkdims=2) -ψ₃ = randomMPS(s, state; linkdims=2) +ψ₁ = random_mps(s, state; linkdims=2) +ψ₂ = random_mps(s, state; linkdims=2) +ψ₃ = random_mps(s, state; linkdims=2) ψ = +(ψ₁, ψ₂; cutoff = 1e-8) @@ -2259,7 +2259,7 @@ end # For example: # # s = siteinds("Qubit", 1) -# ψ = randomMPS(s) +# ψ = random_mps(s) # # # U = Z₁X₁ # U = Prod{Op}() diff --git a/src/lib/ITensorMPS/src/deprecated.jl b/src/lib/ITensorMPS/src/deprecated.jl index 8421a0b9db..4b4b5f1480 100644 --- a/src/lib/ITensorMPS/src/deprecated.jl +++ b/src/lib/ITensorMPS/src/deprecated.jl @@ -21,15 +21,7 @@ @deprecate MPO(A::MPS; kwargs...) outer(A', A; kwargs...) # mps/mps.jl -@deprecate randomMPS(sites::Vector{<:Index}, linkdims::Integer) randomMPS( - sites; linkdims=linkdims -) -@deprecate randomMPS(ElType::Type, sites::Vector{<:Index}, linkdims::Integer) randomMPS( - ElType, sites; linkdims=linkdims -) -@deprecate randomMPS(sites::Vector{<:Index}, state, linkdims::Integer) randomMPS( - sites, state; linkdims=linkdims -) +@deprecate randomMPS(args...; kwargs...) = random_mps(args...; kwargs...) # Pass throughs of old name to new name: diff --git a/src/lib/ITensorMPS/src/exports.jl b/src/lib/ITensorMPS/src/exports.jl index 4dae77014c..d857091ad4 100644 --- a/src/lib/ITensorMPS/src/exports.jl +++ b/src/lib/ITensorMPS/src/exports.jl @@ -62,7 +62,7 @@ export linkind, linkinds, productMPS, - randomMPS, + random_mps, replacebond, replacebond!, sample, diff --git a/src/lib/ITensorMPS/src/mpo.jl b/src/lib/ITensorMPS/src/mpo.jl index a82d5ca666..eedde86d6d 100644 --- a/src/lib/ITensorMPS/src/mpo.jl +++ b/src/lib/ITensorMPS/src/mpo.jl @@ -171,8 +171,8 @@ For example: ```julia s = siteinds("S=1/2", 5) -x = randomMPS(s) -y = randomMPS(s) +x = random_mps(s) +y = random_mps(s) outer(x, y) # Incorrect! Site indices must be unique. outer(x', y) # Results in an MPO with pairs of primed and unprimed indices. ``` @@ -295,7 +295,7 @@ function inner_mps_mpo_mps_deprecation_warning() ```julia s = siteinds("S=1/2") - psi = randomMPS(s) + psi = random_mps(s) H = MPO(s, "Id") inner(psi, H, psi) ``` diff --git a/src/lib/ITensorMPS/src/mps.jl b/src/lib/ITensorMPS/src/mps.jl index d3ab6b8b11..423089bcdc 100644 --- a/src/lib/ITensorMPS/src/mps.jl +++ b/src/lib/ITensorMPS/src/mps.jl @@ -225,7 +225,7 @@ function _fill_linkdims(linkdims::Integer, sites::Vector{<:Index}) end """ - randomMPS(eltype::Type{<:Number}, sites::Vector{<:Index}; linkdims=1) + random_mps(eltype::Type{<:Number}, sites::Vector{<:Index}; linkdims=1) Construct a random MPS with link dimension `linkdims` of type `eltype`. @@ -234,13 +234,13 @@ type `eltype`. `length(linkdims) == length(sites) - 1` for constructing an MPS with non-uniform bond dimension. """ -function randomMPS( +function random_mps( ::Type{ElT}, sites::Vector{<:Index}; linkdims::Union{Integer,Vector{<:Integer}}=1 ) where {ElT<:Number} - return randomMPS(Random.default_rng(), ElT, sites; linkdims) + return random_mps(Random.default_rng(), ElT, sites; linkdims) end -function randomMPS( +function random_mps( rng::AbstractRNG, ::Type{ElT}, sites::Vector{<:Index}; @@ -248,7 +248,7 @@ function randomMPS( ) where {ElT<:Number} _linkdims = _fill_linkdims(linkdims, sites) if any(hasqns, sites) - error("initial state required to use randomMPS with QNs") + error("initial state required to use random_mps with QNs") end # For non-QN-conserving MPS, instantiate @@ -257,8 +257,8 @@ function randomMPS( end """ - randomMPS(sites::Vector{<:Index}; linkdims=1) - randomMPS(eltype::Type{<:Number}, sites::Vector{<:Index}; linkdims=1) + random_mps(sites::Vector{<:Index}; linkdims=1) + random_mps(eltype::Type{<:Number}, sites::Vector{<:Index}; linkdims=1) Construct a random MPS with link dimension `linkdims` which by default has element type `Float64`. @@ -267,41 +267,41 @@ default has element type `Float64`. `length(linkdims) == length(sites) - 1` for constructing an MPS with non-uniform bond dimension. """ -function randomMPS(sites::Vector{<:Index}; linkdims::Union{Integer,Vector{<:Integer}}=1) - return randomMPS(Random.default_rng(), sites; linkdims) +function random_mps(sites::Vector{<:Index}; linkdims::Union{Integer,Vector{<:Integer}}=1) + return random_mps(Random.default_rng(), sites; linkdims) end -function randomMPS( +function random_mps( rng::AbstractRNG, sites::Vector{<:Index}; linkdims::Union{Integer,Vector{<:Integer}}=1 ) - return randomMPS(rng, Float64, sites; linkdims) + return random_mps(rng, Float64, sites; linkdims) end -function randomMPS( +function random_mps( sites::Vector{<:Index}, state; linkdims::Union{Integer,Vector{<:Integer}}=1 ) - return randomMPS(Random.default_rng(), sites, state; linkdims) + return random_mps(Random.default_rng(), sites, state; linkdims) end -function randomMPS( +function random_mps( rng::AbstractRNG, sites::Vector{<:Index}, state; linkdims::Union{Integer,Vector{<:Integer}}=1, ) - return randomMPS(rng, Float64, sites, state; linkdims) + return random_mps(rng, Float64, sites, state; linkdims) end -function randomMPS( +function random_mps( eltype::Type{<:Number}, sites::Vector{<:Index}, state; linkdims::Union{Integer,Vector{<:Integer}}=1, ) - return randomMPS(Random.default_rng(), eltype, sites, state; linkdims) + return random_mps(Random.default_rng(), eltype, sites, state; linkdims) end -function randomMPS( +function random_mps( rng::AbstractRNG, eltype::Type{<:Number}, sites::Vector{<:Index}, @@ -316,15 +316,15 @@ function randomMPS( end @doc """ - randomMPS(sites::Vector{<:Index}, state; linkdims=1) + random_mps(sites::Vector{<:Index}, state; linkdims=1) Construct a real, random MPS with link dimension `linkdims`, made by randomizing an initial product state specified by -`state`. This version of `randomMPS` is necessary when creating +`state`. This version of `random_mps` is necessary when creating QN-conserving random MPS (consisting of QNITensors). The initial `state` array provided determines the total QN of the resulting random MPS. -""" randomMPS(::Vector{<:Index}, ::Any) +""" random_mps(::Vector{<:Index}, ::Any) """ MPS(::Type{T<:Number}, ivals::Vector{<:Pair{<:Index}}) @@ -725,12 +725,12 @@ N = 30 m = 4 s = siteinds("S=1/2", N) -psi = randomMPS(s; linkdims=m) +psi = random_mps(s; linkdims=m) Czz = correlation_matrix(psi, "Sz", "Sz") Czz = correlation_matrix(psi, [1/2 0; 0 -1/2], [1/2 0; 0 -1/2]) # same as above s = siteinds("Electron", N; conserve_qns=true) -psi = randomMPS(s, n -> isodd(n) ? "Up" : "Dn"; linkdims=m) +psi = random_mps(s, n -> isodd(n) ? "Up" : "Dn"; linkdims=m) Cuu = correlation_matrix(psi, "Cdagup", "Cup"; sites=2:8) ``` """ @@ -967,7 +967,7 @@ of expectation values. N = 10 s = siteinds("S=1/2", N) -psi = randomMPS(s; linkdims=8) +psi = random_mps(s; linkdims=8) Z = expect(psi, "Sz") # compute for all sites Z = expect(psi, "Sz"; sites=2:4) # compute for sites 2,3,4 Z3 = expect(psi, "Sz"; sites=3) # compute for site 3 only (output will be a scalar) @@ -975,7 +975,7 @@ XZ = expect(psi, ["Sx", "Sz"]) # compute Sx and Sz for all sites Z = expect(psi, [1/2 0; 0 -1/2]) # same as expect(psi,"Sz") s = siteinds("Electron", N) -psi = randomMPS(s; linkdims=8) +psi = random_mps(s; linkdims=8) dens = expect(psi, "Ntot") updens, dndens = expect(psi, "Nup", "Ndn") # pass more than one operator ``` diff --git a/src/lib/ITensorMPS/test/base/backup/test_arraystorage.jl b/src/lib/ITensorMPS/test/base/backup/test_arraystorage.jl index 6c88f6de27..d9c25b51d1 100644 --- a/src/lib/ITensorMPS/test/base/backup/test_arraystorage.jl +++ b/src/lib/ITensorMPS/test/base/backup/test_arraystorage.jl @@ -14,7 +14,7 @@ using Test return os end H = MPO(heisenberg_opsum(n), s) - ψ = randomMPS(s, j -> isodd(j) ? "↑" : "↓"; linkdims=4) + ψ = random_mps(s, j -> isodd(j) ? "↑" : "↓"; linkdims=4) dmrg_kwargs = (; nsweeps=2, cutoff=[1e-4, 1e-12], maxdim=10, outputlevel=0) e1, ψ1 = dmrg(NDTensors.to_arraystorage.((H, ψ))...; dmrg_kwargs...) e2, ψ2 = dmrg(H, ψ; dmrg_kwargs...) diff --git a/src/lib/ITensorMPS/test/base/test_algorithm.jl b/src/lib/ITensorMPS/test/base/test_algorithm.jl index 5cc03d8e28..eb0f2e1ddc 100644 --- a/src/lib/ITensorMPS/test/base/test_algorithm.jl +++ b/src/lib/ITensorMPS/test/base/test_algorithm.jl @@ -9,7 +9,7 @@ using Test s = siteinds("S=1/2", 4) A = MPO(s, "Id") - ψ = randomMPS(s) + ψ = random_mps(s) @test_throws MethodError contract(alg, A, ψ) @test_throws MethodError contract(A, ψ; method="X") diff --git a/src/lib/ITensorMPS/test/base/test_autompo.jl b/src/lib/ITensorMPS/test/base/test_autompo.jl index e7a82a2c78..6eb5c4bd98 100644 --- a/src/lib/ITensorMPS/test/base/test_autompo.jl +++ b/src/lib/ITensorMPS/test/base/test_autompo.jl @@ -865,7 +865,7 @@ end Ha = MPO(os, sites) He = NNheisenbergMPO(sites, J1, J2) - psi = randomMPS(sites, [isodd(n) ? "Up" : "Dn" for n in 1:N]) + psi = random_mps(sites, [isodd(n) ? "Up" : "Dn" for n in 1:N]) Oa = inner(psi', Ha, psi) Oe = inner(psi', He, psi) @test Oa ≈ Oe @@ -1071,7 +1071,7 @@ end @test norm(prod(W) - prod(M)) < 1E-10 - psi = randomMPS(s, [isodd(n) ? "1" : "0" for n in 1:length(s)]; linkdims=4) + psi = random_mps(s, [isodd(n) ? "1" : "0" for n in 1:length(s)]; linkdims=4) Mpsi = apply(M, psi; alg="naive") Wpsi = apply(M, psi; alg="naive") @test abs(inner(Mpsi, Wpsi) / inner(Mpsi, Mpsi) - 1.0) < 1E-10 diff --git a/src/lib/ITensorMPS/test/base/test_dmrg.jl b/src/lib/ITensorMPS/test/base/test_dmrg.jl index e73d7567c5..cd1b95b950 100644 --- a/src/lib/ITensorMPS/test/base/test_dmrg.jl +++ b/src/lib/ITensorMPS/test/base/test_dmrg.jl @@ -14,7 +14,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range end H = MPO(os, sites) - psi = randomMPS(sites) + psi = random_mps(sites) sweeps = Sweeps(3) @test length(sweeps) == 3 @@ -41,7 +41,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range H = MPO(os, sites) state = [isodd(n) ? "Up" : "Dn" for n in 1:N] - psi = randomMPS(sites, state; linkdims=4) + psi = random_mps(sites, state; linkdims=4) sweeps = Sweeps(3) @test length(sweeps) == 3 @@ -68,7 +68,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range H = MPO(os, sites) state = [isodd(n) ? "Up" : "Dn" for n in 1:N] - psi = randomMPS(sites, state; linkdims=4) + psi = random_mps(sites, state; linkdims=4) sweeps = Sweeps(3) @test length(sweeps) == 3 @@ -95,7 +95,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range H = MPO(os, sites) state = [isodd(n) ? "Up" : "Dn" for n in 1:N] - psi = randomMPS(sites, state; linkdims=4) + psi = random_mps(sites, state; linkdims=4) PH = ProjMPO(H) PHc = copy(PH) @@ -109,7 +109,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range @test length(PHdisk) == N @test site_range(PH) == n:(n + 1) @test eltype(PH) == Float64 - ## TODO sometimes randomMPS gives a linkdim value of 3 + ## TODO sometimes random_mps gives a linkdim value of 3 ## which causes an error in `calculated_dim = 3^2 * 4^2` calculated_dim = linkdim(psi, n - 1) * @@ -146,7 +146,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range HB = MPO(osB, sites) state = [isodd(n) ? "Up" : "Dn" for n in 1:N] - psi = randomMPS(sites, state; linkdims=4) + psi = random_mps(sites, state; linkdims=4) energy, psi = dmrg( [HA, HB], psi; nsweeps=3, maxdim=[10, 20, 30], write_when_maxdim_exceeds=10 @@ -171,7 +171,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range H2 = MPO(os2, sites) state = [isodd(n) ? "Up" : "Dn" for n in 1:N] - psi = randomMPS(sites, state; linkdims=4) + psi = random_mps(sites, state; linkdims=4) PH1 = ProjMPO(H1) PH = ProjMPOSum([H1, H2]) PH1c = copy(PH1) @@ -196,7 +196,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range N = 32 sites = siteinds("S=1/2", N) Random.seed!(432) - psi0 = randomMPS(sites) + psi0 = random_mps(sites) os = OpSum() for j in 1:N @@ -221,7 +221,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range N = 32 sites = siteinds("S=1/2", N) Random.seed!(432) - psi0 = randomMPS(sites) + psi0 = random_mps(sites) function ising(N; h=1.0) os = OpSum() @@ -248,7 +248,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range Random.seed!(432) state = [isodd(j) ? "↑" : "↓" for j in 1:N] - psi0 = randomMPS(sites, state) + psi0 = random_mps(sites, state) os = OpSum() for j in 1:N @@ -279,7 +279,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range N = 10 sites = siteinds("S=1/2", N) Random.seed!(42) - psi0 = randomMPS(sites) + psi0 = random_mps(sites) os = OpSum() for j in 1:(N - 1) @@ -328,7 +328,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range end HXY = MPO(osXY, sites) - psi = randomMPS(sites) + psi = random_mps(sites) sweeps = Sweeps(3) maxdim!(sweeps, 10, 20, 40) @@ -355,7 +355,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range end H = MPO(os, sites) - psi0i = randomMPS(sites; linkdims=10) + psi0i = random_mps(sites; linkdims=10) sweeps = Sweeps(4) maxdim!(sweeps, 10, 20, 100, 100) @@ -365,7 +365,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range energy0, psi0 = dmrg(H, psi0i, sweeps; outputlevel=0) @test energy0 < -11.5 - psi1i = randomMPS(sites; linkdims=10) + psi1i = random_mps(sites; linkdims=10) energy1, psi1 = dmrg(H, [psi0], psi1i, sweeps; outputlevel=0, weight=weight) @test energy1 > energy0 @@ -432,7 +432,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range maxdim!(sweeps, 50, 100, 200, 400, 800, 800) cutoff!(sweeps, 1E-10) state = ["Up", "Dn", "Dn", "Up", "Emp", "Up", "Up", "Emp", "Dn", "Dn"] - psi0 = randomMPS(sites, state; linkdims=10) + psi0 = random_mps(sites, state; linkdims=10) energy, psi = dmrg(H, psi0, sweeps; outputlevel=0) @test (-8.02 < energy < -8.01) end @@ -453,7 +453,7 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range maxdim!(sweeps, 10) cutoff!(sweeps, 1E-11) - psi0 = randomMPS(sites; linkdims=4) + psi0 = random_mps(sites; linkdims=4) # Test that input works with wrong ortho center: orthogonalize!(psi0, 5) diff --git a/src/lib/ITensorMPS/test/base/test_inference.jl b/src/lib/ITensorMPS/test/base/test_inference.jl index 312e2a5021..c408db0b58 100644 --- a/src/lib/ITensorMPS/test/base/test_inference.jl +++ b/src/lib/ITensorMPS/test/base/test_inference.jl @@ -12,7 +12,7 @@ using Test opsum += 0.5, "S-", j, "S+", j + 1 end H = MPO(opsum, sites) - psi0 = randomMPS(sites; linkdims=10) + psi0 = random_mps(sites; linkdims=10) sweeps = Sweeps(5) setmaxdim!(sweeps, 10, 20, 100, 100, 200) setcutoff!(sweeps, 1E-11) diff --git a/src/lib/ITensorMPS/test/base/test_mpo.jl b/src/lib/ITensorMPS/test/base/test_mpo.jl index 07f22f969f..21408b05a0 100644 --- a/src/lib/ITensorMPS/test/base/test_mpo.jl +++ b/src/lib/ITensorMPS/test/base/test_mpo.jl @@ -52,7 +52,7 @@ end @test ITensors.data(MPO(copy(ITensors.data(K)))) == ITensors.data(K) @testset "orthogonalize!" begin - phi = randomMPS(sites) + phi = random_mps(sites) K = randomMPO(sites) orthogonalize!(phi, 1) orthogonalize!(K, 1) @@ -95,10 +95,10 @@ end end @testset "inner " begin - phi = randomMPS(sites) + phi = random_mps(sites) K = randomMPO(sites) @test maxlinkdim(K) == 1 - psi = randomMPS(sites) + psi = random_mps(sites) phidag = dag(phi) prime!(phidag) phiKpsi = phidag[1] * K[1] * psi[1] @@ -108,7 +108,7 @@ end @test phiKpsi[] ≈ inner(phi', K, psi) badsites = [Index(2, "Site") for n in 1:(N + 1)] - badpsi = randomMPS(badsites) + badpsi = random_mps(badsites) @test_throws DimensionMismatch inner(phi', K, badpsi) # make bigger random MPO... @@ -154,8 +154,8 @@ end c = 2 s = siteinds("S=1/2", n) - ψ = c .* randomMPS(s; linkdims=4) - Φ = c .* randomMPS(s; linkdims=4) + ψ = c .* random_mps(s; linkdims=4) + Φ = c .* random_mps(s; linkdims=4) K = randomMPO(s) @test log(complex(inner(ψ', K, Φ))) ≈ loginner(ψ', K, Φ) @@ -186,7 +186,7 @@ end @test phiJdagKpsi[] ≈ inner(J, phi, K, psi) badsites = [Index(2, "Site") for n in 1:(N + 1)] - badpsi = randomMPS(badsites) + badpsi = random_mps(badsites) @test_throws DimensionMismatch inner(J, phi, K, badpsi) # generic tags and prime levels @@ -228,7 +228,7 @@ end @test dist ≈ error_contract(phi, K, psi) badsites = [Index(2, "Site") for n in 1:(N + 1)] - badpsi = randomMPS(badsites) + badpsi = random_mps(badsites) # Apply K to phi and check that error_contract is close to 0. Kphi = contract(K, phi; method="naive", cutoff=1E-8) @test error_contract(noprime(Kphi), K, phi) ≈ 0.0 atol = 1e-4 @@ -239,10 +239,10 @@ end end @testset "contract" begin - phi = randomMPS(sites) + phi = random_mps(sites) K = randomMPO(sites) @test maxlinkdim(K) == 1 - psi = randomMPS(sites) + psi = random_mps(sites) psi_out = contract(K, psi; maxdim=1) @test inner(phi', psi_out) ≈ inner(phi', K, psi) psi_out = contract(psi, K; maxdim=1) @@ -252,7 +252,7 @@ end @test_throws MethodError contract(K, psi; method="fakemethod") badsites = [Index(2, "Site") for n in 1:(N + 1)] - badpsi = randomMPS(badsites) + badpsi = random_mps(badsites) @test_throws DimensionMismatch contract(K, badpsi) # make bigger random MPO... @@ -294,7 +294,7 @@ end L = randomMPO(shsites) M = add(K, L) @test length(M) == N - psi = randomMPS(shsites) + psi = random_mps(shsites) k_psi = contract(K, psi; maxdim=1) l_psi = contract(L, psi; maxdim=1) @test inner(psi', k_psi + l_psi) ≈ ⋅(psi', M, psi) atol = 5e-3 @@ -305,12 +305,12 @@ end L = basicRandomMPO(shsites; dim=dim) M = K + L @test length(M) == N - psi = randomMPS(shsites) + psi = random_mps(shsites) k_psi = contract(K, psi) l_psi = contract(L, psi) @test inner(psi', k_psi + l_psi) ≈ dot(psi', M, psi) atol = 5e-3 @test inner(psi', sum([k_psi, l_psi])) ≈ inner(psi', M, psi) atol = 5e-3 - psi = randomMPS(shsites) + psi = random_mps(shsites) M = add(K, L; cutoff=1E-9) k_psi = contract(K, psi) l_psi = contract(L, psi) @@ -351,7 +351,7 @@ end end @testset "contract(::MPO, ::MPO)" begin - psi = randomMPS(sites) + psi = random_mps(sites) K = randomMPO(sites) L = randomMPO(sites) @test maxlinkdim(K) == 1 @@ -370,8 +370,8 @@ end replaceind!(L[ii], sites[ii]', othersitesl[ii]) end KL = contract(K, L; maxdim=1) - psik = randomMPS(othersitesk) - psil = randomMPS(othersitesl) + psik = random_mps(othersitesk) + psil = random_mps(othersitesl) psi_kl_out = contract(K, contract(L, psil; maxdim=1); maxdim=1) @test inner(psik, KL, psil) ≈ inner(psik, psi_kl_out) atol = 5e-3 @@ -381,7 +381,7 @@ end end @testset "*(::MPO, ::MPO)" begin - psi = randomMPS(sites) + psi = random_mps(sites) K = randomMPO(sites) L = randomMPO(sites) @test maxlinkdim(K) == 1 @@ -406,8 +406,8 @@ end replaceind!(L[ii], sites[ii]', othersitesl[ii]) end KL = *(K, L; maxdim=1) - psik = randomMPS(othersitesk) - psil = randomMPS(othersitesl) + psik = random_mps(othersitesk) + psil = random_mps(othersitesl) psi_kl_out = *(K, *(L, psil; maxdim=1); maxdim=1) @test dot(psik, KL, psil) ≈ psik ⋅ psi_kl_out atol = 5e-3 @@ -417,9 +417,9 @@ end end @testset "Multi-arg apply(::MPO...)" begin - ρ1 = (x -> outer(x', x; maxdim=4))(randomMPS(sites; linkdims=2)) - ρ2 = (x -> outer(x', x; maxdim=4))(randomMPS(sites; linkdims=2)) - ρ3 = (x -> outer(x', x; maxdim=4))(randomMPS(sites; linkdims=2)) + ρ1 = (x -> outer(x', x; maxdim=4))(random_mps(sites; linkdims=2)) + ρ2 = (x -> outer(x', x; maxdim=4))(random_mps(sites; linkdims=2)) + ρ3 = (x -> outer(x', x; maxdim=4))(random_mps(sites; linkdims=2)) @test apply(ρ1, ρ2, ρ3; cutoff=1e-8) ≈ apply(apply(ρ1, ρ2; cutoff=1e-8), ρ3; cutoff=1e-8) end @@ -610,9 +610,9 @@ end s = siteinds("S=1/2", N; conserve_qns=true) state(n) = isodd(n) ? "Up" : "Dn" χψ = 3 - ψ = randomMPS(ComplexF64, s, state; linkdims=χψ) + ψ = random_mps(ComplexF64, s, state; linkdims=χψ) χϕ = 4 - ϕ = randomMPS(ComplexF64, s, state; linkdims=χϕ) + ϕ = random_mps(ComplexF64, s, state; linkdims=χϕ) ψ[only(ortho_lims(ψ))] *= 2 @@ -661,8 +661,8 @@ end N = 4 s1 = siteinds("S=1/2", N) s2 = siteinds("S=1/2", N) - psi1 = randomMPS(s1) - psi2 = randomMPS(s2) + psi1 = random_mps(s1) + psi2 = random_mps(s2) H1 = MPO(OpSum() + ("Id", 1), s1) H2 = MPO(OpSum() + ("Id", 1), s2) @@ -748,7 +748,7 @@ end M end - psi = randomMPS(s; linkdims=chi2) + psi = random_mps(s; linkdims=chi2) Apsi = contract(A, psi) @@ -793,7 +793,7 @@ end @test linkdims(H²) == fill(1, length(s) - 1) @test H² ≈ H̃² - e, ψ = dmrg(H, randomMPS(s, n -> isodd(n) ? "↑" : "↓"); nsweeps=2, outputlevel=0) + e, ψ = dmrg(H, random_mps(s, n -> isodd(n) ? "↑" : "↓"); nsweeps=2, outputlevel=0) @test e ≈ 1 end @@ -809,7 +809,7 @@ end sites = [Index(2, "Site,n=$n") for n in 1:N] seed = 623 mt = MersenneTwister(seed) - K = randomMPS(mt, sites) + K = random_mps(mt, sites) L = MPO(K) result = sample(mt, L) @test result ≈ [1, 2, 1, 1, 2, 2] diff --git a/src/lib/ITensorMPS/test/base/test_mps.jl b/src/lib/ITensorMPS/test/base/test_mps.jl index 7962a90a26..1e59a0b86e 100644 --- a/src/lib/ITensorMPS/test/base/test_mps.jl +++ b/src/lib/ITensorMPS/test/base/test_mps.jl @@ -183,9 +183,9 @@ include(joinpath(@__DIR__, "utils", "util.jl")) end end - @testset "randomMPS with chi==1" begin - phi = randomMPS(sites) - phic = randomMPS(ComplexF64, sites) + @testset "random_mps with chi==1" begin + phi = random_mps(sites) + phic = random_mps(ComplexF64, sites) @test maxlinkdim(phi) == 1 @test maxlinkdim(phic) == 1 @@ -199,34 +199,34 @@ include(joinpath(@__DIR__, "utils", "util.jl")) @test norm(phic[4]) ≈ 1.0 end - @testset "randomMPS with chi>1" for linkdims in [1, 4] - phi = randomMPS(Float32, sites; linkdims) + @testset "random_mps with chi>1" for linkdims in [1, 4] + phi = random_mps(Float32, sites; linkdims) @test LinearAlgebra.promote_leaf_eltypes(phi) === Float32 @test all(x -> eltype(x) === Float32, phi) @test maxlinkdim(phi) == linkdims - phic = randomMPS(ComplexF32, sites; linkdims) + phic = random_mps(ComplexF32, sites; linkdims) @test LinearAlgebra.promote_leaf_eltypes(phic) === ComplexF32 @test maxlinkdim(phic) == linkdims @test all(x -> eltype(x) === ComplexF32, phic) end - @testset "randomMPS with nonuniform dimensions" begin + @testset "random_mps with nonuniform dimensions" begin _linkdims = [2, 3, 4, 2, 4, 3, 2, 2, 2] - phi = randomMPS(sites; linkdims=_linkdims) + phi = random_mps(sites; linkdims=_linkdims) @test linkdims(phi) == _linkdims end - @testset "QN randomMPS" begin + @testset "QN random_mps" begin s = siteinds("S=1/2", 5; conserve_qns=true) - ψ = randomMPS(s, n -> isodd(n) ? "↑" : "↓"; linkdims=2) + ψ = random_mps(s, n -> isodd(n) ? "↑" : "↓"; linkdims=2) @test linkdims(ψ) == [2, 2, 2, 2] - ψ = randomMPS(s, n -> isodd(n) ? "↑" : "↓"; linkdims=[2, 3, 2, 2]) + ψ = random_mps(s, n -> isodd(n) ? "↑" : "↓"; linkdims=[2, 3, 2, 2]) @test linkdims(ψ) == [2, 3, 2, 2] end @testset "inner different MPS" begin - phi = randomMPS(sites) - psi = randomMPS(sites) + phi = random_mps(sites) + psi = random_mps(sites) phipsi = dag(phi[1]) * psi[1] for j in 2:length(psi) phipsi *= dag(phi[j]) * psi[j] @@ -234,7 +234,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) @test phipsi[] ≈ inner(phi, psi) badsites = [Index(2) for n in 1:(length(psi) + 1)] - badpsi = randomMPS(badsites) + badpsi = random_mps(badsites) @test_throws DimensionMismatch inner(phi, badpsi) end @@ -243,7 +243,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) c = 2 s = siteinds("S=1/2", n) - ψ = c .* randomMPS(s; linkdims=4) + ψ = c .* random_mps(s; linkdims=4) @test exp(loginner(ψ, ψ)) ≈ c^(2n) @test exp(loginner(ψ, -ψ)) ≈ -c^(2n) @@ -252,7 +252,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) end @testset "broadcasting" begin - psi = randomMPS(sites) + psi = random_mps(sites) orthogonalize!(psi, 1) @test ortho_lims(psi) == 1:1 @test dim.(psi) == fill(2, length(psi)) @@ -273,7 +273,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) @testset "copy and deepcopy" begin s = siteinds("S=1/2", 3) - M1 = randomMPS(s; linkdims=3) + M1 = random_mps(s; linkdims=3) @test norm(M1) ≈ 1 M2 = deepcopy(M1) @@ -293,7 +293,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) end @testset "inner same MPS" begin - psi = randomMPS(sites) + psi = random_mps(sites) psidag = dag(psi) #ITensors.prime_linkinds!(psidag) psipsi = psidag[1] * psi[1] @@ -306,7 +306,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) @testset "norm MPS (eltype=$elt)" for elt in ( Float32, Float64, Complex{Float32}, Complex{Float64} ) - psi = randomMPS(elt, sites; linkdims=10) + psi = random_mps(elt, sites; linkdims=10) psidag = sim(linkinds, dag(psi)) psi² = ITensor(1) for j in 1:length(psi) @@ -315,12 +315,12 @@ include(joinpath(@__DIR__, "utils", "util.jl")) @test psi²[] ≈ psi ⋅ psi @test sqrt(psi²[]) ≈ norm(psi) - psi = randomMPS(elt, sites; linkdims=10) + psi = random_mps(elt, sites; linkdims=10) psi .*= 1:length(psi) @test norm(psi) ≈ factorial(length(psi)) @test norm(psi) isa real(elt) - psi = randomMPS(elt, sites; linkdims=10) + psi = random_mps(elt, sites; linkdims=10) for j in 1:length(psi) psi[j] .*= j end @@ -330,7 +330,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) reset_ortho_lims!(psi) @test norm(psi) ≈ factorial(length(psi)) - psi = randomMPS(elt, sites; linkdims=10) + psi = random_mps(elt, sites; linkdims=10) norm_psi = norm(psi) @test norm_psi ≈ 1 @@ -358,13 +358,13 @@ include(joinpath(@__DIR__, "utils", "util.jl")) # up to a certain tolerance Random.seed!(1234) s = siteinds("S=1/2", 10) - ψ = randomMPS(ComplexF64, s; linkdims=4) + ψ = random_mps(ComplexF64, s; linkdims=4) reset_ortho_lims!(ψ) @test exp(lognorm(ψ)) ≈ 1 end @testset "normalize/normalize! MPS" begin - psi = randomMPS(sites; linkdims=10) + psi = random_mps(sites; linkdims=10) @test norm(psi) ≈ 1 @test norm(normalize(psi)) ≈ 1 @@ -394,7 +394,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) @test norm(normalize(0phi)) == 0 # Large number of sites - psi = randomMPS(siteinds("S=1/2", 1_000); linkdims=10) + psi = random_mps(siteinds("S=1/2", 1_000); linkdims=10) @test norm(psi) ≈ 1.0 @test lognorm(psi) ≈ 0.0 atol = 1e-15 @@ -410,7 +410,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) @test norm(phi) ≈ 1 # Test scaling only a subset of sites - psi = randomMPS(siteinds("S=1/2", 10); linkdims=10) + psi = random_mps(siteinds("S=1/2", 10); linkdims=10) @test norm(psi) ≈ 1.0 @test lognorm(psi) ≈ 0.0 atol = 1e-15 @@ -431,7 +431,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) # Output the lognorm α = 2 - psi = randomMPS(siteinds("S=1/2", 30); linkdims=10) + psi = random_mps(siteinds("S=1/2", 30); linkdims=10) psi = α .* psi @test norm(psi) ≈ α^length(psi) @test lognorm(psi) ≈ length(psi) * log(α) @@ -443,7 +443,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) end @testset "lognorm MPS" begin - psi = randomMPS(sites; linkdims=10) + psi = random_mps(sites; linkdims=10) for j in eachindex(psi) psi[j] .*= j end @@ -463,26 +463,26 @@ include(joinpath(@__DIR__, "utils", "util.jl")) end @testset "scaling MPS" begin - psi = randomMPS(sites; linkdims=4) + psi = random_mps(sites; linkdims=4) twopsidag = 2.0 * dag(psi) #ITensors.prime_linkinds!(twopsidag) @test inner(twopsidag, psi) ≈ 2.0 * inner(psi, psi) end @testset "flip sign of MPS" begin - psi = randomMPS(sites) + psi = random_mps(sites) minuspsidag = -dag(psi) #ITensors.primelinkinds!(minuspsidag) @test inner(minuspsidag, psi) ≈ -inner(psi, psi) end @testset "add MPS" begin - psi = randomMPS(sites) + psi = random_mps(sites) phi = deepcopy(psi) xi = add(psi, phi) @test inner(xi, xi) ≈ 4.0 * inner(psi, psi) # sum of many MPSs - Ks = [randomMPS(sites) for i in 1:3] + Ks = [random_mps(sites) for i in 1:3] K12 = add(Ks[1], Ks[2]) K123 = add(K12, Ks[3]) @test inner(sum(Ks), K123) ≈ inner(K123, K123) @@ -491,20 +491,20 @@ include(joinpath(@__DIR__, "utils", "util.jl")) end @testset "+ MPS" begin - psi = randomMPS(sites) + psi = random_mps(sites) phi = deepcopy(psi) xi = psi + phi @test inner(xi, xi) ≈ 4.0 * inner(psi, psi) # sum of many MPSs - Ks = [randomMPS(sites) for i in 1:3] + Ks = [random_mps(sites) for i in 1:3] K12 = Ks[1] + Ks[2] K123 = K12 + Ks[3] @test inner(sum(Ks), K123) ≈ inner(K123, K123) χ1 = 2 χ2 = 3 - ψ1 = randomMPS(sites; linkdims=χ1) - ψ2 = 0.0 * randomMPS(sites; linkdims=χ2) + ψ1 = random_mps(sites; linkdims=χ1) + ψ2 = 0.0 * random_mps(sites; linkdims=χ2) ϕ1 = +(ψ1, ψ2; alg="densitymatrix", cutoff=nothing) for j in 2:7 @@ -527,9 +527,9 @@ include(joinpath(@__DIR__, "utils", "util.jl")) s = siteinds("S=1/2", 20; conserve_qns=conserve_qns) state = n -> isodd(n) ? "↑" : "↓" - ψ₁ = randomMPS(s, state; linkdims=4) - ψ₂ = randomMPS(s, state; linkdims=4) - ψ₃ = randomMPS(s, state; linkdims=4) + ψ₁ = random_mps(s, state; linkdims=4) + ψ₂ = random_mps(s, state; linkdims=4) + ψ₃ = random_mps(s, state; linkdims=4) ψ = ψ₁ + ψ₂ @@ -580,7 +580,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) @test length(psi) == 10 # just make sure this works @test length(siteinds(psi)) == length(psi) - psi = randomMPS(sites) + psi = random_mps(sites) l0s = linkinds(psi) orthogonalize!(psi, length(psi) - 1) ls = linkinds(psi) @@ -592,7 +592,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) orthogonalize!(psi, 2) @test ITensorMPS.leftlim(psi) == 1 @test ITensorMPS.rightlim(psi) == 3 - psi = randomMPS(sites) + psi = random_mps(sites) ITensorMPS.setrightlim!(psi, length(psi) + 1) # do this to test qr # from rightmost tensor orthogonalize!(psi, div(length(psi), 2)) @@ -603,7 +603,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) @testset "replacebond!" begin # make sure factorization preserves the bond index tags - psi = randomMPS(sites) + psi = random_mps(sites) phi = psi[1] * psi[2] bondindtags = tags(linkind(psi, 1)) replacebond!(psi, 1, phi) @@ -629,7 +629,7 @@ include(joinpath(@__DIR__, "utils", "util.jl")) @test ITensorMPS.rightlim(psi) == 7 # check that replacebond! runs with svd kwargs - psi = randomMPS(sites) + psi = random_mps(sites) phi = psi[1] * psi[2] replacebond!(psi, 1, phi; ortho="left", which_decomp="svd", use_relative_cutoff=true) phi = psi[5] * psi[6] @@ -749,7 +749,7 @@ end @testset "Other MPS methods" begin @testset "sample! method" begin sites = [Index(3, "Site,n=$n") for n in 1:10] - psi = randomMPS(sites; linkdims=3) + psi = random_mps(sites; linkdims=3) nrm2 = inner(psi, psi) psi[1] *= (1.0 / sqrt(nrm2)) @@ -776,10 +776,10 @@ end @test length(s) == length(psi) end - @testset "randomMPS with chi > 1" begin + @testset "random_mps with chi > 1" begin chi = 8 sites = siteinds(2, 20) - M = randomMPS(sites; linkdims=chi) + M = random_mps(sites; linkdims=chi) @test ITensorMPS.leftlim(M) == 0 @test ITensorMPS.rightlim(M) == 2 @@ -799,17 +799,17 @@ end end # Complex case - Mc = randomMPS(sites; linkdims=chi) + Mc = random_mps(sites; linkdims=chi) @test inner(Mc, Mc) ≈ 1.0 + 0.0im end - @testset "randomMPS from initial state (QN case)" begin + @testset "random_mps from initial state (QN case)" begin chi = 8 sites = siteinds("S=1/2", 20; conserve_qns=true) # Make flux-zero random MPS state = [isodd(n) ? 1 : 2 for n in 1:length(sites)] - M = randomMPS(sites, state; linkdims=chi) + M = random_mps(sites, state; linkdims=chi) @test flux(M) == QN("Sz", 0) @test ITensorMPS.leftlim(M) == 0 @@ -822,17 +822,17 @@ end # Test making random MPS with different flux state[1] = 2 - M = randomMPS(sites, state; linkdims=chi) + M = random_mps(sites, state; linkdims=chi) @test flux(M) == QN("Sz", -2) state[3] = 2 - M = randomMPS(sites, state; linkdims=chi) + M = random_mps(sites, state; linkdims=chi) @test flux(M) == QN("Sz", -4) end @testset "expect Function" begin N = 8 s = siteinds("S=1/2", N) - psi = randomMPS(ComplexF64, s; linkdims=2) + psi = random_mps(ComplexF64, s; linkdims=2) eSz = zeros(N) eSx = zeros(N) @@ -891,7 +891,7 @@ end # Non-fermionic real case - spin system with QNs (very restrictive on allowed ops) s = siteinds("S=1/2", 4; conserve_qns=true) - psi = randomMPS(s, n -> isodd(n) ? "Up" : "Dn"; linkdims=m) + psi = random_mps(s, n -> isodd(n) ? "Up" : "Dn"; linkdims=m) test_correlation_matrix(psi, [("S-", "S+"), ("S+", "S-")]) @test correlation_matrix(psi, [1/2 0; 0 -1/2], [1/2 0; 0 -1/2]) ≈ @@ -901,7 +901,7 @@ end @test expect(psi, [[1/2 0; 0 -1/2], [1/2 0; 0 -1/2]]) ≈ expect(psi, ["Sz", "Sz"]) s = siteinds("S=1/2", length(s); conserve_qns=false) - psi = randomMPS(s, n -> isodd(n) ? "Up" : "Dn"; linkdims=m) + psi = random_mps(s, n -> isodd(n) ? "Up" : "Dn"; linkdims=m) test_correlation_matrix( psi, [ @@ -927,7 +927,7 @@ end #Test sites feature s = siteinds("S=1/2", 8; conserve_qns=false) - psi = randomMPS(s, n -> isodd(n) ? "Up" : "Dn"; linkdims=m) + psi = random_mps(s, n -> isodd(n) ? "Up" : "Dn"; linkdims=m) PM = expect(psi, "S+ * S-") Cpm = correlation_matrix(psi, "S+", "S-") range = 3:7 @@ -938,7 +938,7 @@ end # With start_site, end_site arguments: s = siteinds("S=1/2", 8) - psi = randomMPS(ComplexF64, s; linkdims=m) + psi = random_mps(ComplexF64, s; linkdims=m) ss, es = 3, 6 Nb = es - ss + 1 Cpm = correlation_matrix(psi, "S+", "S-"; sites=ss:es) @@ -953,13 +953,13 @@ end # Electron case s = siteinds("Electron", 8) - psi = randomMPS(s; linkdims=m) + psi = random_mps(s; linkdims=m) test_correlation_matrix( psi, [("Cdagup", "Cup"), ("Cup", "Cdagup"), ("Cup", "Cdn"), ("Cdagdn", "Cdn")] ) s = siteinds("Electron", 8; conserve_qns=false) - psi = randomMPS(s; linkdims=m) + psi = random_mps(s; linkdims=m) test_correlation_matrix( psi, [ @@ -990,11 +990,11 @@ end # Fermion case s = siteinds("Fermion", 8) - psi = randomMPS(s; linkdims=m) + psi = random_mps(s; linkdims=m) test_correlation_matrix(psi, [("N", "N"), ("Cdag", "C"), ("C", "Cdag"), ("C", "C")]) s = siteinds("Fermion", 8; conserve_qns=false) - psi = randomMPS(s; linkdims=m) + psi = random_mps(s; linkdims=m) test_correlation_matrix(psi, [("N", "N"), ("Cdag", "C"), ("C", "Cdag"), ("C", "C")]) # @@ -1044,7 +1044,7 @@ end @testset "expect regression test for in-place modification of input MPS" begin s = siteinds("S=1/2", 5) - psi = randomMPS(s; linkdims=3) + psi = random_mps(s; linkdims=3) orthogonalize!(psi, 1) expect_init = expect(psi, "Sz") norm_scale = 10 @@ -1060,7 +1060,7 @@ end @testset "correlation_matrix regression test for in-place modification of input MPS" begin s = siteinds("S=1/2", 5) - psi = randomMPS(s; linkdims=3) + psi = random_mps(s; linkdims=3) orthogonalize!(psi, 1) correlation_matrix_init = correlation_matrix(psi, "Sz", "Sz") norm_scale = 10 @@ -1076,7 +1076,7 @@ end @testset "swapbondsites" begin sites = siteinds("S=1/2", 5) - ψ0 = randomMPS(sites) + ψ0 = random_mps(sites) ψ = replacebond(ψ0, 3, ψ0[3] * ψ0[4]; swapsites=true, cutoff=1e-15) @test siteind(ψ, 1) == siteind(ψ0, 1) @test siteind(ψ, 2) == siteind(ψ0, 2) @@ -1131,14 +1131,14 @@ end @testset "setindex!(::MPS, _, ::Colon)" begin s = siteinds("S=½", 4) - ψ = randomMPS(s) + ψ = random_mps(s) ϕ = MPS(s, "↑") orthogonalize!(ϕ, 1) ψ[:] = ϕ @test ITensorMPS.orthocenter(ψ) == 1 @test inner(ψ, ϕ) ≈ 1 - ψ = randomMPS(s) + ψ = random_mps(s) ϕ = MPS(s, "↑") orthogonalize!(ϕ, 1) ψ[:] = ITensorMPS.data(ϕ) @@ -1149,7 +1149,7 @@ end @testset "findsite[s](::MPS/MPO, is)" begin s = siteinds("S=1/2", 5) - ψ = randomMPS(s) + ψ = random_mps(s) l = linkinds(ψ) A = randomITensor(s[4]', s[2]', dag(s[4]), dag(s[2])) @@ -1185,7 +1185,7 @@ end @testset "[first]siteind[s](::MPS/MPO, j::Int)" begin s = siteinds("S=1/2", 5) - ψ = randomMPS(s) + ψ = random_mps(s) @test siteind(first, ψ, 3) == s[3] @test siteind(ψ, 4) == s[4] @test isnothing(siteind(ψ, 4; plev=1)) @@ -1249,7 +1249,7 @@ end @test ITensorMPS.orthocenter(ψ) == N @test maxlinkdim(ψ) == 1 - ψ0 = randomMPS(s; linkdims=2) + ψ0 = random_mps(s; linkdims=2) A = prod(ψ0) ψ = MPS(A, s; cutoff=1e-15, orthocenter=2) @test prod(ψ) ≈ A @@ -1289,7 +1289,7 @@ end @testset "Set range of MPS tensors" begin N = 5 s = siteinds("S=1/2", N) - ψ0 = randomMPS(s; linkdims=3) + ψ0 = random_mps(s; linkdims=3) ψ = orthogonalize(ψ0, 2) A = prod(ITensorMPS.data(ψ)[2:(N - 1)]) @@ -1317,7 +1317,7 @@ end @testset "movesites reverse sites" begin N = 6 s = siteinds("S=1/2", N) - ψ0 = randomMPS(s) + ψ0 = random_mps(s) ψ = movesites(ψ0, 1:N .=> reverse(1:N)) for n in 1:N @test siteind(ψ, n) == s[N - n + 1] @@ -1327,7 +1327,7 @@ end @testset "movesites subsets of sites" begin N = 6 s = siteinds("S=1/2", N) - ψ = randomMPS(s) + ψ = random_mps(s) for i in 1:N, j in 1:N ns = [i, j] @@ -1881,7 +1881,7 @@ end @testset "Spinful Fermions (Electron) gate evolution" begin N = 8 s = siteinds("Electron", N; conserve_qns=true) - ψ0 = randomMPS(s, n -> isodd(n) ? "↑" : "↓") + ψ0 = random_mps(s, n -> isodd(n) ? "↑" : "↓") t = 1.0 U = 1.0 opsum = OpSum() @@ -1919,7 +1919,7 @@ end @testset "dense conversion of MPS" begin N = 4 s = siteinds("S=1/2", N; conserve_qns=true) - QM = randomMPS(s, ["Up", "Dn", "Up", "Dn"]; linkdims=4) + QM = random_mps(s, ["Up", "Dn", "Up", "Dn"]; linkdims=4) qsz1 = scalar(QM[1] * op("Sz", s[1]) * dag(prime(QM[1], "Site"))) M = dense(QM) @@ -1935,7 +1935,7 @@ end sinds = IndexSet.(sout, sin) Cs = combiner.(sinds) cinds = combinedind.(Cs) - ψ = randomMPS(cinds) + ψ = random_mps(cinds) @test norm(ψ) ≈ 1 @test inner(ψ, ψ) ≈ 1 ψ .*= dag.(Cs) @@ -1953,7 +1953,7 @@ end a .+= "Sz", j, "Sz", j + 1 end H = MPO(a, s) - ψ = randomMPS(s, n -> isodd(n) ? "↑" : "↓"; linkdims=10) + ψ = random_mps(s, n -> isodd(n) ? "↑" : "↓"; linkdims=10) # Create MPO/MPS with pairs of sites merged H2 = MPO([H[b] * H[b + 1] for b in 1:2:N]) ψ2 = MPS([ψ[b] * ψ[b + 1] for b in 1:2:N]) diff --git a/src/lib/ITensorMPS/test/base/test_qnmpo.jl b/src/lib/ITensorMPS/test/base/test_qnmpo.jl index 1f417d5db8..ae72519499 100644 --- a/src/lib/ITensorMPS/test/base/test_qnmpo.jl +++ b/src/lib/ITensorMPS/test/base/test_qnmpo.jl @@ -107,7 +107,7 @@ end @test phiJdagKpsi[] ≈ inner(J, phi, K, psi) badsites = [Index(2, "Site") for n in 1:(N + 1)] - badpsi = randomMPS(badsites) + badpsi = random_mps(badsites) @test_throws DimensionMismatch inner(J, phi, K, badpsi) end @@ -140,12 +140,12 @@ end # L = basicRandomMPO(N, shsites; dim=dim) # M = K + L # @test length(M) == N - # psi = randomMPS(shsites) + # psi = random_mps(shsites) # k_psi = contract(K, psi) # l_psi = contract(L, psi) # @test inner(psi, k_psi + l_psi) ≈ dot(psi, M, psi) atol=5e-3 # @test inner(psi, sum([k_psi, l_psi])) ≈ inner(psi, M, psi) atol=5e-3 - # psi = randomMPS(shsites) + # psi = random_mps(shsites) # M = add(K, L; cutoff=1E-9) # k_psi = contract(K, psi) # l_psi = contract(L, psi) @@ -257,9 +257,9 @@ end end H = MPO(a, s) if conserve_szparity - ψ = randomMPS(s, n -> isodd(n) ? "↑" : "↓") + ψ = random_mps(s, n -> isodd(n) ? "↑" : "↓") else - ψ = randomMPS(s) + ψ = random_mps(s) end # MPO * MPS diff --git a/src/lib/ITensorMPS/test/base/test_readme.jl b/src/lib/ITensorMPS/test/base/test_readme.jl index 414a8d4872..164e4367ac 100644 --- a/src/lib/ITensorMPS/test/base/test_readme.jl +++ b/src/lib/ITensorMPS/test/base/test_readme.jl @@ -85,7 +85,7 @@ using ITensors, Test H = MPO(opsum, sites) # Create an initial random matrix product state - psi0 = randomMPS(sites) + psi0 = random_mps(sites) sweeps = Sweeps(2) maxdim!(sweeps, 10, 20, 100, 100, 200) diff --git a/src/lib/ITensorMPS/test/base/utils/util.jl b/src/lib/ITensorMPS/test/base/utils/util.jl index 3ba280fa8c..f6d44a747e 100644 --- a/src/lib/ITensorMPS/test/base/utils/util.jl +++ b/src/lib/ITensorMPS/test/base/utils/util.jl @@ -17,7 +17,7 @@ end inner_add(ψ⃗...) = inner_add(fill_trivial_coefficients.(ψ⃗)...) -# TODO: this is no longer needed, use randomMPS +# TODO: this is no longer needed, use random_mps function makeRandomMPS(sites; chi::Int=4)::MPS N = length(sites) v = Vector{ITensor}(undef, N) diff --git a/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_chainrules.jl b/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_chainrules.jl index 193c3d8121..2720036447 100644 --- a/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_chainrules.jl +++ b/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_chainrules.jl @@ -10,7 +10,7 @@ Random.seed!(1234) # https://github.com/ITensor/ITensors.jl/issues/936 n = 2 s = siteinds("S=1/2", n) - x = (x -> outer(x', x))(randomMPS(s)) + x = (x -> outer(x', x))(random_mps(s)) f1 = x -> tr(x) f2 = x -> 2tr(x) f3 = x -> -tr(x) @@ -34,7 +34,7 @@ Random.seed!(1234) return os end H = MPO(heisenberg(n), s) - ψ = randomMPS(s, n -> isodd(n) ? "Up" : "Dn"; linkdims=2) + ψ = random_mps(s, n -> isodd(n) ? "Up" : "Dn"; linkdims=2) f = x -> inner(x, x) args = (ψ,) @@ -56,8 +56,8 @@ Random.seed!(1234) # apply on MPS s = siteinds("S=1/2", n) - ϕ = randomMPS(ElType, s) - ψ = randomMPS(ElType, s) + ϕ = random_mps(ElType, s) + ψ = random_mps(ElType, s) f = function (x) U = [op("Ry", s[2]; θ=x), op("CX", s[1], s[2]), op("Rx", s[3]; θ=x)] ψθ = apply(U, ψ) @@ -72,7 +72,7 @@ Random.seed!(1234) @testset "MPS rrules" begin Random.seed!(1234) s = siteinds("S=1/2", 4) - ψ = randomMPS(s) + ψ = random_mps(s) args = (ψ,) f = x -> inner(x, x) # TODO: Need to make MPS type compatible with FiniteDifferences. @@ -87,9 +87,9 @@ Random.seed!(1234) d_args = gradient(f, args...) @test norm(d_args[1] - 2 * args[1]) ≈ 0 atol = 1e-13 - ψ = randomMPS(ComplexF64, s) + ψ = random_mps(ComplexF64, s) ψtensors = ITensors.data(ψ) - ϕ = randomMPS(ComplexF64, s) + ϕ = random_mps(ComplexF64, s) f = function (x) ψ̃tensors = [x^j * ψtensors[j] for j in 1:length(ψtensors)] ψ̃ = MPS(ψ̃tensors) @@ -114,7 +114,7 @@ Random.seed!(1234) # # #ρ = randomMPO(s) # #ρtensors = ITensors.data(ρ) - # #ϕ = randomMPS(ComplexF64, s) + # #ϕ = random_mps(ComplexF64, s) # #f = function (x) # # ρ̃tensors = [2 * x * ρtensors[1], log(x) * ρtensors[2]] # # ρ̃ = MPO(ρ̃tensors) @@ -156,7 +156,7 @@ Random.seed!(1234) end H = MPO(ising(n, 1.0), s) A = randomMPO(s) - ϕ = randomMPS(ComplexF64, s; linkdims=10) + ϕ = random_mps(ComplexF64, s; linkdims=10) # apply on mpo with apply_dag=true f = function (x) @@ -258,7 +258,7 @@ Random.seed!(1234) @testset "contract/apply MPOs" begin n = 2 s = siteinds("S=1/2", n) - x = (x -> outer(x', x))(randomMPS(s; linkdims=4)) + x = (x -> outer(x', x))(random_mps(s; linkdims=4)) x_itensor = contract(x) f = x -> tr(apply(x, x)) @@ -277,9 +277,9 @@ Random.seed!(1234) @testset "contract/apply MPOs on MPSs" begin n = 2 s = siteinds("S=1/2", n) - x = (x -> outer(x', x))(randomMPS(s; linkdims=4)) + x = (x -> outer(x', x))(random_mps(s; linkdims=4)) x_itensor = contract(x) - y = randomMPS(s; linkdims=4) + y = random_mps(s; linkdims=4) y_itensor = contract(y) f = x -> inner(apply(x, y), apply(x, y)) diff --git a/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_optimization.jl b/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_optimization.jl index f618c382b4..f5266065e5 100644 --- a/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_optimization.jl +++ b/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_optimization.jl @@ -17,7 +17,7 @@ include(joinpath(@__DIR__, "utils", "circuit.jl")) os .+= "Sz", n, "Sz", n + 1 end Hmpo = MPO(os, s) - ψ₀mps = randomMPS(s, n -> isodd(n) ? "↑" : "↓") + ψ₀mps = random_mps(s, n -> isodd(n) ? "↑" : "↓") H = prod(Hmpo) ψ₀ = prod(ψ₀mps) # The Rayleigh quotient to minimize @@ -51,7 +51,7 @@ include(joinpath(@__DIR__, "utils", "circuit.jl")) Hmpo = MPO(os, s) Random.seed!(1234) - ψ₀mps = randomMPS(s, n -> isodd(n) ? "↑" : "↓"; linkdims=χ) + ψ₀mps = random_mps(s, n -> isodd(n) ? "↑" : "↓"; linkdims=χ) H = ITensors.data(Hmpo) ψ₀ = ITensors.data(ψ₀mps) diff --git a/src/lib/ITensorsNamedDimsArraysExt/examples/example_dmrg.jl b/src/lib/ITensorsNamedDimsArraysExt/examples/example_dmrg.jl index d86ee586b8..3942e853b2 100644 --- a/src/lib/ITensorsNamedDimsArraysExt/examples/example_dmrg.jl +++ b/src/lib/ITensorsNamedDimsArraysExt/examples/example_dmrg.jl @@ -1,5 +1,5 @@ using Adapt: adapt -using ITensors: MPO, dmrg, randomMPS, siteinds +using ITensors: MPO, dmrg, random_mps, siteinds using ITensors.Ops: OpSum using ITensors.ITensorsNamedDimsArraysExt: to_nameddimsarray @@ -10,7 +10,7 @@ function main(; n, conserve_qns=false, nsweeps=3, cutoff=1e-4, arraytype=Array) ℋ = sum(j -> ("S-", j, "S+", j + 1), 1:(n - 1); init=ℋ) ℋ = sum(j -> ("Sz", j, "Sz", j + 1), 1:(n - 1); init=ℋ) H = MPO(ℋ, s) - ψ₀ = randomMPS(s, j -> isodd(j) ? "↑" : "↓") + ψ₀ = random_mps(s, j -> isodd(j) ? "↑" : "↓") H = adapt(arraytype, H) ψ = adapt(arraytype, ψ₀) diff --git a/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl b/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl index b5d989e817..f0e2d6fc3b 100644 --- a/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl +++ b/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl @@ -247,7 +247,7 @@ const ITENSORS_EXPORTED_NAMES = [ :randn!, :randomITensor, :randomMPO, - :randomMPS, + :random_mps, :readcpp, :removeqn, :removeqns, From fe5b0a7c8a5b833c9d01a6593d71395b37b695a8 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 16 May 2024 15:45:29 -0400 Subject: [PATCH 5/9] Rename randomMPO to random_mpo --- src/lib/ITensorMPS/src/deprecated.jl | 1 + src/lib/ITensorMPS/src/exports.jl | 2 +- src/lib/ITensorMPS/src/mpo.jl | 8 +-- src/lib/ITensorMPS/test/base/test_mpo.jl | 60 +++++++++---------- src/lib/ITensorMPS/test/base/test_mps.jl | 4 +- src/lib/ITensorMPS/test/base/test_qnmpo.jl | 2 +- .../test_chainrules.jl | 24 ++++---- .../TestITensorsExportedNames.jl | 2 +- 8 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/lib/ITensorMPS/src/deprecated.jl b/src/lib/ITensorMPS/src/deprecated.jl index 4b4b5f1480..ceedcc9d9c 100644 --- a/src/lib/ITensorMPS/src/deprecated.jl +++ b/src/lib/ITensorMPS/src/deprecated.jl @@ -19,6 +19,7 @@ # mps/mpo.jl @deprecate MPO(A::MPS; kwargs...) outer(A', A; kwargs...) +@deprecate randomMPO(args...; kwargs...) = random_mpo(args...; kwargs...) # mps/mps.jl @deprecate randomMPS(args...; kwargs...) = random_mps(args...; kwargs...) diff --git a/src/lib/ITensorMPS/src/exports.jl b/src/lib/ITensorMPS/src/exports.jl index d857091ad4..bf581e747e 100644 --- a/src/lib/ITensorMPS/src/exports.jl +++ b/src/lib/ITensorMPS/src/exports.jl @@ -42,7 +42,7 @@ export orthogonalize!, outer, projector, - randomMPO, + random_mpo, truncate, truncate!, unique_siteind, diff --git a/src/lib/ITensorMPS/src/mpo.jl b/src/lib/ITensorMPS/src/mpo.jl index eedde86d6d..cd4b076fc8 100644 --- a/src/lib/ITensorMPS/src/mpo.jl +++ b/src/lib/ITensorMPS/src/mpo.jl @@ -123,17 +123,17 @@ end MPO(sites::Vector{<:Index}, op::Matrix{ElT}) where {ElT<:Number} = MPO(ElT, sites, op) -function randomMPO(sites::Vector{<:Index}, m::Int=1) - return randomMPO(Random.default_rng(), sites, m) +function random_mpo(sites::Vector{<:Index}, m::Int=1) + return random_mpo(Random.default_rng(), sites, m) end -function randomMPO(rng::AbstractRNG, sites::Vector{<:Index}, m::Int=1) +function random_mpo(rng::AbstractRNG, sites::Vector{<:Index}, m::Int=1) M = MPO(sites, "Id") for i in eachindex(sites) randn!(rng, M[i]) normalize!(M[i]) end - m > 1 && throw(ArgumentError("randomMPO: currently only m==1 supported")) + m > 1 && throw(ArgumentError("random_mpo: currently only m==1 supported")) return M end diff --git a/src/lib/ITensorMPS/test/base/test_mpo.jl b/src/lib/ITensorMPS/test/base/test_mpo.jl index 21408b05a0..f14c7005d7 100644 --- a/src/lib/ITensorMPS/test/base/test_mpo.jl +++ b/src/lib/ITensorMPS/test/base/test_mpo.jl @@ -21,7 +21,7 @@ end @testset "[first]siteinds(::MPO)" begin N = 5 s = siteinds("S=1/2", N) - M = randomMPO(s) + M = random_mpo(s) v = siteinds(M) for n in 1:N @test hassameinds(v[n], (s[n], s[n]')) @@ -48,12 +48,12 @@ end @test hasind(P[1], sites[1]) @test hasind(P[1], prime(sites[1])) # test constructor from Vector{ITensor} - K = randomMPO(sites) + K = random_mpo(sites) @test ITensors.data(MPO(copy(ITensors.data(K)))) == ITensors.data(K) @testset "orthogonalize!" begin phi = random_mps(sites) - K = randomMPO(sites) + K = random_mpo(sites) orthogonalize!(phi, 1) orthogonalize!(K, 1) orig_inner = ⋅(phi', K, phi) @@ -63,7 +63,7 @@ end end @testset "norm MPO" begin - A = randomMPO(sites) + A = random_mpo(sites) Adag = sim(linkinds, dag(A)) A² = ITensor(1) for j in 1:N @@ -79,7 +79,7 @@ end end @testset "lognorm MPO" begin - A = randomMPO(sites) + A = random_mpo(sites) for j in 1:N A[j] .*= j end @@ -96,7 +96,7 @@ end @testset "inner " begin phi = random_mps(sites) - K = randomMPO(sites) + K = random_mpo(sites) @test maxlinkdim(K) == 1 psi = random_mps(sites) phidag = dag(phi) @@ -156,7 +156,7 @@ end s = siteinds("S=1/2", n) ψ = c .* random_mps(s; linkdims=4) Φ = c .* random_mps(s; linkdims=4) - K = randomMPO(s) + K = random_mpo(s) @test log(complex(inner(ψ', K, Φ))) ≈ loginner(ψ', K, Φ) end @@ -240,7 +240,7 @@ end @testset "contract" begin phi = random_mps(sites) - K = randomMPO(sites) + K = random_mpo(sites) @test maxlinkdim(K) == 1 psi = random_mps(sites) psi_out = contract(K, psi; maxdim=1) @@ -290,8 +290,8 @@ end @testset "add(::MPO, ::MPO)" begin shsites = siteinds("S=1/2", N) - K = randomMPO(shsites) - L = randomMPO(shsites) + K = random_mpo(shsites) + L = random_mpo(shsites) M = add(K, L) @test length(M) == N psi = random_mps(shsites) @@ -352,8 +352,8 @@ end @testset "contract(::MPO, ::MPO)" begin psi = random_mps(sites) - K = randomMPO(sites) - L = randomMPO(sites) + K = random_mpo(sites) + L = random_mpo(sites) @test maxlinkdim(K) == 1 @test maxlinkdim(L) == 1 KL = contract(prime(K), L; maxdim=1) @@ -363,8 +363,8 @@ end # where both K and L have differently labelled sites othersitesk = [Index(2, "Site,aaa") for n in 1:N] othersitesl = [Index(2, "Site,bbb") for n in 1:N] - K = randomMPO(sites) - L = randomMPO(sites) + K = random_mpo(sites) + L = random_mpo(sites) for ii in 1:N replaceind!(K[ii], sites[ii]', othersitesk[ii]) replaceind!(L[ii], sites[ii]', othersitesl[ii]) @@ -376,14 +376,14 @@ end @test inner(psik, KL, psil) ≈ inner(psik, psi_kl_out) atol = 5e-3 badsites = [Index(2, "Site") for n in 1:(N + 1)] - badL = randomMPO(badsites) + badL = random_mpo(badsites) @test_throws DimensionMismatch contract(K, badL) end @testset "*(::MPO, ::MPO)" begin psi = random_mps(sites) - K = randomMPO(sites) - L = randomMPO(sites) + K = random_mpo(sites) + L = random_mpo(sites) @test maxlinkdim(K) == 1 @test maxlinkdim(L) == 1 KL = *(prime(K), L; maxdim=1) @@ -399,8 +399,8 @@ end # where both K and L have differently labelled sites othersitesk = [Index(2, "Site,aaa") for n in 1:N] othersitesl = [Index(2, "Site,bbb") for n in 1:N] - K = randomMPO(sites) - L = randomMPO(sites) + K = random_mpo(sites) + L = random_mpo(sites) for ii in 1:N replaceind!(K[ii], sites[ii]', othersitesk[ii]) replaceind!(L[ii], sites[ii]', othersitesl[ii]) @@ -412,7 +412,7 @@ end @test dot(psik, KL, psil) ≈ psik ⋅ psi_kl_out atol = 5e-3 badsites = [Index(2, "Site") for n in 1:(N + 1)] - badL = randomMPO(badsites) + badL = random_mpo(badsites) @test_throws DimensionMismatch K * badL end @@ -428,7 +428,7 @@ end O = MPO(sites, "Sz") @test length(O) == N # just make sure this works - @test_throws ArgumentError randomMPO(sites, 2) + @test_throws ArgumentError random_mpo(sites, 2) @test isnothing(linkind(MPO(fill(ITensor(), N), 0, N + 1), 1)) @testset "movesites $N sites" for N in 1:7 @@ -436,7 +436,7 @@ end ψ0 = MPO(s0, "Id") for perm in permutations(1:N) s = s0[perm] - ψ = randomMPO(s) + ψ = random_mpo(s) ns′ = [findsite(ψ0, i) for i in s] @test ns′ == perm ψ′ = movesites(ψ, 1:N .=> ns′) @@ -537,7 +537,7 @@ end @testset "Set range of MPO tensors" begin N = 5 s = siteinds("S=1/2", N) - ψ0 = randomMPO(s) + ψ0 = random_mpo(s) ψ = orthogonalize(ψ0, 2) A = prod(ITensors.data(ψ)[2:(N - 1)]) @@ -565,7 +565,7 @@ end @testset "swapbondsites MPO" begin N = 5 sites = siteinds("S=1/2", N) - ψ0 = randomMPO(sites) + ψ0 = random_mpo(sites) # TODO: implement this? #ψ = replacebond(ψ0, 3, ψ0[3] * ψ0[4]; @@ -680,8 +680,8 @@ end @testset "unsupported kwarg in dot, logdot" begin N = 6 sites = [Index(2, "Site,n=$n") for n in 1:N] - K = randomMPO(sites) - L = randomMPO(sites) + K = random_mpo(sites) + L = random_mpo(sites) @test_throws ErrorException dot(K, L, make_inds_match=true) @test_throws ErrorException logdot(K, L, make_inds_match=true) end @@ -768,8 +768,8 @@ end # MPO not an MPS N = 8 s = siteinds(2, N) - A = randomMPO(s) - B = randomMPO(s) + A = random_mpo(s) + B = random_mpo(s) C = apply(A, B; alg="naive") @test C isa MPO end @@ -800,8 +800,8 @@ end @testset "consistent precision of apply" for T in (Float32, Float64, ComplexF32, ComplexF64) sites = siteinds("S=1/2", 4) - A = randn(T) * convert_leaf_eltype(T, randomMPO(sites)) - B = randn(T) * convert_leaf_eltype(T, randomMPO(sites)) + A = randn(T) * convert_leaf_eltype(T, random_mpo(sites)) + B = randn(T) * convert_leaf_eltype(T, random_mpo(sites)) @test scalartype(apply(A, B)) == T end @testset "sample" begin diff --git a/src/lib/ITensorMPS/test/base/test_mps.jl b/src/lib/ITensorMPS/test/base/test_mps.jl index 1e59a0b86e..9a7123d3e1 100644 --- a/src/lib/ITensorMPS/test/base/test_mps.jl +++ b/src/lib/ITensorMPS/test/base/test_mps.jl @@ -1165,7 +1165,7 @@ end @test findsites(ψ, (l[2], l[3])) == [2, 3, 4] @test findsites(ψ, A) == [2, 4] - M = randomMPO(s) + M = random_mpo(s) lM = linkinds(M) @test findsite(M, s[4]) == 4 @@ -1192,7 +1192,7 @@ end @test siteinds(ψ, 3) == IndexSet(s[3]) @test siteinds(ψ, 3; plev=1) == IndexSet() - M = randomMPO(s) + M = random_mpo(s) @test noprime(siteind(first, M, 4)) == s[4] @test siteind(first, M, 4; plev=0) == s[4] @test siteind(first, M, 4; plev=1) == s[4]' diff --git a/src/lib/ITensorMPS/test/base/test_qnmpo.jl b/src/lib/ITensorMPS/test/base/test_qnmpo.jl index ae72519499..93e128828d 100644 --- a/src/lib/ITensorMPS/test/base/test_qnmpo.jl +++ b/src/lib/ITensorMPS/test/base/test_qnmpo.jl @@ -190,7 +190,7 @@ end O = MPO(sites, "Sz") @test length(O) == N # just make sure this works - @test_throws ArgumentError randomMPO(sites, 2) + @test_throws ArgumentError random_mpo(sites, 2) @test isnothing(linkind(MPO(fill(ITensor(), N), 0, N + 1), 1)) end diff --git a/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_chainrules.jl b/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_chainrules.jl index 2720036447..a6e9817c49 100644 --- a/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_chainrules.jl +++ b/src/lib/ITensorMPS/test/ext/ITensorMPSChainRulesCoreExt/test_chainrules.jl @@ -99,7 +99,7 @@ Random.seed!(1234) ϵ = 1e-10 @test f'(x) ≈ (f(x + ϵ) - f(x)) / ϵ atol = 1e-6 - ρ = randomMPO(s) + ρ = random_mpo(s) f = function (x) ψ̃tensors = [x^j * ψtensors[j] for j in 1:length(ψtensors)] ψ̃ = MPS(ψ̃tensors) @@ -112,7 +112,7 @@ Random.seed!(1234) # Random.seed!(1234) # s = siteinds("S=1/2", 2) # - # #ρ = randomMPO(s) + # #ρ = random_mpo(s) # #ρtensors = ITensors.data(ρ) # #ϕ = random_mps(ComplexF64, s) # #f = function (x) @@ -128,7 +128,7 @@ Random.seed!(1234) # ##@test f'(x) ≈ (f(x+ϵ) - f(x)) / ϵ atol = 1e-6 # # # - # #ϕ = randomMPO(s) + # #ϕ = random_mpo(s) # #f = function (x) # # ψ̃tensors = [2 * x * ψtensors[1], log(x) * ψtensors[2]] # # ψ̃ = MPS(ψ̃tensors) @@ -138,7 +138,7 @@ Random.seed!(1234) # #ϵ = 1e-8 # #@test f'(x) ≈ (f(x+ϵ) - f(x)) / ϵ atol = 1e-6 # - # #ρ = randomMPO(s) + # #ρ = random_mpo(s) #end @testset "MPO: apply" begin Random.seed!(1234) @@ -155,7 +155,7 @@ Random.seed!(1234) return os end H = MPO(ising(n, 1.0), s) - A = randomMPO(s) + A = random_mpo(s) ϕ = random_mps(ComplexF64, s; linkdims=10) # apply on mpo with apply_dag=true @@ -204,7 +204,7 @@ Random.seed!(1234) @test ∇f ≈ ∇num atol = 1e-5 # multiply two MPOs - V = randomMPO(s) + V = random_mpo(s) f = function (x) U = [op("Ry", s[2]; θ=x), op("CX", s[1], s[2]), op("Rx", s[3]; θ=x)] Hθ = apply(U, H; apply_dag=false) @@ -218,8 +218,8 @@ Random.seed!(1234) @test ∇f ≈ ∇num atol = 1e-5 # trace(MPO) - V1 = randomMPO(s) - V2 = randomMPO(s) + V1 = random_mpo(s) + V2 = random_mpo(s) f = function (x) U = [op("Ry", s[2]; θ=x), op("CX", s[1], s[2]), op("Rx", s[3]; θ=x)] Hθ = apply(U, H; apply_dag=false) @@ -237,8 +237,8 @@ Random.seed!(1234) z = x + y return inner(z, z) end - V1 = randomMPO(s) - V2 = randomMPO(s) + V1 = random_mpo(s) + V2 = random_mpo(s) g1, g2 = gradient(f, V1, V2) @test g1 ≈ 2 * (V1 + V2) @test g2 ≈ 2 * (V1 + V2) @@ -248,8 +248,8 @@ Random.seed!(1234) z = x - y return inner(z, z) end - V1 = randomMPO(s) - V2 = randomMPO(s) + V1 = random_mpo(s) + V2 = random_mpo(s) g1, g2 = gradient(f, V1, V2) @test g1 ≈ 2 * (V1 - V2) @test g2 ≈ -2 * (V1 - V2) diff --git a/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl b/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl index f0e2d6fc3b..c706755dc3 100644 --- a/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl +++ b/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl @@ -246,7 +246,7 @@ const ITENSORS_EXPORTED_NAMES = [ :qr, :randn!, :randomITensor, - :randomMPO, + :random_mpo, :random_mps, :readcpp, :removeqn, From 3518855680fd46b4e1a92140d1c7eabfc5767d29 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Thu, 16 May 2024 15:55:47 -0400 Subject: [PATCH 6/9] Fix deprecation syntax --- src/lib/ITensorMPS/src/deprecated.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/ITensorMPS/src/deprecated.jl b/src/lib/ITensorMPS/src/deprecated.jl index ceedcc9d9c..b01aa5dfdf 100644 --- a/src/lib/ITensorMPS/src/deprecated.jl +++ b/src/lib/ITensorMPS/src/deprecated.jl @@ -19,7 +19,7 @@ # mps/mpo.jl @deprecate MPO(A::MPS; kwargs...) outer(A', A; kwargs...) -@deprecate randomMPO(args...; kwargs...) = random_mpo(args...; kwargs...) +@deprecate randomMPO(args...; kwargs...) random_mpo(args...; kwargs...) # mps/mps.jl @deprecate randomMPS(args...; kwargs...) = random_mps(args...; kwargs...) From 80aa97353713da1c1442351f093370a3b5fbe58f Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Thu, 16 May 2024 15:56:33 -0400 Subject: [PATCH 7/9] Fix deprecation syntax --- src/lib/ITensorMPS/src/deprecated.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/ITensorMPS/src/deprecated.jl b/src/lib/ITensorMPS/src/deprecated.jl index b01aa5dfdf..d504a20d33 100644 --- a/src/lib/ITensorMPS/src/deprecated.jl +++ b/src/lib/ITensorMPS/src/deprecated.jl @@ -22,7 +22,7 @@ @deprecate randomMPO(args...; kwargs...) random_mpo(args...; kwargs...) # mps/mps.jl -@deprecate randomMPS(args...; kwargs...) = random_mps(args...; kwargs...) +@deprecate randomMPS(args...; kwargs...) random_mps(args...; kwargs...) # Pass throughs of old name to new name: From 98e1a0f76d328c8170aebf780a2f24848cdd494f Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Thu, 16 May 2024 16:29:42 -0400 Subject: [PATCH 8/9] Update TestITensorsExportedNames.jl --- .../TestITensorsExportedNames/TestITensorsExportedNames.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl b/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl index c706755dc3..783641e483 100644 --- a/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl +++ b/test/base/utils/TestITensorsExportedNames/TestITensorsExportedNames.jl @@ -246,6 +246,8 @@ const ITENSORS_EXPORTED_NAMES = [ :qr, :randn!, :randomITensor, + :randomMPO, + :randomMPS, :random_mpo, :random_mps, :readcpp, From cfdd75e4c00ea7eb089aefd2ed0cc60074c81168 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 16 May 2024 18:29:41 -0400 Subject: [PATCH 9/9] Add back missing randomMPS constructors --- src/lib/ITensorMPS/src/deprecated.jl | 12 +++++++++ .../ITensorMPS/test/base/test_deprecated.jl | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/lib/ITensorMPS/test/base/test_deprecated.jl diff --git a/src/lib/ITensorMPS/src/deprecated.jl b/src/lib/ITensorMPS/src/deprecated.jl index d504a20d33..c0fc43a6c9 100644 --- a/src/lib/ITensorMPS/src/deprecated.jl +++ b/src/lib/ITensorMPS/src/deprecated.jl @@ -24,6 +24,18 @@ # mps/mps.jl @deprecate randomMPS(args...; kwargs...) random_mps(args...; kwargs...) +# Deprecated syntax for specifying link dimensions. +@deprecate randomMPS(elt::Type{<:Number}, sites::Vector{<:Index}, state, linkdims::Integer) random_mps( + elt, sites, state; linkdims +) +@deprecate randomMPS(elt::Type{<:Number}, sites::Vector{<:Index}, linkdims::Integer) random_mps( + elt, sites; linkdims +) +@deprecate randomMPS(sites::Vector{<:Index}, state, linkdims::Integer) random_mps( + sites, state; linkdims +) +@deprecate randomMPS(sites::Vector{<:Index}, linkdims::Integer) random_mps(sites; linkdims) + # Pass throughs of old name to new name: unique_siteind(A::AbstractMPS, B::AbstractMPS, j::Integer) = siteinds(uniqueind, A, B, j) diff --git a/src/lib/ITensorMPS/test/base/test_deprecated.jl b/src/lib/ITensorMPS/test/base/test_deprecated.jl new file mode 100644 index 0000000000..cfc48ec32c --- /dev/null +++ b/src/lib/ITensorMPS/test/base/test_deprecated.jl @@ -0,0 +1,27 @@ +@eval module $(gensym()) +using ITensors.ITensorMPS: MPS, maxlinkdim, randomMPS, siteinds +using LinearAlgebra: norm +using Test: @test, @testset +@testset "randomMPS" begin + sites = siteinds("S=1/2", 4) + state = j -> isodd(j) ? "↑" : "↓" + linkdims = 2 + # Deprecated linkdims syntax + for mps in [ + randomMPS(Float64, sites, state; linkdims), + randomMPS(Float64, sites; linkdims), + randomMPS(sites, state; linkdims), + randomMPS(sites, linkdims), + # Deprecated linkdims syntax + randomMPS(Float64, sites, state, linkdims), + randomMPS(Float64, sites, linkdims), + randomMPS(sites, state, linkdims), + randomMPS(sites, linkdims), + ] + @test mps isa MPS + @test length(mps) == 4 + @test maxlinkdim(mps) == 2 + @test norm(mps) > 0 + end +end +end