Skip to content

Commit

Permalink
Update QMCI: variable representing y-axis must be provided in qargs, …
Browse files Browse the repository at this point in the history
…minor updates QMCI documentation, tutorial
  • Loading branch information
renezander90 committed Dec 21, 2024
1 parent b60b37f commit 87cd809
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
8 changes: 4 additions & 4 deletions documentation/source/general/tutorial/QMCItutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ 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 <IQAE>`.
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 <https://www.nature.com/articles/s41598-024-61010-9>`_ and `Option pricing using Quantum computers <https://arxiv.org/pdf/1905.02666>`_ for QMCI and `Accelerated Quantum Amplitude Estimation
without QFT <https://arxiv.org/pdf/2407.16795>`_ 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

Expand Down Expand Up @@ -145,8 +145,8 @@ It receives the ``@auto_uncompute`` :ref:`decorator <uncomputation>` 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 <IQAE>`, with a chosen error tolerance ``eps`` and a confidence level ``alpha``.
We also have to rescale the result with the previously calculated volume ``V0``.
Expand Down
24 changes: 14 additions & 10 deletions src/qrisp/algorithms/qmci.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def QMCI(qargs, function, distribution=None):
Implements a general algorithm for `Quantum Monte Carlo Integration <https://www.nature.com/articles/s41598-024-61010-9>`_.
This implementation utilizes :ref:`IQAE`. A detailed explanation can be found in the :ref:`tutorial <QMCItutorial>`.
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::
Expand All @@ -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 <QuantumFloat>` as inputs,
and returns a :ref:`QuantumFloat` containing the values of the integrand.
Expand All @@ -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)``.
::
Expand All @@ -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:
Expand All @@ -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 <QMCItutorial>`.
"""
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:
Expand Down

0 comments on commit 87cd809

Please sign in to comment.