Let's consider a set of $N = 2^n$ binary values $S_i \in \left\{-1, 1\right\}$ and define the diagonal matrix $W$ as a quantum unitary operator acting on a system of $n$ qubits: $$ W = \begin{pmatrix} S_1 & 0 & 0 & 0 & 0\\ 0 & S_2 & 0 & 0 & 0\\ 0 & 0 & \dots & 0 & 0\\ 0 & 0 & 0 & S_{N-1} & 0\\ 0 & 0 & 0 & 0 & S_N \end{pmatrix} $$ Is it possible to build a parameterized quantum circuit $U(\theta)$ to get all and only the unitary operators written as $W$ (with just $1$ or $-1$ on the main diagonal) varying the values of the $\theta$ parameters?
For example, if I consider the matrix: $$ W_1 = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & -1 & 0\\ 0 & 0 & 0 & 1\\ \end{pmatrix} $$ the equivalent circuit can be built using the following Qiskit code:
import numpy as np
from qiskit import QuantumCircuit, transpile
W1 = np.diag([1, 1, -1, 1])
qc = QuantumCircuit(2)
qc.unitary(W1, qubits=[0, 1])
transpile(qc, basis_gates=['u', 'cx']).draw('mpl')
However, if I consider another matrix as for instance $$ W_2 = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & -1 & 0 & 0\\ 0 & 0 & -1 & 0\\ 0 & 0 & 0 & 1\\ \end{pmatrix} $$ the same piece of code gives me a different circuit:
How can I build an ansatz for all the quantum unitary operators written like $W$?