Wiring¶
Einsum wiring rules that generalize binary composition to contractions over any number of tensors under a composition rule.
wiring
¶
Operadic n-ary tensor contractions for V-enriched categories.
A CompositionRule defines binary composition:
(f >> g)[i, k] = ⋁_j f[i, j] ⊗ g[j, k]. That covers the
2-cells of a 2-category. Useful but narrow — many models combine
three or more tensors at a shared reduction (e.g. two argument
tensors plus a kernel folded at a single contraction):
out[s, c] = ⋁_{p, q} (
arg1[s, p] ⊗ arg2[s, q] ⊗ kernel[p, q, c]
)
This isn't expressible as a chain of binary >>: it's an
n-input wiring diagram whose centre contracts two axes at once
under a fixed tensor_op and join. The general framework is
operads / wiring diagrams (Spivak 2014); this module
exposes a Python surface for them.
The unifying construct is WiringRule: an n-ary
generalization of binary composition that pairs a
CompositionRule (supplying tensor_op and join)
with an einsum-style wiring spec naming which input axes are
contracted vs preserved.
Categorically a WiringRule is a single operation of a
colored operad whose colors are the V-Cat object types; the
CompositionRule supplies the operad's enriching algebra.
WiringRule
¶
Bases: ABC
An n-ary morphism contraction under a fixed
CompositionRule.
Generalizes binary composition >> to arbitrary-arity
tensor networks. A WiringRule carries:
- a
composition_rulewhosetensor_opandjoindrive the contraction; - a wiring specification that names which axes of which inputs participate in which contractions, and which axes survive to the output.
Subclasses implement apply to perform the contraction
given a list of input tensors.
composition_rule
abstractmethod
property
¶
composition_rule: CompositionRule
The underlying binary composition rule used as the enriching algebra for this n-ary contraction.
EinsumWiring
¶
EinsumWiring(composition_rule: CompositionRule, spec: str)
Bases: WiringRule
A multi-input contraction specified by an einsum-style string.
Spec syntax::
"input_1, input_2, ..., input_n -> output"
where each input_i and output is a sequence of axis
letters identifying which dimensions of that tensor align.
Axes that appear in some input but not in the output are
contracted (reduced via the composition rule's join);
axes that appear in the output are preserved (broadcast as
needed).
Examples::
EinsumWiring(product_fuzzy, "ij, jk -> ik")
# binary composition: contract over j, preserve (i, k)
EinsumWiring(product_fuzzy, "sp, sq, pqo -> so")
# ternary contraction: two argument tensors over
# (s, *) and a kernel contracted over (p, q),
# preserving (s, o)
The contraction algorithm:
- Determine the universe of axis letters from inputs and output.
- Broadcast each input tensor against the full axis universe (singleton dims for axes the input doesn't carry).
- Fold all broadcast inputs with
tensor_op(left fold). - Reduce over the contracted axes using
join. - Permute to the output's letter order.
Steps 3-4 correspond to the categorical content; steps 1, 2,
and 5 are the einsum bookkeeping that places axes in a common
shape so the binary tensor_op can fold over the n inputs.
Source code in src/quivers/core/wiring.py
einsum_wiring
¶
einsum_wiring(composition_rule: CompositionRule, spec: str) -> EinsumWiring
Convenience constructor for EinsumWiring.
| PARAMETER | DESCRIPTION |
|---|---|
composition_rule
|
The rule supplying
TYPE:
|
spec
|
An einsum specification such as
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EinsumWiring
|
The wiring rule for |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/quivers/core/wiring.py
contract
¶
contract(rule: WiringRule, *tensors: Tensor) -> Tensor
Apply a WiringRule to a sequence of input tensors.
Thin wrapper around WiringRule.apply for call-site
readability::
result = contract(my_rule, arg1, arg2, kernel)
is an alternative to the method-call form.
| PARAMETER | DESCRIPTION |
|---|---|
rule
|
The wiring rule to apply.
TYPE:
|
*tensors
|
The input tensors, in the order the rule expects.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The contracted tensor. |