I am not sure exactly which summation problem you are trying to solve; maybe you can give a bit more detail?

For the Wick story, here's a bit of code I put together a while ago which may help you get things implemented. It basically gives you a `contract`

function which takes a product of fields and replaces all pairs with propagators. You'll need to expand this of course to do real world problems but the gist of the solution is in here.

First, you need something that takes a list of numbers and returns all possible ways in

which you can pair numbers:

```
def all_pairs(lst):
if len(lst) < 2:
yield []
return
else:
a = lst[0]
for i in range(1,len(lst)):
pair = (a,lst[i])
for rest in all_pairs(lst[1:i]+lst[i+1:]):
yield [pair] + rest
```

That's just a bit of standard Python, nothing Cadabra-specific. Then you need something that takes a cadabra expression and does the contractions in there:

```
def contract(ex):
amp:=0.
L=list(range(len(ex)))
for c in all_pairs(L):
diag:=1;
for p in c:
a1=ex[p[0]][0]
a2=ex[p[1]][0]
diag *= $G( @(a1) - @(a2) )$
amp += diag
return amp
```

Hopefully this makes it clear how to take Cadabra expressions apart and build new ones from the pieces. Then the following works:

```
ex:=\phi(x_1) \phi(x_2) \phi(x_3) \phi(x_4);
contract(ex);
```

gives

$$G\left(x_{1}-x_{2}\right) G\left(x_{3}-x_{4}\right)+G\left(x_{1}-x_{3}\right) G\left(x_{2}-x_{4}\right)+G\left(x_{1}-x_{4}\right) G\left(x_{2}-x_{3}\right)$$

For any serious work you'll probably have to rewrite the `all_pairs`

function so that it is not recursive.