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

Define + for LocalOperators #63

Merged
merged 8 commits into from
Aug 21, 2024
Merged
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
84 changes: 74 additions & 10 deletions src/operators/localoperator.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,62 @@

# Hamiltonian consisting of local terms
# -------------------------------------
"""
struct LocalOperator{T<:Tuple,S}

A sum of local operators acting on a lattice. The lattice is stored as a matrix of vector spaces,
and the terms are stored as a tuple of pairs of indices and operators.

# Fields

- `lattice::Matrix{S}`: The lattice on which the operator acts.
- `terms::T`: The terms of the operator, stored as a tuple of pairs of indices and operators.

# Constructors

LocalOperator(lattice::Matrix{S}, terms::Pair...)
LocalOperator{T,S}(lattice::Matrix{S}, terms::T) where {T,S} # expert mode

# Examples

```julia
lattice = fill(ℂ^2, 1, 1) # single-site unitcell
O1 = LocalOperator(lattice, ((1, 1),) => σx, ((1, 1), (1, 2)) => σx ⊗ σx, ((1, 1), (2, 1)) => σx ⊗ σx)
```
"""
struct LocalOperator{T<:Tuple,S}
lattice::Matrix{S}
terms::T
end
function LocalOperator(lattice::Matrix{S}, terms::Pair...) where {S}
lattice′ = PeriodicArray(lattice)
for (inds, operator) in terms
@assert operator isa AbstractTensorMap
@assert numout(operator) == numin(operator) == length(inds)
for i in 1:length(inds)
@assert space(operator, i) == lattice′[inds[i]]
function LocalOperator{T,S}(lattice::Matrix{S}, terms::T) where {T,S}
plattice = PeriodicArray(lattice)
# Check if the indices of the operator are valid with themselves and the lattice
for (inds, operator) in terms
@assert operator isa AbstractTensorMap
@assert numout(operator) == numin(operator) == length(inds)
@assert spacetype(operator) == S

for i in 1:length(inds)
@assert space(operator, i) == plattice[inds[i]]
end
end
return new{T,S}(lattice, terms)
end
return LocalOperator{typeof(terms),S}(lattice, terms)
end
function LocalOperator(
lattice::Matrix,
terms::Pair...;
atol=maximum(x -> eps(real(scalartype(x[2])))^(3 / 4), terms),
)
allinds = getindex.(terms, 1)
alloperators = getindex.(terms, 2)

relevant_terms = []
for inds in unique(allinds)
operator = sum(alloperators[findall(==(inds), allinds)])
norm(operator) > atol && push!(relevant_terms, inds => operator)
end

terms_tuple = Tuple(relevant_terms)
return LocalOperator{typeof(terms_tuple),eltype(lattice)}(lattice, terms_tuple)
end

"""
Expand All @@ -27,6 +69,9 @@
function checklattice(args...)
return checklattice(Bool, args...) || throw(ArgumentError("Lattice mismatch."))
end
function checklattice(::Type{Bool}, H1::LocalOperator, H2::LocalOperator)
return H1.lattice == H2.lattice

Check warning on line 73 in src/operators/localoperator.jl

View check run for this annotation

Codecov / codecov/patch

src/operators/localoperator.jl#L72-L73

Added lines #L72 - L73 were not covered by tests
end
function checklattice(::Type{Bool}, peps::InfinitePEPS, O::LocalOperator)
return size(peps) == size(O.lattice)
end
Expand Down Expand Up @@ -57,3 +102,22 @@
end
return LocalOperator(lattice, terms...)
end

# Linear Algebra
# --------------
function Base.:*(α::Number, O::LocalOperator)
scaled_terms = map(((inds, operator),) -> (inds => α * operator), O.terms)
return LocalOperator{typeof(scaled_terms),eltype(O.lattice)}(O.lattice, scaled_terms)

Check warning on line 110 in src/operators/localoperator.jl

View check run for this annotation

Codecov / codecov/patch

src/operators/localoperator.jl#L108-L110

Added lines #L108 - L110 were not covered by tests
end
Base.:*(O::LocalOperator, α::Number) = α * O

Check warning on line 112 in src/operators/localoperator.jl

View check run for this annotation

Codecov / codecov/patch

src/operators/localoperator.jl#L112

Added line #L112 was not covered by tests

Base.:/(O::LocalOperator, α::Number) = O * inv(α)
Base.:\(α::Number, O::LocalOperator) = inv(α) * O

Check warning on line 115 in src/operators/localoperator.jl

View check run for this annotation

Codecov / codecov/patch

src/operators/localoperator.jl#L114-L115

Added lines #L114 - L115 were not covered by tests

function Base.:+(O1::LocalOperator, O2::LocalOperator)
checklattice(O1, O2)
return LocalOperator(O1.lattice, O1.terms..., O2.terms...)

Check warning on line 119 in src/operators/localoperator.jl

View check run for this annotation

Codecov / codecov/patch

src/operators/localoperator.jl#L117-L119

Added lines #L117 - L119 were not covered by tests
end

Base.:-(O::LocalOperator) = -1 * O
Base.:-(O1::LocalOperator, O2::LocalOperator) = O1 + (-O2)

Check warning on line 123 in src/operators/localoperator.jl

View check run for this annotation

Codecov / codecov/patch

src/operators/localoperator.jl#L122-L123

Added lines #L122 - L123 were not covered by tests