Inversion About the Average

Inversion about the average is a way of converting phase differences into magnitude differences. It is simple to understand, easy to implement, and a key part of Grover's Algorithm, the quantum search algorithm.

Set Up

Suppose we have a state vector. Each component is a probability amplitude.

import math
import numpy as np

# There are 4 possible outcomes and each has a 25% chance of occurring
P_V = np.array([1, 1, 1, 1] / math.sqrt(4))

To get the probability of measuring a state, we multiply the probability amplitude by its complex conjugate.

def calculate_probabilities(amplitudes):
    return [amplitude * amplitude.conjugate() for amplitude in amplitudes]p

Each state corresponds to one of the possible outcomes, so the sum of the square of the probability amplitudes must be unity, 100%.

inversion-about-the-average_ca6b7d7af8c5cc9852404a2b326dc0787195cca0.png 1

In our state vector, every outcome is equally likely. The state probability amplitudes and state probabilities can be represented, respectively, by the graphs in figure 1 and figure 2

amp1.png prob1.png

Suppose we had a way to flip the state we want to measure, a selective phase flip. If the state we want to measure is the third state, for example, then we would have the following vector.

# There are 4 possible outcomes and each has a 25% chance of occurring
P_V = np.array([1, 1, -1, 1] / math.sqrt(4))

The corresponding measurements are seen in figure 3 and figure 4.

amp2.png prob2.png

We can perform a selective phase flip using unitary operations, but that's outside the scope of this blog post. For now, lets just assume we can.

Note that even with the phase flip, each state still has a 25% probability of being measured. It's as if we made no change at all, but that's not true. Inversion about the average allows us to transform a difference in phase into a difference in magnitude.

Inversion About the Average

As noted by Noson S. Yanofsky and Mirco A. Mannucci in Quantum Computing for Computer Scientists, an average can be geometrically interpreted as "the number such that the sum of the lengths of the lines above the average is the the same as the sum of the lengths of the lines below."

Try it Again

We can repeat the operation to increase the magntiude more. Unfortunately, we can over do it. Performing phase shifts and then average around the mean will at first raise the magnitude of our goal, but will eventually decrease the magnitude.


The pattern is cyclical. It follows the pattern of a sin wave. The following equaiton gives how many points are required to land on a peak:

inversion-about-the-average_1af6ee4002f09ccb624079359a9f62dfae20fae5.png 2

inversion-about-the-average_b9321c1d418912f7acbf82779d62ee2f2390d42e.png is the number of iterations, inversion-about-the-average_7c2bb95389ccf01922863d5fcee4da65f2c26e83.png is the size of the search space and inversion-about-the-average_b0a272239ff60dd1dd3fa31dfcf4516418ecabb0.png is the number of correct answers. This equation, however, assumes inversion-about-the-average_7ba020bd7bcabc79bb48e4a80c09ba0f83637b06.png. For our example, this doesn't hold; there's only four inputs. I've fudged the inversion-about-the-average_5beee75a5e301998a422480e2ae0d874cb617dc8.png wave to make it match the input values as close as I can. The periodicity of inversaion about the average should be clear.



In this article, I described inversion about the average using code samples and visuals. Inversion about the average is periodic; too many applications will reverse the effects.