# How do two H gates act on two entangled qubits?

In this circuit, if the two qubits are initial in state 0, then after the oracle they are entangled and in state:

$$0.5 * (|00\rangle+|01\rangle+|10\rangle-|11\rangle)$$

My question is how do the two H gates act on them? Do they act like:

$$0.25 * [(|0\rangle+|1\rangle)\otimes(|0\rangle+|1\rangle) + (|0\rangle+|1\rangle)\otimes(|0\rangle-|1\rangle) + (|0\rangle-|1\rangle)\otimes(|0\rangle+|1\rangle) - (|0\rangle-|1\rangle)\otimes(|0\rangle-|1\rangle)]$$

which in the end nothing has changed. My intuition tells me that it's wrong.

Update: The figure above is from a circuit to implement Grover's algorithm. The whole figure is given below:

I have seen the answer that the state is indeed not changed after the two H gates, why they add them here?

• Welcome to QCSE, L霞客! After someone answers your question, you should avoid making changes to the question that render the existing answers invalid or incomplete. If you have additional questions, you can always submit more questions. This helps keep answers focused and helps you by attracting more attention to your problem :-) – Adam Zalcman Feb 6 at 15:44

Probably the best way to play and learn about the effect of specific gates on a state is with Quirk.

Here is the first situation you are describing (the $$H$$s and $$CZ$$):

You can hover over the amplitude graph to see the amplitude value for $$|11\rangle$$: $$-0.5$$ as you correctly have.

Add the second layer of $$H$$s to see its effect (here):

Not change! Your math is correct (and the intuition is not, as almost always when dealing with quantum computing :) )

The reason to add that layer in Grover is because the initialization, oracle, and diffuser are different stages that are meant to be generic. They situation you are described happens specifically with this oracle, but you could change the oracle and Grover will continue working.

The compilation process takes care of these redundancies and cancel them out. Here is your example on Qiskit:

from qiskit import *
circuit = QuantumCircuit(2)
# Initialization
circuit.h([0, 1])
# oracle
circuit.cz(0, 1)
# diffuser
circuit.h([0, 1])
circuit.z([0, 1])
circuit.cz(0, 1)
circuit.h([0, 1])

print(circuit)

     ┌───┐   ┌───┐┌───┐   ┌───┐
q_0: ┤ H ├─■─┤ H ├┤ Z ├─■─┤ H ├
├───┤ │ ├───┤├───┤ │ ├───┤
q_1: ┤ H ├─■─┤ H ├┤ Z ├─■─┤ H ├
└───┘   └───┘└───┘   └───┘


Before running, the circuit is optimized in a process that Qiskit calls "transpilation":

optimized_circuit = transpile(circuit, basis_gates=['cx', 'u3'], optimization_level=3)
print(optimized_circuit)

     ┌─────────────┐     ┌─────────────┐      ┌─────────────┐
q_0: ┤ U3(π/2,0,π) ├──■──┤ U3(π/2,π,π) ├───■──┤ U3(π/2,0,π) ├
└─────────────┘┌─┴─┐├─────────────┴┐┌─┴─┐└─────────────┘
q_1: ───────────────┤ X ├┤ U3(π/2,0,2π) ├┤ X ├───────────────
└───┘└──────────────┘└───┘


This final circuit is equivalent to yours and it is closer to the circuit that will run. However, presenting you this circuit as part of the Grover explanation is not very pedagogic, as it is important to first understand each stage.

• Hi, Thank you for your answer! I have updated the question and included the whole circuit. Since the state is indeed unchanged, could you please tell me why they add those gates here to implement Grover's algorithm? – L霞客 Feb 6 at 14:21
• Answer updated. – luciano Feb 6 at 16:34