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

Ind*_*jee 5 quantum-computing q#

如标题所述,如何在Q#中实现Grover扩散算子?我知道它的定义是任意数量的qubit的统一状态2 ?s|s? - I在哪里|s?。这可以进一步用夹在一对H门之间的Z0(称为U0)门来定义。我无法在量子图元和佳能文档中找到任何以可能的名称(例如Grover,diff等)开头的函数。

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

小智 4

扩散操作有点棘手。我发现最简单的方法是将其分解为几部分:

  1. 正如您所指出的,查看 X 基础中的扩散操作要简单得多。如果将 H 应用于之前和之后的每个量子位,那么在中间,均匀状态看起来像 000...0 状态。
  2. 扩散运算(在 X 基中)在 000...0 状态下为 -1,在所有其他基状态上为恒等式 (+1)。第一步是挑选000...0状态;我可以使用多控制 X 门来做到这一点 - 除非我需要首先将所有量子位从 0 翻转到 1(反之亦然,因为受控操作寻找 1,而不是 0。当然,在控制 X 之后,我需要撤消翻转。
  3. 为了生成 -1,我可以从 |-> 状态的辅助开始,这样 X 就会将其变成 -|->。
  4. 完成所有操作后,我需要重置辅助功能,以便可以将其返回到 |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
}
Run Code Online (Sandbox Code Playgroud)

这是未编译的代码,因此可能存在一些拼写错误......