# Partial trace and Partial Transposition of a matrix easily? [closed]

Could someone help me to understand as to how to compute the partial trace and partial-transposition of an arbitrary matrix? I mean, is there any code to carry out these operations in Mathematica?

EDIT: Using the code suggested by @Budha_the_Scientist, following error messages appear:

In[29]:= mat = {{1, 2}, {3, 0}};

In[30]:= partialtrace[a_List?MatrixQ, dim_, sys_] :=
Module[{mat, gtb, i, j}, mat = Partition[a, {dim[[2]], dim[[2]]}];
If[sys == 1, gtb = Tr[mat, Plus, 2],
gtb = Table[Tr[mat[[i, j]]], {i, dim[[1]]}, {j, dim[[1]]}]];
normalize[gtb]]

normalize[a_List?MatrixQ] := a/Tr[a]

partialtranspose[a_List?MatrixQ, dim_, sys_] :=
Module[{mat, pt}, mat = Partition[a, {dim[[2]], dim[[2]]}];
If[sys == 1, pt = ArrayFlatten[Transpose[mat]],
pt = ArrayFlatten[
Table[Transpose[mat[[i, j]]], {i, dim[[1]]}, {j, dim[[1]]}]]];
Return[pt];]

In[33]:= partialtrace[mat, {2, 2}, 2]

During evaluation of In[33]:= Part::partw: Part 2 of {{{1,2},{3,0}}} does not exist. >>

During evaluation of In[33]:= Part::partw: Part 2 of {{{{1,2},{3,0}}}} does not exist. >>

During evaluation of In[33]:= Part::partw: Part 2 of {{{{1,2},{3,0}}}} does not exist. >>

During evaluation of In[33]:= General::stop: Further output of Part::partw will be suppressed during this calculation. >>

Out[33]= {{1/(1 + Tr[{{{{1, 2}, {3, 0}}}}[[2, 2]]]),
Tr[{{{{1, 2}, {3, 0}}}}[[1, 2]]]/(
1 + Tr[{{{{1, 2}, {3, 0}}}}[[2, 2]]])}, {Tr[{{{{1, 2}, {3, 0}}}}[[2,
1]]]/(1 + Tr[{{{{1, 2}, {3, 0}}}}[[2, 2]]]),
Tr[{{{{1, 2}, {3, 0}}}}[[2, 2]]]/(
1 + Tr[{{{{1, 2}, {3, 0}}}}[[2, 2]]])}}


## closed as unclear what you're asking by Daniel Lichtblau, m_goldberg, eyorble, MarcoB, Henrik SchumacherDec 12 '18 at 16:42

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Can you explain these terms? Perhaps include a link to a definition? – mikado Dec 6 '18 at 20:10

I used to do those things years ago and I have a code which is supposed to be working but please check it and tell me if it is really correct:

partialtrace[a_List?MatrixQ, dim_, sys_] :=
Module[{mat, gtb, i, j},
mat = Partition[a, {dim[[2]], dim[[2]]}];
If[
sys == 1 , gtb = Tr[mat, Plus, 2] ,
gtb = Table[Tr[mat[[i, j]]], {i, dim[[1]]}, {j, dim[[1]]}]
];
normalize[gtb]
]

normalize[a_List?MatrixQ] := a/Tr[a]

partialtranspose[a_List?MatrixQ, dim_, sys_] :=
Module[{mat, pt},
mat = Partition[a, {dim[[2]], dim[[2]]}];
If[
sys == 1 , pt = ArrayFlatten[Transpose[mat]] ,
pt = ArrayFlatten[
Table[Transpose[mat[[i, j]]], {i, dim[[1]]}, {j, dim[[1]]}]]
];
Return[pt];
]


The first parameters of the functions is the matrix that you want to work on, the second element is the dimension of the each subspace for example {2,2} and the third element is the subspace that is traced or transposed either 1 or 2.

I think I have initially took them from a quantum information package and then changed them slightly.

EDIT: By the way it seems you haven't searched the SE enough because there are also this post and this post which can be useful.

• Thanks, @Buddha_the_Scientist. It would be nice if you could post an example to better understand the various notations in your code. – Zilch Dec 7 '18 at 9:05
• For example you have a composite system comprised of $2$ dimensional subsystems which corresponds to the density matrix: mat, and then you want to trace over the second system then you use the command partialtrace[mat,{2,2},2] – Mad_Maximus Dec 7 '18 at 9:13