View on GitHub
Open this notebook in GitHub to run it yourself
Setting Up the Problem
We choose this equation: where , are 2-bit unsigned integers. This equation has six solutions. The goal is to estimate the number of valid solutions out of the 16 possible inputs, with precision .Amplitude Estimation Using Phase Estimation
We first show how to use quantum phase estimation algorithm for quantum counting [3], then solve it using the IQAE method. Given a state such that we can measure up to arbitrary precision, given the following building blocks:- State preparation: A unitary such that: .
- Oracle: A unitary such that , which adds a phase to and does nothing to any orthognal states to .
Arithmetic Oracle
We define the oracle:State Preparation Oracle
The state preparation function reflects knowledge about the solution space and can be used to eliminate invalid assignments. Here we assume no knowledge of the solution space; hence, we use the uniform superposition state preparation.Wrapping All to the Phase Estimation
We will achieve the desired precision only in the IQAE phase. Here, we compute the worst-case precision for five phase qubits:Output:
grover_operator library function, where we plug in the defined oracles. We wrap it by the qpe function, that accepts the grover operator as a unitary, and additional phase register.
Synthesizing the Model to a Quantum Program
Output:
Executing the Quantum Program
phase_reg is already coded as fixed QNum in the range [0,1].
Output:

Output:
Amplitude Estimation Using Iterative Quantum Amplitude Estimation
Now we are ready for the iterative method. Instead of QPE, the algorithm applies the unitary where , the number of repetitions, changes between iterations of the algorithm. There is one subtlety that changes the way we work with the Grover operator. The classical algorithm expects an additional indicator qubit that marks the “good” states, i.e.: So now, most of our logic goes into the state preparation oracle (). It combines the loading of the solution space with setting the indicator qubit.Wrapping All to the Iterative Quantum Amplitude Estimation Algorithm
We use the built-inIQAE class that the quantum code for the algorithm as well as the classical execution code.
The circuit starts with the state , then applies iterations of the Grover operator.
Note that the algorithm applies a varied number of Grover iterations on each execution.
The number of iterations is chosen dynamically based on previous execution results, using statistical inference methods. It expects a state preparation function that creates the following state:
Where the indicator qubit is marking the wanted state.
Synthesizing the Model to a Quantum Program
Output:
Executing the Quantum Program
alpha is the tail probability of estimating the result with accuracy .
Output:
Output: