Keep Ordering of Bra Ket When Printing

I would like to keep the ordering of the bra ket-print but right now this doesn't happen. Any Idea? I tried playing with HoldForm but then i could evaluate the expression inside properly

Ns = 1;
Table[
(Ket[(Replace[ Quotient[i - 1, Ns], {0 -> "\[UpArrow] ",
1 -> "\[DownArrow] "}] <> ToString@Mod[i, Ns])]
Bra[(Replace[        Quotient[j - 1, Ns], {0 -> "\[UpArrow] ",
1 -> "\[DownArrow] "}] <> ToString@Mod[j, Ns])])

, {i, 1, Ns*2}, {j, 1, Ns*2}   ] // MatrixForm


Right now this gives:

I would like it to give something more like:

Thank you

-

I would do it entirely without ToString. The main tool in combining mixed type output in a given order is Row:

Ns = 1;
Table[Row[
{
Ket[Row[{
Replace[
Quotient[i - 1, Ns], {0 -> "\[UpArrow] ",
1 -> "\[DownArrow] "}], Mod[i, Ns]}]
],
Bra[Row[{Replace[
Quotient[j - 1, Ns], {0 -> "\[UpArrow] ",
1 -> "\[DownArrow] "}], Mod[j, Ns]}]
]}],
{i, 1, Ns*2}, {j, 1, Ns*2}] // MatrixForm


$\left( \begin{array}{cc} |\text{$\uparrow $}0\rangle \langle \text{$\uparrow $}0| & |\text{$\uparrow $}0\rangle \langle \text{$\downarrow $}0| \\ |\text{$\downarrow $}0\rangle \langle \text{$\uparrow $}0| & |\text{$\downarrow $}0\rangle \langle \text{$\downarrow $}0| \\ \end{array} \right)$

Edit to elaborate on comment

In the long run, it may be better to separate the formatting from the algebra. To that end, I would first observe that there is an algebraic reason for the order of the bra and ket to be maintained. You're really forming something that in a matrix representation corresponds to non-commutative multiplication. So it makes sense to enter these bras and kets with a specially defined multiplication symbol, say, \[CenterDot] -- the $\cdot$ symbol. This symbol has no pre-defined meaning and allows you to tailor any output format for it that you like. That is, you input a "multiplication" with $\cdot$ but we can set it up to output the product without explicit multiplication symbol. Moreover, if the labels of the kets and bras are always a spin arrow and a number, you can predefine that in a Format statement too:

arrows = {0 -> "\[UpArrow]", 1 -> "\[DownArrow]"};
Format[Ket[x_, y_]] := Ket@Row[{x /. arrows, "\[ThinSpace]", y}];
Format[Bra[x_, y_]] := Bra@Row[{x /. arrows, "\[ThinSpace]", y}];
Format[CenterDot[x__]] := Row[Riffle[{x}, "\[ThinSpace]"]];

Ns = 1;
Table[
Ket[
Quotient[i - 1, Ns], Mod[i, Ns]
]\[CenterDot]Bra[
Quotient[j - 1, Ns], Mod[j, Ns]
],
{i, 1, Ns*2}, {j, 1, Ns*2}] // MatrixForm


The point here is that where it says \[CenterDot] in the code, I actually only had to enter the shortcut Esc.Esc. So the ordered product is now very easy to enter, and the actual algebraic expression is much easier to read.

I again used Row for formatting, combined with \[ThinSpace] as a spacer between elements of the bras and kets, as well as in the product.

-
That is quite useful. Are there any other good commands relevant to this I should look at too? Thank you Jens –  AimForClarity Mar 11 '13 at 3:08
@AimForClarity Yes, the formatting could be separated further from the content, to improve readablilty. I'll add that to my answer. –  Jens Mar 11 '13 at 3:44
Thank you, that is a good tip. I have been defining matrices to represent the algebra of a number space and a "qubit" space for a random walk by using the kronecker product. I wonder if instead I used the algebra and defined the non-commutative operations on the bra-ket row structure via the \cdot, if that would have been a better approach. Any thoughts? –  AimForClarity Mar 11 '13 at 4:35
@AimForClarity Actually, I'd say your approach sounds just fine. For a finite-dimensional Hilbert space, bras and kets are completely analogous to row- and column vectors in a vector space defined by some working basis, and then you may as well leverage the built-in capabilities for matrix multiplication, instead of re-implementing the corresponding algebra symbolically. That's also how I've done it before. –  Jens Mar 11 '13 at 4:45
Thanks Jens. I have also used the homepage.cem.itesm.mx/lgomez/quantum package but found it slow for any useful work. There they define all the associated operations on bra and kets –  AimForClarity Mar 11 '13 at 5:19

Well, I just figured out a simple minded solution, but I hope that someone has a more elegant one perhaps.

Basically you can just use StringReplace & use string all the way

  Clear@i1
Table[
HS2[[i, j]]*
StringReplace[
"\!$$\*TemplateBox[{\"row\"},\n\"Ket\"]$$\!$$\*TemplateBox[{\"col\ \"},\n\"Bra\"]$$", {"row" -> ( (Replace[
Quotient[i - 1, Ns], {0 -> "\[UpArrow] ",
1 -> "\[DownArrow] "}] <> ToString@Mod[i, Ns])),
"col" -> (Replace[
Quotient[j - 1, Ns], {0 -> "\[UpArrow] ",
1 -> "\[DownArrow] "}] <> ToString@Mod[j, Ns])

}]
, {i, 1, Dimensions[HS2, 1][[1]]}, {j, 1,
Dimensions[HS2, 2][[1]]}   ] // MatrixForm


An Example output:

To turn into nice representation:

-