# Ising models

From Church Wiki

The Ising model is an undirected model. As such, it must be expressed as part of the condition of a query. To do this we shift each coupling term to have max energy zero (or less than zero), then condition on a log-flip with this weight being true.

For instance, a simple undirected model on three binary variables:
```
```

```
```(mh-query
(define a (flip))
(define b (flip))
(define c (flip))
(list a b c)
(and (flip (if (equal? a b)) 1.0 0.3)
(flip (if (equal? b c)) 1.0 0.3)))

```
```

A standard ferromagnetic Ising:
```
```

```
```(define num-spins 4)
(define J '((0 1 0 0)
(0 0 1 0)
(0 0 0 1)
(0 0 0 0)))
(mh-query
(define spins (repeat num-spins (lambda () (if (flip) 1 -1))))
spins
(all
(map (lambda (J-row x)
(map (lambda (energy y)
(log-flip (- (* energy x y) (abs energy))))
J-row spins))
J spins)))

```
```