From b739c4066987b427d8b963ab511ccce949ddc78c Mon Sep 17 00:00:00 2001 From: Miles Date: Sun, 16 Jun 2024 15:14:18 -0600 Subject: [PATCH] [SiteTypes] Add S2 operator for S=1 and update code style (#1502) --- src/lib/SiteTypes/src/sitetypes/aliases.jl | 18 +++ src/lib/SiteTypes/src/sitetypes/qubit.jl | 30 ++-- src/lib/SiteTypes/src/sitetypes/spinone.jl | 156 +++++++++------------ test/base/test_phys_site_types.jl | 2 + 4 files changed, 105 insertions(+), 101 deletions(-) diff --git a/src/lib/SiteTypes/src/sitetypes/aliases.jl b/src/lib/SiteTypes/src/sitetypes/aliases.jl index 30e395234c..727ad7f990 100644 --- a/src/lib/SiteTypes/src/sitetypes/aliases.jl +++ b/src/lib/SiteTypes/src/sitetypes/aliases.jl @@ -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"() diff --git a/src/lib/SiteTypes/src/sitetypes/qubit.jl b/src/lib/SiteTypes/src/sitetypes/qubit.jl index 149539355e..e07bd547f0 100644 --- a/src/lib/SiteTypes/src/sitetypes/qubit.jl +++ b/src/lib/SiteTypes/src/sitetypes/qubit.jl @@ -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 # @@ -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) diff --git a/src/lib/SiteTypes/src/sitetypes/spinone.jl b/src/lib/SiteTypes/src/sitetypes/spinone.jl index bae0b765df..1de6902243 100644 --- a/src/lib/SiteTypes/src/sitetypes/spinone.jl +++ b/src/lib/SiteTypes/src/sitetypes/spinone.jl @@ -1,5 +1,7 @@ using ..ITensors: complex!, QN +alias(::SiteType"SpinOne") = SiteType"S=1"() + """ space(::SiteType"S=1"; conserve_qns = false, @@ -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)) diff --git a/test/base/test_phys_site_types.jl b/test/base/test_phys_site_types.jl index 6ff1154893..21d25d7532 100644 --- a/test/base/test_phys_site_types.jl +++ b/test/base/test_phys_site_types.jl @@ -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²", 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