I'm trying to write some code to do basic algebraic manipulations in BraKet notation.
Thus far I have a function KetToVec which will convert any expression of the form a1 Ket[s1]+ b Ket[s2] +... to a column vector, and the inverse function VecToKet which does the reverse. One problem I'm having is in assigning matrix multiplication rules to the Ket form of the vectors.
Currently I have
Ket:/Dot[M_?MatrixQ,Ket[s__]]:= VecToKet[M.KetToVec[Ket[s]]]
which works for a single Ket. However I can't figure out how to make a similar assignment for multiplication to distribute over a linear combination of Ket's without having to overload one of the internal functions Times, Plus or Dot.
What I want is a way for it to evaluate
X.(a*Ket[s]) := a*(X.Ket[s])
and hence in general
X.(a*Ket[s1]+ b*Ket[s2]+..) := a*(X.Ket[s1]) + b*(X.Ket[s2])+...
But if I try something like
Ket:/Dot[M_?MatrixQ,Times[a_,Ket[s__]]]:= a*VecToKet[M.KetToVec[Ket[s]]]
it returns the error message "Tag Ket in ... is too deep for an assignment rule to be found"
Is this possible? Cheers!