Skip to content

Commit 3cf7a86

Browse files
committed
support for non-sorted indices
1 parent 28b2a91 commit 3cf7a86

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/operators/mpohamiltonian.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,16 @@ function instantiate_operator(lattice::AbstractArray{<:VectorSpace}, (inds′, O
309309
mpo = O isa FiniteMPO ? O : FiniteMPO(O)
310310

311311
# convert to linear index type
312-
operators = parent(mpo)
313312
indices = map(inds) do I
314313
return Base._to_linear_index(lattice, Tuple(I)...) # this should mean all inds are valid...
315314
end
315+
316+
# sort indices and deduplicate
317+
indices, mpo = canonicalize_indices(indices, mpo)
318+
operators = parent(mpo)
319+
320+
@assert allunique(indices) && issorted(indices) "From here on we require unique and ascending indices\n$indices"
321+
316322
T = eltype(mpo)
317323
local_mpo = Union{T, scalartype(T)}[]
318324
sites = Int[]
@@ -332,6 +338,16 @@ function instantiate_operator(lattice::AbstractArray{<:VectorSpace}, (inds′, O
332338
return sites => local_mpo
333339
end
334340

341+
function canonicalize_indices(indices, mpo)
342+
issorted(indices) && return indices, mpo
343+
mpo = copy(mpo)
344+
I = TensorKit.TupleTools.sortperm(indices)
345+
for s in TensorKit.permutation2swaps(I)
346+
swap!(mpo, s)
347+
end
348+
return TensorKit.TupleTools.getindices(indices, I), mpo
349+
end
350+
335351
# yields the promoted tensortype of all tensors
336352
function _find_tensortype(nonzero_operators::AbstractArray)
337353
return mapreduce(promote_type, nonzero_operators) do x

0 commit comments

Comments
 (0)