# Quantum Circuit explaination

I have a circuit that generates a 4 qubit linear cluster.

The steps i understand are

1. Initialize the 4 qubits to $$|0000\rangle$$.

2. Apply Hadamard $$H$$ on all.

3. Then apply a controlled $$Z$$ gate .

All this is clear, but what is not clear to me is

1. What are the two Hadamards doing on wire 1 and 4? what does this mean?

At the end of the circuit we get the state as $$|a\rangle=\frac{1}{2}(|0000\rangle+|0011\rangle+|1100\rangle-|1111\rangle)$$ Can somebody explain the two hadamards to me?

• There is nothing special in the last 2 Hadamard gates, they are just Hadamard gates; what exactly you want to be explained? Jun 19 '20 at 11:54

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.

• Yes, thanks for the step-wise explanation. Jun 19 '20 at 18:09

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)$$
• On the third wire you have done a hadamard twice since that is identity?? And then used one of them two get $H.cZ.H=cX$?? Jun 23 '20 at 21:10
• @Upstart, there are several ways to obtain the circuit. One way is to replace all $cZ$ with $I \otimes H \cdot cX \cdot I \otimes H$ and then to take into account that $HH = I$. Note that the upper $cZ$ is flipped before applying these identities (the roles of control and target qubits for $cZ$ can be changed). Jun 23 '20 at 21:42