Use this file to discover all available pages before exploring further.
View on GitHub
Open this notebook in GitHub to run it yourself
Monte Carlo integration refers to estimating expectation values of a function f(x), where x is a random variable drawn from some known distribution p:Ep(x)=∫f(x)p(x)dx.(1)Such evaluations appear in the context of option pricing or credit risk analysis.The basic idea of QMCI assumes that we have a quantum function A, which, for a given f and p, loads the following state of n+1 qubits:A∣0⟩n∣0⟩=i=0∑2n−1fipi∣i⟩n∣1⟩+i=0∑2n−11−fipi∣i⟩n∣0⟩=a∣ψ1⟩+1−a∣ψ0⟩,(2)where it is understood that the first 2n states represent a discretized space of x, and that 0≤f(x)≤1.
Then, by applying the amplitude estimation (AE) algorithm for the “good-state” ∣ψ1⟩, we can estimate its amplitude:a=i=0∑2n−1fipi.The QMCI algorithm can be separated into two parts:
Constructing a Grover operator for the specific problem.
This is done here almost from scratch.
Applying the AE algorithm based on the Grover operator [1].
This is done by calling the Classiq Quantum Phase Estimation (QPE) function.
For simplicity we consider a simple use case. We take a probability distribution on the integerspi=Ni for i∈{0,…23−1},(3)where N is a normalization constant, and we would like to evaluate the expectation value of the functionf(x)=sin2(0.25x+0.2).(4)Therefore, the value we want to evaluate isa=N1k=0∑7sin2(0.25k+0.2)k≈0.834.*This tutorial illustrats how to construct a Quantum Monte Carlo Integration (QMCI), defining all its building blocks in Qmod (rather than using open-library functions).The example below demonstrates how we can exploit various concepts of modeling quantum algorithms with Classiq when building our own functions*.
The Grover operator suitable for QMCI is defined as follows:Q≡−Sψ1A†S0A,with S0 and Sψ1 being reflection operators around the zero state ∣0⟩n∣0⟩ and the good-state ∣ψ1⟩, respectively, and the function A is defined in Eq. (2).In subsections (1.1)-(1.3) below we build each of the quantum sub-functions, and then in subsection (1.4) we combine them to define a complete Grover operator. On the way we introduce several concepts of functional modeling, which allow the Classiq synthesis engine to reach better optimized circuits.
We start with constructing the A operator in Eq. (2). We define a quantum function and give it the name state_loading.The function’s signature declares two arguments:
A quantum register x declared as QArray (an array of qubits with an unspecified size) that is used to represent the discretization of space.
A quantum register ind of size 1 declared as QBit to indicate the good state.
Next, we construct the logic flow of the state_loading function.The function body consists of two quantum function calls:
As can be seen from Eq. (2), the load_probabilities function is constructed using the Classiq inplace_prepare_state function call on n=3 qubits with probabilities pi.
The amplitude_loading body calls the Classiq linear_pauli_rotations function.
The linear_pauli_rotations loads the amplitude of the function f(x)=sin2(0.25x+0.2).Note: The amplitude should be sin so the probability is sin2.The function uses an auxiliary qubit that is utilized so that the desired probability reflects on the auxiliary qubit if it is in the |1> state.We use the function with the Pauli Y matrix and enter the appropriate slope and offset to achieve the right parameters.We define the probabilities according to the specific problem described by Eqs. (3-4).
The next quantum function we define is the one that reflects around the good state: any n+1 state in which the ind register is at state ∣1⟩.This function can be constructed with a ZGate on the ind register.
To implement the Grover Diffuser we aim to perform a controlled-Z operation on the ∣0>n state.We can define a zero_oracle quantum function with the x and ind registers as its arguments.The within_apply operator takes two function arguments—compute and action—and invokes the sequence compute(), action(), and invert(compute()).Quantum objects that are allocated and prepared by compute are subsequently uncomputed and released.
The inplace xor operation, ind ^= x==0, is equivalent to control(x==0, X(ind)).
We can verify that∣00…0⟩ctrl(−Z)(target=q0,ctrl=q1…qn)−∣00…0⟩,∣10…0⟩ctrl(−Z)(target=q0,ctrl=q1…qn)∣10…0⟩,∣11…0⟩ctrl(−Z)(target=q0,ctrl=q1…qn)∣11…0⟩,∣11…1⟩ctrl(−Z)(target=q0,ctrl=q1…qn)∣11…1⟩,which is exactly the functionality we want.
Applying Amplitude Estimation (AE) with Quantum Phase Estimation (QPE)
Here we apply a basic AE algorithm that is based on QPE.The idea behind this algorithm is the following:The state A∣0⟩n∣0⟩ is spanned by two eigenvectors of our Grover operator Q, with the two corresponding eigenvaluesλ±=exp(±i2πθ),sin2(πθ)≡a.(5)Therefore, if we apply a QPE on A∣0⟩n∣0⟩, we have these two eigenvalues encoded in the QPE register. However, both give the value of a, so there is no ambiguity.To find a we build a simple quantum model, applying A on a quantum register of size n+1 initialized to zero, and then applying the Classiq QPE with the my_grover_operator we defined.Below is the main function from which we can build our model and synthesize it. In particular, we define the output register phase as QNum to hold the phase register output of the QPE. We choose a QPE with phase register of size 3, governing the accuracy of our phase-, and thus amplitude-, estimation.
Executing the Circuit and Measuring the Approximated Amplitude
We execute on a simulator:
result = execute(qprog_3).result_value()
## mapping between register string to phasesphases_counts = dict( (sampled_state.state["phase"], sampled_state.shots) for sampled_state in result.parsed_counts)
Upon plotting the resulting histogram we see two phase values with high probability (however, both correspond to the same amplitude a):
plt.bar(phases_counts.keys(), phases_counts.values(), width=0.1)plt.xticks(rotation=90)print("phase with max probability: ", max(phases_counts, key=phases_counts.get))
Output:
phase with max probability: 0.375
Recalling the relation in Eq. (5), we can read the amplitude a from the phase with maximum probability and compare to the expected amplitude:
measured_amplitude = np.sin(np.pi * max(phases_counts, key=phases_counts.get)) ** 2exact_amplitude = sum( np.sin(0.5 * n / 2 + 0.4 / 2) ** 2 * probabilities[n] for n in range(2**3))print(f"measured amplitude: {measured_amplitude}")print(f"exact amplitude: {exact_amplitude}")