queries
queries
¤
IntegrateQuery
¤
Bases: Query
The integration query object allows marginalising out variables.
Computes output in two forward passes
a) The normal circuit forward pass for input x b) The integration forward pass where all variables are marginalised
A mask over random variables is computed based on the scopes passed as input. This determines whether the integrated or normal circuit result is returned for each variable.
Source code in cirkit/backend/torch/queries.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 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 140 141 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 | |
_circuit = circuit
instance-attribute
¤
__call__(x, *, integrate_vars)
¤
Solve an integration query, given an input batch and the variables to integrate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
An input batch of shape \((B, C, D)\), where \(B\) is the batch size, \(C\) is the number of channels per variable, and \(D\) is the number of variables. |
required |
integrate_vars
|
Tensor | Scope | Iterable[Scope]
|
The variables to integrate. It must be a subset of the variables on which the circuit given in the constructor is defined on. The format can be one of the following three: 1. Tensor of shape (B, D) where B is the batch size and D is the number of variables in the scope of the circuit. Its dtype should be torch.bool and have True in the positions of random variables that should be marginalised out and False elsewhere. 2. Scope, in this case the same integration mask is applied for all entries of the batch 3. List of Scopes, where the length of the list must be either 1 or B. If the list has length 1, behaves as above. |
required |
Returns: The result of the integration query, given as a tensor of shape \((B, O, K)\), where \(B\) is the batch size, \(O\) is the number of output vectors of the circuit, and \(K\) is the number of units in each output vector.
Source code in cirkit/backend/torch/queries.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 110 | |
__init__(circuit)
¤
Initialize an integration query object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
TorchCircuit
|
The circuit to integrate over. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the circuit to integrate is not smooth or not decomposable. |
Source code in cirkit/backend/torch/queries.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
_layer_fn(layer, x, *, integrate_vars_mask)
staticmethod
¤
Source code in cirkit/backend/torch/queries.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 | |
scopes_to_mask(circuit, batch_integrate_vars)
staticmethod
¤
Accepts a batch of scopes and returns a boolean mask as a tensor with True in positions of specified scope indices and False otherwise.
Source code in cirkit/backend/torch/queries.py
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 | |
Query
¤
Bases: ABC
An object used to run queries of circuits compiled using the torch backend.
Source code in cirkit/backend/torch/queries.py
13 14 15 16 17 | |
__init__()
¤
Source code in cirkit/backend/torch/queries.py
16 17 | |
SamplingQuery
¤
Bases: Query
The sampling query object.
Source code in cirkit/backend/torch/queries.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
_circuit = circuit
instance-attribute
¤
__call__(num_samples=1)
¤
Sample a number of data points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_samples
|
int
|
The number of samples to return. |
1
|
Return
A pair (samples, mixture_samples), consisting of (i) an assignment to the observed variables the circuit is defined on, and (ii) the samples of the finitely-discrete latent variables associated to the sum units. The samples (i) are returned as a tensor of shape (num_samples, num_channels, num_variables).
Raises:
| Type | Description |
|---|---|
ValueError
|
if the number of samples is not a positive number. |
Source code in cirkit/backend/torch/queries.py
214 215 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 243 244 245 | |
__init__(circuit)
¤
Initialize a sampling query object. Currently, only sampling from the joint distribution is supported, i.e., sampling won't work in the case of circuits obtained by marginalization, or by observing evidence. Conditional sampling is currently not implemented.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
TorchCircuit
|
The circuit to sample from. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the circuit to sample from is not normalised. |
Source code in cirkit/backend/torch/queries.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
_layer_fn(layer, *inputs, num_samples, mixture_samples)
¤
Source code in cirkit/backend/torch/queries.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
_pad_samples(samples, scope_idx)
¤
Pads univariate samples to the size of the scope of the circuit (output dimension) according to scope for compatibility in downstream inner nodes.
Source code in cirkit/backend/torch/queries.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |