Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
lehner committed Jun 14, 2024
1 parent 598b9c1 commit 9e95579
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/gpt/algorithms/group/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# GPT - Grid Python Toolkit
# Copyright (C) 2024 Christoph Lehner ([email protected], https://github.com/lehner/gpt)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from gpt.algorithms.group.symmetric_functional import symmetric_functional
from gpt.algorithms.group.locally_coherent_functional import locally_coherent_functional
75 changes: 75 additions & 0 deletions lib/gpt/algorithms/group/locally_coherent_functional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#
# GPT - Grid Python Toolkit
# Copyright (C) 2024 Christoph Lehner ([email protected], https://github.com/lehner/gpt)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
import gpt as g
from gpt.core.group import differentiable_functional

class locally_coherent_functional(differentiable_functional):
def __init__(self, inner, block):
self.inner = inner
self.block = block

def reduce(self, fields):
n = len(fields)
assert n % 2 == 0
n //= 2

right = self.block.embed(fields[n:2*n])

Check failure on line 32 in lib/gpt/algorithms/group/locally_coherent_functional.py

View workflow job for this annotation

GitHub Actions / lint

E226:missing whitespace around arithmetic operator

return [g(g.group.compose(x,y)) for x, y in zip(fields[0:n], right)]

Check failure on line 34 in lib/gpt/algorithms/group/locally_coherent_functional.py

View workflow job for this annotation

GitHub Actions / lint

E231:missing whitespace after ','

Check failure on line 35 in lib/gpt/algorithms/group/locally_coherent_functional.py

View workflow job for this annotation

GitHub Actions / lint

W293:blank line contains whitespace
def __call__(self, fields):
return self.inner(self.reduce(fields))

def gradient(self, fields, dfields):
n = len(fields)
assert n % 2 == 0
n //= 2
left = fields[0:n]
right = fields[n:2*n]

Check failure on line 44 in lib/gpt/algorithms/group/locally_coherent_functional.py

View workflow job for this annotation

GitHub Actions / lint

E226:missing whitespace around arithmetic operator

# f(left right)
# left derivative is like original: f(idA left right)
# right derivative is: f(left idA right ) = f(idA2 left right)
# with dA2 = left dA left^dag

indices = [mu for mu in range(n) if dfields[mu] in fields or dfields[mu + n] in fields]

r = self.reduce(fields)

inner_gradient = self.inner.gradient(r, [r[mu] for mu in indices])

dSdA = []

for f in dfields:
if f in left:
mu = left.index(f)
igi = indices.index(mu)
dSdA.append(inner_gradient[igi])
elif f in right:
mu = right.index(f)
igi = indices.index(mu)
fgrad = g(g.group.inverse(left[mu]) * inner_gradient[igi] * left[mu])
fgrad.otype = left[0].otype
gr = self.block.sum(fgrad)
gr.otype = inner_gradient[igi].otype
dSdA.append(gr)
else:
assert False

return dSdA
69 changes: 69 additions & 0 deletions lib/gpt/algorithms/group/symmetric_functional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# GPT - Grid Python Toolkit
# Copyright (C) 2024 Christoph Lehner ([email protected], https://github.com/lehner/gpt)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
import gpt as g
from gpt.core.group import differentiable_functional

class symmetric_functional(differentiable_functional):
def __init__(self, inner):
self.inner = inner

def reduce(self, fields):
n = len(fields)
assert n % 2 == 0
n //= 2
return [g(g.group.compose(x, g.group.inverse(y))) for x, y in zip(fields[0:n], fields[n:2*n])]

Check failure on line 30 in lib/gpt/algorithms/group/symmetric_functional.py

View workflow job for this annotation

GitHub Actions / lint

E226:missing whitespace around arithmetic operator

Check failure on line 31 in lib/gpt/algorithms/group/symmetric_functional.py

View workflow job for this annotation

GitHub Actions / lint

W293:blank line contains whitespace
def __call__(self, fields):
return self.inner(self.reduce(fields))

def gradient(self, fields, dfields):
n = len(fields)
assert n % 2 == 0
n //= 2
left = fields[0:n]
right = fields[n:2*n]

Check failure on line 40 in lib/gpt/algorithms/group/symmetric_functional.py

View workflow job for this annotation

GitHub Actions / lint

E226:missing whitespace around arithmetic operator

# f(left right^dag)
# left derivative is like original: f(idA left right^dag)
# right derivative is: f(left right^dag (-i)dA ) = f(idA2 left right^dag)
# with dA2 = -left right^dag dA right left^dag = -r dA r^dag

indices = [mu for mu in range(n) if dfields[mu] in fields or dfields[mu + n] in fields]

r = self.reduce(fields)

inner_gradient = self.inner.gradient(r, [r[mu] for mu in indices])

dSdA = []

for f in dfields:
if f in left:
mu = left.index(f)
igi = indices.index(mu)
dSdA.append(inner_gradient[igi])
elif f in right:
mu = right.index(f)
igi = indices.index(mu)
gr = g(-g.group.inverse(r[mu]) * inner_gradient[igi] * r[mu])
gr.otype = inner_gradient[igi].otype
dSdA.append(gr)
else:
assert False

return dSdA

0 comments on commit 9e95579

Please sign in to comment.