Oracle Construction for Grover's Algorithm

In Mike and Ike's "Quantum Computation and Quantum Information", Grover's algorithm is explained in great detail. However, in the book, and in all explanations I have found online for Grover's algorithm, there seems to be no mention of how Grover's Oracle is constructed, unless we already know which state it is that we are searching for, defeating the purpose of the algorithm. Specifically, my question is this: given some f(x) such that for some x value, f(x)=1, but for all others, f(x)=0, how does one construct an oracle that will get us from our initial, arbitrary state |x>|y> to |x>|y+f(x)>? As much explicit detail as possible (perhaps an example?) would be greatly appreciated. If such a construction for any arbitrary function is possible with Hadamard, Pauli, or other standard quantum gates, a method for construction with these would be appreciated.

• "here seems to be no mention of how Grover's Oracle is constructed, unless we already know which state it is that we are searching for, defeating the purpose of the algorithm. " ... "Grover's Oracle" is the problem to be solved. You don't construct it. You're given (oracle access to) it and asked to perform computation to uncover the value. If it helps, pretend that I construct the oracle, and then ask you to solve the problem. (Also, note that reading/writing/preparing a database of $N$ items takes longer than running Grover's $\sqrt{N}$-time algorithm.) – Daniel Apon Jul 4 '17 at 16:38
• But what if instead of being given the oracle, we are given some f(x)? Imagine we are solving a 3-SAT problem and want to use Grover's to provide a speedup to the solution. We know the f(x) in question (the 3-SAT truth clauses), but don't necessarily know which bit string x will yield a true result when plugged into the 3-SAT. Mustn't there be a way to construct an oracle from the 3-SAT function to find the correct bit string? If there isn't, and it is as you suggest, something to be provided by someone else, Grover's algorithm seems rather artificial, merely an exercise given to you. – Will Jul 5 '17 at 18:11

The oracle is basically just an implementation of the predicate you want to search for a satisfying solution to.

For example, suppose you have a 3-sat problem:

(¬x1 ∨ ¬x3 ∨ ¬x4) ∧
(x2 ∨ x3 ∨ ¬x4) ∧
(x1 ∨ ¬x2 ∨ x4) ∧
(x1 ∨ x3 ∨ x4) ∧
(¬x1 ∨ x2 ∨ ¬x3)


Or, in table form with each row being a 3-clause, x meaning "this variable false", o meaning "this variable true", and space meaning "not in clause":

1 2 3 4
-------
x   x x
o o x
o x   o
x o x


Now make a circuit that computes whether the input is a solution, like this:

Now, to turn your circuit into an oracle, hit the output bit with a Z gate and uncompute any garbage you made (i.e. run the compute circuit in reverse order):

That's all there is to it. Compute the predicate, hit the result with a Z, uncompute the predicate. That's an oracle.

Iterate diffusion steps with oracle steps, and you've got yourself a grover search:

... although you should probably pick an example with fewer solutions, so the progress is gradual (instead of rotating along the start-state-solution-state plane by more than 90 degrees per step as my example is).

• Thanks, this was immensely helpful. Incredibly clear, answered everything I asked (and even used common quantum gates!) Is there any reason you decide to change all of your starting qubits to the |1> state before putting them in superposition with Hadamard gates instead of just putting the |0> state qubits through Hadamards (i.e. is there an advantage to this)? Also, what operation is that for your diffusion steps? Looks like controlled X, but are you using |1>'s or |0>'s as controls? – Will Jul 6 '17 at 18:03
• @Will I used a different starting state. You can do that as long as you also change the state negated by the diffusion operator (they have to match). All starting/diffusion states that have equal angle-distance to every computational basis state work equally well. The little circled-plus control in my diagram is an "X-axis control", which is equivalent to a normal control surrounded by Hadamard gates. It looks like a NOT because they're interchangeable. My starting/diffusion state is $(\frac{1}{\sqrt{2}} |0\rangle - \frac{1}{\sqrt{2}} |1\rangle)^{\otimes n}$. – Craig Gidney Jul 6 '17 at 18:43
• Fantastic answer, and thanks for the link to algassert.com/quirk ! – Frédéric Grosshans Jul 11 '17 at 13:17