# 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);

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


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. Aug 6, 2019 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.