jfeist / QuantumAlgebra.jl

Quantum operator algebra in Julia
MIT License
60 stars 10 forks source link

QuantumAlgebra not simplifying complex expressions by adding terms #2

Closed arnelg closed 3 years ago

arnelg commented 3 years ago

I have found that the package sometimes does not do obvious simplifications by adding together terms. Here is a fairly minimal example I was able to construct.

α = param(:α)
x = adjoint(α) * a(1)  + α * a(1)
display(x - x)

Instead of zero the output is

α' a(1) + α a(1) + -1 α' a(1) + -1 α a(1)

image

jfeist commented 3 years ago

Thanks for the report! This was a bug with sorting involving conjugated parameters (which I've rarely used). I've just fixed it, and will tag a new release in a minute.

As a comment, I've been working off and on over the past year on a complete rewrite of the codebase which results in a major speed gain that can be many orders of magnitude when the expressions become large. The external interface is mostly unchanged, with the biggest change being that the canonical normal form is not automatically enforced anymore, and you have to call normal_form(x) to simplify x. Also, the normal order is now more "standard", with all creation operators coming before all annihilation operators (before, e.g., bosonic and fermionic operators were grouped separately). Apart from that, there is an additional restriction that indices have to be single (unicode) characters with at most an integer subindex (i.e., you can have a(:i_2) or even a(:🦞_1), but not a(:long) or a(:i_j). If that is a problem for any practical use case, it could be changed, though.) In any case, within that rewrite, this bug was already fixed (because the algorithm for simplifying sums is "smarter"). If you would like to try it out, install it with ]add QuantumAlgebra#full_rewrite.

arnelg commented 3 years ago

Hi Johannes. No problem, thanks for writing the package, it's very nice! The rewrite sounds interesting, I will certainly try it out if you think it is reasonably stable?

On another note, do you take feature requests at this point? The two things that has come up for me so far is 1. Defining real parameters such that adjoint(x) is equal to x. 2. Pretty printing powers of parameters such that x*x*x is printed as etc. If you take requests I'll add them as separate issues if you want.

jfeist commented 3 years ago

Hi Arne. Yes, the rewrite should be quite stable by now, it passes all the tests that I've thrown at it. The reason I haven't released it yet is because I still have to update the documentation, and because there is one last piece that is not implemented yet (but that probably does not affect most users). BTW, it also has much nicer output of the expressions on the REPL (LaTeX is mostly unchanged, but also has some small improvements).

Yes, feature requests are absolutely welcome! Real parameters are already implemented - you can define them either as param(:α,'r'), where 'r' stands for "purely real" ('n' would give a "normal" parameter, and 'c' directly the conjugated parameter), or use the convenience string macro Pr"α", where Pr gives real parameters (there's also Pc"α" for complex parameters). For including indices with the string macros, you can use underscores. To summarize:

Pc"ω_i,j" == param(:ω,'n',:i,:j)
Pr"ω_i,j" == param(:ω,'r',:i,:j)

Pretty printing powers of parameters or operators is a good idea, please do open an issue for it!