From 72f45c84dc4d4ae99ba3bf86c93446cb98997d0a Mon Sep 17 00:00:00 2001 From: Rene Zander Date: Thu, 28 Nov 2024 14:14:27 +0100 Subject: [PATCH] Updates documentation QMCI --- .../source/general/tutorial/index.rst | 2 +- src/qrisp/alg_primitives/iterative_qae.py | 6 ++-- src/qrisp/algorithms/qmci.py | 29 +++++++++++++---- tests/test_QMCI.py | 31 +++++++++++++++++++ 4 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 tests/test_QMCI.py diff --git a/documentation/source/general/tutorial/index.rst b/documentation/source/general/tutorial/index.rst index 520c68a5..995d73eb 100644 --- a/documentation/source/general/tutorial/index.rst +++ b/documentation/source/general/tutorial/index.rst @@ -17,7 +17,7 @@ To gradually qrispify your programming game you will start with the basics and g - :ref:`Implementing Shor's algorithm ` will guide you through our state-of-the-art implementation of **Shor's algorithm**, allowing you to factor numbers and fiddle around encrypting and decrypting hidden messages. -- :ref:`The Quantum Monte Carlo Integration tutorial ` will show you how to integrate functions with **Quantum Monte Carlo methods**, using **accelerated Iterative Quantum Ampltide Estimation**. +- :ref:`The Quantum Monte Carlo Integration tutorial ` will show you how to integrate functions with **Quantum Monte Carlo methods**, using **Iterative Quantum Ampltide Estimation**. - :ref:`Fault-Tolerant compilation of Shor's algorithm ` delves into the realm of **compiling for fault-tolerant quantum devices**, exploring the specialized techniques and considerations that set this stage apart from the compilation challenges encountered in NISQ environments. At the end you will also optimize the implementation of Shor's from the tutorial above. diff --git a/src/qrisp/alg_primitives/iterative_qae.py b/src/qrisp/alg_primitives/iterative_qae.py index 3cea8c28..a446ecef 100644 --- a/src/qrisp/alg_primitives/iterative_qae.py +++ b/src/qrisp/alg_primitives/iterative_qae.py @@ -20,7 +20,7 @@ import numpy as np -def IQAE(qargs,state_function, oracle_function, eps, alpha, kwargs_oracle = {}): +def IQAE(qargs, state_function, oracle_function, eps, alpha, kwargs_oracle = {}): r""" Accelerated Quantum Amplitude Estimation (IQAE). This function performs :ref:`QAE ` with a fraction of the quantum resources of the well-known `QAE algorithm `_. See `Accelerated Quantum Amplitude Estimation without QFT `_. @@ -38,11 +38,11 @@ def IQAE(qargs,state_function, oracle_function, eps, alpha, kwargs_oracle = {}): the quantum amplitude estimation is performed on. state_function : function A Python function preparing the state :math:`\ket{\Psi}`. - This function will receive the variables in the list ``args`` as arguments in the + This function will receive the variables in the list ``qargs`` as arguments in the course of this algorithm. oracle_function : function A Python function tagging the good state :math:`\ket{\Psi_1}`. - This function will receive the variables in the list ``args`` as arguments in the + This function will receive the variables in the list ``qargs`` as arguments in the course of this algorithm. eps : float Accuracy $\epsilon>0$ of the algorithm. diff --git a/src/qrisp/algorithms/qmci.py b/src/qrisp/algorithms/qmci.py index 5d290936..f4737911 100644 --- a/src/qrisp/algorithms/qmci.py +++ b/src/qrisp/algorithms/qmci.py @@ -24,39 +24,56 @@ def uniform(*args): def QMCI(qargs, function, distribution=None): - """ - Implements a general algorithms for `Quantum Monte Carlo Integration `. + r""" + 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 probility distributions: + + .. math:: + + \int_{[0,1]^n} f(x_1 , ... , x_n) \mathrm{d}\mu (x_1 , ... , x_n) Parameters ---------- - qargs : list[ref:`QuantumFloat`] + qargs : list[:ref:`QuantumFloat`] The quantum variables the given ``function`` acts on. function : function A Python function which takes :ref:`QuantumFloats ` as inputs and applies the ``function`` which is to be integrated. distribution : function A Python function which takes :ref:`QuantumFloats ` as inputs and applies the distribution over which to integrate. + By default, the uniform distribution is applied. Returns ------- float - The result of the integration. + The result of the numerical integration. Examples -------- We integrate the function $f(x)=x^2$ over the integral $[0,1]$. - Therefore the function is evaluated at $8=2^3$ points as specified by ``QuantumFloat(3,-3)``. + Therefore, the function is evaluated at $8=2^3$ sampling points as specified by ``QuantumFloat(3,-3)``. :: from qrisp import QuantumFloat - from qrisp.algorithms.qmci import * + from qrisp.algorithms.qmci import QMCI def f(qf): return qf*qf qf = QuantumFloat(3,-3) QMCI([qf], f) + # Yields: 0.27373180511103606 + + This result is consistent with numerically calculating the integral by evaluating the function $f$ at 8 sampling points: + + :: + + N = 8 + sum((i/N)**2 for i in range(N))/N + # Yields: 0.2734375 """ if distribution==None: diff --git a/tests/test_QMCI.py b/tests/test_QMCI.py new file mode 100644 index 00000000..7f8141b1 --- /dev/null +++ b/tests/test_QMCI.py @@ -0,0 +1,31 @@ +""" +\******************************************************************************** +* Copyright (c) 2024 the Qrisp authors +* +* This program and the accompanying materials are made available under the +* terms of the Eclipse Public License 2.0 which is available at +* http://www.eclipse.org/legal/epl-2.0. +* +* This Source Code may also be made available under the following Secondary +* Licenses when the conditions for such availability set forth in the Eclipse +* Public License, v. 2.0 are satisfied: GNU General Public License, version 2 +* with the GNU Classpath Exception which is +* available at https://www.gnu.org/software/classpath/license.html. +* +* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 +********************************************************************************/ +""" + + +def test_QMCI(): + from qrisp import QuantumFloat + from qrisp.algorithms.qmci import QMCI + import numpy as np + + def f(qf): + return qf*qf + + qf = QuantumFloat(3,-3) + a = QMCI([qf], f) + + assert np.abs(a-0.2734375)<0.01 \ No newline at end of file