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_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) -> 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

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) -> 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
290
291
292
293
294
295
296
297
298
299
300
301
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,
        sl.num_channels,
        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
282
283
284
285
286
287
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, sl.num_channels, num_states=sl.num_states, weight=weight
    )
    return CircuitBlock.from_layer(sl)

conjugate_gaussian_layer(sl) ¤

Source code in cirkit/symbolic/operators.py
304
305
306
307
308
def conjugate_gaussian_layer(sl: GaussianLayer) -> CircuitBlock:
    mean = sl.mean.ref() if sl.mean is not None else None
    stddev = sl.stddev.ref() if sl.stddev is not None else None
    sl = GaussianLayer(sl.scope, sl.num_output_units, sl.num_channels, mean=mean, stddev=stddev)
    return CircuitBlock.from_layer(sl)

conjugate_polynomial_layer(sl) ¤

Source code in cirkit/symbolic/operators.py
311
312
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, sl.num_channels, 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, ch_idx, order=1) ¤

Source code in cirkit/symbolic/operators.py
266
267
268
269
270
271
272
273
274
275
276
277
278
279
def differentiate_polynomial_layer(
    sl: PolynomialLayer, *, var_idx: int, ch_idx: int, order: int = 1
) -> CircuitBlock:
    # PolynomialLayer is constructed univariate, but we still take the 2 idx for unified interface
    assert (var_idx, ch_idx) == (0, 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, sl.num_channels, degree=coeff.shape[-1] - 1, coeff=coeff
    )
    return CircuitBlock.from_layer(sl)

integrate_categorical_layer(sl, *, scope) ¤

Source code in cirkit/symbolic/operators.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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=2)
        reduce_channels = ReduceSumParameter(reduce_lse.shape, axis=1)
        log_partition = Parameter.from_sequence(sl.logits.ref(), reduce_lse, reduce_channels)
    sl = ConstantValueLayer(sl.num_output_units, log_space=True, value=log_partition)
    return CircuitBlock.from_layer(sl)

integrate_embedding_layer(sl, *, scope) ¤

Source code in cirkit/symbolic/operators.py
36
37
38
39
40
41
42
43
44
45
46
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=2)
    reduce_prod = ReduceProductParameter(reduce_sum.shape, axis=1)
    value = Parameter.from_sequence(sl.weight.ref(), reduce_sum, reduce_prod)
    sl = ConstantValueLayer(sl.num_output_units, log_space=False, value=value)
    return CircuitBlock.from_layer(sl)

integrate_gaussian_layer(sl, *, scope) ¤

Source code in cirkit/symbolic/operators.py
65
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:
        reduce_channels = ReduceSumParameter(sl.log_partition.shape, axis=1)
        log_partition = Parameter.from_unary(reduce_channels, sl.log_partition.ref())
    sl = ConstantValueLayer(sl.num_output_units, log_space=True, value=log_partition)
    return CircuitBlock.from_layer(sl)

multiply_categorical_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
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
140
141
142
143
144
145
146
147
148
149
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_channels != sl2.num_channels:
        raise ValueError(
            f"Expected Categorical layers to have the number of channels,"
            f"but found '{sl1.num_channels}' and '{sl2.num_channels}'"
        )
    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:
        sl1_logits = Parameter.from_unary(LogParameter(sl1.probs.shape), sl1.probs.ref())
    else:
        sl1_logits = sl1.logits.ref()
    if sl2.logits is 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_channels=sl1.num_channels,
        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
104
105
106
107
108
109
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_channels != sl2.num_channels:
        raise ValueError(
            f"Expected Embedding layers to have the number of channels,"
            f"but found '{sl1.num_channels}' and '{sl2.num_channels}'"
        )
    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_channels=sl1.num_channels,
        num_states=sl1.num_states,
        weight=weight,
    )
    return CircuitBlock.from_layer(sl)

multiply_gaussian_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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}'"
        )
    if sl1.num_channels != sl2.num_channels:
        raise ValueError(
            f"Expected Gaussian layers to have the number of channels,"
            f"but found '{sl1.num_channels}' and '{sl2.num_channels}'"
        )

    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:
        if sl1.log_partition is None:
            log_partition1 = ConstantParameter(sl1.num_output_units, sl1.num_channels, value=0.0)
        else:
            log_partition1 = sl1.log_partition.ref()
        if sl2.log_partition is None:
            log_partition2 = ConstantParameter(sl2.num_output_units, sl2.num_channels, 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,
        num_channels=sl1.num_channels,
        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
245
246
247
248
249
250
def multiply_hadamard_layers(sl1: HadamardLayer, sl2: HadamardLayer) -> CircuitBlock:
    sl = HadamardLayer(
        sl1.num_input_units * sl2.num_input_units,
        arity=max(sl1.arity, sl2.arity),
    )
    return CircuitBlock.from_layer(sl)

multiply_polynomial_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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}'"
        )
    if sl1.num_channels != sl2.num_channels:
        raise ValueError(
            f"Expected Polynomial layers to have the number of channels,"
            f"but found '{sl1.num_channels}' and '{sl2.num_channels}'"
        )

    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,
        num_channels=sl1.num_channels,
        degree=sl1.degree + sl2.degree,
        coeff=coeff,
    )
    return CircuitBlock.from_layer(sl)

multiply_sum_layers(sl1, sl2) ¤

Source code in cirkit/symbolic/operators.py
253
254
255
256
257
258
259
260
261
262
263
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)