Skip to main content
added 32 characters in body
Source Link
glS
  • 7.8k
  • 1
  • 24
  • 64

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

enter image description here

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].

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_] := SparseArray[
    {
        {i_, j_} :> 1 /; Equal[
            Permute[indexToQubitIndices[i, numQubits], permutation],
            indexToQubitIndices[j, numQubits]
        ]
    },
    2^numQubits {1,1}
];
permutationMatrix[3, {1, 3, 2}] // Normal // MatrixForm

enter image description here

Note how you retrieve identity and Swap with permutationMatrix[2, {1, 2}] and permutationMatrix[2, {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, #]& /@ Permutations @ Range[4].

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[permutation_] := With[{numQubits = Length @ permutation},
    SparseArray[
        {
            {i_, j_} :> 1 /; Equal[
                Permute[indexToQubitIndices[i, numQubits], permutation],
                indexToQubitIndices[j, numQubits]
            ]
        },
        2^numQubits {1,1}
    ]
];
permutationMatrix[{1, 3, 2}] // Normal // MatrixForm

enter image description here

Note how you retrieve identity and Swap with permutationMatrix@{1, 2} and 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 /@ Permutations @ Range[4].

Source Link
glS
  • 7.8k
  • 1
  • 24
  • 64

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_] := SparseArray[
    {
        {i_, j_} :> 1 /; Equal[
            Permute[indexToQubitIndices[i, numQubits], permutation],
            indexToQubitIndices[j, numQubits]
        ]
    },
    2^numQubits {1,1}
];
permutationMatrix[3, {1, 3, 2}] // Normal // MatrixForm

enter image description here

Note how you retrieve identity and Swap with permutationMatrix[2, {1, 2}] and permutationMatrix[2, {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, #]& /@ Permutations @ Range[4].