Skip to content

Commit

Permalink
Fix Base.similar for physical and contractible network types (#149)
Browse files Browse the repository at this point in the history
* Patch `Base.similar` for physical and contractible network types

* restrict to two-argument `similar` which can only change scalartype
  • Loading branch information
leburgel authored Mar 9, 2025
1 parent a5e0b1d commit c6cfcd1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/networks/infinitesquarenetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Base.eltype(n::InfiniteSquareNetwork) = eltype(typeof(n))
Base.eltype(::Type{InfiniteSquareNetwork{O}}) where {O} = O

Base.copy(n::InfiniteSquareNetwork) = InfiniteSquareNetwork(copy(unitcell(n)))
function Base.similar(n::InfiniteSquareNetwork, args...)
return InfiniteSquareNetwork(similar(unitcell(n), args...))
function Base.similar(n::InfiniteSquareNetwork, T::Type{TorA}=scalartype(n)) where {TorA}
return InfiniteSquareNetwork(map(t -> similar(t, T), unitcell(n)))
end
function Base.repeat(n::InfiniteSquareNetwork, counts...)
return InfiniteSquareNetwork(repeat(unitcell(n), counts...))
Expand All @@ -53,6 +53,7 @@ end
virtualspace(n::InfiniteSquareNetwork, r::Int, c::Int, dir) = virtualspace(n[r, c], dir)

## Vector interface

function VectorInterface.scalartype(::Type{T}) where {T<:InfiniteSquareNetwork}
return scalartype(eltype(T))
end
Expand Down
13 changes: 12 additions & 1 deletion src/operators/infinitepepo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ Base.eltype(::Type{InfinitePEPO{T}}) where {T} = T
Base.eltype(A::InfinitePEPO) = eltype(typeof(A))

Base.copy(A::InfinitePEPO) = InfinitePEPO(copy(unitcell(A)))
Base.similar(A::InfinitePEPO, args...) = InfinitePEPO(similar(unitcell(A), args...))
function Base.similar(A::InfinitePEPO, T::Type{TorA}=scalartype(A)) where {TorA}
return InfinitePEPO(map(t -> similar(t, T), unitcell(A)))
end
Base.repeat(A::InfinitePEPO, counts...) = InfinitePEPO(repeat(unitcell(A), counts...))

Base.getindex(A::InfinitePEPO, args...) = Base.getindex(unitcell(A), args...)
Expand Down Expand Up @@ -169,6 +171,15 @@ function InfiniteSquareNetwork(top::InfinitePEPS, mid::InfinitePEPO, bot::Infini
)
end

## Vector interface

function VectorInterface.scalartype(::Type{NT}) where {NT<:InfinitePEPO}
return scalartype(eltype(NT))
end
function VectorInterface.zerovector(A::InfinitePEPO)
return InfinitePEPO(zerovector(unitcell(A)))
end

## (Approximate) equality
function Base.:(==)(A₁::InfinitePEPO, A₂::InfinitePEPO)
return all(zip(unitcell(A₁), unitcell(A₂))) do (p₁, p₂)
Expand Down
15 changes: 13 additions & 2 deletions src/states/infinitepartitionfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ Base.eltype(::Type{InfinitePartitionFunction{T}}) where {T} = T
Base.eltype(A::InfinitePartitionFunction) = eltype(typeof(A))

Base.copy(A::InfinitePartitionFunction) = InfinitePartitionFunction(copy(unitcell(A)))
function Base.similar(A::InfinitePartitionFunction, args...)
return InfinitePartitionFunction(similar(unitcell(A), args...))
function Base.similar(
A::InfinitePartitionFunction, T::Type{TorA}=scalartype(A)
) where {TorA}
return InfinitePartitionFunction(map(t -> similar(t, T), unitcell(A)))
end
function Base.repeat(A::InfinitePartitionFunction, counts...)
return InfinitePartitionFunction(repeat(unitcell(A), counts...))
Expand All @@ -147,6 +149,15 @@ function InfiniteSquareNetwork(state::InfinitePartitionFunction)
return InfiniteSquareNetwork(unitcell(state))
end

## Vector interface

function VectorInterface.scalartype(::Type{NT}) where {NT<:InfinitePartitionFunction}
return scalartype(eltype(NT))
end
function VectorInterface.zerovector(A::InfinitePartitionFunction)
return InfinitePartitionFunction(zerovector(unitcell(A)))
end

## (Approximate) equality
function Base.:(==)(A₁::InfinitePartitionFunction, A₂::InfinitePartitionFunction)
return all(zip(unitcell(A₁), unitcell(A₂))) do (p₁, p₂)
Expand Down
4 changes: 3 additions & 1 deletion src/states/infinitepeps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ Base.eltype(::Type{InfinitePEPS{T}}) where {T} = T
Base.eltype(A::InfinitePEPS) = eltype(typeof(A))

Base.copy(A::InfinitePEPS) = InfinitePEPS(copy(unitcell(A)))
Base.similar(A::InfinitePEPS, args...) = InfinitePEPS(similar(unitcell(A), args...))
function Base.similar(A::InfinitePEPS, T::Type{TorA}=scalartype(A)) where {TorA}
return InfinitePEPS(map(t -> similar(t, T), unitcell(A)))
end
Base.repeat(A::InfinitePEPS, counts...) = InfinitePEPS(repeat(unitcell(A), counts...))

Base.getindex(A::InfinitePEPS, args...) = Base.getindex(unitcell(A), args...)
Expand Down

0 comments on commit c6cfcd1

Please sign in to comment.