Package: yarrow-polycirc
yarrow-polycirc: Differentiable IR for Zero-Knowledge Machine Learningyarrow-polycircis a a library for representing anddifferentiatingpolynomial (arithmetic) circuits.It's used for two things:A graph-based, platform-agnostic datastructure for representing arithmetic circuitsDifferentiatingarithmetic circuits for verified Zero-Knowledge ML.Install withpip:pip install yarrow-polycircPolynomial CircuitsPolynomial circuits are a bit like boolean circuits,
except their wires can have values in an arbitrary semiring, not just{0, 1}.Here's an example of a polynomial circuit over the semiringZ_32:x₀ ───────────┐   ┌───┐
              └───┤   │
                  │ * ├──── y₀
      ┌───┐   ┌───┤   │
x₁ ───┤ - ├───┘   └───┘
      └───┘Think of this circuit as computing the expressiony₀ = x₀ * (-x₁).
The "dangling wires" on the left are the inputsx₀andx₁,
and on the right are the outputsy₀.DifferentiabilityPolycirc provides a functionrdiffto differentiate circuits:from polycirc import ir, rdiff
c = ir.dot(2) # example circuit - dot product of two 2-dimensional vectors
rc = rdiff(c)See./examples/iris.pyfor an example of usingrdifffor
machine learning.
Theoretical details onrdiffare in Section 10 ofthis paper.How to use itUse theirmodule to build circuits in an algebraic style.
For example, let's first import the ir module:from polycirc import irBasic circuits in the IR can be combined by stacking:f = ir.identity(1) @ ir.negate(1)Think offvisually like this:x₀ ─────────── y₀
      ┌───┐   
x₁ ───┤ - ├─── y₁
      └───┘We can plug the outputs offinto another circuit using thecompositionoperation>>:c = f >> ir.mul(1)which looks like this:x₀ ───────────┐   ┌───┐
              └───┤   │
                  │ * ├──── y₀
      ┌───┐   ┌───┤   │
x₁ ───┤ - ├───┘   └───┘
      └───┘We can print this program as Python code:from polycirc.ast import diagram_to_ast
print(diagram_to_ast(c, 'multiply_negate'))And we get the following:defmultiply_negate(x0,x1):x2=-(x1)x3=x0*x2return[x3]Iris demoSee the includediris examplewhich uses differentiability
of the IR to train a simple linear model
for theiris dataset.
Bothtraining and inferenceof this model happens with polynomial circuits, soyarrow-polycirccan be used foron-chain trainingof models.First, install the example dependenciespip install '.[dev,example]'Download the Iris dataset./data/get-iris-data.shRun the example:python -m examples.iris trainYou should see something like this:loading data...
final parameters [-118, 912, -884, 408, -20, -189, 68, 28, -1407, -641, 1407, 2336]
predicting...
accuracy: 96.0You can also print the iris model circuits as python code:> python -m examples.iris print
def predict(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x17, x22, x27):
    x13 = x12
    x14 = x12
    x18 = x17

    ⋮
    <snipped>Adding a backendIf you want to add your own backend, you don't need to work with diagrams
(circuits) directly.
Instead, you can use thepolycirc.astmodule to transform a circuit to a
generic AST withpolycirc.ast.diagram_to_ast.from polycirc.ast import diagram_to_ast
c = make_some_circuit()
diagram_to_ast(c, 'function_name')From this generic AST, you can then convert your arithmetic circuit to your
target language.For an in depth example, see the providedLeo backend exampleand thedocsRunning TestsInstall dev dependenciespip install '.[dev]'and run testspytest