Skip to content

Commit e9b2b1d

Browse files
committed
support for non-sorted indices
1 parent f9d4507 commit e9b2b1d

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
@@ -313,10 +313,16 @@ function instantiate_operator(lattice::AbstractArray{<:VectorSpace}, (inds′, O
313313
mpo = O isa FiniteMPO ? O : FiniteMPO(O)
314314

315315
# convert to linear index type
316-
operators = parent(mpo)
317316
indices = map(inds) do I
318317
return Base._to_linear_index(lattice, Tuple(I)...) # this should mean all inds are valid...
319318
end
319+
320+
# sort indices and deduplicate
321+
indices, mpo = canonicalize_indices(indices, mpo)
322+
operators = parent(mpo)
323+
324+
@assert allunique(indices) && issorted(indices) "From here on we require unique and ascending indices\n$indices"
325+
320326
T = eltype(mpo)
321327
local_mpo = Union{T, scalartype(T)}[]
322328
sites = Int[]
@@ -339,6 +345,16 @@ function instantiate_operator(lattice::AbstractArray{<:VectorSpace}, (inds′, O
339345
return sites => local_mpo
340346
end
341347

348+
function canonicalize_indices(indices, mpo)
349+
issorted(indices) && return indices, mpo
350+
mpo = copy(mpo)
351+
I = TensorKit.TupleTools.sortperm(indices)
352+
for s in TensorKit.permutation2swaps(I)
353+
swap!(mpo, s)
354+
end
355+
return TensorKit.TupleTools.getindices(indices, I), mpo
356+
end
357+
342358
# yields the promoted tensortype of all tensors
343359
function _find_tensortype(nonzero_operators::AbstractArray)
344360
return mapreduce(promote_type, nonzero_operators) do x

0 commit comments

Comments
 (0)