Performance tips
The examples on this page use the Fermion type and mul_effect defined on the Home page.
The time to sort a product grows quadratically with its number of factors, so long products are slow. A product of sums takes time proportional to the number of term pairs, and memory proportional to the size of the result.
op = prod(Fermion(n) + Fermion(n)' + 1 for n in 1:10)NonCommutativeProducts.nterms(op) # 59049To cut down on allocations, one can use NonCommutativeProducts.add!! which tries to perform addition in place, but widens the type if not possible.
let op = 0 for n in 1:100 op += Fermion(n)'*Fermion(n) end op2 = zero(op) for n in 1:100 op2 = NonCommutativeProducts.add!!(op2, Fermion(n)'*Fermion(n)) end op == op2 # trueendEach + copies the sum, so a long += loop or sum(x for ...) takes time and memory quadratic in the number of terms. sum(f, 1:n) and sum of an array add in pairs, which scales almost linearly. For many terms, use foldl(NonCommutativeProducts.add!!, itr; init=0). Keep init=0, since without it the first element may be modified.
Mixing different symbolic types together is slower than using a single type. For factors of an abstract element type, use foreach_segment to work on each type with compiled code.
Use with_autosort(false) only if you don't need the result sorted. It forms products faster, but like terms don't combine, and sorting afterwards is slower than sorting as they are formed.
Finally, instead of using ordinary addition and multiplication in mul_effect, you can use Swap and AddTerms which are more efficient. Their behaviour is:
Swap(λ::Number): Replacesa*bbyλ*b*a.AddTerms(terms):a*bshould be replaced by a sum of terms.termsshould be an iterable such as a vector or a tuple, and the elements can be of the other allowed return types exceptnothing.
For example, the mul_effect for fermions can be implemented as
import NonCommutativeProducts: Swap, AddTermsfunction mul_effect(a::Fermion, b::Fermion) (a.dagger, a.label) == (b.dagger, b.label) && return 0 (!a.dagger, a.label) < (!b.dagger, b.label) && return nothing (!a.dagger, a.label) == (b.dagger, b.label) && return AddTerms((Swap(-1), 1)) return Swap(-1)endwhich sorts the same way:
julia> Fermion(1)*Fermion(1)'1I - c†[1]*c[1]When products of a term with a sum have structure that mul_effect cannot use, define a method of mul_terms!! for your types. mul!! calls mul_terms!!(c, a, b) with the left factor a, and so does * when either factor is a sum. For a sum a, the default calls mul_terms!!(c, x, b) for each term x of a, so a method can handle single terms x, or a sum a to compute the whole product at once. Autosort is disabled during the call, so the method must add terms that are already sorted.
For example, |n⟩⟨m| applied to a sum of kets is the coefficient of |m⟩ times |n⟩. A method can find it in one pass over the terms of the sum with termpairs, and add it to c with add!!. Other products go through the default method:
julia> import NonCommutativeProducts as NCjulia> struct Ket n::Int bra::Bool endjulia> Base.adjoint(k::Ket) = Ket(k.n, !k.bra)julia> Base.show(io::IO, k::Ket) = print(io, k.bra ? "⟨" : "|", k.n, k.bra ? "|" : "⟩")julia> NC.@nc Ketjulia> NC.mul_effect(a::Ket, b::Ket) = a.bra && !b.bra ? Int(a.n == b.n) : nothingjulia> isket(fs) = length(fs) == 1 && !only(fs).bra;julia> function NC.mul_terms!!(c, x::NC.NCMul{<:Any,Ket}, b::NC.NCAdd) fs = NC.factors(x) if length(fs) != 2 || fs[1].bra || !fs[2].bra || !all(isket ∘ first, NC.termpairs(b)) return invoke(NC.mul_terms!!, Tuple{Any,NC.NCMul,NC.NCAdd}, c, x, b) end for (kets, coeff) in NC.termpairs(b) if only(kets).n == fs[2].n c = NC.add!!(c, NC.coefficient(x) * coeff * Ket(fs[1].n, false)) end end return c end;julia> ψ = sum(n * Ket(n, false) for n in 1:3);julia> Ket(1, false) * Ket(3, true) * ψ3*|1⟩For inner products of large sums, see Fast inner products.