13.3 Grover’s algorithm
In Chapter 12, we studied Grover’s algorithm in detail, and, as you surely remember, there are two main ingredients that it relies on: a Boolean function oracle that encodes the elements that we want to find and Grover’s diffusion operator. In Section 10.3.1, we gave a general (but not necessarily efficient) way of implementing oracles, so we will just focus on coding Grover’s diffusion operator.
Grover’s diffusion operator is in charge of performing the inversion about the mean operation. As we discussed in Section 12.2.3, this can be implemented with the following circuit:

We already know how to write Qiskit code for every gate in the circuit with the only exception being the multi-controlled Z gate. In order to implement it, we will just use a ZGate object (which will instantiate a Z gate) and add to it a bunch of controls with the ZGate (). control ( n -1) instruction. Thus, we can use the following function...