Difference between revisions of "Twirl"

From QETLAB
Jump to navigation Jump to search
(Created page with "{{Function |name=Twirl |desc=Twirls a bipartite or multipartite operator |rel=IsotropicState<br />RandomUnitary<br />WernerState |upd=September 19, 2014 |v=1.00}} ...")
 
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
|name=Twirl
 
|name=Twirl
 
|desc=Twirls a bipartite or multipartite operator
 
|desc=Twirls a bipartite or multipartite operator
|rel=[[IsotropicState]]<br />[[RandomUnitary]]<br />[[WernerState]]
+
|rel=[[IsotropicState]]<br />[[Pauli]]<br />[[RandomUnitary]]<br />[[WernerState]]
|upd=September 19, 2014
+
|cat=[[List of functions#Superoperators|Superoperators]]
|v=1.00}}
+
|upd=November 27, 2014}}
<tt>'''Twirl'''</tt> is a [[List of functions|function]] that twirls an operator. That is, it implements the following superoperator:
+
<tt>'''Twirl'''</tt> is a [[List of functions|function]] that twirls an operator. That is, it implements a superoperator like the following one:
  
 
<math>X \mapsto \int_{U(d)} (U \otimes U)X(U \otimes U)^\dagger \, dU,</math>
 
<math>X \mapsto \int_{U(d)} (U \otimes U)X(U \otimes U)^\dagger \, dU,</math>
  
where integration is performed with respect to [http://en.wikipedia.org/wiki/Haar_measure Haar measure] on the unitary group. Multipartite twirling can also be performed, as can some other related twirls (in particular, twirling over the real orthogonal group and isotropic twirling). The output of this function is always sparse.
+
where integration is performed with respect to [http://en.wikipedia.org/wiki/Haar_measure Haar measure] on the unitary group. Multipartite twirling can also be performed, as can some other related twirls (in particular, twirling over the real orthogonal group, isotropic twirling, and Pauli twirling). The output of this function is always sparse.
  
 
==Syntax==
 
==Syntax==
Line 18: Line 18:
 
==Argument descriptions==
 
==Argument descriptions==
 
* <tt>X</tt>: A square operator to have its twirl computed.
 
* <tt>X</tt>: A square operator to have its twirl computed.
* <tt>TYPE</tt> (optional, by default <tt>'werner'</tt>): A string indicating what type of twirl should be performed. Can equal one of the following three values:
+
* <tt>TYPE</tt> (optional, by default <tt>'werner'</tt>): A string indicating what type of twirl should be performed. Can equal one of the following four values:
 
** If <tt>TYPE = 'werner'</tt> then the twirl performed is:<math>X \mapsto \int_{U(d)} (U \otimes U)X(U \otimes U)^\dagger \, dU,</math>where integration is performed with respect to Haar measure over the group of unitary matrices. Can also perform the natural multipartite generalization of the above integral (see the optional parameter <tt>P</tt>).
 
** If <tt>TYPE = 'werner'</tt> then the twirl performed is:<math>X \mapsto \int_{U(d)} (U \otimes U)X(U \otimes U)^\dagger \, dU,</math>where integration is performed with respect to Haar measure over the group of unitary matrices. Can also perform the natural multipartite generalization of the above integral (see the optional parameter <tt>P</tt>).
 
** If <tt>TYPE = 'isotropic'</tt> then the twirl performed is:<math>X \mapsto \int_{U(d)} (U \otimes \overline{U})X(U \otimes \overline{U})^\dagger \, dU,</math>where integration is performed with respect to Haar measure over the group of unitary matrices.
 
** If <tt>TYPE = 'isotropic'</tt> then the twirl performed is:<math>X \mapsto \int_{U(d)} (U \otimes \overline{U})X(U \otimes \overline{U})^\dagger \, dU,</math>where integration is performed with respect to Haar measure over the group of unitary matrices.
** If <tt>TYPE = 'real'</tt> then the twirl performed is:<math>X \mapsto \int_{O(d)} (O \otimes O)X(O \otimes O)^\dagger \, dO,</math>where integration is performed with respect to Haar measure over the group of real orthogonal matrices.
+
** If <tt>TYPE = 'real'</tt> then the twirl performed is:<math>X \mapsto \int_{O(d)} (O \otimes O)X(O \otimes O)^\dagger \, dO,</math>where integration is performed with respect to Haar measure over the group of real orthogonal matrices. Can also perform the natural multipartite generalization of the above integral (see the optional parameter <tt>P</tt>).
* <tt>P</tt> (optional, by default <tt>2</tt>): The number of parties that <tt>X</tt> acts on. That is, $X \in M_n^{\otimes P}$ for some $n$. Must equal <tt>2</tt> unless <tt>TYPE = 'werner'</tt>.
+
** If <tt>TYPE = 'pauli'</tt> then the twirl performed is:<math>X \mapsto \frac{1}{4^q}\sum_{\text{Paulis } Q}(Q \otimes Q)X(Q \otimes Q)^\dagger,</math>where $X \in M_{2^q} \otimes M_{2^q} \cong M_{4^q}$ for some $q \geq 1$.
 +
* <tt>P</tt> (optional, by default <tt>2</tt>): The number of parties that <tt>X</tt> acts on. That is, $X \in M_n^{\otimes P}$ for some $n$. Must equal <tt>2</tt> if <tt>TYPE = 'isotropic'</tt> or if <tt>TYPE = 'pauli'</tt>.
  
 
==Examples==
 
==Examples==
Line 31: Line 32:
  
 
where $P_S$ is the [[SymmetricProjection|projection onto the symmetric subspace]] and $P_A$ is the [[AntisymmetricProjection|projection onto the antisymmetric subspace]]. We can see this fact numerically as follows:
 
where $P_S$ is the [[SymmetricProjection|projection onto the symmetric subspace]] and $P_A$ is the [[AntisymmetricProjection|projection onto the antisymmetric subspace]]. We can see this fact numerically as follows:
<pre<noinclude></noinclude>>
+
<syntaxhighlight>
 
>> d = 2;
 
>> d = 2;
>> rho = [[RandomDensityMatrix|RandomDensityMatrix(d^2)]];
+
>> rho = RandomDensityMatrix(d^2);
>> PS = [[SymmetricProjection|SymmetricProjection(2)]];
+
>> PS = SymmetricProjection(2);
>> PA = [[AntisymmetricProjection|AntisymmetricProjection(2)]];
+
>> PA = AntisymmetricProjection(2);
 
>> full(Twirl(rho))
 
>> full(Twirl(rho))
  
Line 53: Line 54:
 
         0  -0.0029    0.2514        0
 
         0  -0.0029    0.2514        0
 
         0        0        0    0.2486
 
         0        0        0    0.2486
</pre>
+
</syntaxhighlight>
  
Multipartite twirling does not have such a simple formula. Nonetheless, the following code demonstrates the effect of twirling a 3-qubit state <tt>X</tt> using the <tt>Twirl</tt> function versus approximately twirling it by generating many [[RandomUnitary|Haar-uniform random unitary matrices]].
+
Multipartite Werner twirling does not have such a simple formula. Nonetheless, the following code demonstrates the effect of twirling a 3-qubit state <tt>X</tt> using the <tt>Twirl</tt> function versus approximately twirling it by generating many [[RandomUnitary|Haar-uniform random unitary matrices]].
<pre<noinclude></noinclude>>
+
<syntaxhighlight>
>> rho = [[RandomDensityMatrix|RandomDensityMatrix(8)]]; % random 3-qubit density matrix
+
>> rho = RandomDensityMatrix(8); % random 3-qubit density matrix
 
>> TX = Twirl(rho,'werner',3);
 
>> TX = Twirl(rho,'werner',3);
 
>> s = 1000000; % we will now compute an approximate twirl using this many unitary matrices
 
>> s = 1000000; % we will now compute an approximate twirl using this many unitary matrices
 
   approx_twirl = zeros(8);
 
   approx_twirl = zeros(8);
 
   for j = 1:s
 
   for j = 1:s
       U = [[RandomUnitary|RandomUnitary(2)]];
+
       U = RandomUnitary(2);
       approx_twirl = approx_twirl + [[Tensor|Tensor(U,U,U)]]*rho*Tensor(U,U,U)';
+
       approx_twirl = approx_twirl + Tensor(U,U,U)*rho*Tensor(U,U,U)';
 
   end
 
   end
 
>> norm(TX - approx_twirl/s) % TX is the twirl as computed by the Twirl function, approx_twirl/s is the approximate twirl computed by many random unitaries
 
>> norm(TX - approx_twirl/s) % TX is the twirl as computed by the Twirl function, approx_twirl/s is the approximate twirl computed by many random unitaries
Line 70: Line 71:
  
 
   1.6331e-04
 
   1.6331e-04
</pre>
+
</syntaxhighlight>
  
 
===Isotropic twirling===
 
===Isotropic twirling===
Line 78: Line 79:
  
 
We can see this fact numerically as follows:
 
We can see this fact numerically as follows:
<pre<noinclude></noinclude>>
+
<syntaxhighlight>
 
>> d = 2;
 
>> d = 2;
>> rho = [[RandomDensityMatrix|RandomDensityMatrix(d^2)]];
+
>> rho = RandomDensityMatrix(d^2);
>> psi = [[MaxEntangled|MaxEntangled(d)]];
+
>> psi = MaxEntangled(d);
 
>> full(Twirl(rho,'isotropic'))
 
>> full(Twirl(rho,'isotropic'))
  
Line 99: Line 100:
 
         0        0    0.2595        0
 
         0        0    0.2595        0
 
   -0.0191        0        0    0.2405
 
   -0.0191        0        0    0.2405
</pre>
+
</syntaxhighlight>
  
 
===Real orthogonal twirling===
 
===Real orthogonal twirling===
Twirling a quantum state by real orthogonal matrices always results in a linear combination of an isotropic state and a Werner state (equivalently, a state in the 3-dimensional space spanned by the [[SymmetricProjection|projection onto the symmetric subspace]], the [[AntisymmetricProjection|projection onto the antisymmetric subspace]], and the [[MaxEntangled|standard maximally-entangled pure state]]). More specifically, the following formula holds:
+
Twirling a bipartite quantum state by real orthogonal matrices always results in a linear combination of an isotropic state and a Werner state (equivalently, a state in the 3-dimensional space spanned by the [[SymmetricProjection|projection onto the symmetric subspace]], the [[AntisymmetricProjection|projection onto the antisymmetric subspace]], and the [[MaxEntangled|standard maximally-entangled pure state]]). More specifically, the following formula holds:
  
 
<center><math>\displaystyle\int_{O(d)} (O \otimes O)X(O \otimes O)^\dagger \, dO = \big( \langle\psi_+|X|\psi_+\rangle \big)|\psi_+\rangle\langle\psi_+| + \frac{\mathrm{Tr}\big(P_A X\big)}{\binom{d}{2}}P_A + \frac{\mathrm{Tr}\big( (P_S-|\psi_+\rangle\langle\psi_+|) X \big)}{\binom{d+1}{2}-1}(P_S - |\psi_+\rangle\langle\psi_+|).</math></center>
 
<center><math>\displaystyle\int_{O(d)} (O \otimes O)X(O \otimes O)^\dagger \, dO = \big( \langle\psi_+|X|\psi_+\rangle \big)|\psi_+\rangle\langle\psi_+| + \frac{\mathrm{Tr}\big(P_A X\big)}{\binom{d}{2}}P_A + \frac{\mathrm{Tr}\big( (P_S-|\psi_+\rangle\langle\psi_+|) X \big)}{\binom{d+1}{2}-1}(P_S - |\psi_+\rangle\langle\psi_+|).</math></center>
  
 
The following code generates the real orthogonal twirl of a random state in two different ways: first by using the Twirl function, and then by constructing 1000000 random (according to Haar measure) orthogonal matrices and averaging the value of $(O \otimes O)X(O \otimes O)^\dagger$ over them:
 
The following code generates the real orthogonal twirl of a random state in two different ways: first by using the Twirl function, and then by constructing 1000000 random (according to Haar measure) orthogonal matrices and averaging the value of $(O \otimes O)X(O \otimes O)^\dagger$ over them:
<pre<noinclude></noinclude>>
+
<syntaxhighlight>
>> rho = [[RandomDensityMatrix|RandomDensityMatrix(4)]];
+
>> rho = RandomDensityMatrix(4);
 
>> full(Twirl(rho,'real'))
 
>> full(Twirl(rho,'real'))
  
Line 121: Line 122:
 
   approx_twirl = zeros(4);
 
   approx_twirl = zeros(4);
 
   for j = 1:s
 
   for j = 1:s
       U = [[RandomUnitary|RandomUnitary(2,1)]]; % the second parameter being 1 ensures that this is an orthogonal matrix
+
       U = RandomUnitary(2,1); % the second parameter being 1 ensures that this is an orthogonal matrix
 
       approx_twirl = approx_twirl + kron(U,U)*rho*kron(U,U)';
 
       approx_twirl = approx_twirl + kron(U,U)*rho*kron(U,U)';
 
   end
 
   end
Line 132: Line 133:
 
   0.0002 - 0.0001i  -0.1079 + 0.0001i  0.2780 + 0.0000i  -0.0001 + 0.0000i
 
   0.0002 - 0.0001i  -0.1079 + 0.0001i  0.2780 + 0.0000i  -0.0001 + 0.0000i
 
   0.0517 + 0.0000i  0.0001 + 0.0000i  -0.0001 - 0.0000i  0.2219 + 0.0000i
 
   0.0517 + 0.0000i  0.0001 + 0.0000i  -0.0001 - 0.0000i  0.2219 + 0.0000i
</pre>
+
</syntaxhighlight>
 +
 
 +
Real orthogonal twirling of a multipartite state does not have such a simple closed-form formula, but it always results in a linear combination of the $p!$ [[PermutationOperator|operators that permute the $p$ tensor factors]] and all of their [[PartialTranspose|partial transpositions]]. The following code generates the real orthogonal twirl of a random 3-qubit state:
 +
<syntaxhighlight>
 +
>> rho = RandomDensityMatrix(8); % random 3-qubit density matrix
 +
>> full(Twirl(rho,'real',3))
 +
 
 +
ans =
 +
 
 +
  Columns 1 through 5
 +
 
 +
  0.1194 + 0.0000i  0.0000 + 0.0000i  0.0000 + 0.0000i  -0.0035 + 0.0093i  0.0000 + 0.0000i
 +
  0.0000 + 0.0000i  0.1525 - 0.0000i  -0.0091 - 0.0094i  0.0000 + 0.0000i  -0.0053 + 0.0241i
 +
  0.0000 + 0.0000i  -0.0091 + 0.0094i  0.1101 + 0.0000i  0.0000 + 0.0000i  0.0181 - 0.0148i
 +
  -0.0035 - 0.0093i  0.0000 + 0.0000i  0.0000 + 0.0000i  0.1179 + 0.0000i  0.0000 + 0.0000i
 +
  0.0000 + 0.0000i  -0.0053 - 0.0241i  0.0181 + 0.0148i  0.0000 + 0.0000i  0.1179 + 0.0000i
 +
  -0.0151 - 0.0055i  0.0000 + 0.0000i  0.0000 + 0.0000i  0.0181 - 0.0148i  0.0000 + 0.0000i
 +
  0.0039 + 0.0148i  0.0000 + 0.0000i  0.0000 + 0.0000i  -0.0053 + 0.0241i  0.0000 + 0.0000i
 +
  0.0000 + 0.0000i  0.0039 - 0.0148i  -0.0151 + 0.0055i  0.0000 + 0.0000i  -0.0035 + 0.0093i
 +
 
 +
  Columns 6 through 8
 +
 
 +
  -0.0151 + 0.0055i  0.0039 - 0.0148i  0.0000 + 0.0000i
 +
  0.0000 + 0.0000i  0.0000 + 0.0000i  0.0039 + 0.0148i
 +
  0.0000 + 0.0000i  0.0000 + 0.0000i  -0.0151 - 0.0055i
 +
  0.0181 + 0.0148i  -0.0053 - 0.0241i  0.0000 + 0.0000i
 +
  0.0000 + 0.0000i  0.0000 + 0.0000i  -0.0035 - 0.0093i
 +
  0.1101 + 0.0000i  -0.0091 + 0.0094i  0.0000 + 0.0000i
 +
  -0.0091 - 0.0094i  0.1525 - 0.0000i  0.0000 + 0.0000i
 +
  0.0000 + 0.0000i  0.0000 + 0.0000i  0.1194 + 0.0000i
 +
</syntaxhighlight>
 +
 
 +
===Pauli twirl of a channel===
 +
A more common way to think of Pauli twirling is as the mapping that sends a superoperator $\Phi : M_{2^q} \rightarrow M_{2^q}$ to the superoperator $\Phi_P : M_{2^q} \rightarrow M_{2^q}$ defined as follows:
 +
: <math>\Phi_P(X) := \frac{1}{4^q}\sum_{\text{Paulis } Q} Q\Phi(Q^\dagger XQ)Q^\dagger.</math>
 +
Applying the <tt>Twirl</tt> function to the Choi matrix of $\Phi$ implements this Pauli twirl on that channel:
 +
<syntaxhighlight>
 +
>> Phi = RandomSuperoperator(4); % random 2-qubit channel
 +
>> PhiP = Twirl(Phi,'pauli'); % this is the Choi matrix of the Pauli twirl of Phi
 +
</syntaxhighlight>
  
 
{{SourceCode|name=Twirl}}
 
{{SourceCode|name=Twirl}}

Latest revision as of 01:51, 28 November 2014

Twirl
Twirls a bipartite or multipartite operator

Other toolboxes required none
Related functions IsotropicState
Pauli
RandomUnitary
WernerState
Function category Superoperators

Twirl is a function that twirls an operator. That is, it implements a superoperator like the following one\[X \mapsto \int_{U(d)} (U \otimes U)X(U \otimes U)^\dagger \, dU,\]

where integration is performed with respect to Haar measure on the unitary group. Multipartite twirling can also be performed, as can some other related twirls (in particular, twirling over the real orthogonal group, isotropic twirling, and Pauli twirling). The output of this function is always sparse.

Syntax

  • TX = Twirl(X)
  • TX = Twirl(X,TYPE)
  • TX = Twirl(X,TYPE,P)

Argument descriptions

  • X: A square operator to have its twirl computed.
  • TYPE (optional, by default 'werner'): A string indicating what type of twirl should be performed. Can equal one of the following four values:
    • If TYPE = 'werner' then the twirl performed is\[X \mapsto \int_{U(d)} (U \otimes U)X(U \otimes U)^\dagger \, dU,\]where integration is performed with respect to Haar measure over the group of unitary matrices. Can also perform the natural multipartite generalization of the above integral (see the optional parameter P).
    • If TYPE = 'isotropic' then the twirl performed is\[X \mapsto \int_{U(d)} (U \otimes \overline{U})X(U \otimes \overline{U})^\dagger \, dU,\]where integration is performed with respect to Haar measure over the group of unitary matrices.
    • If TYPE = 'real' then the twirl performed is\[X \mapsto \int_{O(d)} (O \otimes O)X(O \otimes O)^\dagger \, dO,\]where integration is performed with respect to Haar measure over the group of real orthogonal matrices. Can also perform the natural multipartite generalization of the above integral (see the optional parameter P).
    • If TYPE = 'pauli' then the twirl performed is\[X \mapsto \frac{1}{4^q}\sum_{\text{Paulis } Q}(Q \otimes Q)X(Q \otimes Q)^\dagger,\]where $X \in M_{2^q} \otimes M_{2^q} \cong M_{4^q}$ for some $q \geq 1$.
  • P (optional, by default 2): The number of parties that X acts on. That is, $X \in M_n^{\otimes P}$ for some $n$. Must equal 2 if TYPE = 'isotropic' or if TYPE = 'pauli'.

Examples

Werner twirling

Werner twirling a quantum state always results in a Werner state. More specifically, in the bipartite case we have the simple formula

\(\displaystyle\int_{U(d)} (U \otimes U)X(U \otimes U)^\dagger \, dU = \frac{\mathrm{Tr}\big( P_S X \big)}{\binom{d+1}{2}}P_S + \frac{\mathrm{Tr}\big(P_A X\big)}{\binom{d}{2}}P_A,\)

where $P_S$ is the projection onto the symmetric subspace and $P_A$ is the projection onto the antisymmetric subspace. We can see this fact numerically as follows:

>> d = 2;
>> rho = RandomDensityMatrix(d^2);
>> PS = SymmetricProjection(2);
>> PA = AntisymmetricProjection(2);
>> full(Twirl(rho))

ans =

    0.2486         0         0         0
         0    0.2514   -0.0029         0
         0   -0.0029    0.2514         0
         0         0         0    0.2486

>> full(trace(PS*rho)*PS/nchoosek(d+1,2) + trace(PA*rho)*PA/nchoosek(d,2))

ans =

    0.2486         0         0         0
         0    0.2514   -0.0029         0
         0   -0.0029    0.2514         0
         0         0         0    0.2486

Multipartite Werner twirling does not have such a simple formula. Nonetheless, the following code demonstrates the effect of twirling a 3-qubit state X using the Twirl function versus approximately twirling it by generating many Haar-uniform random unitary matrices.

>> rho = RandomDensityMatrix(8); % random 3-qubit density matrix
>> TX = Twirl(rho,'werner',3);
>> s = 1000000; % we will now compute an approximate twirl using this many unitary matrices
   approx_twirl = zeros(8);
   for j = 1:s
       U = RandomUnitary(2);
       approx_twirl = approx_twirl + Tensor(U,U,U)*rho*Tensor(U,U,U)';
   end
>> norm(TX - approx_twirl/s) % TX is the twirl as computed by the Twirl function, approx_twirl/s is the approximate twirl computed by many random unitaries

ans =

   1.6331e-04

Isotropic twirling

Isotropic twirling a quantum state always results in an isotropic state. More specifically, if $|\psi_+\rangle$ is the standard maximally-entangled pure state then we have

\(\displaystyle\int_{U(d)} (U \otimes \overline{U})X(U \otimes \overline{U})^\dagger \, dU = \big( \langle\psi_+|X|\psi_+\rangle \big)|\psi_+\rangle\langle\psi_+| + \frac{\mathrm{Tr}\big((I - |\psi_+\rangle\langle\psi_+|) X\big)}{d^2-1}(I - |\psi_+\rangle\langle\psi_+|).\)

We can see this fact numerically as follows:

>> d = 2;
>> rho = RandomDensityMatrix(d^2);
>> psi = MaxEntangled(d);
>> full(Twirl(rho,'isotropic'))

ans =

    0.2405         0         0   -0.0191
         0    0.2595         0         0
         0         0    0.2595         0
   -0.0191         0         0    0.2405

>> (psi'*rho*psi)*(psi*psi') + trace((eye(d^2)-psi*psi')*rho)*(eye(d^2)-psi*psi')/(d^2-1)

ans =

    0.2405         0         0   -0.0191
         0    0.2595         0         0
         0         0    0.2595         0
   -0.0191         0         0    0.2405

Real orthogonal twirling

Twirling a bipartite quantum state by real orthogonal matrices always results in a linear combination of an isotropic state and a Werner state (equivalently, a state in the 3-dimensional space spanned by the projection onto the symmetric subspace, the projection onto the antisymmetric subspace, and the standard maximally-entangled pure state). More specifically, the following formula holds:

\(\displaystyle\int_{O(d)} (O \otimes O)X(O \otimes O)^\dagger \, dO = \big( \langle\psi_+|X|\psi_+\rangle \big)|\psi_+\rangle\langle\psi_+| + \frac{\mathrm{Tr}\big(P_A X\big)}{\binom{d}{2}}P_A + \frac{\mathrm{Tr}\big( (P_S-|\psi_+\rangle\langle\psi_+|) X \big)}{\binom{d+1}{2}-1}(P_S - |\psi_+\rangle\langle\psi_+|).\)

The following code generates the real orthogonal twirl of a random state in two different ways: first by using the Twirl function, and then by constructing 1000000 random (according to Haar measure) orthogonal matrices and averaging the value of $(O \otimes O)X(O \otimes O)^\dagger$ over them:

>> rho = RandomDensityMatrix(4);
>> full(Twirl(rho,'real'))

ans =

    0.2219         0         0    0.0517
         0    0.2781   -0.1079         0
         0   -0.1079    0.2781         0
    0.0517         0         0    0.2219

>> s = 1000000; % we will now compute an approximate twirl using this many unitary matrices
   approx_twirl = zeros(4);
   for j = 1:s
       U = RandomUnitary(2,1); % the second parameter being 1 ensures that this is an orthogonal matrix
       approx_twirl = approx_twirl + kron(U,U)*rho*kron(U,U)';
   end
>> approx_twirl/s

ans =

   0.2220 + 0.0000i   0.0000 - 0.0001i   0.0002 + 0.0001i   0.0517 - 0.0000i
   0.0000 + 0.0001i   0.2781 + 0.0000i  -0.1079 - 0.0001i   0.0001 - 0.0000i
   0.0002 - 0.0001i  -0.1079 + 0.0001i   0.2780 + 0.0000i  -0.0001 + 0.0000i
   0.0517 + 0.0000i   0.0001 + 0.0000i  -0.0001 - 0.0000i   0.2219 + 0.0000i

Real orthogonal twirling of a multipartite state does not have such a simple closed-form formula, but it always results in a linear combination of the $p!$ operators that permute the $p$ tensor factors and all of their partial transpositions. The following code generates the real orthogonal twirl of a random 3-qubit state:

>> rho = RandomDensityMatrix(8); % random 3-qubit density matrix
>> full(Twirl(rho,'real',3))

ans =

  Columns 1 through 5

   0.1194 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0035 + 0.0093i   0.0000 + 0.0000i
   0.0000 + 0.0000i   0.1525 - 0.0000i  -0.0091 - 0.0094i   0.0000 + 0.0000i  -0.0053 + 0.0241i
   0.0000 + 0.0000i  -0.0091 + 0.0094i   0.1101 + 0.0000i   0.0000 + 0.0000i   0.0181 - 0.0148i
  -0.0035 - 0.0093i   0.0000 + 0.0000i   0.0000 + 0.0000i   0.1179 + 0.0000i   0.0000 + 0.0000i
   0.0000 + 0.0000i  -0.0053 - 0.0241i   0.0181 + 0.0148i   0.0000 + 0.0000i   0.1179 + 0.0000i
  -0.0151 - 0.0055i   0.0000 + 0.0000i   0.0000 + 0.0000i   0.0181 - 0.0148i   0.0000 + 0.0000i
   0.0039 + 0.0148i   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0053 + 0.0241i   0.0000 + 0.0000i
   0.0000 + 0.0000i   0.0039 - 0.0148i  -0.0151 + 0.0055i   0.0000 + 0.0000i  -0.0035 + 0.0093i

  Columns 6 through 8

  -0.0151 + 0.0055i   0.0039 - 0.0148i   0.0000 + 0.0000i
   0.0000 + 0.0000i   0.0000 + 0.0000i   0.0039 + 0.0148i
   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0151 - 0.0055i
   0.0181 + 0.0148i  -0.0053 - 0.0241i   0.0000 + 0.0000i
   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0035 - 0.0093i
   0.1101 + 0.0000i  -0.0091 + 0.0094i   0.0000 + 0.0000i
  -0.0091 - 0.0094i   0.1525 - 0.0000i   0.0000 + 0.0000i
   0.0000 + 0.0000i   0.0000 + 0.0000i   0.1194 + 0.0000i

Pauli twirl of a channel

A more common way to think of Pauli twirling is as the mapping that sends a superoperator $\Phi : M_{2^q} \rightarrow M_{2^q}$ to the superoperator $\Phi_P : M_{2^q} \rightarrow M_{2^q}$ defined as follows: \[\Phi_P(X) := \frac{1}{4^q}\sum_{\text{Paulis } Q} Q\Phi(Q^\dagger XQ)Q^\dagger.\] Applying the Twirl function to the Choi matrix of $\Phi$ implements this Pauli twirl on that channel:

>> Phi = RandomSuperoperator(4); % random 2-qubit channel
>> PhiP = Twirl(Phi,'pauli'); % this is the Choi matrix of the Pauli twirl of Phi

Source code

Click here to view this function's source code on github.