Transformations and Composition Rules¶
A morphism's scores live in one Algebra.
Two mechanisms move between algebras or change how scores combine. A transformation
rewrites a morphism's scores into another Algebra, and a composition rule fixes
the operations \(\otimes\) and \(\bigoplus\) that >>
uses. A contraction combines more than two morphisms under the composition
rule.
First-class transformations¶
The library has two kinds of transformation, and in QVR both are values of the sort
Trans:
- An entrywise map \(\varphi : \mathcal V \to \mathcal W\) (class
CarrierMap) sends each score to a score in the targetAlgebra, one entry at a time. When \(\varphi\) is monotone and preserves \(\otimes\), \(\bigoplus\), and the unit up to inequality, applying it to every morphism is a change of base (Kelly, 1982). - A morphism transformation (class
MorphismTransformation) rewrites the whole tensor and may read several entries at once. Row normalization by softmax, L1 and L2 row normalization, and Bayesian inversion under a prior (Cho and Jacobs, 2019) are of this kind.
The difference matters for composition. Suppose a row contains [0.2, 0.8]. A
threshold at 0.5 decides each entry on its own and gives [0, 1]. Softmax compares
the entries and gives approximately [0.354, 0.646], a row that sums to one, so
raising one score lowers the normalized weight of the others. An entrywise map
commutes with composition only when it preserves both combining operations, and
thresholding does not preserve product: two scores of 0.6 each pass the threshold,
while their product 0.36 fails. Thresholding before or after composing can thus give
different relations.
QVR binds transformations with define, composes them with >>>, and applies them
with the postfix change_base. Transformation names live in their own namespace,
apart from morphism names.
Transformations without arguments¶
These transformations take no argument:
| Name | Source | Target | Entrywise action |
|---|---|---|---|
expectation |
Markov |
ProductFuzzy |
clamp to \([0, 1]\) |
material_implication |
ProductFuzzy |
Godel |
clamp to \([0, 1]\) |
log_prob |
ProductFuzzy |
LogProb |
\(\log\) |
max_plus |
ProductFuzzy |
MaxPlus |
\(\log\) |
threshold |
ProductFuzzy |
Boolean |
\(1\) if the score exceeds \(0.5\), else \(0\) |
boolean_embedding |
Boolean |
ProductFuzzy |
inclusion |
probability_clamp |
Real |
Probability |
clamp to \([0, 1]\) |
probability_to_real |
Probability |
Real |
inclusion |
counting_from_real |
Real |
Counting |
clamp below at \(0\), then floor |
counting_to_real |
Counting |
Real |
inclusion |
Each is an entrywise map. The logarithms clamp their input below at \(10^{-30}\), so a score of zero becomes a large negative number.
Constructors¶
These transformations take one argument, an object or a morphism:
| Constructor | Argument | Source | Target |
|---|---|---|---|
softmax(B) |
object | ProductFuzzy |
Markov |
l1_normalize(B) |
object | Real |
Markov |
l2_normalize(B) |
object | Real |
Real |
bayes_invert(prior) |
morphism | Markov |
Markov |
The object argument of the normalizers names the output axes over which each row is normalized. All four are morphism transformations.
Binding and composing transformations¶
Morphisms and transformations compose in different pipelines. f >> g joins two
morphisms through a shared object and requires the codomain of f to equal the
domain of g. t1 >>> t2 applies one transformation after another and requires the
target Algebra of t1 to equal the source of t2. The compiler checks both
boundaries.
composition product_fuzzy [level=algebra]
object A : FinSet 3
object B : FinSet 4
morphism f : A -> B [role=parameter]
define s = softmax(B)
define pipe = s >>> expectation
define g = f.change_base(pipe)
export g
Here softmax(B) goes from ProductFuzzy to Markov and expectation goes back, so
g is a row-normalized morphism with scores in ProductFuzzy. Since expectation
only clamps to \([0, 1]\), the row [0.2, 0.8] from above ends as approximately
[0.354, 0.646]. The argument of change_base may be a singleton, a defined name, a
constructor call, or a >>> chain.
Python API¶
quivers.core.trans provides the same operations, and QVR's >>> compiles to
compose_trans:
from quivers.core.trans import compose_trans
from quivers.core.morphism_transformations import softmax
from quivers.core.algebra_morphisms import MARKOV_TO_PRODUCT_FUZZY
pipe = compose_trans(softmax(B), MARKOV_TO_PRODUCT_FUZZY)
g = f.change_base(pipe)
Support maps¶
The support map sends a positive weight to 1
and zero to 0. It accepts BOOLEAN, PRODUCT_FUZZY, GODEL,
PROBABILITY, COUNTING, and MARKOV. On each carrier, a sum is positive
exactly when at least one summand is positive, and a product is positive
exactly when both factors are positive. Thus support preserves both
operations and their identities:
Applying support entrywise commutes with binary matrix composition. For Boolean, Gödel, Counting, and Markov, whose operations form semirings, this defines a functor between matrix categories. ProductFuzzy and Probability preserve the displayed operations, but their matrix composition is not associative. Standard MV has nonzero factors whose product is zero, and signed real addition can cancel, so neither admits this support map.
import torch
from quivers.core import FinSet, MARKOV, SupportMap, observed
state = FinSet(name="State", cardinality=2)
weights = observed(
state, state, torch.tensor([[0.0, 0.2], [1.0, 0.0]]), algebra=MARKOV
)
relation = weights.change_base(SupportMap(MARKOV))
assert relation.tensor.tolist() == [[0.0, 1.0], [1.0, 0.0]]
SupportMap validates the declared carrier and is not differentiable.
The laws hold for mathematical carrier values; floating-point products
can underflow to zero. The threshold transformation uses 0.5, while
SupportMap(source) selects positivity explicitly through the Python API.
The hierarchy of composition rules¶
A composition rule (class CompositionRule) is the
pair of operations \(\otimes\) and \(\bigoplus\) that >> uses:
Its level records which laws the rule claims. Associativity lets
(f >> g) >> h be regrouped as f >> (g >> h), and an identity lets a
morphism that changes nothing be inserted. For this matrix product both follow when \(\otimes\)
and \(\bigoplus\) form a semiring
(Goodman, 1999), and product with noisy-OR is not one
(Core Types). The classes are:
flowchart TD
CR["CompositionRule: ⊗ and ⊕, no promise"]
MG["Magmoid: no associativity or identity promise"]
SG["Semigroupoid: associative, no identity"]
AL["Algebra: unit, zero, meet, negation, so identities"]
SR["Semiring: associative with identities"]
IS["IdempotentSemiring: ⊕ idempotent, a lattice"]
CR --> MG
CR --> SG
CR --> AL
SG --> SR
AL --> SR
SR --> IS
A rule declared at the Semigroupoid level makes its morphisms a
semicategory, composition that is
associative but may lack identities (Tilson, 1987), provided the rule really
is associative. An Algebra gives identities but does not claim associativity. A
Semiring claims both, and the eight built-in semirings are of that class
(Core Types). Magmoid is the class for a
composition rule with no associativity or identity guarantee, whose morphisms form a
magmoid. The library checks no level's laws
when it runs.
Each module declares its rule with composition and a level:
| Declaration | Required class | Operations the compiler accepts |
|---|---|---|
composition X [level=algebra] |
Algebra (a Semiring qualifies) |
>>, @, identity(A), f.dagger, f.trace(A), cup(A), cap(A) |
composition X [level=semigroupoid] |
Semigroupoid (a Semiring qualifies) |
>>, @ |
composition X [level=magmoid] |
Magmoid |
>>, @ |
composition X [level=rule] |
any CompositionRule |
whatever the resolved rule's class allows |
Using identity, dagger, trace, cup, or cap in a module whose rule is not an
Algebra raises a CompileError that names the operation and the rule's level.
User-defined inline rules¶
A composition declaration can define its rule in an indented body. Each entry's
right side is an arithmetic expression of the kind allowed in let:
composition my_sum_product [level=algebra]
tensor_op(a, b) = a * b
join(t) = sum(t)
unit = 1.0
zero = 0.0
object A : FinSet 3
morphism f : A -> A [role=parameter]
define g = f >> f
export g
composition average_rule [level=magmoid]
tensor_op(a, b) = (a + b) * 0.5
join(t) = sum(t)
object A : FinSet 3
morphism f : A -> A [role=parameter]
define g = f >> f
export g
The first rule is ordinary sum-product. The second averages its operands, which is
not associative, so it is declared at the magmoid level. The entries each
level requires are:
| Level | Required | Optional |
|---|---|---|
algebra |
tensor_op, join, unit, zero |
negation, meet |
semigroupoid |
tensor_op, join |
|
magmoid |
tensor_op, join |
The compiler builds inline rules without checking any law. The Python constructors
check a little. CustomAlgebra tests that the unit is a left and right identity for
\(\otimes\) and that the zero absorbs on the left, on five sample values.
CustomSemigroupoid tests that the scalar \(\otimes\) is associative on a fixed random
sample, unless verify_associative=False is passed. CustomMagmoid checks
nothing. A passing test does not establish a law, since it covers finitely many
inputs, and associativity of the scalar \(\otimes\) does not by itself make composition
associative.
Material implication¶
The QVR rule material_impl uses the
Reichenbach implication
\(a \otimes b = 1 - a + a b\) (Baczyński and Jayaram, 2008) with the product as its reduction,
\(\bigoplus_j x_j = \prod_j x_j\):
composition material_impl [level=magmoid]
object A : FinSet 3
object B : FinSet 3
morphism f : A -> B [role=parameter]
morphism g : B -> B [role=parameter]
define composed = f >> g
export composed
The library registers it as a Magmoid, since neither its \(\otimes\) nor its
composition is associative, so a module declares it at the magmoid level. For one-element objects composition is \(\otimes\)
itself, and with every score \(0\) the two groupings give \((0 \otimes 0) \otimes 0 = 0\)
and \(0 \otimes (0 \otimes 0) = 1\). Parenthesize chains under this rule explicitly. The transformation
material_implication in the table above is a different object, an entrywise map
into Godel.
Contractions of several inputs¶
Binary composition f >> g sums over one shared axis. A contraction declaration
combines three or more morphisms in one step, as a
tensor contraction written in
einsum notation
and evaluated under the module's composition rule. Operations of several inputs of this
kind compose by plugging outputs into inputs, and wiring diagrams are the standard
account of that structure (Spivak, 2013).
DSL Contractions covers the QVR syntax in full.
Wiring spec¶
A wiring spec lists the axes of each input, separated by commas, and the axes of the output after an arrow:
Letters that appear in the inputs and not in the output are reduced with the rule's
\(\bigoplus\), and each output letter must appear in at least one input. In this spec,
s and o index the output and p and q range over intermediate values. Each
choice of p and q gives the \(\otimes\)-product of three entries, and the rule's
\(\bigoplus\) combines those products.
Declaration¶
composition product_fuzzy [level=algebra]
object A : FinSet 2
object B : FinSet 3
object C : FinSet 4
object D : FinSet 5
contraction op_apply (
arg1 : A -> B,
arg2 : A -> C,
kernel : B * C -> D,
) : A -> D [rule=product_fuzzy, wiring="sp,sq,pqd->sd"]
morphism a1 : A -> B [role=parameter]
morphism a2 : A -> C [role=parameter]
morphism k : B * C -> D [role=parameter]
define combined = op_apply(a1, a2, k)
export combined
op_apply can then be called wherever a morphism expression is allowed. Each call
checks the number of arguments against the declaration and each argument's domain
and codomain sizes (number of elements) against the declared signature.
Validation¶
The compiler checks the wiring spec where it is declared and raises CompileError
there, not at a call site, when:
- an input lists no letters;
- a letter appears twice within one input, which would ask for a diagonal or a trace;
- the output repeats a letter;
- an output letter appears in no input.
Python API¶
quivers.core.wiring provides the corresponding functions:
from quivers.core.wiring import einsum_wiring, contract
wiring = einsum_wiring(PRODUCT_FUZZY, "sp, sq, pqo -> so")
out = contract(wiring, arg1, arg2, kernel)
EinsumWiring accepts any CompositionRule, including material implication:
from quivers.core.algebras import material_implication
mi = material_implication()
wiring = einsum_wiring(mi, "ij, jk -> ik")
out = wiring.apply(f_tensor, g_tensor)
How a contraction is evaluated depends on the laws its rule establishes. A rule that
establishes the commutative-semiring laws
(ELIMINATION_LAWS)
is contracted by variable elimination,
summing out one letter at a time in a greedy order, so the full product of all inputs
is never formed. Any other rule, such as material implication, is evaluated in the
written order: the inputs are folded left to right with \(\otimes\) and the contracted
letters reduced at once with \(\bigoplus\).
EinsumWiring.plan
returns the chosen plan, and
EinsumWiring.apply_with_work
returns the output with a count of its operations. Binary composition and
Day convolution use the same engine.
See also¶
- QVR tutorial chapter 7: transformations and composition rules in a worked model.
- Python tutorial chapters 6 and 7: the Python API.
- Composition rules: the denotation of each level.
- Semirings, t-norms, and change of base: the built-in instances and the registry of entrywise maps.
Cited sources¶
G. M. Kelly. Basic Concepts of Enriched Category Theory. Cambridge University Press, 1982. Reprinted in Reprints in Theory and Applications of Categories 10 (2005), 1–136. URL: https://www.tac.mta.ca/tac/reprints/articles/10/tr10abs.html.
Kenta Cho and Bart Jacobs. Disintegration and Bayesian Inversion via String Diagrams. Mathematical Structures in Computer Science, 29(7):938–971, 2019. URL: https://doi.org/10.1017/S0960129518000488, doi:10.1017/S0960129518000488.
Joshua Goodman. Semiring Parsing. Computational Linguistics, 25(4):573–606, 1999. URL: https://aclanthology.org/J99-4004/.
Bret Tilson. Categories as Algebra: An Essential Ingredient in the Theory of Monoids. Journal of Pure and Applied Algebra, 48(1–2):83–198, 1987. doi:10.1016/0022-4049(87)90108-3.
Michał Baczyński and Balasubramaniam Jayaram. Fuzzy Implications. Studies in Fuzziness and Soft Computing. Springer, 2008. doi:10.1007/978-3-540-69082-5.
David I. Spivak. The Operad of Wiring Diagrams: Formalizing a Graphical Language for Databases, Recursion, and Plug-and-Play Circuits. 2013. arXiv:1305.0297, doi:10.48550/arXiv.1305.0297.