Q#中如何实现Grover扩散算子?

How to realize the Grover's Diffusion Operator in Q#?

正如标题所说,如何在Q#中实现Grover's Diffusion Operator?我知道它被定义为 2 ⟨s|s⟩ - I,其中 |s⟩ 是任意数量的量子位的统一状态。这可以进一步定义为夹在一对 H-gates 之间的 Z0(看到它称为 U0)门。我无法在 quantum primitive 和 canon 文档中找到任何以 Grover、diff 等可能名称开头的函数。

我不想使用函数 AmpAmpByOracle,因为它是非常高级的实现,我的理解并不清晰。我想实现一个函数,该函数采用 oracle Uf(我假设未知)和它采用的量子比特数 (N),并通过简单地遵循 Grover's Algorithm | Wikipedia 中给出的电路并通过以下方式测量所需状态来执行 Grover 算法在 r = approx(2^(N/2)) 迭代结束时测量所有 N 个量子位。

扩散操作有点棘手。我发现将它分解成碎片最简单:

  1. 正如您所指出的,在 X 基础上查看扩散操作要简单得多。如果你将 H 应用于前后的每个量子位,那么在中间,均匀状态看起来像 000...0 状态。
  2. 扩散操作(在 X 基础上)在 000...0 状态上为 -1,在所有其他基础状态上为恒等式 (+1)。第一步是挑出000...0状态;我可以使用多控制 X 门来做到这一点——除了我需要先将所有量子位从 0 翻转到 1(并且 反之亦然 ),因为受控操作寻找 1,不是0。当然,在控制 X 之后,我需要撤消翻转。
  3. 要生成-1,我可以从|->状态的ancilla开始,这样X会把它变成-|->。
  4. 完成所有操作后,我需要重置 ancilla,以便 return 它处于 |0> 状态。

这一切变成:

// 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
}

这是未编译的代码,所以可能会有一些拼写错误...