# Controlled Z gate acting on 3 qubits in matrix form

For a controlled Z gate $$CZ_{1,2,3}$$ acting on 3 qubits, which of the following is correct? If it is the first one then what is the difference between that and a CZ gate acting on qubits 1 and 3?

$$I \otimes I \otimes I − |1⟩⟨1| \otimes I \otimes (Z − I)$$

$$I \otimes I \otimes I − |1⟩⟨1| \otimes |1⟩⟨1| \otimes (Z − I)$$

The difficulty is that the wording describing what you want is unclear. What your two stated options do is:

$$I \otimes I \otimes I − |1⟩⟨1| \otimes I \otimes (Z − I)$$

This is a standard controlled-phase gate acting between qubits 1 and 3, doing nothing to qubit 2.

$$I \otimes I \otimes I − |1⟩⟨1| \otimes |1⟩⟨1| \otimes (Z − I)$$

This is a controlled-controlled-phase gate acting on all three qubits, so it applies a -1 phase if all three are in the $$1\rangle$$ and leaves them unchanged otherwise. The symmetry of the system is perhaps better represented by writing it as $$I \otimes I \otimes I − 2|1⟩⟨1| \otimes |1⟩⟨1| \otimes |1⟩⟨1|.$$

Assuming the first qubit is the control, and then there are three target qubits, the $$CZZZ$$ gate would be:

$$|0\rangle\langle0| \otimes I \otimes I \otimes I + |1\rangle\langle1| \otimes Z \otimes Z \otimes Z$$

Essentially, the first term projects out the $$|0\rangle$$ state of the control qubit and applies the identity to the targets in that case, and the second projects out the $$|1\rangle$$ state and applies $$Z$$ to each of the targets in that case.

If you want a $$CCZ$$ gate (doubly-controlled $$Z$$), that's a little bit more complicated:

$$(I \otimes I - |1\rangle\langle1| \otimes |1\rangle\langle1|) \otimes I + |1\rangle\langle1| \otimes |1\rangle\langle1| \otimes Z$$

The second term projects out the $$|1\rangle$$ state of the two controls and applies the $$Z$$ in that state; the first term projects out the other state combinations and applies the identity in that case.

What do you mean by $$CZ_{1,2,3}$$? You should specify which ones are control and which ones are being acted on.

It is a (useful such as for graph states) coincidence that for $$CZ_{1,2}$$ you get the same result by interpreting either as the control and the other as being acted on.

$$\begin{pmatrix} 1&0&0&0\\ 0&1&0&0\\ 0&0&1&0\\ 0&0&0&-1\\ \end{pmatrix}$$

is invariant under the action of permuting the tensor factors. That is by conjugating by the following matrix:

$$\begin{pmatrix} 1&0&0&0\\ 0&0&1&0\\ 0&1&0&0\\ 0&0&0&1\\ \end{pmatrix}$$

but that is not generally true.

$$CU_{1,2}$$ with the $$1$$ qubit acting as control and $$2$$ being acted on by the unitary 2 by 2 matrix $$U$$ in that case. In total this would be the 4 by 4 unitary matrix (again in usual basis)

$$\begin{pmatrix} 1&0&0&0\\ 0&1&0&0\\ 0&0&u_{00}&u_{01}\\ 0&0&u_{10}&u_{11}\\ \end{pmatrix}$$

You can easily see that in this general case, it matters which is the control and which is acted on. It is not invariant under exchange of factors.

So the first thing you wrote is the result of interpreting the first as the control and acting on the third. The second thing you wrote means you have two controls. Both 1 and 2 are controls in that case. Again acting on the third.