Skip to content

Commit e0419a8

Browse files
leburgellkdvos
andauthored
Remove support for integer space specifiers in state and environment constructors (#266)
* Remove for integer space specifiers, simplify(?) constructors * Typo * Typo * Switch optional to kwarg * Forgot about tests * Do nothing if already properly filled * Typo * Forgot the kwargs * Missed a few more * Fix(?) ambiguity * If it's not used, probably don't need it * Update src/environments/ctmrg_environments.jl Co-authored-by: Lukas Devos <ldevos98@gmail.com> --------- Co-authored-by: Lukas Devos <ldevos98@gmail.com>
1 parent 73a5056 commit e0419a8

14 files changed

Lines changed: 123 additions & 209 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ ctmrg_tol = 1e-10
5656
grad_tol = 1e-4
5757

5858
# initialize a PEPS and CTMRG environment
59-
peps₀ = InfinitePEPS(2, D)
59+
peps₀ = InfinitePEPS(ComplexSpace(2), ComplexSpace(D))
6060
env₀, = leading_boundary(CTMRGEnv(peps₀, ComplexSpace(χ)), peps₀; tol=ctmrg_tol)
6161

6262
# ground state search

docs/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ctmrg_tol = 1e-10
4141
grad_tol = 1e-4
4242

4343
# initialize a PEPS and CTMRG environment
44-
peps₀ = InfinitePEPS(2, D)
44+
peps₀ = InfinitePEPS(ComplexSpace(2), ComplexSpace(D))
4545
env₀, = leading_boundary(CTMRGEnv(peps₀, ComplexSpace(χ)), peps₀; tol=ctmrg_tol)
4646

4747
# ground state search

src/environments/ctmrg_environments.jl

Lines changed: 70 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,22 @@ struct CTMRGEnv{C, T}
3131
edges::Array{T, 3}
3232
end
3333

34-
const ProductSpaceLike{N} = Union{
35-
NTuple{N, Int}, NTuple{N, <:ElementarySpace}, ProductSpace{<:ElementarySpace, N},
36-
}
37-
const SpaceLike = Union{ElementarySpaceLike, ProductSpaceLike}
38-
39-
_elementwise_dual(s::NTuple{N, <:ElementarySpace}) where {N} = dual.(s)
40-
41-
_spacetype(::Int) = ComplexSpace
42-
_spacetype(::S) where {S <: ElementarySpace} = S
43-
_spacetype(s::ProductSpaceLike) = _spacetype(first(s))
44-
45-
_to_space::Int) =^χ
46-
_to_space::ElementarySpace) = χ
47-
_to_space::ProductSpaceLike) = prod(_to_space, χ)
48-
4934
function _corner_tensor(
5035
f, ::Type{T}, left_vspace::S, right_vspace::S = left_vspace
51-
) where {T, S <: ElementarySpaceLike}
52-
return f(T, _to_space(left_vspace) _to_space(right_vspace))
36+
) where {T, S <: ElementarySpace}
37+
return f(T, left_vspace right_vspace)
5338
end
5439

5540
function _edge_tensor(
5641
f, ::Type{T}, left_vspace::S, pspaces::P, right_vspace::S = left_vspace
57-
) where {T, S <: ElementarySpaceLike, P <: ProductSpaceLike}
58-
return f(T, _to_space(left_vspace) _to_space(pspaces), _to_space(right_vspace))
42+
) where {T, S <: ElementarySpace, P <: ProductSpace}
43+
return f(T, left_vspace pspaces, right_vspace)
5944
end
6045

6146
"""
6247
CTMRGEnv(
6348
[f=randn, T=ComplexF64], Ds_north::A, Ds_east::A, chis_north::B, [chis_east::B], [chis_south::B], [chis_west::B]
64-
) where {A<:AbstractMatrix{<:SpaceLike}, B<:AbstractMatrix{<:ElementarySpaceLike}}
49+
) where {A<:AbstractMatrix{<:VectorSpace}, B<:AbstractMatrix{<:ElementarySpace}}
6550
6651
Construct a CTMRG environment by specifying matrices of north and east virtual spaces of the
6752
corresponding partition function and the north, east, south and west virtual spaces of the
@@ -76,32 +61,27 @@ of the corresponding edge tensor for each direction. Specifically, for a given s
7661
`chis_west[r, c]` corresponds to the north space of the west edge tensor.
7762
7863
Each entry of the `Ds_north` and `Ds_east` matrices corresponds to an effective local space
79-
of the partition function, represented as a tuple of elementary spaces encoding a product
80-
space. This can either contain a single elementary space for the case of a partition
81-
function defined in terms of local rank-4 tensors, or a tuple of elementary spaces
82-
representing a product space for the case of a partition function representing overlaps of
83-
PEPSs and PEPOs.
64+
of the partition function, and can be represented as an `ElementarySpace` (e.g. for the case
65+
of a partition function defined in terms of local rank-4 tensors) or a `ProductSpace` (e.g.
66+
for the case of a network representing overlaps of PEPSs and PEPOs).
8467
"""
8568
function CTMRGEnv(
86-
Ds_north::A, Ds_east::A,
87-
chis_north::B, chis_east::B = chis_north, chis_south::B = chis_north, chis_west::B = chis_north,
88-
) where {A <: AbstractMatrix{<:ProductSpaceLike}, B <: AbstractMatrix{<:ElementarySpaceLike}}
89-
return CTMRGEnv(
90-
randn, ComplexF64, N, Ds_north, Ds_east, chis_north, chis_east, chis_south, chis_west,
91-
)
92-
end
93-
function CTMRGEnv(
94-
f, T,
95-
Ds_north::A, Ds_east::A,
96-
chis_north::B, chis_east::B = chis_north, chis_south::B = chis_north, chis_west::B = chis_north,
97-
) where {A <: AbstractMatrix{<:ProductSpaceLike}, B <: AbstractMatrix{<:ElementarySpaceLike}}
69+
f, T, Ds_north::A, Ds_east::A, chis_north::B, chis_east::B = chis_north,
70+
chis_south::B = chis_north, chis_west::B = chis_north,
71+
) where {
72+
A <: AbstractMatrix{<:ProductSpace}, B <: AbstractMatrix{<:ElementarySpace},
73+
}
74+
# check all of the sizes
75+
size(Ds_north) == size(Ds_east) == size(chis_north) == size(chis_east) ==
76+
size(chis_south) == size(chis_west) || throw(ArgumentError("Input spaces should have equal sizes."))
77+
9878
# no recursive broadcasting?
9979
Ds_south = _elementwise_dual.(circshift(Ds_north, (-1, 0)))
10080
Ds_west = _elementwise_dual.(circshift(Ds_east, (0, 1)))
10181

10282
# do the whole thing
10383
N = length(first(Ds_north))
104-
st = _spacetype(first(Ds_north))
84+
st = spacetype(first(Ds_north))
10585
C_type = tensormaptype(st, 1, 1, T)
10686
T_type = tensormaptype(st, N + 1, 1, T)
10787

@@ -140,12 +120,37 @@ function CTMRGEnv(
140120
edges[:, :, :] ./= norm.(edges[:, :, :])
141121
return CTMRGEnv(corners, edges)
142122
end
123+
function CTMRGEnv(D_north::P, args...; kwargs...) where {P <: Union{Matrix{VectorSpace}, VectorSpace}}
124+
return CTMRGEnv(randn, ComplexF64, D_north, args...; kwargs...)
125+
end
126+
127+
# expand physical edge spaces to unit cell size
128+
function _fill_edge_physical_spaces(
129+
D_north::S, D_east::S = D_north; unitcell::Tuple{Int, Int} = (1, 1)
130+
) where {S <: VectorSpace}
131+
return fill(ProductSpace(D_north), unitcell), fill(ProductSpace(D_east), unitcell)
132+
end
133+
134+
# expand virtual environment spaces to unit cell size
135+
function _fill_environment_virtual_spaces(
136+
chis_north::S, chis_east::S = chis_north, chis_south::S = chis_north, chis_west::S = chis_north;
137+
unitcell::Tuple{Int, Int} = (1, 1)
138+
) where {S <: ElementarySpace}
139+
return fill(chis_north, unitcell), fill(chis_east, unitcell), fill(chis_south, unitcell), fill(chis_west, unitcell)
140+
end
141+
function _fill_environment_virtual_spaces(
142+
chis_north::M, chis_east::M = chis_north, chis_south::M = chis_north, chis_west::M = chis_north;
143+
unitcell::Tuple{Int, Int} = (1, 1)
144+
) where {M <: AbstractMatrix{<:ElementarySpace}}
145+
@assert size(chis_north) == size(chis_east) == size(chis_south) == size(chis_west) == unitcell "Incompatible size"
146+
return chis_north, chis_east, chis_south, chis_west
147+
end
143148

144149
"""
145150
CTMRGEnv(
146151
[f=randn, T=ComplexF64], D_north::P, D_east::P, chi_north::S, [chi_east::S], [chi_south::S], [chi_west::S];
147152
unitcell::Tuple{Int,Int}=(1, 1),
148-
) where {P<:ProductSpaceLike,S<:ElementarySpaceLike}
153+
) where {P<:VectorSpace,S<:ElementarySpace}
149154
150155
Construct a CTMRG environment by specifying the north and east virtual spaces of the
151156
corresponding [`InfiniteSquareNetwork`](@ref) and the north, east, south and west virtual
@@ -156,122 +161,55 @@ same.
156161
The environment virtual spaces for each site correspond to virtual space of the
157162
corresponding edge tensor for each direction.
158163
"""
159-
function CTMRGEnv(
160-
D_north::P, D_east::P,
161-
chi_north::S, chi_east::S = chi_north, chi_south::S = chi_north, chi_west::S = chi_north;
162-
unitcell::Tuple{Int, Int} = (1, 1),
163-
) where {P <: ProductSpaceLike, S <: ElementarySpaceLike}
164-
return CTMRGEnv(
165-
randn, ComplexF64,
166-
fill(D_north, unitcell), fill(D_east, unitcell),
167-
fill(chi_north, unitcell), fill(chi_east, unitcell),
168-
fill(chi_south, unitcell), fill(chi_west, unitcell),
169-
)
170-
end
171-
function CTMRGEnv(
172-
f, T,
173-
D_north::P, D_east::P,
174-
chi_north::S, chi_east::S = chi_north,
175-
chi_south::S = chi_north, chi_west::S = chi_north;
176-
unitcell::Tuple{Int, Int} = (1, 1),
177-
) where {P <: ProductSpaceLike, S <: ElementarySpaceLike}
178-
return CTMRGEnv(
179-
f, T, N,
180-
fill(D_north, unitcell), fill(D_east, unitcell),
181-
fill(chi_north, unitcell), fill(chi_east, unitcell),
182-
fill(chi_south, unitcell), fill(chi_west, unitcell),
183-
)
184-
end
185-
186-
"""
187-
CTMRGEnv(
188-
[f=randn, T=ComplexF64], network::InfiniteSquareNetwork, chis_north::A, [chis_east::A], [chis_south::A], [chis_west::A]
189-
) where {A<:AbstractMatrix{<:ElementarySpaceLike}}}
190-
191-
Construct a CTMRG environment by specifying a corresponding [`InfiniteSquareNetwork`](@ref), and the
192-
north, east, south and west virtual spaces of the environment as matrices. Each respective
193-
matrix entry corresponds to a site in the unit cell. By default, the virtual spaces for all
194-
directions are taken to be the same.
195-
196-
The environment virtual spaces for each site correspond to the north or east virtual space
197-
of the corresponding edge tensor for each direction. Specifically, for a given site
198-
`(r, c)`, `chis_north[r, c]` corresponds to the east space of the north edge tensor,
199-
`chis_east[r, c]` corresponds to the north space of the east edge tensor,
200-
`chis_south[r, c]` corresponds to the east space of the south edge tensor, and
201-
`chis_west[r, c]` corresponds to the north space of the west edge tensor.
202-
"""
203-
function CTMRGEnv(
204-
network::InfiniteSquareNetwork,
205-
chis_north::A, chis_east::A = chis_north, chis_south::A = chis_north, chis_west::A = chis_north,
206-
) where {A <: AbstractMatrix{<:ElementarySpaceLike}}
207-
Ds_north = _north_env_spaces(network)
208-
Ds_east = _east_env_spaces(network)
209-
return CTMRGEnv(
210-
randn, scalartype(network),
211-
Ds_north, Ds_east,
212-
_to_space.(chis_north), _to_space.(chis_east), _to_space.(chis_south), _to_space.(chis_west),
213-
)
214-
end
215164
function CTMRGEnv(
216165
f, T,
217-
network::InfiniteSquareNetwork,
218-
chis_north::A, chis_east::A = chis_north, chis_south::A = chis_north, chis_west::A = chis_north,
219-
) where {A <: AbstractMatrix{<:ElementarySpaceLike}}
220-
Ds_north = _north_env_spaces(network)
221-
Ds_east = _east_env_spaces(network)
166+
D_north::S, D_east::S, virtual_spaces...; unitcell::Tuple{Int, Int} = (1, 1),
167+
) where {S <: VectorSpace}
222168
return CTMRGEnv(
223169
f, T,
224-
Ds_north, Ds_east,
225-
_to_space.(chis_north), _to_space.(chis_east), _to_space.(chis_south), _to_space.(chis_west),
170+
_fill_edge_physical_spaces(D_north, D_east; unitcell)...,
171+
_fill_environment_virtual_spaces(virtual_spaces...; unitcell)...,
226172
)
227173
end
228174

229-
function _north_env_spaces(network::InfiniteSquareNetwork)
175+
# get edge physical spaces from network
176+
function _north_edge_physical_spaces(network::InfiniteSquareNetwork)
230177
return map(ProductSpace _elementwise_dual north_virtualspace, unitcell(network))
231178
end
232-
function _east_env_spaces(network::InfiniteSquareNetwork)
179+
function _east_edge_physical_spaces(network::InfiniteSquareNetwork)
233180
return map(ProductSpace _elementwise_dual east_virtualspace, unitcell(network))
234181
end
235182

236183
"""
237184
CTMRGEnv(
238-
[f=randn, T=ComplexF64,] network::InfiniteSquareNetwork, chi_north::S, [chi_east::S], [chi_south::S], [chi_west::S],
239-
) where {S<:ElementarySpaceLike}
185+
[f=randn, T=ComplexF64], network::InfiniteSquareNetwork, chis_north::A, [chis_east::A], [chis_south::A], [chis_west::A]
186+
) where {A<:Union{AbstractMatrix{<:ElementarySpace}, ElementarySpace}}
240187
241188
Construct a CTMRG environment by specifying a corresponding
242189
[`InfiniteSquareNetwork`](@ref), and the north, east, south and west virtual spaces of the
243-
environment. By default, the virtual spaces for all directions are taken to be the same.
190+
environment. The virtual spaces can either be specified as matrices of `ElementarySpace`s,
191+
or as individual `ElementarySpace`s which are then filled to match the size of the unit
192+
cell. Each respective matrix entry corresponds to a site in the unit cell. By default, the
193+
virtual spaces for all directions are taken to be the same.
244194
245-
The environment virtual spaces for each site correspond to virtual space of the
246-
corresponding edge tensor for each direction.
195+
The environment virtual spaces for each site correspond to the north or east virtual space
196+
of the corresponding edge tensor for each direction. Specifically, for a given site
197+
`(r, c)`, `chis_north[r, c]` corresponds to the east space of the north edge tensor,
198+
`chis_east[r, c]` corresponds to the north space of the east edge tensor,
199+
`chis_south[r, c]` corresponds to the east space of the south edge tensor, and
200+
`chis_west[r, c]` corresponds to the north space of the west edge tensor.
247201
"""
248-
function CTMRGEnv(
249-
network::InfiniteSquareNetwork,
250-
chi_north::S, chi_east::S = chi_north, chi_south::S = chi_north, chi_west::S = chi_north,
251-
) where {S <: ElementarySpaceLike}
252-
return CTMRGEnv(
253-
network,
254-
fill(chi_north, size(network)), fill(chi_east, size(network)),
255-
fill(chi_south, size(network)), fill(chi_west, size(network)),
256-
)
202+
function CTMRGEnv(f, T, network::InfiniteSquareNetwork, virtual_spaces...)
203+
Ds_north = _north_edge_physical_spaces(network)
204+
Ds_east = _east_edge_physical_spaces(network)
205+
virtual_spaces = _fill_environment_virtual_spaces(virtual_spaces...; unitcell = size(network))
206+
return CTMRGEnv(f, T, Ds_north, Ds_east, virtual_spaces...)
257207
end
258-
function CTMRGEnv(
259-
f, T,
260-
network::InfiniteSquareNetwork,
261-
chi_north::S, chi_east::S = chi_north, chi_south::S = chi_north, chi_west::S = chi_north,
262-
) where {S <: ElementarySpaceLike}
263-
return CTMRGEnv(
264-
f, T,
265-
network,
266-
fill(chi_north, size(network)), fill(chi_east, size(network)),
267-
fill(chi_south, size(network)), fill(chi_west, size(network)),
268-
)
208+
function CTMRGEnv(network::Union{InfiniteSquareNetwork, InfinitePartitionFunction, InfinitePEPS}, virtual_spaces...)
209+
return CTMRGEnv(randn, scalartype(network), network, virtual_spaces...)
269210
end
270211

271212
# allow constructing environments for implicitly defined contractible networks
272-
function CTMRGEnv(state::Union{InfinitePartitionFunction, InfinitePEPS}, args...)
273-
return CTMRGEnv(InfiniteSquareNetwork(state), args...)
274-
end
275213
function CTMRGEnv(f, T, state::Union{InfinitePartitionFunction, InfinitePEPS}, args...)
276214
return CTMRGEnv(f, T, InfiniteSquareNetwork(state), args...)
277215
end

src/states/infinitepartitionfunction.jl

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,9 @@ Create an `InfinitePartitionFunction` by specifying the physical, north virtual
4949
of the PEPS tensor at each site in the unit cell as a matrix. Each individual space can be
5050
specified as either an `Int` or an `ElementarySpace`.
5151
"""
52-
function InfinitePartitionFunction(
53-
Nspaces::A, Espaces::A
54-
) where {A <: AbstractMatrix{<:ElementarySpaceLike}}
55-
return InfinitePartitionFunction(randn, ComplexF64, Nspaces, Espaces)
56-
end
5752
function InfinitePartitionFunction(
5853
f, T, Nspaces::M, Espaces::M = Nspaces
59-
) where {M <: AbstractMatrix{<:ElementarySpaceLike}}
54+
) where {M <: AbstractMatrix{<:ElementarySpace}}
6055
size(Nspaces) == size(Espaces) ||
6156
throw(ArgumentError("Input spaces should have equal sizes."))
6257

@@ -69,6 +64,9 @@ function InfinitePartitionFunction(
6964

7065
return InfinitePartitionFunction(A)
7166
end
67+
function InfinitePartitionFunction(Nspaces::A, args...) where {A <: Union{AbstractMatrix{<:ElementarySpace}, ElementarySpace}}
68+
return InfinitePartitionFunction(randn, ComplexF64, Nspaces, args...)
69+
end
7270

7371
"""
7472
InfinitePartitionFunction(A; unitcell=(1, 1))
@@ -99,22 +97,15 @@ end
9997
"""
10098
InfinitePartitionFunction(
10199
[f=randn, T=ComplexF64,] Pspace::S, Nspace::S, [Espace::S]; unitcell=(1,1)
102-
) where {S<:ElementarySpaceLike}
100+
) where {S<:ElementarySpace}
103101
104102
Create an InfinitePartitionFunction by specifying its physical, north and east spaces and unit cell.
105103
Spaces can be specified either via `Int` or via `ElementarySpace`.
106104
"""
107-
function InfinitePartitionFunction(
108-
Nspace::S, Espace::S = Nspace; unitcell::Tuple{Int, Int} = (1, 1)
109-
) where {S <: ElementarySpaceLike}
110-
return InfinitePartitionFunction(
111-
randn, ComplexF64, fill(Nspace, unitcell), fill(Espace, unitcell)
112-
)
113-
end
114105
function InfinitePartitionFunction(
115106
f, T, Nspace::S, Espace::S = Nspace; unitcell::Tuple{Int, Int} = (1, 1)
116-
) where {S <: ElementarySpaceLike}
117-
return InfinitePartitionFunction(f, T, fill(Nspace, unitcell), fill(Espace, unitcell))
107+
) where {S <: ElementarySpace}
108+
return InfinitePartitionFunction(f, T, _fill_state_virtual_spaces(Nspace, Espace; unitcell)...)
118109
end
119110

120111
## Unit cell interface

0 commit comments

Comments
 (0)