@@ -309,16 +309,17 @@ function instantiate_operator(state::AbstractMPS, O::Pair)
309309 return instantiate_operator (physicalspace (state), O)
310310end
311311function instantiate_operator (lattice:: AbstractArray{<:VectorSpace} , (inds′, O):: Pair )
312- inds = inds′ isa Int ? tuple ( inds′) : inds′
313- mpo = O isa FiniteMPO ? O : FiniteMPO (O)
312+ inds = inds′ isa Int ? [ inds′] : inds′
313+ mpo = O isa FiniteMPO ? copy (O) : FiniteMPO (O)
314314
315315 # convert to linear index type
316- indices = map (inds) do I
317- return Base. _to_linear_index (lattice, Tuple (I)... ) # this should mean all inds are valid...
316+ indices = Vector {Int} (undef, length (inds))
317+ for i in eachindex (indices)
318+ indices[i] = Base. _to_linear_index (lattice, Tuple (inds[i])... ) # this should mean all inds are valid...
318319 end
319320
320321 # sort indices and deduplicate
321- indices, mpo = canonicalize_indices (indices, mpo)
322+ indices, mpo = canonicalize_indices! (indices, mpo)
322323 operators = parent (mpo)
323324
324325 @assert allunique (indices) && issorted (indices) " From here on we require unique and ascending indices\n $indices "
@@ -345,14 +346,23 @@ function instantiate_operator(lattice::AbstractArray{<:VectorSpace}, (inds′, O
345346 return sites => local_mpo
346347end
347348
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)
349+ function canonicalize_indices! (indices, mpo)
350+ # swap non-sorted entries
351+ for i in 2 : length (indices)
352+ for j in reverse (i: length (indices))
353+ if indices[j] < indices[j - 1 ]
354+ swap! (mpo, j - 1 )
355+ indices[j - 1 ], indices[j] = indices[j], indices[j - 1 ]
356+ end
357+ end
358+ end
359+ for i in length (indices): - 1 : 2
360+ if indices[i] == indices[i - 1 ]
361+ multiply_neighbours! (mpo, i - 1 )
362+ popat! (indices, i)
363+ end
354364 end
355- return TensorKit . TupleTools . getindices ( indices, I) , mpo
365+ return indices, mpo
356366end
357367
358368# yields the promoted tensortype of all tensors
0 commit comments