P vs NP: the fewest gates
Easy to check, easy to find? Exact small circuits, proofs that none smaller exists, and the 2026 claims around P vs NP, rerun and read

| 1 | #!/usr/bin/env python3 |
| 2 | """Find a small circuit with a SAT solver, or write the CNF whose refutation is a lower bound. |
| 3 | |
| 4 | python3 scripts/circuit.py cnf FUNCTION S OUT.cnf the approved encoding of "size(FUNCTION) <= S" (checks/circuits.py) |
| 5 | python3 scripts/circuit.py find FUNCTION S OUT.ckt solve it with PySAT (pip install python-sat) and write the circuit |
| 6 | |
| 7 | FUNCTION is an id: mod3_<n>_<r>, sum<n>, maj<n>, thr<k>_<n>. A lower bound "size(F) > S" is a proof that the CNF has |
| 8 | no model, which a solver writes as LRAT, for example CaDiCaL: cadical --lrat=true --binary=false F.cnf F.gtS.lrat |
| 9 | (gzip it if it is large: the check reads F.gtS.lrat.gz). The check regenerates the CNF itself, so hand in the proof |
| 10 | only. Finding is the hard direction here too: SLIM (Kulikov, Pechenev and Slezkin, 2022) report seconds for 6 gates and |
| 11 | days for 7 to 12; their local improvement (github.com/alexanderskulikov/circuit_improvement) goes further. |
| 12 | """ |
| 13 | import sys |
| 14 | import time |
| 15 | from pathlib import Path |
| 16 | |
| 17 | sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "checks")) |
| 18 | from circuits import computes, dimacs, encode, function # noqa: E402 |
| 19 | |
| 20 | |
| 21 | def circuit(names, model, fid): |
| 22 | """The circuit a model describes, as .ckt text: gate i reads the pair its selector picks and computes the normal |
| 23 | function its three bits give; an output that must be negated has its gate's table flipped and every reader of |
| 24 | that gate rewired, so the circuit keeps its size.""" |
| 25 | true = {v for v in model if v > 0} |
| 26 | n, s = names["n"], names["s"] |
| 27 | node = [f"x{i + 1}" for i in range(n)] + [f"g{i + 1}" for i in range(s)] |
| 28 | gates = {} |
| 29 | for (i, j, k), v in names["sel"].items(): |
| 30 | if v in true: |
| 31 | bits = ["0"] + ["1" if names["fun"][(i, a, b)] in true else "0" for a, b in ((0, 1), (1, 0), (1, 1))] |
| 32 | gates[i] = [node[j], node[k], bits] |
| 33 | outs = sorted((h, i) for (h, i), v in names["out"].items() if v in true) |
| 34 | flip = {i for h, i in outs if names["neg"][h]} |
| 35 | if any(not names["neg"][h] and i in flip for h, i in outs): |
| 36 | raise SystemExit("an output and its negation share a gate; try again with S + 1") |
| 37 | for i in sorted(flip): |
| 38 | gates[i][2] = ["1" if b == "0" else "0" for b in gates[i][2]] |
| 39 | for g in gates.values(): # a reader sees the flipped node: index its table by the negated input |
| 40 | a, b = g[0] == node[i], g[1] == node[i] |
| 41 | if a or b: |
| 42 | tt = g[2] |
| 43 | g[2] = [tt[2 * (x ^ a) + (y ^ b)] for x in (0, 1) for y in (0, 1)] |
| 44 | lines = [f"{n} {s} {len(outs)}", " ".join(node[:n])] |
| 45 | lines += [f"{node[i]} {a} {b} {''.join(tt)}" for i, (a, b, tt) in sorted(gates.items())] |
| 46 | lines.append(" ".join(node[i] for h, i in outs)) |
| 47 | text = "\n".join(lines) + "\n" |
| 48 | assert computes(fid, text, "the circuit read back") == s |
| 49 | return text |
| 50 | |
| 51 | |
| 52 | def main(cmd, fid, s, out): |
| 53 | function(fid) |
| 54 | nv, clauses, names = encode(fid, int(s)) |
| 55 | if cmd == "cnf": |
| 56 | Path(out).write_text(dimacs(nv, clauses, f"{fid}: some circuit of at most {s} gates over B2")) |
| 57 | return print(f"{out}: {nv} variables, {len(clauses)} clauses") |
| 58 | from pysat.solvers import Solver |
| 59 | t = time.time() |
| 60 | with Solver(name="cadical195", bootstrap_with=clauses) as solver: |
| 61 | if not solver.solve(): |
| 62 | return print(f"no circuit of at most {s} gates computes {fid} ({time.time() - t:.1f} s): a lower bound, if a proof says so") |
| 63 | Path(out).write_text(circuit(names, solver.get_model(), fid)) |
| 64 | print(f"{out}: {fid} in {s} gates, found in {time.time() - t:.1f} s") |
| 65 | |
| 66 | |
| 67 | if __name__ == "__main__": |
| 68 | if len(sys.argv) != 5 or sys.argv[1] not in ("cnf", "find"): |
| 69 | sys.exit(__doc__) |
| 70 | main(*sys.argv[1:]) |