pipeline
pipeline
¤
_PIPELINE_CONTEXT = ContextVar('_PIPELINE_CONTEXT', default=PipelineContext.from_default_backend())
module-attribute
¤
PipelineContext
¤
Bases: AbstractContextManager
A pipeline context is a Python context manager used to compile circuits and specify backend-specific compilation flags and optimizations. A pipeline context can also be used to register compilation rules for user-defined layers, parameterizations and initialization methods. Furthermore, new layer operators can be added to the context.
Source code in cirkit/pipeline.py
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 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 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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
_backend = backend
instance-attribute
¤
_backend_kwargs = backend_kwargs
instance-attribute
¤
_compiler = retrieve_compiler(backend, **backend_kwargs)
instance-attribute
¤
_op_registry = OperatorRegistry.from_default_rules()
instance-attribute
¤
_token = None
instance-attribute
¤
__enter__()
¤
Enters a pipeline context.
Returns:
| Type | Description |
|---|---|
PipelineContext
|
Itself. |
Source code in cirkit/pipeline.py
78 79 80 81 82 83 84 85 86 | |
__exit__(__exc_type, __exc_value, __traceback)
¤
Exit a pipeline context.
Source code in cirkit/pipeline.py
88 89 90 91 92 93 94 95 96 97 98 | |
__getitem__(sc)
¤
Retrieves a compiled circuit, given a symbolic one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sc
|
Circuit
|
The symbolic circuit. |
required |
Returns:
| Type | Description |
|---|---|
CompiledCircuit
|
The circuit compiled from the given symbolic circuit. |
Source code in cirkit/pipeline.py
67 68 69 70 71 72 73 74 75 76 | |
__init__(backend='torch', **backend_kwargs)
¤
Initialzes a pipeline context, given the compilation backend and the compilation flags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str
|
The compilation backend. The only backend supported is 'torch'. |
'torch'
|
backend_kwargs
|
The compilation flags to pass to the compiler. |
{}
|
Raises:
| Type | Description |
|---|---|
ValuerError
|
if the compilation backend is unknown. |
Source code in cirkit/pipeline.py
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 | |
add_initializer_compilation_rule(func)
¤
Add a new initialization method compilation rule to the current compilation backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
InitializerCompilationFunc
|
The initializer compilation rule. |
required |
Source code in cirkit/pipeline.py
125 126 127 128 129 130 131 | |
add_layer_compilation_rule(func)
¤
Add a new layer compilation rule to the current compilation backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
LayerCompilationFunc
|
The layer compilation rule. |
required |
Source code in cirkit/pipeline.py
109 110 111 112 113 114 115 | |
add_operator_rule(op, func)
¤
Add a new layer operator to the context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
op
|
LayerOperator
|
The layer operator. |
required |
func
|
LayerOperatorFunc
|
The layer operator implementation. |
required |
Source code in cirkit/pipeline.py
100 101 102 103 104 105 106 107 | |
add_parameter_compilation_rule(func)
¤
Add a new parameter compilation rule to the current compilation backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
ParameterCompilationFunc
|
The parameter compilation rule. |
required |
Source code in cirkit/pipeline.py
117 118 119 120 121 122 123 | |
compile(sc)
¤
Compile a symbolic circuit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sc
|
Circuit
|
The symbolic circuit. |
required |
Returns:
| Type | Description |
|---|---|
CompiledCircuit
|
A compiled circuit, whose type depends on the chosen compilation backend. |
Source code in cirkit/pipeline.py
133 134 135 136 137 138 139 140 141 142 | |
concatenate(*cc)
¤
Circuit concatenation interface for compiled circuits. See concantenate for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cc
|
CompiledCircuit
|
A sequence of compiled circuits. |
()
|
Returns:
| Type | Description |
|---|---|
CompiledCircuit
|
The circuit that encodes the concatenation of the given circuits. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the given circuits have not been compiled in this context. |
Source code in cirkit/pipeline.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
conjugate(cc)
¤
Circuit conjugation interface for compiled circuits. See conjugate for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cc
|
CompiledCircuit
|
The compiled circuit. |
required |
Returns:
| Type | Description |
|---|---|
CompiledCircuit
|
The circuit that encodes the complex conjugation of the given compiled circuit. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the given circuit has not been compiled in this context. |
Source code in cirkit/pipeline.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
differentiate(cc, *, order=1)
¤
Circuit differentiation interface for compiled circuits. See differentiate for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cc
|
CompiledCircuit
|
The compiled circuit. |
required |
order
|
int
|
The differentiation order. |
1
|
Returns:
| Type | Description |
|---|---|
CompiledCircuit
|
The circuit that encodes the differentiation of the given compiled circuit. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the given circuit has not been compiled in this context. |
ValueError
|
if the differentiation order is not a positive integer. |
Source code in cirkit/pipeline.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
from_default_backend()
classmethod
¤
Construts a pipeline context from the default backend. The default backend is 'torch'.
Returns:
| Type | Description |
|---|---|
PipelineContext
|
A pipeline context from the default backend by specifying default values for the compilation flags. |
Source code in cirkit/pipeline.py
56 57 58 59 60 61 62 63 64 65 | |
get_compiled_circuit(sc)
¤
Retrieves a compiled circuit, given a symbolic one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sc
|
Circuit
|
The symbolic circuit. |
required |
Returns:
| Type | Description |
|---|---|
CompiledCircuit
|
The circuit compiled from the given symbolic circuit. |
Source code in cirkit/pipeline.py
167 168 169 170 171 172 173 174 175 176 | |
get_symbolic_circuit(cc)
¤
Retrieves a symbolic circuit, given a compiled one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cc
|
CompiledCircuit
|
The compiled circuit. |
required |
Returns:
| Type | Description |
|---|---|
Circuit
|
The symbolic circuit associated to the given compiled one. |
Source code in cirkit/pipeline.py
178 179 180 181 182 183 184 185 186 187 | |
has_symbolic(cc)
¤
Check whether a compiled circuit has a corresponding symbolic circuit in this context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cc
|
CompiledCircuit
|
The compiled circuit. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
False if the given compiled circuit has been compiled in this context, False otherwise. |
Source code in cirkit/pipeline.py
155 156 157 158 159 160 161 162 163 164 165 | |
integrate(cc, scope=None)
¤
Circuit integration interface for compiled circuits. See concantenate for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cc
|
CompiledCircuit
|
A compiled circuit. |
required |
scope
|
Scope | None
|
The variables scope to integrate. If it is None, then all variables the given circuit is defined on are integrated. |
None
|
Returns:
| Type | Description |
|---|---|
CompiledCircuit
|
The circuit that encodes the integration over (some) variables of the given circuit. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the given circuit has not been compiled in this context. |
Source code in cirkit/pipeline.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
is_compiled(sc)
¤
Check whether a symbolic circuit has been compiled in this context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sc
|
Circuit
|
The symbolic circuit. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the given symbolic circuit has been compiled in this context, False otherwise. |
Source code in cirkit/pipeline.py
144 145 146 147 148 149 150 151 152 153 | |
multiply(cc1, cc2)
¤
Circuit multiplication interface for compiled circuits. See multiply for more details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cc1
|
CompiledCircuit
|
The first compiled circuit. |
required |
cc2
|
CompiledCircuit
|
The second compiled circuit. |
required |
Returns:
| Type | Description |
|---|---|
CompiledCircuit
|
The circuit that encodes the multiplicaton between the given circuits. |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the given circuits have not been compiled in this context. |
Source code in cirkit/pipeline.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
compile(sc, ctx=None)
¤
Source code in cirkit/pipeline.py
296 297 298 299 | |
concatenate(*cc, ctx=None)
¤
Source code in cirkit/pipeline.py
302 303 304 305 | |
conjugate(cc, ctx=None)
¤
Source code in cirkit/pipeline.py
334 335 336 337 | |
differentiate(cc, ctx=None, *, order=1)
¤
Source code in cirkit/pipeline.py
326 327 328 329 330 331 | |
integrate(cc, scope=None, ctx=None)
¤
Source code in cirkit/pipeline.py
308 309 310 311 312 313 314 315 | |
multiply(cc1, cc2, ctx=None)
¤
Source code in cirkit/pipeline.py
318 319 320 321 322 323 | |
retrieve_compiler(backend, **backend_kwargs)
¤
Source code in cirkit/pipeline.py
340 341 342 343 344 345 346 347 348 | |