Skip to content

Commit

Permalink
[SiteTypes] Add S2 operator for S=1 and update code style (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
emstoudenmire authored Jun 16, 2024
1 parent 9cf5301 commit b739c40
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 101 deletions.
18 changes: 18 additions & 0 deletions src/lib/SiteTypes/src/sitetypes/aliases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,21 @@ alias(::OpName"ntot") = OpName"Ntot"()
alias(::OpName"F↑") = OpName"Fup"()
alias(::OpName"F↓") = OpName"Fdn"()
alias(::OpName"I") = OpName"Id"()

alias(::OpName"S²") = OpName"S2"()
alias(::OpName"Sᶻ") = OpName"Sz"()
alias(::OpName"Sʸ") = OpName"Sy"()
alias(::OpName"iSʸ") = OpName"iSy"()
alias(::OpName"Sˣ") = OpName"Sx"()
alias(::OpName"S⁻") = OpName"S-"()
alias(::OpName"Sminus") = OpName"S-"()
alias(::OpName"Sm") = OpName"S-"()
alias(::OpName"S⁺") = OpName"S+"()
alias(::OpName"Splus") = OpName"S+"()
alias(::OpName"Sp") = OpName"S+"()
alias(::OpName"projUp") = OpName"ProjUp"()
alias(::OpName"projDn") = OpName"ProjDn"()

alias(::OpName"Proj0") = OpName"ProjUp"()
alias(::OpName"Proj1") = OpName"ProjDn"()
alias(::OpName"Rn̂") = OpName"Rn"()
30 changes: 15 additions & 15 deletions src/lib/SiteTypes/src/sitetypes/qubit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ function op(::OpName"Rn", ::SiteType"Qubit"; θ::Real, ϕ::Real, λ::Real)
]
end

function op(::OpName"Rn̂", t::SiteType"Qubit"; kwargs...)
return op(OpName("Rn"), t; kwargs...)
function op(on::OpName"Rn̂", t::SiteType"Qubit"; kwargs...)
return op(alias(on), t; kwargs...)
end

#
Expand Down Expand Up @@ -435,68 +435,68 @@ op(::OpName"Sz", ::SiteType"Qubit") = [
0.0 -0.5
]

op(::OpName"Sᶻ", t::SiteType"Qubit") = op(OpName("Sz"), t)
op(on::OpName"Sᶻ", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"S+", ::SiteType"Qubit") = [
0 1
0 0
]

op(::OpName"S⁺", t::SiteType"Qubit") = op(OpName("S+"), t)
op(on::OpName"S⁺", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"Splus", t::SiteType"Qubit") = op(OpName("S+"), t)
op(on::OpName"Splus", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"S-", ::SiteType"Qubit") = [
0 0
1 0
]

op(::OpName"S⁻", t::SiteType"Qubit") = op(OpName("S-"), t)
op(on::OpName"S⁻", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"Sminus", t::SiteType"Qubit") = op(OpName("S-"), t)
op(on::OpName"Sminus", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"Sx", ::SiteType"Qubit") = [
0.0 0.5
0.5 0.0
]

op(::OpName"Sˣ", t::SiteType"Qubit") = op(OpName("Sx"), t)
op(on::OpName"Sˣ", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"iSy", ::SiteType"Qubit") = [
0.0 0.5
-0.5 0.0
]

op(::OpName"iSʸ", t::SiteType"Qubit") = op(OpName("iSy"), t)
op(on::OpName"iSʸ", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"Sy", ::SiteType"Qubit") = [
0.0 -0.5im
0.5im 0.0
]

op(::OpName"Sʸ", t::SiteType"Qubit") = op(OpName("Sy"), t)
op(on::OpName"Sʸ", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"S2", ::SiteType"Qubit") = [
0.75 0.0
0.0 0.75
]

op(::OpName"S²", t::SiteType"Qubit") = op(OpName("S2"), t)
op(on::OpName"S²", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"ProjUp", ::SiteType"Qubit") = [
1 0
0 0
]

op(::OpName"projUp", t::SiteType"Qubit") = op(OpName("ProjUp"), t)
op(on::OpName"projUp", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"Proj0", t::SiteType"Qubit") = op(OpName("ProjUp"), t)
op(on::OpName"Proj0", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"ProjDn", ::SiteType"Qubit") = [
0 0
0 1
]

op(::OpName"projDn", t::SiteType"Qubit") = op(OpName("ProjDn"), t)
op(on::OpName"projDn", t::SiteType"Qubit") = op(alias(on), t)

op(::OpName"Proj1", t::SiteType"Qubit") = op(OpName("ProjDn"), t)
op(on::OpName"Proj1", t::SiteType"Qubit") = op(alias(on), t)
156 changes: 70 additions & 86 deletions src/lib/SiteTypes/src/sitetypes/spinone.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using ..ITensors: complex!, QN

alias(::SiteType"SpinOne") = SiteType"S=1"()

"""
space(::SiteType"S=1";
conserve_qns = false,
Expand Down Expand Up @@ -51,109 +53,91 @@ state(::StateName"Y+", ::SiteType"S=1") = [-1 / 2, -im / sqrt(2), 1 / 2]
state(::StateName"Y0", ::SiteType"S=1") = [1 / sqrt(2), 0, 1 / sqrt(2)]
state(::StateName"Y-", ::SiteType"S=1") = [-1 / 2, im / sqrt(2), 1 / 2]

function op!(Op::ITensor, ::OpName"Sz", ::SiteType"S=1", s::Index)
Op[s' => 1, s => 1] = +1.0
return Op[s' => 3, s => 3] = -1.0
end
op(::OpName"Sz", ::SiteType"S=1") = [
1.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 -1.0
]

function op!(Op::ITensor, ::OpName"Sᶻ", t::SiteType"S=1", s::Index)
return op!(Op, OpName("Sz"), t, s)
end
op(on::OpName"Sᶻ", t::SiteType"S=1") = op(alias(on), t)

function op!(Op::ITensor, ::OpName"S+", ::SiteType"S=1", s::Index)
Op[s' => 2, s => 3] = sqrt(2)
return Op[s' => 1, s => 2] = sqrt(2)
end
op(::OpName"S+", ::SiteType"S=1") = [
0.0 2 0.0
0.0 0.0 2
0.0 0.0 0.0
]

function op!(Op::ITensor, ::OpName"S⁺", t::SiteType"S=1", s::Index)
return op!(Op, OpName("S+"), t, s)
end
op(on::OpName"S⁺", t::SiteType"S=1") = op(alias(on), t)
op(on::OpName"Splus", t::SiteType"S=1") = op(alias(on), t)
op(on::OpName"Sp", t::SiteType"S=1") = op(alias(on), t)

function op!(Op::ITensor, ::OpName"Splus", t::SiteType"S=1", s::Index)
return op!(Op, OpName("S+"), t, s)
end
op(::OpName"S-", ::SiteType"S=1") = [
0.0 0.0 0.0
2 0.0 0.0
0.0 2 0.0
]

function op!(Op::ITensor, ::OpName"Sp", t::SiteType"S=1", s::Index)
return op!(Op, OpName("S+"), t, s)
end
op(on::OpName"S⁻", t::SiteType"S=1") = op(alias(on), t)
op(on::OpName"Sminus", t::SiteType"S=1") = op(alias(on), t)
op(on::OpName"Sm", t::SiteType"S=1") = op(alias(on), t)

function op!(Op::ITensor, ::OpName"S-", ::SiteType"S=1", s::Index)
Op[s' => 3, s => 2] = sqrt(2)
return Op[s' => 2, s => 1] = sqrt(2)
end
op(::OpName"Sx", ::SiteType"S=1") = [
0.0 1/√2 0.0
1/√2 0.0 1/√2
0.0 1/√2 0.0
]

function op!(Op::ITensor, ::OpName"S⁻", t::SiteType"S=1", s::Index)
return op!(Op, OpName("S-"), t, s)
end
op(on::OpName"Sˣ", t::SiteType"S=1") = op(alias(on), t)

function op!(Op::ITensor, ::OpName"Sminus", t::SiteType"S=1", s::Index)
return op!(Op, OpName("S-"), t, s)
end
op(::OpName"iSy", ::SiteType"S=1") = [
0.0 1/√2 0.0
-1/√2 0.0 1/√2
0.0 -1/√2 0.0
]

function op!(Op::ITensor, ::OpName"Sm", t::SiteType"S=1", s::Index)
return op!(Op, OpName("S-"), t, s)
end
op(on::OpName"iSʸ", t::SiteType"S=1") = op(alias(on), t)

function op!(Op::ITensor, ::OpName"Sx", ::SiteType"S=1", s::Index)
Op[s' => 2, s => 1] = 1 / sqrt(2)
Op[s' => 1, s => 2] = 1 / sqrt(2)
Op[s' => 3, s => 2] = 1 / sqrt(2)
return Op[s' => 2, s => 3] = 1 / sqrt(2)
end
op(::OpName"Sy", ::SiteType"S=1") = [
0.0 -im/√2 0.0
im/√2 0.0 -im/√2
0.0 im/√2 0.0
]

function op!(Op::ITensor, ::OpName"Sˣ", t::SiteType"S=1", s::Index)
return op!(Op, OpName("Sx"), t, s)
end
op(on::OpName"Sʸ", t::SiteType"S=1") = op(alias(on), t)

function op!(Op::ITensor, ::OpName"iSy", ::SiteType"S=1", s::Index)
Op[s' => 2, s => 1] = -1 / sqrt(2)
Op[s' => 1, s => 2] = +1 / sqrt(2)
Op[s' => 3, s => 2] = -1 / sqrt(2)
return Op[s' => 2, s => 3] = +1 / sqrt(2)
end
op(::OpName"Sz2", ::SiteType"S=1") = [
1.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 1.0
]

function op!(Op::ITensor, ::OpName"iSʸ", t::SiteType"S=1", s::Index)
return op!(Op, OpName("iSy"), t, s)
end
op(::OpName"Sx2", ::SiteType"S=1") = [
0.5 0.0 0.5
0.0 1.0 0.0
0.5 0.0 0.5
]

function op!(Op::ITensor, ::OpName"Sy", ::SiteType"S=1", s::Index)
complex!(Op)
Op[s' => 2, s => 1] = +1im / sqrt(2)
Op[s' => 1, s => 2] = -1im / sqrt(2)
Op[s' => 3, s => 2] = +1im / sqrt(2)
return Op[s' => 2, s => 3] = -1im / sqrt(2)
end
op(::OpName"Sy2", ::SiteType"S=1") = [
0.5 0.0 -0.5
0.0 1.0 0.0
-0.5 0.0 0.5
]

function op!(Op::ITensor, ::OpName"Sʸ", t::SiteType"S=1", s::Index)
return op!(Op, OpName("Sy"), t, s)
end
op(::OpName"S2", ::SiteType"S=1") = [
2.0 0.0 0.0
0.0 2.0 0.0
0.0 0.0 2.0
]

function op!(Op::ITensor, ::OpName"Sz2", ::SiteType"S=1", s::Index)
Op[s' => 1, s => 1] = +1.0
return Op[s' => 3, s => 3] = +1.0
end
op(on::OpName"S²", t::SiteType"S=1") = op(alias(on), t)

function op!(Op::ITensor, ::OpName"Sx2", ::SiteType"S=1", s::Index)
Op[s' => 1, s => 1] = 0.5
Op[s' => 3, s => 1] = 0.5
Op[s' => 2, s => 2] = 1.0
Op[s' => 1, s => 3] = 0.5
return Op[s' => 3, s => 3] = 0.5
end

function op!(Op::ITensor, ::OpName"Sy2", ::SiteType"S=1", s::Index)
Op[s' => 1, s => 1] = +0.5
Op[s' => 3, s => 1] = -0.5
Op[s' => 2, s => 2] = +1.0
Op[s' => 1, s => 3] = -0.5
return Op[s' => 3, s => 3] = +0.5
end
space(st::SiteType"SpinOne"; kwargs...) = space(alias(st); kwargs...)

space(::SiteType"SpinOne"; kwargs...) = space(SiteType("S=1"); kwargs...)
state(name::StateName, st::SiteType"SpinOne") = state(name, alias(st))
val(name::ValName, st::SiteType"SpinOne") = val(name, alias(st))

state(name::StateName, ::SiteType"SpinOne") = state(name, SiteType("S=1"))
val(name::ValName, ::SiteType"SpinOne") = val(name, SiteType("S=1"))

function op!(Op::ITensor, o::OpName, ::SiteType"SpinOne", s::Index)
return op!(Op, o, SiteType("S=1"), s)
function op!(Op::ITensor, o::OpName, st::SiteType"SpinOne", s::Index)
return op!(Op, o, alias(st), s)
end

op(o::OpName, st::SiteType"SpinOne") = op(o, alias(st))
2 changes: 2 additions & 0 deletions test/base/test_phys_site_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ using ITensors, LinearAlgebra, Test
@test Array(op("Sz2", s, 2), s[2]', s[2]) [1.0 0 0; 0 0 0; 0 0 +1.0]
@test Array(op("Sx2", s, 2), s[2]', s[2]) [0.5 0 0.5; 0 1.0 0; 0.5 0 0.5]
@test Array(op("Sy2", s, 2), s[2]', s[2]) [0.5 0 -0.5; 0 1.0 0; -0.5 0 0.5]
@test Array(op("S2", s, 2), s[2]', s[2]) [2.0 0.0 0.0; 0.0 2.0 0.0; 0.0 0.0 2.0]
@test Array(op("", s, 2), s[2]', s[2]) [2.0 0.0 0.0; 0.0 2.0 0.0; 0.0 0.0 2.0]
end
end

Expand Down

0 comments on commit b739c40

Please sign in to comment.