Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add real_gaussian_integral function #371

Merged
merged 37 commits into from
Apr 15, 2024
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
58a5bf4
add real_gaussian_integral
sylviemonet Mar 21, 2024
750fabc
add tests for real gaussian integral
sylviemonet Mar 26, 2024
9e74514
fix bugs and add changelog
sylviemonet Mar 26, 2024
84c3723
add hardcoded A matrix in the tests
sylviemonet Mar 26, 2024
cfb042e
Merge branch 'develop' into real_gaussian_integral
sylviemonet Mar 26, 2024
a8b5932
fix bugs in tests
sylviemonet Mar 26, 2024
6eb0c3c
blacked
sylviemonet Mar 26, 2024
78d4efa
Merge branch 'develop' into real_gaussian_integral
sylviemonet Mar 27, 2024
547a0ac
ad more tests
sylviemonet Mar 27, 2024
5406eda
Merge branch 'develop' into real_gaussian_integral
ziofil Apr 5, 2024
7391198
remove the redundency line
sylviemonet Apr 11, 2024
e4d2f16
changed the long math as Fillippo suggested
sylviemonet Apr 11, 2024
1eaa2ba
Merge branch 'develop' into real_gaussian_integral
sylviemonet Apr 11, 2024
93a49f2
change the file name and move all related functions together
sylviemonet Apr 11, 2024
09805e6
change things accordingly inside bargmann
sylviemonet Apr 11, 2024
af7ba6c
fix code factor issue
sylviemonet Apr 11, 2024
554bedb
blacked
sylviemonet Apr 11, 2024
3e23674
Update mrmustard/physics/gaussian_integrals.py
sylviemonet Apr 11, 2024
79e4ae5
add tag for contract_two_abc
sylviemonet Apr 12, 2024
56a08f6
add tests
sylviemonet Apr 12, 2024
8dbd063
fix the importation errors
sylviemonet Apr 12, 2024
7a31213
Merge branch 'develop' into real_gaussian_integral
sylviemonet Apr 12, 2024
ccfebe0
Update mrmustard/physics/gaussian_integrals.py
sylviemonet Apr 12, 2024
fca8dbd
Update mrmustard/physics/gaussian_integrals.py
sylviemonet Apr 12, 2024
89073aa
fix small bugs
sylviemonet Apr 12, 2024
bf2edcb
add type hints
sylviemonet Apr 12, 2024
26cc481
fix the docs
sylviemonet Apr 12, 2024
bd69cbe
fix the docs bug
sylviemonet Apr 12, 2024
cf383ae
blacked
sylviemonet Apr 12, 2024
96f9358
Merge branch 'develop' into real_gaussian_integral
sylviemonet Apr 12, 2024
73b09e0
rerun
sylviemonet Apr 12, 2024
4b888c8
add the comments from Sam
sylviemonet Apr 15, 2024
50ade05
empty
SamFerracin Apr 15, 2024
4ddc026
Merge branch 'real_gaussian_integral' of https://github.com/XanaduAI/…
SamFerracin Apr 15, 2024
7c9a9d7
change back contract_two_Abc
sylviemonet Apr 15, 2024
0260104
change the text
sylviemonet Apr 15, 2024
c095dfe
fix docs
sylviemonet Apr 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions mrmustard/physics/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2024 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This module contains helper functions for physics.
"""
import numpy as np

from mrmustard import math


def real_gaussian_integral(
Abc: tuple,
idx: tuple[int, ...],
):
r"""Computes the Gaussian integral of the exponential of a real quadratic form.
The integral is defined as (note that in general we integrate over a subset of m dimensions):

:math:`\int_{R^m} F(x) dx`
sylviemonet marked this conversation as resolved.
Show resolved Hide resolved

where

:math:`F(x) = \textrm{exp}(0.5 x^T A x + b^T x)`

Here, ``x`` is an ``n``-dim real vector, ``A`` is an ``n x n`` real matrix,
``b`` is an ``n``-dim real vector, ``c`` is a real scalar. The integral indices
are specified by ``idx``.

Arguments:
A,b,c: the ``(A,b,c)`` triple
idx: the tuple of indices of the x variables

Returns:
The ``(A,b,c)`` triple of the result of the integral.
"""
A, b, c = Abc

n = len(idx)

if not idx:
return A, b, c
not_idx = tuple(i for i in range(A.shape[-1]) if i not in idx)

I = math.eye(n, dtype=A.dtype)
Z = math.zeros((n, n), dtype=A.dtype)
X = math.block([[Z, I], [I, Z]])
M = math.gather(math.gather(A, idx, axis=-1), idx, axis=-2) + X
bM = math.gather(b, idx, axis=-1)

not_idx = tuple(i for i in range(A.shape[-1]) if i not in idx)
sylviemonet marked this conversation as resolved.
Show resolved Hide resolved
if math.asnumpy(not_idx).shape != (0,):
D = math.gather(math.gather(A, idx, axis=-1), not_idx, axis=-2)
R = math.gather(math.gather(A, not_idx, axis=-1), not_idx, axis=-2)
bR = math.gather(b, not_idx, axis=-1)
A_post = R - math.matmul(D, math.inv(M), math.transpose(D))
b_post = bR - math.matvec(D, math.solve(M, bM))
sylviemonet marked this conversation as resolved.
Show resolved Hide resolved
else:
A_post = math.astensor([])
b_post = math.astensor([])

c_post = (
c
* math.sqrt((2 * np.pi) ** n / math.det(M))
* math.exp(-0.5 * math.sum(bM * math.solve(M, bM)))
)

return A_post, b_post, c_post
Loading