Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ITensorMPS] Small simplification to OpSum examples #1456

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/examples/DMRG.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,10 @@ let
#
os = OpSum()
for j=1:N-1
os += -4,"Sz",j,"Sz",j+1
os -= 4,"Sz",j,"Sz",j+1
end
for j=1:N
os += -2*h,"Sx",j;
os -= 2*h,"Sx",j;
end
H = MPO(os,sites)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ gradtol = 1e-4 # Tolerance for stopping gradient descent
function ising_hamiltonian(nsites; h)
ℋ = OpSum()
for j in 1:(nsites - 1)
+= -1, "Z", j, "Z", j + 1
-= 1, "Z", j, "Z", j + 1
end
for j in 1:nsites
ℋ += h, "X", j
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ITensorMPS/examples/autodiff/mps_autodiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ using Zygote
function ising(n; J, h)
os = OpSum()
for j in 1:(n - 1)
os += -J, "Z", j, "Z", j + 1
os -= J, "Z", j, "Z", j + 1
end
for j in 1:n
os += -h, "X", j
os -= h, "X", j
end
return os
end
Expand Down
16 changes: 8 additions & 8 deletions src/lib/ITensorMPS/examples/dmrg/1d_hubbard_extended.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ let

os = OpSum()
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 -= 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
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
for i in 1:N
os += U, "Nupdn", i
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ITensorMPS/examples/dmrg/1d_ising_with_observer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function tfimMPO(sites, h::Float64)
N = length(sites)
os = OpSum()
for j in 1:(N - 1)
os += -1, "Z", j, "Z", j + 1
os -= 1, "Z", j, "Z", j + 1
end
for j in 1:N
os += h, "X", j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function main(; Nx=6, Ny=3, U=4.0, t=1.0)

os = OpSum()
for b in lattice
os += -t, "Cdagup", b.s1, "Cup", b.s2
os += -t, "Cdagup", b.s2, "Cup", b.s1
os += -t, "Cdagdn", b.s1, "Cdn", b.s2
os += -t, "Cdagdn", b.s2, "Cdn", b.s1
os -= t, "Cdagup", b.s1, "Cup", b.s2
os -= t, "Cdagup", b.s2, "Cup", b.s1
os -= t, "Cdagdn", b.s1, "Cdn", b.s2
os -= t, "Cdagdn", b.s2, "Cdn", b.s1
end
for n in 1:N
os += U, "Nupdn", n
Expand Down
18 changes: 9 additions & 9 deletions src/lib/ITensorMPS/test/base/test_autompo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ end
M1 = MPO(a1, s)

a2 = OpSum()
a2 += -1, "C", 3, "Cdag", 1
a2 -= 1, "C", 3, "Cdag", 1
M2 = MPO(a2, s)

a3 = OpSum()
Expand Down Expand Up @@ -938,7 +938,7 @@ end
M1 = MPO(a1, s)

a2 = OpSum()
a2 += -1, "Cdn", 3, "Cdagdn", 1
a2 -= 1, "Cdn", 3, "Cdagdn", 1
M2 = MPO(a2, s)

p0uu = MPS(s, [1, 2, 2, 1, 1])
Expand Down Expand Up @@ -1013,9 +1013,9 @@ end
if m == i && n == j && p == l && q == k
exact_val += V[i, j, l, k]
elseif m == i && n == j && p == k && q == l
exact_val += -V[i, j, k, l]
exact_val -= V[i, j, k, l]
elseif m == j && n == i && p == l && q == k
exact_val += -V[j, i, l, k]
exact_val -= V[j, i, l, k]
elseif m == j && n == i && p == k && q == l
exact_val += V[j, i, k, l]
end
Expand All @@ -1036,7 +1036,7 @@ end
os = OpSum()
for i in 1:(N - 1)
os += +1im, "S+", i, "S-", i + 1
os += -1im, "S-", i, "S+", i + 1
os -= 1im, "S-", i, "S+", i + 1
end
H = MPO(os, sites)
psiud = MPS(sites, [1, 2, 1, 2])
Expand Down Expand Up @@ -1100,8 +1100,8 @@ end
t = 1.0
os = OpSum()
for n in 1:(N - 1)
os .+= -t, "Cdag", n, "C", n + 1
os .+= -t, "Cdag", n + 1, "C", n
os .-= t, "Cdag", n, "C", n + 1
os .-= t, "Cdag", n + 1, "C", n
end
s = siteinds("Fermion", N; conserve_qns=true)
os_original = deepcopy(os)
Expand Down Expand Up @@ -1138,8 +1138,8 @@ end

os = OpSum()
for j in 1:(N - 1)
os += -t, "Adag", j, "A", j + 1
os += -t, "A", j, "Adag", j + 1
os -= t, "Adag", j, "A", j + 1
os -= t, "A", j, "Adag", j + 1
os += V1, "N", j, "N", j + 1
end
for j in 1:(N - 2)
Expand Down
20 changes: 10 additions & 10 deletions src/lib/ITensorMPS/test/base/test_dmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range

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

Expand Down Expand Up @@ -390,13 +390,13 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range

os = OpSum()
for j in 1:(N - 1)
os += -t1, "Cdag", j, "C", j + 1
os += -t1, "Cdag", j + 1, "C", j
os -= t1, "Cdag", j, "C", j + 1
os -= t1, "Cdag", j + 1, "C", j
os += V, "N", j, "N", j + 1
end
for j in 1:(N - 2)
os += -t2, "Cdag", j, "C", j + 2
os += -t2, "Cdag", j + 2, "C", j
os -= t2, "Cdag", j, "C", j + 2
os -= t2, "Cdag", j + 2, "C", j
end
H = MPO(os, s)

Expand All @@ -421,10 +421,10 @@ using ITensors.ITensorMPS: nsite, set_nsite!, site_range
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 -= 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
H = MPO(os, sites)
Expand Down
28 changes: 14 additions & 14 deletions src/lib/ITensorMPS/test/base/test_fermions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ import ITensors: Out, In
t1 = 1.0
os = OpSum()
for b in 1:(N - 1)
os += -t1, "Cdag", b, "C", b + 1
os += -t1, "Cdag", b + 1, "C", b
os -= t1, "Cdag", b, "C", b + 1
os -= t1, "Cdag", b + 1, "C", b
end
H = MPO(os, sites)

Expand All @@ -72,8 +72,8 @@ import ITensors: Out, In

os = OpSum()
for b in 1:(N - 1)
os += -t1, "Cdag", b, "C", b + 1
os += -t1, "Cdag", b + 1, "C", b
os -= t1, "Cdag", b, "C", b + 1
os -= t1, "Cdag", b + 1, "C", b
os += V1, "N", b, "N", b + 1
end
H = MPO(os, sites)
Expand Down Expand Up @@ -110,12 +110,12 @@ import ITensors: Out, In
s = siteinds("Fermion", N; conserve_qns=true)
os = OpSum()
for n in 1:(N - 1)
os += -t1, "Cdag", n, "C", n + 1
os += -t1, "Cdag", n + 1, "C", n
os -= t1, "Cdag", n, "C", n + 1
os -= t1, "Cdag", n + 1, "C", n
end
for n in 1:(N - 2)
os += -t2, "Cdag", n, "C", n + 2
os += -t2, "Cdag", n + 2, "C", n
os -= t2, "Cdag", n, "C", n + 2
os -= t2, "Cdag", n + 2, "C", n
end
H = MPO(os, s)

Expand Down Expand Up @@ -173,8 +173,8 @@ import ITensors: Out, In
ost = OpSum()
osV = OpSum()
for b in 1:(N - 1)
ost += -t1, "Cdag", b, "C", b + 1
ost += -t1, "Cdag", b + 1, "C", b
ost -= t1, "Cdag", b, "C", b + 1
ost -= t1, "Cdag", b + 1, "C", b
osV += V1, "N", b, "N", b + 1
end
Ht = MPO(ost, s)
Expand Down Expand Up @@ -213,12 +213,12 @@ import ITensors: Out, In

ost = OpSum()
for b in 1:(N - 1)
ost += -t1, "Cdag", b, "C", b + 1
ost += -t1, "Cdag", b + 1, "C", b
ost -= t1, "Cdag", b, "C", b + 1
ost -= t1, "Cdag", b + 1, "C", b
end
for b in 1:(N - 2)
ost += -t2, "Cdag", b, "C", b + 2
ost += -t2, "Cdag", b + 2, "C", b
ost -= t2, "Cdag", b, "C", b + 2
ost -= t2, "Cdag", b + 2, "C", b
end
Ht = MPO(ost, s)

Expand Down
12 changes: 6 additions & 6 deletions src/lib/ITensorMPS/test/base/test_mps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1811,8 +1811,8 @@ end
U = 1.0
opsum = OpSum()
for b in 1:(N - 1)
opsum .+= -t, "Cdag", b, "C", b + 1
opsum .+= -t, "Cdag", b + 1, "C", b
opsum .-= t, "Cdag", b, "C", b + 1
opsum .-= t, "Cdag", b + 1, "C", b
opsum .+= U, "N", b, "N", b + 1
end
H = MPO(opsum, s)
Expand Down Expand Up @@ -1897,10 +1897,10 @@ end
U = 1.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
for n in 1:N
opsum .+= U, "Nupdn", n
Expand Down
10 changes: 5 additions & 5 deletions src/lib/ITensorMPS/test/base/test_qnmpo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ end
a = OpSum()
h = 0.5
for j in 1:(N - 1)
a .+= -1, "Sx", j, "Sx", j + 1
a .-= 1, "Sx", j, "Sx", j + 1
end
for j in 1:N
a .+= h, "Sz", j
Expand Down Expand Up @@ -323,10 +323,10 @@ function make_hubbard_opsum(
for dn in 1:NNN
tj, Vj = t / dn, V / dn
for n in 1:(N - dn)
os += -tj, "Cdagup", n, "Cup", n + dn
os += -tj, "Cdagup", n + dn, "Cup", n
os += -tj, "Cdagdn", n, "Cdn", n + dn
os += -tj, "Cdagdn", n + dn, "Cdn", n
os -= tj, "Cdagup", n, "Cup", n + dn
os -= tj, "Cdagup", n + dn, "Cup", n
os -= tj, "Cdagdn", n, "Cdn", n + dn
os -= tj, "Cdagdn", n + dn, "Cdn", n
os += Vj, "Ntot", n, "Ntot", n + dn
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/lib/ITensorMPS/test/base/test_threading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ end
lattice = square_lattice(Nx, Ny; yperiodic=true)
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
for n in 1:N
opsum .+= U, "Nupdn", n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ Random.seed!(1234)
function ising(n, h)
os = OpSum()
for j in 1:(n - 1)
os += -1, "Z", j, "Z", j + 1
os += -h, "X", j
os -= 1, "Z", j, "Z", j + 1
os -= h, "X", j
end
os += -h, "X", n
os -= h, "X", n
return os
end
H = MPO(ising(n, 1.0), s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ include(joinpath(@__DIR__, "utils", "circuit.jl"))
function ising_hamiltonian(nsites; h)
ℋ = OpSum()
for j in 1:(nsites - 1)
+= -1, "Z", j, "Z", j + 1
-= 1, "Z", j, "Z", j + 1
end
for j in 1:nsites
ℋ += h, "X", j
Expand Down
Loading