@@ -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
333339end
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
336352function _find_tensortype (nonzero_operators:: AbstractArray )
337353 return mapreduce (promote_type, nonzero_operators) do x
0 commit comments