In this tutorial, we implement a Quantum Monte Carlo Integration (QMCI) circuit to estimate π.The approach samples lattice points on a discretized grid and uses Quantum Amplitude Estimation (QAE) to estimate the fraction of points that lie inside a quarter circle.This fraction directly determines the value of π.Following Ref.~[1], lattice-based uniform sampling is appropriate for the present π-estimation problem because the task reduces to estimating the fraction of marked grid points. In this setting, QAE estimates the corresponding amplitude with a quadratic speedup over classical Monte Carlo methods.This should be distinguished from classical grid-based quadrature, whose cost generally scales exponentially with the dimension. In contrast, the QAE-based QMCI approach can avoid this curse of dimensionality at the level of amplitude estimation. In more general QMCI applications, such as finance, the practical advantage further depends on whether the required probability distribution and oracle can be implemented efficiently.
The goal of this tutorial is to estimate π from the area ratio between a square and an inscribed quarter circle. We sample lattice points (x,y) on a discretized square and determine whether each point lies inside the quarter circle. If the fraction of points inside the quarter circle is denoted by α, thenα=4π,and thereforeπ=4α.Two n-qubit registers are used to represent the x- and y-coordinates, respectively:0≤x≤2n−1,0≤y≤2n−1.Thus, the computational basis corresponds to 22n lattice points in a square of side length 2n. We define the indicator functionf(x,y)={10if x2+y2<22n,if x2+y2≥22n.With uniform sampling,p(x,y)=22n1,the target quantity is the expectation valueα=x=0∑2n−1y=0∑2n−1f(x,y)p(x,y).This is the probability that a uniformly sampled lattice point lies inside the quarter circle.To estimate this quantity on a quantum computer, we prepare the uniform superposition over all lattice points usingP^=H⊗2n,so that(P^⊗I)∣0⟩2n∣0⟩=22n1x=0∑2n−1y=0∑2n−1∣x,y⟩2n∣0⟩.Then an oracle R^ marks whether each point is inside the quarter circle by writing the value of f(x,y) into an ancilla qubit:R^∣x,y⟩2n∣0⟩=∣x,y⟩2n∣f(x,y)⟩.Thus,R^(P^⊗I)∣0⟩2n∣0⟩=22n1x=0∑2n−1y=0∑2n−1∣x,y⟩2n∣f(x,y)⟩.Therefore, the probability of measuring the ancilla qubit in the state ∣1⟩ is exactly α. By applying Quantum Amplitude Estimation (QAE), we estimate this amplitude more efficiently than by classical Monte Carlo integration, and finally computeπ=4α.Compared with classical grid-based quadrature, whose cost grows exponentially with the dimension, this QAE-based approach avoids the curse of dimensionality.Compared with classical Monte Carlo integration, QAE provides a quadratic speedup in the estimation error. In this π-estimation task, lattice-based uniform sampling is appropriate because the problem reduces to estimating the fraction of marked grid points. In more general QMCI applications, such as finance, the advantage additionally depends on whether the required probability distribution and oracle can be implemented efficiently.
result = execute(qprog).result_value()phases_counts = dict( (sampled_state.state["phase_reg"], sampled_state.shots) for sampled_state in result.parsed_counts)
expected_alpha = np.sin(np.pi * max(phases_counts, key=phases_counts.get))print("QMCI pi = ", expected_alpha * 4)print("exact pi = ", np.pi)
Output:
QMCI pi = 3.695518130045147 exact pi = 3.141592653589793
This result indicates that QMCI successfully approaches the value of π, although the current estimate still has a significant error compared with the exact value. In the next cell, we investigate how accurately π can be estimated by applying IQAE and examining its achievable precision.
For the numerical test, we use the standard Classiq IAQE routine.The two main parameters are the target accuracy ϵ and the failure probability α.We setϵ=0.03,α=0.01.Here, ϵ specifies the target additive accuracy of the amplitude estimation, while α specifies the allowed failure probability. Thus, α=0.01 corresponds to a 99 confidence level.This setting is suitable for an initial validation because it keeps the estimation cost moderate while still requiring a reliable confidence level.Once the implementation is verified, ϵ can be reduced for a higher-precision estimation.
In this experiment, we examine whether the estimated value approaches the exact solution as the number of qubits increases.Because a larger number of qubits allows exponentially more sampling points to be represented, the discretization becomes finer, and the estimate is expected to become more accurate. By tracking this trend, we can directly evaluate how increasing the qubit number improves the precision of the π estimation.
How much computational advantage can be achieved? According to Ref. [2], the query complexity of QMCI is given byNquery≈ϵC⋅ln!(α2).By contrast, in the classical grid-based calculation considered here, all lattice points are evaluated explicitly. Hence, the computational cost istclassical=22n=O(1/ϵ2).The table below shows a theoretical comparison of the required number of evaluations and queries.
Item
Classical Grid-Based Integration
Quantum Monte Carlo Integration (IQAE)
n=2
24=16
12,716
n=4
28=256
12,716
n=10
220=1,048,576
12,716
This comparison indicates that the quantum method does not provide an advantage at small scales, but becomes more efficient than the classical approach once the problem size is sufficiently large.