# Is it possible to change the oracle in Grover's search to ponderate probabilities in multiple values search?

If I want to to search 2 values with Grover's algorithm, it outputs the same probability for both that is bigger that the no searched states that have the same lower percent.

If I use an oracle matrix with all 1's in the diagonals except on the searched states that are -1.

But suppose you are going to find the drink that you want. For example, I like coke 57% , and pepsi 10%, the rest are 1%.

If I change the -1 in those two values in the oracle matrix for example to near 1.5 for coke and 0.5 for pepsi, it will output more probability from bigger to lower: coke module 0.57, pepsi module 0.39 and the others randomly to 0.13.

So is it possible in real life to do this? I ask because I don't know how I can change it in Q# reflection.

/// Reflection RM about the marked state.
operation ReflectMarked (markedQubit : Qubit) : Unit {
R1(PI(), markedQubit);
}

/// Reflection about the |00…0〉 state.
/// A register of n qubits initially in the |00…0〉 state.
operation ReflectZero (databaseRegister : Qubit[]) : Unit {

let nQubits = Length(databaseRegister);

for (idxQubit in 0 .. nQubits - 1) {
X(databaseRegister[idxQubit]);
}

Controlled Z(databaseRegister[1 .. nQubits - 1], databaseRegister[0]);

for (idxQubit in 0 .. nQubits - 1) {
X(databaseRegister[idxQubit]);
}
}

• Hi Luis! Although I think there's probably a reasonable question here about a Q# implementation of a modified version of Grover's algorithm, it is not so clear in the wording of the original question. I've taken to lightly editing your question for clarity; I hope I did not modify the spirit of your question. In the future, please make an effort for formatting your question. – Mark S Jul 9 '19 at 19:18
• For example, please remember to capitalize proper names such as Grover's, and to capitalize I (and spell out "you"). – Mark S Jul 9 '19 at 19:19

This questions seems to be less about Q# implementation and more about the algorithm itself. It is not possible to implement the transformation you described, since it is not unitary. Consider an example for just 1 bit (to distinguish between Coke and Pepsi in your example): the matrix would have to be

$$U = \begin{bmatrix} 1.5 & 0 \\ 0 & 0.5 \end{bmatrix}$$

and the condition of it being a unitary transformation is $$U^\dagger U = I$$, which clearly doesn't hold:

$$U^\dagger U = \begin{bmatrix} 2.25 & 0 \\ 0 & 0.25 \end{bmatrix} \neq \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} = I$$

For a transformation with a diagonal matrix to be unitary, all elements on the diagonal must have absolute values of 1, which is not the case for your example.

If you have detailed information about which outcome you want to get with which probability, you can just prepare a superposition state that will give you these outcomes with these probabilities when measured, i.e.

$$0.57|Coke\rangle + 0.1|Pepsi\rangle + \alpha \sum_{other\ drinks} |drink\rangle$$

(here $$\alpha$$ is a normalization coefficient that depends on the number of other drinks you consider).

You can use the Q# library operation PrepareArbitraryState to do this.

• Thanks, but to prepare the superposition there must be a place in the brain with that info. Also I can change the oracle diagonal for 3 Qbits like 1 | -1.2 | -0.8 | 1| 1 | 1 | 1 | 1, and I will get more % for state 1 than for state 2, no matter the unitary sum is not exactly 1. I'm suggesting that Grovers is our brain selecting memory and the oracle constains our preferences, because Grover have stability and random (unexpected result)for evoution. – Luis ALberto Aug 6 '19 at 11:17

As far as Grover's Search Algorithm is concerned, it will amplify each marked state to the maximum possible probability (which may not be 1) and then de-amplify to minimum value cyclically depending on the number of marked states and the number of iterations. However, if the initial probability of marked states is different then they will reach their maximum probability at different iterations of the algorithm.