# 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]]])}}

• Can you explain these terms? Perhaps include a link to a definition? Dec 6, 2018 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. Dec 7, 2018 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]
– user59583
Dec 7, 2018 at 9:13
• @user59583 Please can you show me how to use partialtranspose in case of a system comprised of three qubits? May 8, 2020 at 15:58