Skip to content

Commit

Permalink
Improved classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mormatti committed Jan 30, 2024
1 parent fc12149 commit 7ef2f62
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 103 deletions.
8 changes: 6 additions & 2 deletions src/Scattensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ module Scattensor
# Using
using ITensors, ITensorTDVP, ITensorGLMakie
using LinearAlgebra, LinearSolve
using SparseArrays
using Optim
using Plots

# Types
include("types/exact_diag_system.jl")
include("types/local_operator.jl")
include("types/exact_diag/system.jl")
include("types/exact_diag/operator.jl")
include("types/exact_diag/local_operator.jl")
include("types/exact_diag/state.jl")
include("types/exact_diag/states_set.jl")
include("types/models.jl")

# Algorithms
Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/bloch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Outputs:
- `h` is the `Vector` of real eigenstates associated to 𝐇;
- `𝛙` is the `Vector` of (common) eigenvectors associated to 𝐇 and 𝐔.
"""
function bloch_states(
function symultaneous_diagonalization_HU(
𝐇::Matrix{ComplexF64},
𝐔::Matrix{ComplexF64};
check_hermiticity::Bool = false,
Expand Down Expand Up @@ -60,7 +60,7 @@ function bloch_states(

return u, h, 𝛙
end
export bloch_states
export symultaneous_diagonalization_HU


"""
Expand Down
44 changes: 0 additions & 44 deletions src/algorithms/translation_operator.jl
Original file line number Diff line number Diff line change
@@ -1,44 +0,0 @@
"""
Generates the translation operator T for a chain of L sites with local dimension d.
The system is assumed to be uniform, i.e. the local dimension is the same for all sites.
The system, in order to perform a translation, must be in periodic boundary conditions.
Inputs:
- `L` is the number of sites of the chain.
- `d` is the local dimension.
Outputs:
- The translation operator `T`.
"""
function translation_operator(
L::Integer, # The length of the system
d::Integer # The local dimension
)::Matrix{ComplexF64}

N::Integer = d^L
𝐓::Matrix{ComplexF64} = zeros(N,N)

Lst = []
c = 0
for i 1:d
lst = []
for j in 1:(N/d)
c = c + 1
push!(lst, c)
end
push!(Lst, lst)
end

print(Lst)

for indL in eachindex(Lst)
lst = Lst[indL]
for ind in eachindex(lst)
j = lst[ind]
𝐓[j, ((d*(j-1)+1)%N) + indL - 1] = 1
end
end

return 𝐓
end
export translation_operator
1 change: 0 additions & 1 deletion src/test.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
println("Testing Scattensor.jl")
"""________________________________________________________"""


# 𝒮 = ExactDiagSystem(3, 2)
# 𝛔ˣ::Matrix{ComplexF64} = [0 1;1 0]
# println(product_local_operators(𝒮,(𝛔ˣ,1),(𝛔ˣ,2)))
42 changes: 27 additions & 15 deletions src/types/local_operator.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
"""Insert the description of the struct."""
mutable struct EmptyLocalOperator
end
export ExactDiagLocalOperator

# TYPE DEFINITION
local_operator() = EmptyLocalOperator()

mutable struct LocalOperator
parent_system::ExactDiagSystem
matrix::Matrix{ComplexF64}
position::Int
mutable struct PositionedLocalOperator
matrix :: Matrix{ComplexF64}
position :: Int
end
export LocalOperator

local_operator(matrix::Matrix{ComplexF64}, position::Int) = PositionedLocalOperator(matrix, position)
matrix(𝒪::EmptyLocalOperator) = 𝒪.matrix
position(𝒪::EmptyLocalOperator) = 𝒪.position
size(𝒪::EmptyLocalOperator) = size(𝒪.matrix)

mutable struct ExactDiagLocalOperator
parent_system :: ExactDiagSystem
positioned_local_operator :: PositionedLocalOperator
end
export ExactDiagLocalOperator

# CONSTRUCTORS
function local_operator(𝒮::ExactDiagSystem, 𝐋::PositionedLocalOperator)
return ExactDiagLocalOperator(𝒮, local_operator(matrix, position))
end

function LocalOperator(𝒮::ExactDiagSystem, matrix::Matrix{ComplexF64}, position::Int)
function local_operator(𝒮::ExactDiagSystem, 𝐌::Matrix{ComplexF64}, j::Int)
d = 𝒮.local_dimension
L = 𝒮.system_size
position = position L
@assert (size(matrix) == (d, d)) "The local operator must have the same dimension of the local space."
return LocalOperator(𝒮, matrix, position)
j = j L
message = "The local operator must have the same dimension of the local space."
@assert (size(𝐌) == (d, d)) message
return ExactDiagLocalOperator(𝒮, 𝐌, j)
end


# STRUCT FUNCTIONS
LocalOperator::Type = Union{EmptyLocalOperator, PositionedLocalOperator, ExactDiagLocalOperator}

function set_position(𝒪::LocalOperator, position::Int)
L = 𝒪.parent_system.system_size
𝒪.position = position L
end

function set_matrix(𝒪::LocalOperator, matrix::Matrix{ComplexF64})
function set_matrix(𝒪::ExactDiagLocalOperator, matrix::Matrix{ComplexF64})
d = 𝒪.parent_system.local_dimension
@assert (size(matrix) == (d, d)) "The local operator must have the same dimension of the local space."
𝒪.matrix = matrix
Expand Down
53 changes: 35 additions & 18 deletions src/types/models.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
"""Insert the description of the struct."""
abstract type Model end

"""Mutable struct for the Ising Model."""
mutable struct IsingModel <: Model
spin_interaction ::Real
transverse_field ::Real
longitudinal_field ::Real
mutable struct IsingModelParameters
spin_interaction :: Real
transverse_field :: Real
longitudinal_field :: Real
end
export IsingModel
export IsingModelParameters

ising_model(; J::Real = 1, hx::Real = 0, hz::Real = 0) = IsingModelParameters(J, hx, hz)
interaction(ℳ::IsingModelParameters) =.spin_interaction
spin_interaction(ℳ::IsingModelParameters) =.spin_interaction
transverse_field(ℳ::IsingModelParameters) =.transverse_field
longitudinal_field(ℳ::IsingModelParameters) =.longitudinal_field

IsingModel::Type = Union{IsingModelParameters}

"""Mutable struct for the Heisenberg Model."""
mutable struct HeisenbergModel <: Model
x_spin_interaction ::Real
y_spin_interaction ::Real
z_spin_interaction ::Real
x_field ::Real
y_field ::Real
z_field ::Real

mutable struct HeisenbergModelParameters <: Model
x_spin_interaction :: Real
y_spin_interaction :: Real
z_spin_interaction :: Real
x_field :: Real
y_field :: Real
z_field :: Real
end
export HeisenbergModel
export HeisenbergModel

heisenberg_model(; Jx::Real = 1, Jy::Real = 1, Jz::Real = 1, hx::Real = 0, hy::Real = 0, hz::Real = 0) = HeisenbergModel(Jx, Jy, Jz, hx, hy, hz)
interaction(ℳ::HeisenbergModel) = (ℳ.x_spin_interaction, ℳ.y_spin_interaction, ℳ.z_spin_interaction)
spin_interaction(ℳ::HeisenbergModel) = interaction(ℳ)
x_spin_interaction(ℳ::HeisenbergModel) =.x_spin_interaction
y_spin_interaction(ℳ::HeisenbergModel) =.y_spin_interaction
z_spin_interaction(ℳ::HeisenbergModel) =.z_spin_interaction
field(ℳ::HeisenbergModel) = (ℳ.x_field, ℳ.y_field, ℳ.z_field)
x_field(ℳ::HeisenbergModel) =.x_field
y_field(ℳ::HeisenbergModel) =.y_field
z_field(ℳ::HeisenbergModel) =.z_field

HeisenbergModel::Type = Union{HeisenbergModelParameters}
93 changes: 93 additions & 0 deletions src/types/operator.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""Insert the description of the struct."""

mutable struct EmptyOperator
end

operator() = EmptyOperator()

mutable struct OperatorProperties
hilbert_space_dimension :: Integer
hermiticity :: Bool
unitarity :: Bool
end

operator_properties() = OperatorProperties(false, false, 0)
operator_properties(::EmptyOperator) = operator_properties()
function operator_properties(𝐌::Matrix)
hermiticity = (𝐌 == 𝐌')
unitarity = (𝐌 * 𝐌' == 𝐌' * 𝐌 == I)
hilbert_space_dimension = size(𝐌)[1]
return OperatorProperties(hermiticity, unitarity, hilbert_space_dimension)
end
hermiticity(𝒪::OperatorProperties) = 𝒪.hermiticity
unitarity(𝒪::OperatorProperties) = 𝒪.unitarity
hilbert_space_dimension(𝒪::OperatorProperties) = 𝒪.hilbert_space_dimension

mutable struct MatrixFormOperator
matrix :: Matrix{ComplexF64}
operator_properties :: OperatorProperties
end

operator(𝐌::Matrix{ComplexF64}) = MatrixFormOperator(𝐌, operator_properties(𝐌))
matrix(𝒪::MatrixFormOperator) = 𝒪.matrix
operator_properties(𝒪::MatrixFormOperator) = 𝒪.operator_properties
hermiticity(𝒪::MatrixFormOperator) = hermiticity(operator_properties(𝒪))
unitarity(𝒪::MatrixFormOperator) = unitarity(operator_properties(𝒪))

mutable struct ExactDiagMatrixFormOperator
matrix_form_operator :: MatrixFormOperator
parent_system :: ExactDiagSystem
translational_invariance :: Bool
end
export ExactDiagOperator

operator(𝐌::MatrixFormOperator, 𝒮::ExactDiagSystem) = ExactDiagMatrixFormOperator(𝐌, 𝒮, translational_invariance(𝐌, 𝒮))
matrix(𝒪::ExactDiagMatrixFormOperator) = matrix(𝒪.matrix_form_operator)
operator_properties(𝒪::ExactDiagMatrixFormOperator) = operator_properties(𝒪.matrix_form_operator)
hermiticity(𝒪::ExactDiagMatrixFormOperator) = hermiticity(𝒪.matrix_form_operator)
unitarity(𝒪::ExactDiagMatrixFormOperator) = unitarity(𝒪.matrix_form_operator)
translational_invariance(𝒪::ExactDiagMatrixFormOperator) = 𝒪.translational_invariance

function translational_invariance(𝐌::Matrix{ComplexF64}, 𝒮::ExactDiagSystem)::Bool
𝐓 = matrix(translation_operator(𝒮))
return 𝐌 * 𝐓 == 𝐓 * 𝐌
end

"""
Generates the translation operator T for a chain of L sites with local dimension d.
The system is assumed to be uniform, i.e. the local dimension is the same for all sites.
The system, in order to perform a translation, must be in periodic boundary conditions.
Inputs:
- `L` is the number of sites of the chain.
- `d` is the local dimension.
Outputs:
- The translation operator `T`.
"""

translation_operator(d::Integer,L::Integer)::Matrix{ComplexF64}
N::Integer = d^L
𝐓::Matrix{ComplexF64} = zeros(N,N)

Lst = []
c = 0
for _ in 1:d
lst = []
for _ in 1:(N/d)
c = c + 1
push!(lst, c)
end
push!(Lst, lst)
end

for indL in eachindex(Lst)
lst = Lst[indL]
for ind in eachindex(lst)
j = lst[ind]
𝐓[j, ((d*(j-1)+1)%N) + indL - 1] = 1
end
end

return 𝐓
end
11 changes: 11 additions & 0 deletions src/types/state.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Insert the description of the struct."""

# TYPE DEFINITION

mutable struct ExactDiagState
vector :: Vector{ComplexF64}
parent_system :: Union{ExactDiagSystem, Missing}
average_energy :: Union{Float64, Missing}
average_momentum :: Union{Float64, Missing}
end
export ExactDiagState
8 changes: 8 additions & 0 deletions src/types/states_set.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Insert the description of the struct."""

# TYPE DEFINITION

mutable struct ExactDiagStatesSet
states_set :: Vector{ExactDiagState}
end
export ExactDiagStatesSet
Loading

0 comments on commit 7ef2f62

Please sign in to comment.