扩散操作有点棘手。我觉得将它分解成碎片最容易:
这一切都变成了:
// 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 }
这是未编译的代码,因此可能存在一些拼写错误...