Assuming I understand what you're asking, you want a Mathematica implementation of the operator $P_\pi$ with action $$P_\pi = \sum_{i_1,..., i_n} |\pi(i_1)\cdots \pi(i_n)\rangle\!\langle i_1,...,i_n|,$$ for arbitrary permutations $\pi\in S_n$. The only minor issue to do this is that if you want this to act as a standard matrix, you'll have to convert indices of input vectors into lists of indices corresponding to each qubit. You can do this for example doing a base-2 decomposition (assuming we're talking qubits; otherwise just use the suitable basis). Here's a possible implementation:
indexToQubitIndices[n_, numQubits_] := PadLeft[IntegerDigits[n - 1, 2], numQubits];
permutationMatrix[numQubits_, permutation_]permutationMatrix[permutation_] := With[{numQubits = Length @ permutation},
SparseArray[
{
{i_, j_} :> 1 /; Equal[
Permute[indexToQubitIndices[i, numQubits], permutation],
indexToQubitIndices[j, numQubits]
]
},
2^numQubits {1,1}
]
];
permutationMatrix[3, permutationMatrix[{1, 3, 2}] // Normal // MatrixForm
Note how you retrieve identity and Swap with permutationMatrix[2, permutationMatrix@{1, 2}]
and permutationMatrix[2, permutationMatrix@{2, 1}]
, respectively.
If you now want all permutations, just use Permutations@{1, 2, 3, 4}
to generate all permutations and use them as the second element for permutationMatrix
above. Eg something like permutationMatrix[4, #]&permutationMatrix /@ Permutations @ Range[4]
.