# 量子回路の説明

4キュービットの線形クラスターを生成する回路があります。

1. 4つのキュービットを初期化して、$$| 0000 \ rangle$$にします。

2. すべてにアダマール$$H$$を適用します。

3. 次に、制御された$$Z$$ゲートを適用します。

これはすべて明らかですが、私には明確ではありません

1. ワイヤー1と4で2つのアダマールは何をしていますか？これはどういう意味ですか？

We need to go throught the gates one by one to understand what's happening. We have to keep a few things in mind.

1. $$H|0\rangle=\frac{1}{\sqrt2}(|0\rangle + |1\rangle) = |+\rangle$$ and $$H|1\rangle=\frac{1}{\sqrt2}(|0\rangle - |1\rangle) = |-\rangle$$.
2. In the Hadamard basis the $$Z$$ gate acts like the $$X$$ gate. Namely $$Z|+\rangle=|-\rangle$$ and $$Z|-\rangle=|+\rangle$$.
3. The controlled Z gate $$cZ$$ does the following here $$cZ|0\rangle \otimes |\pm\rangle = |0\rangle \otimes |\pm\rangle$$ and $$cZ|1\rangle \otimes |\pm\rangle = |1\rangle \otimes |\mp\rangle$$.

Now we start in the state $$|0000\rangle$$ and this gets converted to $$|++++\rangle$$ by the first column of Hadamard. Lets focus on the first 2 qubits. The $$cZ$$ gate takes the state $$|++\rangle = \frac{1}{\sqrt2}(|0\rangle + |1\rangle)|+\rangle$$ and transforms it into

$$cZ\frac{1}{\sqrt2}(|0\rangle + |1\rangle)|+\rangle = \frac{1}{\sqrt2}(|0\rangle \otimes |+\rangle + |1\rangle \otimes |-\rangle) = \frac{1}{\sqrt2}(|+\rangle \otimes |0\rangle + |-\rangle \otimes |1\rangle)$$

The last expression can be derived using a expanding and rearranging of the terms.

We now move onto the 2nd $$cZ$$ gate which is applied on the 2nd and 3rd qubit. Remember the 3rd and 4th qubit both are still in $$|+\rangle$$ state. Application of the next $$cZ$$ state can be written as follows.

$$I \otimes cZ\frac{1}{\sqrt2}(|+\rangle \otimes |0\rangle + |-\rangle \otimes |1\rangle)|+\rangle \\ = \frac{1}{\sqrt2}(I|+\rangle \otimes cZ|0\rangle|+\rangle + I|-\rangle \otimes cZ|1\rangle|+\rangle) \\ = \frac{1}{\sqrt2}(|+\rangle|0\rangle|+\rangle + |-\rangle|1\rangle|-\rangle) \\ = \frac{1}{2}(|+\rangle|0\rangle|0\rangle + |+\rangle|0\rangle|1\rangle + |-\rangle|1\rangle|0\rangle - |-\rangle|1\rangle|1\rangle) \\ = \frac{1}{2}\big((|+\rangle|0\rangle + |-\rangle|1\rangle) \otimes |0\rangle + (|+\rangle|0\rangle - |-\rangle|1\rangle) \otimes |1\rangle)\big)$$

We now apply the final $$cZ$$ gate on the 3rd and 4th qubits. Remember the 4th qubit is still in $$|+\rangle$$ state. $$I \otimes I \otimes cZ \frac{1}{2}\big((|+\rangle|0\rangle + |-\rangle|1\rangle) \otimes |0\rangle + (|+\rangle|0\rangle - |-\rangle|1\rangle) \otimes |1\rangle)\big) \otimes |0\rangle \\ = \frac{1}{2}\big((|+\rangle|0\rangle + |-\rangle|1\rangle) \otimes cZ|0\rangle|+\rangle + (|+\rangle|0\rangle - |-\rangle|1\rangle) \otimes cZ|1\rangle|+\rangle)\big) \\ = \frac{1}{2}\big((|+\rangle|0\rangle + |-\rangle|1\rangle) \otimes |0\rangle|+\rangle + (|+\rangle|0\rangle - |-\rangle|1\rangle) \otimes |1\rangle|-\rangle)\big) \\ = \frac{1}{2}\big(|+\rangle|0\rangle|0\rangle|+\rangle + |-\rangle|1\rangle|0\rangle|+\rangle + |+\rangle|0\rangle|1\rangle|-\rangle - |-\rangle|1\rangle|1\rangle|-\rangle\big)$$

Now the final step is the application of the 2 $$H$$ gates on 1st and 4th qubit. From the final expression above, we can clearly see that only the 1st and 4th qubit are in Hadamard basis. Hence to convert them back to computational basis we apply $$H$$ again.

Finally after applying $$H$$ on these qubits we get $$\frac{1}{2}H\otimes I \otimes I \otimes H\big(|+\rangle|0\rangle|0\rangle|+\rangle + |-\rangle|1\rangle|0\rangle|+\rangle + |+\rangle|0\rangle|1\rangle|-\rangle - |-\rangle|1\rangle|1\rangle|-\rangle\big) \\ = \frac{1}{2}\big(|0\rangle|0\rangle|0\rangle|0\rangle + |1\rangle|1\rangle|0\rangle|0\rangle + |0\rangle|0\rangle|1\rangle|1\rangle - |1\rangle|1\rangle|1\rangle|1\rangle\big) \\ = \frac{1}{2}\big(|0000\rangle + |1100\rangle + |0011\rangle - |1111\rangle\big)$$

This is the exact answer as you said the circuit would give. Hence I suppose the purpose of the $$H$$ is to convert the 1st and 4th qubits back into computational basis.

Because $$CNOT = I\otimes H \cdot CZ \cdot I\otimes H$$ as was mentioned here, and because $$CZ(q_1, q_2) = CZ(q_2, q_1)$$, we can rewrite the circuit in this way (by adding Hadamards as needed):

The link to the circuit created with Quirk. As one can see from the circuit above, after the first Hadamard gate and two $$CNOT$$ gates, we will have a GHZ state for the first three qubits:

$$|\psi_1 \rangle = \frac{1}{\sqrt{2}} \left( |000\rangle + |111\rangle\right) |0\rangle$$

$$|\psi_2 \rangle = \frac{1}{2} \big( | 000 \rangle + | 001 \rangle + | 110 \rangle - | 111 \rangle \big) |0\rangle$$
And after the final $$CNOT$$ gate:
$$|\psi_3 \rangle = \frac{1}{2} \big( | 0000 \rangle + | 0011 \rangle + | 1100 \rangle - | 1111 \rangle \big)$$