Quantum Amplitude Estimation#

QAE(args, state_function, oracle_function, kwargs_oracle={}, precision=None, target=None)[source]#

This method implements the canonical quantum amplitude estimation (QAE) algorithm by Brassard et al..

The problem of quantum amplitude estimation is described as follows:

  • Given a unitary operator A, let |Ψ=A|0.

  • Write |Ψ=|Ψ1+|Ψ0 as a superposition of the orthogonal good and bad components of |Ψ.

  • Find an estimate for a=Ψ1|Ψ1, the probability that a measurement of |Ψ yields a good state.

Parameters:
argsQuantumVariable or list[QuantumVariable]

The (list of) QuantumVariables which represent the state, the quantum amplitude estimation is performed on.

state_functionfunction

A Python function preparing the state |Ψ. This function will receive the variables in the list args as arguments in the course of this algorithm.

oracle_functionfunction

A Python function tagging the good state |Ψ1. This function will receive the variables in the list args as arguments in the course of this algorithm.

kwargs_oracledict, optional

A dictionary containing keyword arguments for the oracle. The default is {}.

precisionint, optional

The precision of the estimation. The default is None.

targetQuantumFloat, optional

A target QuantumFloat to perform the estimation into. The default is None. If given neither a precision nor a target, an Exception will be raised.

Returns:
resQuantumFloat

A QuantumFloat encoding the angle θ as a fraction of π, such that a~=sin2(θ) is an estimate for a.

More precisely, we have θ=πyM for y{0,,M1} and M=2precision. After measurement, the estimate a~=sin2(θ) satisfies

|aa~|2πM+π2M2

with probability of at least 8/π2.

Examples

We define a function that prepares the state |Ψ=cos(π8)|0+sin(π8)|1 and an oracle that tags the good state |1. In this case, we have a=sin2(π8).

from qrisp import z, ry, QuantumBool, QAE
import numpy as np

def state_function(qb):
    ry(np.pi/4,qb)

def oracle_function(qb):   
    z(qb)

qb = QuantumBool()

res = QAE([qb], state_function, oracle_function, precision=3)
>>> res.get_measurement()
{0.125: 0.5, 0.875: 0.5}

That is, after measurement we find θ=π8 or θ=7π8 with probability 12, respectively. Therefore, we obtain the estimate a~=sin2(π8) or a~=sin2(7π8). In this case, both results coincide with the exact value a.

Numerical integration

Here, we demonstarate how to use QAE for numerical integration.

Consider a continuous function f:[0,1][0,1]. We wish to evaluate

A=01f(x)dx.

For this, we set up the corresponding state_function acting on the input_list:

from qrisp import QuantumFloat, QuantumBool, control, z, h, ry, QAE
import numpy as np

n = 6 
inp = QuantumFloat(n,-n)
tar = QuantumBool()
input_list = [inp, tar]

Here, N=2n is the number of sampling points the function f is evaluated on. The state_function acts as

|0|01Nx=0N1|x(1f(x/N)|0+f(x/N)|1).

Then the probability of measuring 1 in the target state tar is

p(1)=1Nx=0N1f(x/N),

which acts as an approximation for the value of the integral A.

The oracle_function, therefore, tags the |1 state of the target state:

def oracle_function(inp, tar):
    z(tar)

For example, if f(x)=sin2(x) the state_function can be implemented as follows:

def state_function(inp, tar):
    h(inp)

    N = 2**inp.size
    for k in range(inp.size):
        with control(inp[k]):
            ry(2**(k+1)/N,tar)

Finally, we apply QAE and obtain an estimate a for the value of the integral A=0.27268.

prec = 6
res = QAE(input_list, state_function, oracle_function, precision=prec)
meas_res = res.get_measurement()

theta = np.pi*max(meas_res, key=meas_res.get)
a = np.sin(theta)**2
>>> a
0.26430