Quantum bra and ket notations

Is there a way to define the above quantum expression in Mathematica. I am using an add on package in this link. http://homepage.cem.itesm.mx/lgomez/quantum/

What I have tried is:

a = 2;

b = SqrtBox["3"] // DisplayForm;

h1[a_, b_] := Subscript[a, b];

Ket[\[Psi]] := (a*(h1[Ket[u], 1])*(h1[Ket[d], 2])) + (b*(h1[Ket[d],
1])*(h1[Ket[u], 2]))


But I can't create down and up arrow as it is displaying error. Also is it fine I use usual multiplication for kets?

• What exactly do you want to do? Only typesetting? I think not, you want to do some calculations. Then you can define some symbol that prints nicely, to your taste. However, you then need to specify which algebra these symbols should obey. E.g. you need to specify what the result of multiplying 2 symbols should be. More specifically the result of multiplying a bar with a ket. And you must specify any other rules you need. Sep 29, 2020 at 9:36
• I recommend taking a look at the notation package Sep 29, 2020 at 14:21

I am pretty sure you are not looking for this, because this will need some work before you can actually do anything useful with it.

I am not familiar with the linked package neither am I familiar with quantum mechanics in any stretch of the imagination.

In case you are interested in a quick way to typeset some expression and you are not trying to do something too complicated then please do read on.

Using Format you can specify how certain expressions are typeset by the front end.

In order to replicate the equation in the question we need to define the following:

(* an expression to represent subscripted expressions *)
Format[subs[body_, sub_]] := Subscript[body, sub]


Evaluating the former and then evaluating eg subs[x,1] will produce in a notebook a subscripted x with the subscript being 1.

Evaluating, in turn subs[x,1] // FullForm will reveal what is actually going on. The front end displays the expression using the rule defined with Format but the actual expression is still subs[x,1]. Roughly speaking, that means that one can define rules for objects with Head subs and have them displayed in a subscripted form.

In a similar fashion, we can define a display rule for the ket notation:

Format[ket[body_]] := Row[{"\[LeftBracketingBar]", body, "\[RightAngleBracket]"}]


After evaluating the line above, one can demonstrate how eg ket[x] displays in a notebook. Also, one can verify that eg subs[ket[x],1] produces the expected result ie a subscripted ket notation for x and a subscript of 1.

Again, using FullForm will show that the underlying expressions remain unchanged ie FullForm[subs[ket[x], 1]] should display the typed expression.

Continuing in a similar manner we can define display rules for the ket notation with up and down double stroked arrows:

Format[upket[]] := Row[{"\[LeftBracketingBar]", "\[DoubleUpArrow]",
"\[RightAngleBracket]"}]
Format[downket[]] := Row[{"\[LeftBracketingBar]", "\[DoubleDownArrow]",
"\[RightAngleBracket]"}]


Please verify that evaluating expressions like eq subs[upket[], 1] or subs[downket[], 2] produces the anticipated results.

Having defined the rules for displaying subscripted expressions and the ket notation, it is trivial to reproduce the equation in the question:

a = 2;
b = Sqrt[3];
expr = subs[ket[ψ], subs[t, 2]] == a subs[upket[], 1] subs[downket[], 2] +
b subs[downket[], 1] subs[upket[], 2];


This is how expr is displayed:

and this is the FullForm:

Edit:

In order to address issues of non-commutativity a quick solution is to add the following Format rule:

Format[nonCommutativeTimes[x__subs]] := Star[x]


and modify the expr accordingly:

expr = subs[ket[ψ], subs[t, 2]] == a nonCommutativeTimes[subs[upket[], 1],
subs[downket[], 2]] + b nonCommutativeTimes[subs[downket[], 1], subs[upket[], 2]]


Evaluating, like before, produces the following:

Like I said in the introduction, this is not intended to replace any specialized package or other functionality but is only presented here as a quick way to obtain the desired result.

If one intends to work with eg algebraic expressions involving subs and ket then they should define the relevant functionality from scratch, or almost from scratch. For example, as far as non-commutative multiplication is concerned, there is a built-in symbol NonCommutativeMultiply which can be used instead of the nonCommutativeTimes Head I used above.

• I agree everything, but one issue is those products in the right side is noncommutative. This means the order they have written shouldn’t change Sep 29, 2020 at 12:57
• I am currently using tensor product instead of multiplication. It's working. Yours is a really great well written code. Sep 30, 2020 at 1:13