Like the title says, how to realize the Grover's Diffusion Operator in Q#? I know it's defined as 2 ⟨s|s⟩ - I where |s⟩ is the uniform state for any arbitrary number of qubits. This can further be defined in terms of Z0 (saw it called U0) gate sandwiched by a pair of H-gates. I was unable to find any function in the quantum primitive and canon docs starting with possible names like Grover, diff etc.
I don't want to use the function AmpAmpByOracle since it is very high level implementation and doesn't clear my understanding. I want to implement a function that takes a oracle Uf(unknown to me suppose) and the number of qubit it takes(N) and perform the Grover's algorithm by simply following the circuit given in Grover's Algorithm | Wikipedia and measure the required state by measuring all the N qubits at the end of r = approx(2^(N/2)) iterations.
The diffusion operation is a bit tricky. I find it easiest to decompose it into pieces:
This all turns into:
// register is the Qubit[] that we want to apply the diffusion operation to
using (ancillae = Qubit[1])
{
  let ancilla = ancillae[0];
  X(ancilla); // Puts the ancilla into the |1> state
  H(ancilla); // And now into the |-> state
  ApplyToEach(H, register);  // Put the register qubits into the X basis
  ApplyToEach(X, register);  // Flip 0->1 and 1->0
  (Controlled X)(register, ancilla);  // Do the controlled flip of the ancilla
  ApplyToEach(X, register);  // Undo the flip
  ApplyToEach(H, register);  // Undo the basis change
  H(ancilla); // Put the ancilla back into |1>
  X(ancilla); // And back to |0> so we can return it
}
This is uncompiled code, so there might be some typos...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With