# What are the correct gates for the following unitary matrix?

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.

This circuit implements the required unitary:

Code:

from qiskit import QuantumCircuit

circ = QuantumCircuit(3)
circ.cz(0, 1)
circ.cz(0, 2)
circ.ccz(0, 1, 2)


To check:

from qiskit.quantum_info import Operator
from qiskit.visualization import array_to_latex

array_to_latex(Operator(circ))

• Many thanks! Could you tell me how you got it? Any tip about how to generalize it to n qubits? Commented Oct 16, 2023 at 9:39
• The matrix is diagonal with $+1$ and $-1$ entries only. So, we can implement it using $Z, CZ, CCZ$ gates only. Now, the first $CZ$ will give us $diag(1, 1, 1, -1, 1, 1, 1, -1)$. The second $CZ$ will give us $diag(1, 1, 1, 1, 1, -1, 1, -1)$. Multiply them together to get $diag(1, 1, 1, -1, 1, -1, 1, 1)$. So, we need $CCZ$ to provide the remaining $-1$ Commented Oct 16, 2023 at 9:52
• Is there a way to generalize it to n qubits? I tried your appproach with 4 qubits but seems not to work. Commented Oct 16, 2023 at 10:23

This can be solved semi-mechanically.

$$\newcommand{\ket}[1]{|{#1}\rangle}\newcommand{\bra}[1]{\langle{#1}|}\newcommand{\+}{\oplus}$$This matrix $$U$$ is of form $$U\ket{abc}=(-1)^{f(abc)}\ket{abc}$$, for boolean variables $$a, b, c$$. Here, $$f(a, b, c)$$ is a boolean function.

And we can read the table for the boolean function $$f$$: $$f(abc)=1$$ iff $$abc=011, 101$$, or $$111$$. (I think it really depends on the ordering we take on the tensor product basis vectors, but, anyway...)

And, if you know the boolean table, then you can write it as a boolean polynomial: $$f(abc)=(1\+a)bc\+a(1\+b)c\+abc$$, since $$f$$ is true iff $$\neg a\wedge b\wedge c$$, or $$a\wedge\neg b\wedge c$$, or $$a\wedge b\wedge c$$ (corresponding to $$011$$, $$101$$, and $$111$$), and those 'OR's are in fact exclusive ORs, since these three cannot be true at the same time.

But, $$(1\+a)bc\+a(1\+b)c\+abc=bc\+ac\+abc\+abc\+abc=bc\+ac\+abc$$.

Then, we have $$U\ket{abc}=(-1)^{bc+ac+abc}\ket{abc}=(-1)^{bc}(-1)^{ac}(-1)^{abc}\ket{abc}$$.