I need a set of gates, even controlled, which are providing the following unitary matrix with three qubits:
$$ U = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & -1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 \\ \end{bmatrix}. $$
The problem is that I do not know any general method to express a unitary matrix in terms of Pauli matrices. In case, I managed to obtained something similar to the one above by applying a Z-gate to the first qubit and a multicontrolled-Z gate (can be obtained with multicontrolled X gate and two Hadamard gates) to any qubit and using the other two as controls (it seems it doesn't matter the application). The matrix in this case reads as:
$$ U = \begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & -1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & -1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\ \end{bmatrix}. $$ So, the first and the last block are exchanged.The code to obtain it is:
qc.z(0)
qc.h(0) # H on target qubit
qc.mcx([1,2], 0, mode='noancilla')
qc.h(0) # again H on target qubit
In general, I need this for any number of qubits.