Quickstart
This page assumes the package is already installed (see Installation). In the next five minutes you’ll solve XOR with logic gates.
The whole thing in 30 seconds
Section titled “The whole thing in 30 seconds”uv sync --extra dev --extra cpu # setup (use --extra cu128 on GPU machines)uv run python examples/01_xor.py # learn XOR → accuracy 1.00 on the discretized circuitmake test # fast test suite (under 2 minutes, always green)Stacking logic-gate layers
Section titled “Stacking logic-gate layers”LutLayer is an ordinary nn.Module, so you can drop it anywhere inside an existing model.
Below is a three-layer N=4 LUT network sized for MNIST.
import torchimport griffith_logic as gl # `gl` is the idiomatic alias
# A 3-layer N=4 LUT network (MNIST-sized)model = torch.nn.Sequential( torch.nn.Flatten(), gl.LutLayer(in_dim=784, out_dim=16000, n_inputs=4), gl.LutLayer(in_dim=16000, out_dim=16000, n_inputs=4), gl.LutLayer(in_dim=16000, out_dim=16000, n_inputs=4), gl.GroupSum(k=10, tau=30.0),)y = model(torch.rand(32, 784)) # (32, 10) class logitsThree things are worth internalizing.
gl.LutLayer(in_dim, out_dim, n_inputs=N)— every output neuron is one N-input LUT (truth table).N=2is difflogic-compatible;N=6maps onto an FPGA LUT6.gl.GroupSum(k, tau)— splits the final gate outputs evenly acrosskclasses, sums each group and divides bytauto produce logits. It’s the exit from the world of logic gates into classification scores.model.eval()— during training gradients flow through a continuous relaxation (softmax and friends); switching to eval applies argmax discretization and the model collapses into a pure Boolean circuit. The accuracy after that collapse (hard accuracy) is the real performance.
Solving XOR in five minutes
Section titled “Solving XOR in five minutes”XOR is the smallest problem that isn’t linearly separable, and the classic way to sanity-check the expressive power of a logic-gate circuit.
-
Run the example as-is.
ターミナルウィンドウ uv run python examples/01_xor.py -
Check the output.
XOR: soft acc=1.00, hard acc=1.00hard acc=1.00means all four input patterns are correct even after discretizing into a pure Boolean circuit. -
Read through it one cell at a time — there’s a walkthrough with real executed output.
The actual code is about 30 lines, and the whole idea is “train with a continuous relaxation, then
collapse into a circuit with eval()”.
model = nn.Sequential( gl.LutLayer(2, 16, n_inputs=2), # softmax over 16 gates (difflogic-compatible) gl.LutLayer(16, 16, n_inputs=2), gl.GroupSum(k=2, tau=2.0),)opt = torch.optim.Adam(model.parameters(), lr=0.05)for step in range(500): opt.zero_grad() F.cross_entropy(model(x), y).backward() opt.step()
model.eval() # discretize (argmax collapses it into a complete Boolean circuit)acc_hard = (model(x).argmax(-1) == y).float().mean()Those 30 lines become this circuit
Section titled “Those 30 lines become this circuit”
On the left, before training: each node is still a mixture of the 16 candidate Boolean
functions, so it can only be labelled LUT, and the paler the color the less decided the node is.
On the right, after training: argmax has settled on one function per node, so each node now carries
its name (AND / XOR / NAND / !A …).
Training is the act of picking one gate out of 16, over and over.
Green = logic gates, orange = the real-valued part (GroupSum), gray = input bits and wiring.
Watch hard, not soft
Section titled “Watch hard, not soft”Here is the test accuracy when the exact same code is scaled up to MNIST (400-bit input, 4000 gates). Orange is the continuous relaxation used during training (soft); green is the discretized circuit itself after argmax (hard), and the latter is the real performance.

→ The full, already-executed walkthrough is in 1. Train on MNIST.
Emitting it as a circuit
Section titled “Emitting it as a circuit”A trained model can be turned into Verilog / VHDL by way of the IR (JSON). This is the exit ramp where the GPU disappears.
from griffith_logic.export import export_irfrom griffith_logic.export.verilog import write_verilogfrom griffith_logic.ir import validate
model.eval()ir = export_ir(model, input_width=2, name='xor')validate(ir, target='hdl')write_verilog(ir, 'out_examples/xor.v')To follow training, Verilog generation and equivalence checking end to end in one go, head to the end-to-end walkthrough.