microsoft / QuantumKatas

Tutorials and programming exercises for learning Q# and quantum computing
MIT License
4.54k stars 1.22k forks source link

[GroversAlgorithm] Emphasize global phase in "Conditional phase flip" task #491

Closed tcNickolas closed 4 years ago

tcNickolas commented 4 years ago

Context

GroversAlgorithm kata has a task "Conditional phase flip", which asks the learner to "Flip the sign of the state of the register if it is not in the |0...0⟩ state.". However, the reference implementation flips the sign of the state if it is in |0...0⟩ state, i.e., implements that transformation up to a global phase of -1. The hint in the task suggests that these two transformations are equivalent, and the tester checks unitary equality up to a global phase.

This is fine if this implementation is used for Grover's search algorithm itself, since the global phase will remain global and not be observable. However, if this implementation is used as a building block for other algorithms, in particular quantum counting algorithm, which converts the global phase difference into the relative phase, this can cause very interesting bugs (see an extended discussion in this Quantum Computing SE question).

Improvement

I would leave the testing harness as is (since for the purposes of Grover's algorithm both implementations are correct!), but put in a bit of effort into clarifying the difference and where it can come into play; so modifying the hint (and in the matching notebook) to explain that for the purpose of Grover's algorithm these transformations will be the same, but if you're using it for something else you'll need an exact implementation. I'd also modify both reference solutions to include a comment along the lines "To fix the global phase difference, use the following line" and a line that uses the R gate to add the right global phase. This way the reference solutions would implement the exact transformation.

Manvi-Agrawal commented 4 years ago

Kindly find the new PR below

Manvi-Agrawal commented 4 years ago

Recreating a new PR due to git issues