polycirc.ir

Build circuits using the IR

polycirc.ir.add(n: int) Diagram

Pointwise addition of two n-dimensional vectors

>>> from polycirc.ast import diagram_to_function
>>> f = diagram_to_function(add(2))
>>> f(1, 2, 3, 4) == [1+3, 2+4]
True
polycirc.ir.sub(n: int) Diagram

Pointwise subtraction of two n-dimensional vectors

polycirc.ir.mul(n: int) Diagram

Pointwise multiplication of two n-dimensional vectors

polycirc.ir.shr(n: int) Diagram

Pointwise right-shift of two n-dimensional vectors

polycirc.ir.negate(n: int) Diagram

Pointwise negation

polycirc.ir.constant(cs: List[int]) Diagram

Return a tensoring of constant values.

>>> from polycirc.ast import diagram_to_function
>>> f = diagram_to_function(constant([1, 2, 3]))
>>> f()
[1, 2, 3]
polycirc.ir.copy(n: int) Diagram

Copy an n-dimensional vector

>>> from polycirc.ast import diagram_to_function
>>> f = diagram_to_function(copy(2))
>>> f(1, 2)
[1, 2, 1, 2]
polycirc.ir.obj(n: int)

Turn an object of polycirc (an integer) into an object of Diagrams (a finite function)

polycirc.ir.addc(c: int, n: int)

Add a constant c to each of n inputs >>> from polycirc.ast import diagram_to_function >>> f = diagram_to_function(addc(1, 3)) >>> f(1, 2, 3) [2, 3, 4]

polycirc.ir.shrc(c: int, n: int)

Shift each of the n inputs by a constant c

polycirc.ir.scale(c: int, n: int)

Multiply each of the n inputs by a constant c

polycirc.ir.sum(n: int)

A diagram of type n 1 which sums all its inputs

>>> from polycirc.ast import diagram_to_function
>>> f = diagram_to_function(sum(3))
>>> f(1, 2, 3)
[6]
polycirc.ir.product(n: int)

A diagram of type n 1 which multiplies all its inputs

polycirc.ir.pointwise(op: Diagram, n: int) Diagram

Given a binary operation (a diagram of type 2 1), create a diagram which applies it pointwise to two n-dimensional vectors. For example, mul(n) == pointwise(Mul().diagram(), n)

polycirc.ir.repeated(f: Diagram, n: int) Diagram

Repeat a diagram f : a b in parallel n times.

>>> from polycirc.ast import diagram_to_function
>>> f = diagram_to_function(repeated(mul(1), 2))
>>> f(1, 2, 3, 4) == [1*2, 3*4]
True

Note this is not the same as pointwise()

polycirc.ir.binopc(f: Diagram, c: int)

Given a binary operation f : n × n n and a constant c, return a circuit which applies f(c, -) to each of the n inputs.

>>> from polycirc.ast import diagram_to_function
>>> f = diagram_to_function(binopc(add(3), 1))
>>> f(1, 2, 3)
[2, 3, 4]
polycirc.ir.reduce(op: Operation, unit: Operation, n: int, flip=False) Diagram

Construct an n 1 reduction circuit using an arbitrary associative operation op : 2 1 and unit : 0 1

polycirc.ir.coreduce(binop: Operation, unit: Operation, n: int) Diagram

“Flipped reduce”: returns a 1 n circuit from circuits binop : 1 2 and unit : 1 0.

polycirc.ir.fanout(n: int)

A diagram of type 1 n which copies its input n times

polycirc.ir.pointwise_fanout(block_size: int, copies: int)

Copy a vector of n inputs into m vectors of size n.

>>> from polycirc.ast import diagram_to_function
>>> f = diagram_to_function(pointwise_fanout(block_size=3, copies=4))
>>> f(1, 2, 3)
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
polycirc.ir.dot(n: int)

Compute the dot product of two n-dimensional inputs

polycirc.ir.mat_mul(n: int, m: int)

A circuit with m×n + m inputs which multiplies a matrix (first m×n inputs) with a vector (last m inputs)

polycirc.ir.f_mul_l(op)

Given a circuit op : 2 1, returns a circuit computing op(x, y) * x

polycirc.ir.f_mul_r(op)

Given a circuit op : 2 1, returns a circuit computing op(x, y) * y

polycirc.ir.min()

min() : 2 1 computes the min of two inputs

polycirc.ir.max()

max() : 2 1 computes the max of two inputs

polycirc.ir.minimum(n: int)

minimum(n) : n + n n computes the pointwise minimum of two vectors

polycirc.ir.maximum(n: int)

maximum(n) : n + n n computes the pointwise maximum of two vectors

polycirc.ir.clip1(low: int, high: int)

clip1(low, high) : 2 1 clips inputs to the range [low, high]

polycirc.ir.clip(low: int, high: int, n: int)

clip(low, high) : n + n n is clip1 applied pointwise to n-dimensional inputs