From 87cd8092710d72545deb71cd3b8829f48131dd34 Mon Sep 17 00:00:00 2001 From: Rene Zander Date: Sat, 21 Dec 2024 20:46:00 +0100 Subject: [PATCH] Update QMCI: variable representing y-axis must be provided in qargs, minor updates QMCI documentation, tutorial --- .../source/general/tutorial/QMCItutorial.rst | 8 +++---- src/qrisp/algorithms/qmci.py | 24 +++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/documentation/source/general/tutorial/QMCItutorial.rst b/documentation/source/general/tutorial/QMCItutorial.rst index faa94aba..87833ef2 100644 --- a/documentation/source/general/tutorial/QMCItutorial.rst +++ b/documentation/source/general/tutorial/QMCItutorial.rst @@ -7,7 +7,7 @@ This tutorial will provide you with an introduction to Quantum Monte Carlo Integ For this purpose, we will first give you a theoretical overview of what this technique is about and where it is used. Then we will dive into the practical implemention within Qrisp. This also includes the usage of :ref:`Iterative Quantum Amplitude Estimation `. -To finish of this tutorial, we investigate the full implementation of a simple example by integrating :math:`f(x)=x^2` over a uniform distribution in the interval :math:`\lbrack 0,1 \rbrack`. +To finish of this tutorial, we investigate the full implementation of a simple example by integrating :math:`f(x)=x^2` w.r.t. the uniform distribution over the interval :math:`\lbrack 0,1 \rbrack`. The relevant literature can be found in the following papers: `A general quantum algorithm for numerical integration `_ and `Option pricing using Quantum computers `_ for QMCI and `Accelerated Quantum Amplitude Estimation without QFT `_ for IQAE. @@ -15,7 +15,7 @@ without QFT `_ for IQAE. Theoretical overview of QMCI ---------------------------- -QMCI tackels the same problems as its classical counterpart: Numerical integration of high-dimensional functions over probility distributions. +QMCI tackels the same problems as its classical counterpart: Numerical integration of high-dimensional functions w.r.t. probility distributions. Mathemically speaking, we want to find an approximation for the following (general) integral @@ -145,8 +145,8 @@ It receives the ``@auto_uncompute`` :ref:`decorator ` ensuring th We apply the chosen distribution to ``qf_x``, which represents the :math:`x`-axis support. As explained earlier, we also discretize the :math:`y`-axis by appling an ``h`` gate to ``qf_y``. -We then evaluate in superposition which states in ``qf_y`` are smaller than the chosen function evaluated on ``qf_x``. -We store the result of the comparison in the QuantumBool ``tar``, by applying an ``x`` gate on the previously mentioned QuantumBool. +Within a :ref:`ConditionEnvironment`, we then evaluate in superposition which states in ``qf_y`` are smaller than the chosen function evaluated on ``qf_x``. +We store the result of the comparison in the QuantumBool ``tar``, by applying an ``x`` gate on the previously mentioned QuantumBool if said condition is satisfied. With everything in place, we can now execute the :ref:`Iterative QAE algorithm `, with a chosen error tolerance ``eps`` and a confidence level ``alpha``. We also have to rescale the result with the previously calculated volume ``V0``. diff --git a/src/qrisp/algorithms/qmci.py b/src/qrisp/algorithms/qmci.py index d4cf3fcf..48d91b0c 100644 --- a/src/qrisp/algorithms/qmci.py +++ b/src/qrisp/algorithms/qmci.py @@ -28,7 +28,7 @@ def QMCI(qargs, function, distribution=None): Implements a general algorithm for `Quantum Monte Carlo Integration `_. This implementation utilizes :ref:`IQAE`. A detailed explanation can be found in the :ref:`tutorial `. - QMCI performs numerical integration of (high-dimensional) functions over probability distributions: + QMCI performs numerical integration of (high-dimensional) functions w.r.t. probability distributions: .. math:: @@ -37,7 +37,7 @@ def QMCI(qargs, function, distribution=None): Parameters ---------- qargs : list[:ref:`QuantumFloat`] - The quantum variables the given ``function`` acts on. + The quantum variables representing the $x$-axes (the variables the given ``function`` acts on), and a quantum variable representing the $y$-axis. function : function A Python function which takes :ref:`QuantumFloats ` as inputs, and returns a :ref:`QuantumFloat` containing the values of the integrand. @@ -55,6 +55,7 @@ def QMCI(qargs, function, distribution=None): We integrate the function $f(x)=x^2$ over the integral $[0,1]$. Therefore, the function is evaluated at $8=2^3$ sampling points as specified by ``QuantumFloat(3,-3)``. + The $y$-axis is representend by ``QuantumFloat(6,-6)``. :: @@ -64,8 +65,9 @@ def QMCI(qargs, function, distribution=None): def f(qf): return qf*qf - qf = QuantumFloat(3,-3) - QMCI([qf], f) + qf_x = QuantumFloat(3,-3) + qf_y = QuantumFloat(6,-6) + QMCI([qf_x,qf_y], f) # Yields: 0.27373180511103606 This result is consistent with numerically calculating the integral by evaluating the function $f$ at 8 sampling points: @@ -76,17 +78,19 @@ def f(qf): sum((i/N)**2 for i in range(N))/N # Yields: 0.2734375 + A detailed explanation of QMCI and its implementation in Qrisp can be found in the :ref:`QMCI tutorial `. + """ if distribution==None: distribution = uniform - dupl_args = [arg.duplicate() for arg in qargs] - dupl_res_qf = function(*dupl_args) - qargs.append(dupl_res_qf.duplicate()) + #dupl_args = [arg.duplicate() for arg in qargs] + #dupl_res_qf = function(*dupl_args) + #qargs.append(dupl_res_qf.duplicate()) - for arg in dupl_args: - arg.delete() - dupl_res_qf.delete() + #for arg in dupl_args: + # arg.delete() + #dupl_res_qf.delete() V0=1 for arg in qargs: