Skip to content

operators

operators ¤

DEFAULT_OPERATOR_RULES = {LayerOperator.INTEGRATION: [integrate_embedding_layer, integrate_categorical_layer, integrate_gaussian_layer], LayerOperator.DIFFERENTIATION: [differentiate_polynomial_layer], LayerOperator.MULTIPLICATION: [multiply_embedding_layers, multiply_categorical_layers, multiply_gaussian_layers, multiply_polynomial_layers, multiply_hadamard_layers, multiply_kronecker_layers, multiply_sum_layers], LayerOperator.CONJUGATION: [conjugate_embedding_layer, conjugate_categorical_layer, conjugate_gaussian_layer, conjugate_polynomial_layer, conjugate_sum_layer]} module-attribute ¤

LayerOperatorSign = tuple[type[Layer], ...] module-attribute ¤

LayerOperatorSpecs = dict[LayerOperatorSign, LayerOperatorFunc] module-attribute ¤

LayerOperatorFunc ¤

Bases: Protocol

The layer operator function protocol.

Source code in cirkit/symbolic/operators.py
325
326
327
328
329
330
331
332
333
334
335
336
337
338
class LayerOperatorFunc(Protocol):
    """The layer operator function protocol."""

    def __call__(self, *sl: Layer, **kwargs: Any) -> CircuitBlock:
        """Apply an operator on one or more layers.

        Args:
            *sl: The sequence of layers.
            **kwargs: The hyperparameters of the operator.

        Returns:
            A circuit block representing the sub computational graph resulting from
                the application of the operator.
        """

__call__(*sl, **kwargs) ¤

Apply an operator on one or more layers.

Parameters:

Name Type Description Default
*sl Layer

The sequence of layers.

()
**kwargs Any

The hyperparameters of the operator.

{}

Returns:

Type Description
CircuitBlock

A circuit block representing the sub computational graph resulting from the application of the operator.

Source code in cirkit/symbolic/operators.py
328
329
330
331
332
333
334
335
336
337
338
def __call__(self, *sl: Layer, **kwargs: Any) -> CircuitBlock:
    """Apply an operator on one or more layers.

    Args:
        *sl: The sequence of layers.
        **kwargs: The hyperparameters of the operator.

    Returns:
        A circuit block representing the sub computational graph resulting from
            the application of the operator.
    """

conjugate_categorical_layer(sl) ¤

Source code in cirkit/symbolic/operators.py
293
294
295
296
297
298
299
300
301
302
303
def conjugate_categorical_layer(sl: CategoricalLayer) -> CircuitBlock:
    logits = sl.logits.ref() if sl.logits is not None else None
    probs = sl.probs.ref() if sl.probs is not None else None
    sl = CategoricalLayer(
        sl.scope,
        sl.num_output_units,
        num_categories=sl.num_categories,
        logits=logits,
        probs=probs,
    )
    return CircuitBlock.from_layer(sl)

conjugate_embedding_layer(sl) ¤

Source code in cirkit/symbolic/operators.py
287
288
289
290
def conjugate_embedding_layer(sl: EmbeddingLayer) -> CircuitBlock:
    weight = Parameter.from_unary(ConjugateParameter(sl.weight.shape), sl.weight.ref())
    sl = EmbeddingLayer(sl.scope, sl.num_output_units, num_states=sl.num_states, weight=weight)
    return CircuitBlock.from_layer(sl)

conjugate_gaussian_layer(sl) ¤

Source code in cirkit/symbolic/operators.py
306
307
308
309
310
def conjugate_gaussian_layer(sl: GaussianLayer) -> CircuitBlock:
    mean = sl.mean.ref()
    stddev = sl.stddev.ref()
    sl = GaussianLayer(sl.scope, sl.num_output_units, mean=mean, stddev=stddev)
    return CircuitBlock.from_layer(sl)

conjugate_polynomial_layer(sl) ¤

Source code in cirkit/symbolic/operators.py
313
314
315
316
def conjugate_polynomial_layer(sl: PolynomialLayer) -> CircuitBlock:
    coeff = Parameter.from_unary(ConjugateParameter(sl.coeff.shape), sl.coeff.ref())
    sl = PolynomialLayer(sl.scope, sl.num_output_units, degree=sl.degree, coeff=coeff)
    return CircuitBlock.from_layer(sl)

conjugate_sum_layer(sl) ¤

Source code in cirkit/symbolic/operators.py
319
320
321
322
def conjugate_sum_layer(sl: SumLayer) -> CircuitBlock:
    weight = Parameter.from_unary(ConjugateParameter(sl.weight.shape), sl.weight.ref())
    sl = SumLayer(sl.num_input_units, sl.num_output_units, arity=sl.arity, weight=weight)
    return CircuitBlock.from_layer(sl)

differentiate_polynomial_layer(sl, *, var_idx, order=1) ¤

Source code in cirkit/symbolic/operators.py
273
274
275
276
277
278
279
280
281
282
283
284
def differentiate_polynomial_layer(
    sl: PolynomialLayer, *, var_idx: int, order: int = 1
) -> CircuitBlock:
    # PolynomialLayer is constructed univariate, but we still take the 2 idx for unified interface
    assert var_idx == 0, "This should not happen"
    if order <= 0:
        raise ValueError("The order of differentiation must be positive.")
    coeff = Parameter.from_unary(
        PolynomialDifferential(sl.coeff.shape, order=order), sl.coeff.ref()
    )
    sl = PolynomialLayer(sl.scope, sl.num_output_units, degree=coeff.shape[-1] - 1, coeff=coeff)
    return CircuitBlock.from_layer(sl)

integrate_categorical_layer(sl, *, scope) ¤

Source code in cirkit/symbolic/operators.py
51
52
53
54
55
56
57
58
59
60
61
62
63
def integrate_categorical_layer(sl: CategoricalLayer, *, scope: Scope) -> CircuitBlock:
    if not len(sl.scope & scope):
        raise ValueError(
            f"The scope of the Categorical layer '{sl.scope}'"
            f" is expected to be a subset of the integration scope '{scope}'"
        )
    if sl.logits is None:
        log_partition = Parameter.from_input(ConstantParameter(sl.num_output_units, value=0.0))
    else:
        reduce_lse = ReduceLSEParameter(sl.logits.shape, axis=1)
        log_partition = Parameter.from_unary(reduce_lse, sl.logits.ref())
    int_sl = ConstantValueLayer(sl.num_output_units, log_space=True, value=log_partition)
    return CircuitBlock.from_layer(int_sl)

integrate_embedding_layer(sl, *, scope) ¤

Source code in cirkit/symbolic/operators.py
39
40
41
42
43
44
45
46
47
48
def integrate_embedding_layer(sl: EmbeddingLayer, *, scope: Scope) -> CircuitBlock:
    if not len(sl.scope & scope):
        raise ValueError(
            f"The scope of the Embedding layer '{sl.scope}'"
            f" is expected to be a subset of the integration scope '{scope}'"
        )
    reduce_sum = ReduceSumParameter(sl.weight.shape, axis=1)
    value = Parameter.from_unary(reduce_sum, sl.weight.ref())
    int_sl = ConstantValueLayer(sl.num_output_units, log_space=False, value=value)
    return CircuitBlock.from_layer(int_sl)

integrate_gaussian_layer(sl, *, scope) ¤

Source code in cirkit/symbolic/operators.py
66
67
68
69
70
71
72
73
74
75
76
77
def integrate_gaussian_layer(sl: GaussianLayer, *, scope: Scope) -> CircuitBlock:
    if not len(sl.scope & scope):
        raise ValueError(
            f"The scope of the Gaussian layer '{sl.scope}'"
            f" is expected to be a subset of the integration scope '{scope}'"
        )
    if sl.log_partition is None:
        log_partition = Parameter.from_input(ConstantParameter(sl.num_output_units, value=0.0))
    else:
        log_partition = sl.log_partition.ref()
    int_sl = ConstantValueLayer(sl.num_output_units, log_space=True, value=log_partition)
    return CircuitBlock.from_layer(int_sl)

multiply_categorical_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def multiply_categorical_layers(sl1: CategoricalLayer, sl2: CategoricalLayer) -> CircuitBlock:
    if sl1.scope != sl2.scope:
        raise ValueError(
            f"Expected Categorical layers to have the same scope,"
            f" but found '{sl1.scope}' and '{sl2.scope}'"
        )
    if sl1.num_categories != sl2.num_categories:
        raise ValueError(
            f"Expected Categorical layers to have the number of categories,"
            f"but found '{sl1.num_categories}' and '{sl2.num_categories}'"
        )

    if sl1.logits is None:
        assert sl1.probs is not None
        sl1_logits = Parameter.from_unary(LogParameter(sl1.probs.shape), sl1.probs.ref())
    else:
        sl1_logits = sl1.logits.ref()
    if sl2.logits is None:
        assert sl2.probs is not None
        sl2_logits = Parameter.from_unary(LogParameter(sl2.probs.shape), sl2.probs.ref())
    else:
        sl2_logits = sl2.logits.ref()
    sl_logits = Parameter.from_binary(
        OuterSumParameter(sl1_logits.shape, sl2_logits.shape, axis=0),
        sl1_logits,
        sl2_logits,
    )
    sl = CategoricalLayer(
        sl1.scope,
        sl1.num_output_units * sl2.num_output_units,
        num_categories=sl1.num_categories,
        logits=sl_logits,
    )
    return CircuitBlock.from_layer(sl)

multiply_embedding_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def multiply_embedding_layers(sl1: EmbeddingLayer, sl2: EmbeddingLayer) -> CircuitBlock:
    if sl1.scope != sl2.scope:
        raise ValueError(
            f"Expected Embedding layers to have the same scope,"
            f" but found '{sl1.scope}' and '{sl2.scope}'"
        )
    if sl1.num_states != sl2.num_states:
        raise ValueError(
            f"Expected Embedding layers to have the number of categories,"
            f"but found '{sl1.num_states}' and '{sl2.num_states}'"
        )

    weight = Parameter.from_binary(
        OuterProductParameter(sl1.weight.shape, sl2.weight.shape, axis=0),
        sl1.weight.ref(),
        sl2.weight.ref(),
    )
    sl = EmbeddingLayer(
        sl1.scope,
        sl1.num_output_units * sl2.num_output_units,
        num_states=sl1.num_states,
        weight=weight,
    )
    return CircuitBlock.from_layer(sl)

multiply_gaussian_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def multiply_gaussian_layers(sl1: GaussianLayer, sl2: GaussianLayer) -> CircuitBlock:
    if sl1.scope != sl2.scope:
        raise ValueError(
            f"Expected Gaussian layers to have the same scope,"
            f" but found '{sl1.scope}' and '{sl2.scope}'"
        )

    mean = Parameter.from_nary(
        GaussianProductMean(sl1.mean.shape, sl1.stddev.shape, sl2.mean.shape, sl2.stddev.shape),
        sl1.mean.ref(),
        sl1.stddev.ref(),
        sl2.mean.ref(),
        sl2.stddev.ref(),
    )
    stddev = Parameter.from_binary(
        GaussianProductStddev(sl1.stddev.shape, sl2.stddev.shape),
        sl1.stddev.ref(),
        sl2.stddev.ref(),
    )
    log_partition = Parameter.from_nary(
        GaussianProductLogPartition(
            sl1.mean.shape, sl1.stddev.shape, sl2.mean.shape, sl2.stddev.shape
        ),
        sl1.mean.ref(),
        sl1.stddev.ref(),
        sl2.mean.ref(),
        sl2.stddev.ref(),
    )

    if sl1.log_partition is not None or sl2.log_partition is not None:
        log_partition1: Parameter | ConstantParameter
        log_partition2: Parameter | ConstantParameter
        if sl1.log_partition is None:
            log_partition1 = ConstantParameter(sl1.num_output_units, value=0.0)
        else:
            log_partition1 = sl1.log_partition.ref()
        if sl2.log_partition is None:
            log_partition2 = ConstantParameter(sl2.num_output_units, value=0.0)
        else:
            log_partition2 = sl2.log_partition.ref()
        log_partition = Parameter.from_binary(
            SumParameter(log_partition.shape, log_partition.shape),
            log_partition,
            Parameter.from_binary(
                OuterSumParameter(log_partition1.shape, log_partition2.shape, axis=0),
                log_partition1,
                log_partition2,
            ),
        )

    sl = GaussianLayer(
        sl1.scope,
        sl1.num_output_units * sl2.num_output_units,
        mean=mean,
        stddev=stddev,
        log_partition=log_partition,
    )
    return CircuitBlock.from_layer(sl)

multiply_hadamard_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
225
226
227
228
229
230
231
def multiply_hadamard_layers(sl1: HadamardLayer, sl2: HadamardLayer) -> CircuitBlock:
    arity = max(sl1.arity, sl2.arity)
    sl = HadamardLayer(
        sl1.num_input_units * sl2.num_input_units,
        arity=arity,
    )
    return CircuitBlock.from_layer(sl)

multiply_kronecker_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def multiply_kronecker_layers(sl1: KroneckerLayer, sl2: KroneckerLayer) -> CircuitBlock:
    arity = max(sl1.arity, sl2.arity)
    kron_sl = KroneckerLayer(sl1.num_input_units * sl2.num_input_units, arity=arity)
    # Start with a reshaped identity matrix
    perm_matrix = np.eye(kron_sl.num_output_units, dtype=np.float32).reshape(
        kron_sl.num_output_units,
        *((sl1.num_input_units,) * sl1.arity),
        *((sl2.num_input_units,) * sl2.arity),
    )
    # Construct the permutation matrix required to represent the product of
    # Kronecker layers as yet another Kronecker layer
    perm_matrix = np.transpose(
        perm_matrix, axes=sum(((1 + a, 1 + a + arity) for a in range(arity)), start=(0,))
    ).reshape(kron_sl.num_output_units, kron_sl.num_output_units)
    # The permutation matrix is applied by using a sum layer having the permutation
    # matrix has constant parameters
    sum_sl = SumLayer(
        kron_sl.num_output_units,
        kron_sl.num_output_units,
        weight=Parameter.from_input(
            ConstantParameter(kron_sl.num_output_units, kron_sl.num_output_units, value=perm_matrix)
        ),
    )
    return CircuitBlock.from_layer_composition(kron_sl, sum_sl)

multiply_polynomial_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def multiply_polynomial_layers(sl1: PolynomialLayer, sl2: PolynomialLayer) -> CircuitBlock:
    if sl1.scope != sl2.scope:
        raise ValueError(
            f"Expected Polynomial layers to have the same scope,"
            f" but found '{sl1.scope}' and '{sl2.scope}'"
        )

    shape1, shape2 = sl1.coeff.shape, sl2.coeff.shape
    coeff = Parameter.from_binary(
        PolynomialProduct(shape1, shape2),
        sl1.coeff.ref(),
        sl2.coeff.ref(),
    )

    sl = PolynomialLayer(
        sl1.scope,
        sl1.num_output_units * sl2.num_output_units,
        degree=sl1.degree + sl2.degree,
        coeff=coeff,
    )
    return CircuitBlock.from_layer(sl)

multiply_sum_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
260
261
262
263
264
265
266
267
268
269
270
def multiply_sum_layers(sl1: SumLayer, sl2: SumLayer) -> CircuitBlock:
    weight = Parameter.from_binary(
        KroneckerParameter(sl1.weight.shape, sl2.weight.shape), sl1.weight.ref(), sl2.weight.ref()
    )
    sl = SumLayer(
        sl1.num_input_units * sl2.num_input_units,
        sl1.num_output_units * sl2.num_output_units,
        arity=sl1.arity * sl2.arity,
        weight=weight,
    )
    return CircuitBlock.from_layer(sl)