# Amplitude Estimation¶

The amplitude estimation function creates a segment that estimates the probability of the "good" states of a quantum state [1]. A quantum state can be decomposed into "good" and "bad" states. The amplitude estimation algorithm takes a state preparation circuit and uses it with Grover's operator to assess the probability of the good states. The algorithm ends with an inverse QFT applied to the evaluation qubits, which can be measured to obtain the required probability.

## Syntax¶

Function: AmplitudeEstimation

Parameters:

• grover_operator: GroverOperator. The Grover operator used in the algorithm is composed of the oracle and the state preparation operator.
• estimation_register_size: PositiveInt. The number of qubits used to estimate the amplitude. A bigger register provides a better estimate of the good states' amplitude.

## Example¶

qmod
{
"functions": [
{
"name": "main",
"port_declarations": {
"phase": {
"name": "phase",
"size": {
"expr": "1"
},
"direction": "output"
}
},
"output_ports_wiring": {
"phase": "phase_output_Identity:phase->Output:phase"
},
"body": [
{
"function": "AmplitudeEstimation",
"function_params": {
"grover_operator": {
"oracle_params": {
"expression": "a == b",
"definitions": {
"a": {
"size": 2,
"bounds": [
0.0,
3.0
],
"name": "a"
},
"b": {
"size": 1,
"bounds": [
0.0,
1.0
],
"name": "b"
}
}
},
"state_preparation_params": {
"probabilities": {
"pmf": [
0.15,
0.19,
0.11,
0.27,
0.0,
0.01,
0.0,
0.27
]
},
"error_metric": {
"KL": {
"upper_bound": 0.0
}
}
}
},
"estimation_register_size": 3
},
"outputs": {
"ESTIMATED_AMPLITUDE_OUTPUT[0]": "AmplitudeEstimation_cs4id_Hltlgs:ESTIMATED_AMPLITUDE_OUTPUT[0]->phase_output_Identity:phase[0]",
"ESTIMATED_AMPLITUDE_OUTPUT[1]": "AmplitudeEstimation_cs4id_Hltlgs:ESTIMATED_AMPLITUDE_OUTPUT[1]->phase_output_Identity:phase[1]",
"ESTIMATED_AMPLITUDE_OUTPUT[2]": "AmplitudeEstimation_cs4id_Hltlgs:ESTIMATED_AMPLITUDE_OUTPUT[2]->phase_output_Identity:phase[2]"
},
"name": "AmplitudeEstimation_cs4id_Hltlgs"
},
{
"function": "Identity",
"params": {},
"function_params": {
"arguments": [
{
"size": 3,
"bounds": [
0.0,
7.0
],
"name": "phase"
}
]
},
"control_states": [],
"inputs": {
"phase[0]": "AmplitudeEstimation_cs4id_Hltlgs:ESTIMATED_AMPLITUDE_OUTPUT[0]->phase_output_Identity:phase[0]",
"phase[1]": "AmplitudeEstimation_cs4id_Hltlgs:ESTIMATED_AMPLITUDE_OUTPUT[1]->phase_output_Identity:phase[1]",
"phase[2]": "AmplitudeEstimation_cs4id_Hltlgs:ESTIMATED_AMPLITUDE_OUTPUT[2]->phase_output_Identity:phase[2]"
},
"inouts": {},
"outputs": {
"phase": "phase_output_Identity:phase->Output:phase"
},
"name": "phase_output_Identity"
}
]
}
],
"classical_functions": [
{
"name": "cmain",
"body": [
{
"name": "result",
"var_type": {
"kind": "histogram"
}
},
{
"invoked_expression": {
"function": "sample",
"target_function": "main"
},
"assigned_variable": "result"
},
{
"saved_variable": "result"
},
{
"name": "estimation",
"var_type": {
"kind": "real"
}
},
{
"invoked_expression": {
"function": "qae_with_qpe_result_post_processing",
"params": {
"estimation_register_size": {
"expr": "3"
},
"estimation_method": {
"expr": "1"
},
"result": {
"expr": "result"
}
}
},
"assigned_variable": "estimation"
},
{
"saved_variable": "estimation"
}
]
}
],
"constraints": {
"optimization_parameter": "width"
}
}

from classiq import Model, synthesize, show, set_constraints, execute
from classiq.builtin_functions import (
AmplitudeEstimation,
ArithmeticOracle,
GroverOperator,
StatePreparation,
)
from classiq.execution import QaeWithQpeEstimationMethod
from classiq.model import Constraints, OptimizationParameter

oracle_params = ArithmeticOracle(
expression="a == b",
definitions=dict(a=dict(size=2), b=dict(size=1)),
)
state_preparation_params = StatePreparation(
probabilities=[0.15, 0.19, 0.11, 0.27, 0, 0.01, 0, 0.27],
error_metric={"KL": {"upper_bound": 0}},
)
grover_operator_params = GroverOperator(
oracle_params=oracle_params,
state_preparation_params=state_preparation_params,
)
qae_params = AmplitudeEstimation(
grover_operator=grover_operator_params,
estimation_register_size=3,
)

model = Model()
qae_out_wires = model.AmplitudeEstimation(params=qae_params)
model.set_outputs({"phase": qae_out_wires["ESTIMATED_AMPLITUDE_OUTPUT"]})
model.sample()
model.post_process_amplitude_estimation(
estimation_register_size=3, estimation_method=QaeWithQpeEstimationMethod.BEST_FIT
)

constraints = Constraints(optimization_parameter=OptimizationParameter.WIDTH)
serialized_model = model.get_model()
serialized_model = set_constraints(serialized_model, constraints)
quantum_program = synthesize(serialized_model)
show(quantum_program)

qae_result = execute(quantum_program)

print(f"The probability estimation of the good states is: {qae_result[1].value}")

print(f"The accurate expected result is: {0.15 + 0.01}")


The example above shows a circuit that implements amplitude estimation. The good basis states are defined (in the SDK code) through oracle_params of grover_operator_params.

The quantum circuit for preparing the state to be estimated is defined by state_preparation, that can load different probabilities for different states.

estimation_register_size is the number of evaluation qubits used in the inverse QFT, in addition to the qubits from the state preparation. A higher parameter leads to a more accurate result.

The output of the inverse QFT block is used to calculate the estimated probability for the good states.

Note that the amplitude estimation post-processing function expects only one output which is the phase register of the Phase Estimation.

The result of the execution is the estimated probability:

The probability estimation of the good states: 0.16404107641230342
The accurate expected result: 0.16


Note that setting estimation_method to be QaeWithQpeEstimationMethod.BEST_FIT gives a much higher resolution than the legacy approach of taking the highest bars by setting estimation_method to QaeWithQpeEstimationMethod.MAXIMUM_LIKELIHOOD, which limits the result by 2**estimation_register_size possible values.

## References¶

[1] G. Brassard, P. Hoyer, M. Mosca, and A. Tapp, “Quantum Amplitude Amplification and Estimation,” arXiv:quant-ph/0005055, vol. 305, pp. 53–74, 2002, doi: 10.1090/conm/305/05215. https://arxiv.org/abs/quant-ph/0005055