Skip to content

compiler

compiler ¤

CompiledCircuit = TypeVar('CompiledCircuit') module-attribute ¤

InitializerCompilationSign = type[Initializer] module-attribute ¤

LayerCompilationSign = type[Layer] module-attribute ¤

ParameterCompilationSign = type[ParameterNode] module-attribute ¤

SUPPORTED_BACKENDS = ['torch'] module-attribute ¤

AbstractCompiler ¤

Bases: ABC

Source code in cirkit/backend/compiler.py
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
class AbstractCompiler(ABC):
    def __init__(
        self,
        layers_registry: CompilerLayerRegistry,
        parameters_registry: CompilerParameterRegistry,
        initializers_registry: CompilerInitializerRegistry,
        **flags,
    ):
        self._layers_registry = layers_registry
        self._parameters_registry = parameters_registry
        self._initializers_registry = initializers_registry
        self._flags = flags
        self._compiled_circuits = CompiledCircuitsMap()

    def is_compiled(self, sc: Circuit) -> bool:
        return self._compiled_circuits.is_compiled(sc)

    def has_symbolic(self, cc: CompiledCircuit) -> bool:
        return self._compiled_circuits.has_symbolic(cc)

    def get_compiled_circuit(self, sc: Circuit) -> CompiledCircuit:
        return self._compiled_circuits.get_compiled_circuit(sc)

    def get_symbolic_circuit(self, cc: CompiledCircuit) -> Circuit:
        return self._compiled_circuits.get_symbolic_circuit(cc)

    def register_compiled_circuit(self, sc: Circuit, cc: CompiledCircuit):
        self._compiled_circuits.register_compiled_circuit(sc, cc)

    def add_layer_rule(self, func: LayerCompilationFunc):
        self._layers_registry.add_rule(func)

    def add_parameter_rule(self, func: ParameterCompilationFunc):
        self._parameters_registry.add_rule(func)

    def add_initializer_rule(self, func: InitializerCompilationFunc):
        self._initializers_registry.add_rule(func)

    def retrieve_layer_rule(self, signature: LayerCompilationSign) -> LayerCompilationFunc:
        return self._layers_registry.retrieve_rule(signature)

    def retrieve_parameter_rule(
        self, signature: ParameterCompilationSign
    ) -> ParameterCompilationFunc:
        return self._parameters_registry.retrieve_rule(signature)

    def retrieve_initializer_rule(
        self, signature: InitializerCompilationSign
    ) -> InitializerCompilationFunc:
        return self._initializers_registry.retrieve_rule(signature)

    def compile(self, sc: Circuit) -> CompiledCircuit:
        if self.is_compiled(sc):
            return self.get_compiled_circuit(sc)
        return self.compile_pipeline(sc)

    @abstractmethod
    def compile_pipeline(self, sc: Circuit) -> CompiledCircuit:
        ...

_compiled_circuits = CompiledCircuitsMap() instance-attribute ¤

_flags = flags instance-attribute ¤

_initializers_registry = initializers_registry instance-attribute ¤

_layers_registry = layers_registry instance-attribute ¤

_parameters_registry = parameters_registry instance-attribute ¤

__init__(layers_registry, parameters_registry, initializers_registry, **flags) ¤

Source code in cirkit/backend/compiler.py
156
157
158
159
160
161
162
163
164
165
166
167
def __init__(
    self,
    layers_registry: CompilerLayerRegistry,
    parameters_registry: CompilerParameterRegistry,
    initializers_registry: CompilerInitializerRegistry,
    **flags,
):
    self._layers_registry = layers_registry
    self._parameters_registry = parameters_registry
    self._initializers_registry = initializers_registry
    self._flags = flags
    self._compiled_circuits = CompiledCircuitsMap()

add_initializer_rule(func) ¤

Source code in cirkit/backend/compiler.py
190
191
def add_initializer_rule(self, func: InitializerCompilationFunc):
    self._initializers_registry.add_rule(func)

add_layer_rule(func) ¤

Source code in cirkit/backend/compiler.py
184
185
def add_layer_rule(self, func: LayerCompilationFunc):
    self._layers_registry.add_rule(func)

add_parameter_rule(func) ¤

Source code in cirkit/backend/compiler.py
187
188
def add_parameter_rule(self, func: ParameterCompilationFunc):
    self._parameters_registry.add_rule(func)

compile(sc) ¤

Source code in cirkit/backend/compiler.py
206
207
208
209
def compile(self, sc: Circuit) -> CompiledCircuit:
    if self.is_compiled(sc):
        return self.get_compiled_circuit(sc)
    return self.compile_pipeline(sc)

compile_pipeline(sc) abstractmethod ¤

Source code in cirkit/backend/compiler.py
211
212
213
@abstractmethod
def compile_pipeline(self, sc: Circuit) -> CompiledCircuit:
    ...

get_compiled_circuit(sc) ¤

Source code in cirkit/backend/compiler.py
175
176
def get_compiled_circuit(self, sc: Circuit) -> CompiledCircuit:
    return self._compiled_circuits.get_compiled_circuit(sc)

get_symbolic_circuit(cc) ¤

Source code in cirkit/backend/compiler.py
178
179
def get_symbolic_circuit(self, cc: CompiledCircuit) -> Circuit:
    return self._compiled_circuits.get_symbolic_circuit(cc)

has_symbolic(cc) ¤

Source code in cirkit/backend/compiler.py
172
173
def has_symbolic(self, cc: CompiledCircuit) -> bool:
    return self._compiled_circuits.has_symbolic(cc)

is_compiled(sc) ¤

Source code in cirkit/backend/compiler.py
169
170
def is_compiled(self, sc: Circuit) -> bool:
    return self._compiled_circuits.is_compiled(sc)

register_compiled_circuit(sc, cc) ¤

Source code in cirkit/backend/compiler.py
181
182
def register_compiled_circuit(self, sc: Circuit, cc: CompiledCircuit):
    self._compiled_circuits.register_compiled_circuit(sc, cc)

retrieve_initializer_rule(signature) ¤

Source code in cirkit/backend/compiler.py
201
202
203
204
def retrieve_initializer_rule(
    self, signature: InitializerCompilationSign
) -> InitializerCompilationFunc:
    return self._initializers_registry.retrieve_rule(signature)

retrieve_layer_rule(signature) ¤

Source code in cirkit/backend/compiler.py
193
194
def retrieve_layer_rule(self, signature: LayerCompilationSign) -> LayerCompilationFunc:
    return self._layers_registry.retrieve_rule(signature)

retrieve_parameter_rule(signature) ¤

Source code in cirkit/backend/compiler.py
196
197
198
199
def retrieve_parameter_rule(
    self, signature: ParameterCompilationSign
) -> ParameterCompilationFunc:
    return self._parameters_registry.retrieve_rule(signature)

CompilationRuleNotFound ¤

Bases: Exception

An exception that is raised when a compilation rule is not found.

Source code in cirkit/backend/compiler.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
class CompilationRuleNotFound(Exception):
    """An exception that is raised when a compilation rule is not found."""

    def __init__(self, msg: str):
        """Initializes a compilation rule not found exception.

        Args:
            msg: The message of the exception.
        """
        super().__init__(msg)

__init__(msg) ¤

Initializes a compilation rule not found exception.

Parameters:

Name Type Description Default
msg str

The message of the exception.

required
Source code in cirkit/backend/compiler.py
 94
 95
 96
 97
 98
 99
100
def __init__(self, msg: str):
    """Initializes a compilation rule not found exception.

    Args:
        msg: The message of the exception.
    """
    super().__init__(msg)

CompiledCircuitsMap ¤

Source code in cirkit/backend/compiler.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class CompiledCircuitsMap:
    def __init__(self):
        self._bimap = BiMap[Circuit, CompiledCircuit]()

    def is_compiled(self, sc: Circuit) -> bool:
        return self._bimap.has_left(sc)

    def has_symbolic(self, cc: CompiledCircuit) -> bool:
        return self._bimap.has_right(cc)

    def get_compiled_circuit(self, sc: Circuit) -> CompiledCircuit:
        return self._bimap.get_left(sc)

    def get_symbolic_circuit(self, cc: CompiledCircuit) -> Circuit:
        return self._bimap.get_right(cc)

    def register_compiled_circuit(self, sc: Circuit, cc: CompiledCircuit):
        self._bimap.add(sc, cc)

_bimap = BiMap[Circuit, CompiledCircuit]() instance-attribute ¤

__init__() ¤

Source code in cirkit/backend/compiler.py
17
18
def __init__(self):
    self._bimap = BiMap[Circuit, CompiledCircuit]()

get_compiled_circuit(sc) ¤

Source code in cirkit/backend/compiler.py
26
27
def get_compiled_circuit(self, sc: Circuit) -> CompiledCircuit:
    return self._bimap.get_left(sc)

get_symbolic_circuit(cc) ¤

Source code in cirkit/backend/compiler.py
29
30
def get_symbolic_circuit(self, cc: CompiledCircuit) -> Circuit:
    return self._bimap.get_right(cc)

has_symbolic(cc) ¤

Source code in cirkit/backend/compiler.py
23
24
def has_symbolic(self, cc: CompiledCircuit) -> bool:
    return self._bimap.has_right(cc)

is_compiled(sc) ¤

Source code in cirkit/backend/compiler.py
20
21
def is_compiled(self, sc: Circuit) -> bool:
    return self._bimap.has_left(sc)

register_compiled_circuit(sc, cc) ¤

Source code in cirkit/backend/compiler.py
32
33
def register_compiled_circuit(self, sc: Circuit, cc: CompiledCircuit):
    self._bimap.add(sc, cc)

CompilerInitializerRegistry ¤

Bases: CompilerRegistry[InitializerCompilationSign, InitializerCompilationFunc]

Source code in cirkit/backend/compiler.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class CompilerInitializerRegistry(
    CompilerRegistry[InitializerCompilationSign, InitializerCompilationFunc]
):
    @classmethod
    def _validate_rule_function(cls, func: InitializerCompilationFunc) -> bool:
        ann = func.__annotations__.copy()
        del ann["return"]
        args = tuple(ann.keys())
        return issubclass(ann[args[-1]], Initializer)

    @classmethod
    def _retrieve_signature(cls, func: ParameterCompilationFunc) -> InitializerCompilationSign:
        ann = func.__annotations__.copy()
        del ann["return"]
        args = tuple(ann.keys())
        return cast(InitializerCompilationSign, ann[args[-1]])

_retrieve_signature(func) classmethod ¤

Source code in cirkit/backend/compiler.py
147
148
149
150
151
152
@classmethod
def _retrieve_signature(cls, func: ParameterCompilationFunc) -> InitializerCompilationSign:
    ann = func.__annotations__.copy()
    del ann["return"]
    args = tuple(ann.keys())
    return cast(InitializerCompilationSign, ann[args[-1]])

_validate_rule_function(func) classmethod ¤

Source code in cirkit/backend/compiler.py
140
141
142
143
144
145
@classmethod
def _validate_rule_function(cls, func: InitializerCompilationFunc) -> bool:
    ann = func.__annotations__.copy()
    del ann["return"]
    args = tuple(ann.keys())
    return issubclass(ann[args[-1]], Initializer)

CompilerLayerRegistry ¤

Bases: CompilerRegistry[LayerCompilationSign, LayerCompilationFunc]

Source code in cirkit/backend/compiler.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class CompilerLayerRegistry(CompilerRegistry[LayerCompilationSign, LayerCompilationFunc]):
    @classmethod
    def _validate_rule_function(cls, func: LayerCompilationFunc) -> bool:
        ann = func.__annotations__.copy()
        del ann["return"]
        args = tuple(ann.keys())
        return issubclass(ann[args[-1]], Layer)

    @classmethod
    def _retrieve_signature(cls, func: LayerCompilationFunc) -> LayerCompilationSign:
        ann = func.__annotations__.copy()
        del ann["return"]
        args = tuple(ann.keys())
        return cast(LayerCompilationSign, ann[args[-1]])

_retrieve_signature(func) classmethod ¤

Source code in cirkit/backend/compiler.py
111
112
113
114
115
116
@classmethod
def _retrieve_signature(cls, func: LayerCompilationFunc) -> LayerCompilationSign:
    ann = func.__annotations__.copy()
    del ann["return"]
    args = tuple(ann.keys())
    return cast(LayerCompilationSign, ann[args[-1]])

_validate_rule_function(func) classmethod ¤

Source code in cirkit/backend/compiler.py
104
105
106
107
108
109
@classmethod
def _validate_rule_function(cls, func: LayerCompilationFunc) -> bool:
    ann = func.__annotations__.copy()
    del ann["return"]
    args = tuple(ann.keys())
    return issubclass(ann[args[-1]], Layer)

CompilerParameterRegistry ¤

Bases: CompilerRegistry[ParameterCompilationSign, ParameterCompilationFunc]

Source code in cirkit/backend/compiler.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class CompilerParameterRegistry(
    CompilerRegistry[ParameterCompilationSign, ParameterCompilationFunc]
):
    @classmethod
    def _validate_rule_function(cls, func: ParameterCompilationFunc) -> bool:
        ann = func.__annotations__.copy()
        del ann["return"]
        args = tuple(ann.keys())
        return issubclass(ann[args[-1]], ParameterNode)

    @classmethod
    def _retrieve_signature(cls, func: ParameterCompilationFunc) -> ParameterCompilationSign:
        ann = func.__annotations__.copy()
        del ann["return"]
        args = tuple(ann.keys())
        return cast(ParameterCompilationSign, ann[args[-1]])

_retrieve_signature(func) classmethod ¤

Source code in cirkit/backend/compiler.py
129
130
131
132
133
134
@classmethod
def _retrieve_signature(cls, func: ParameterCompilationFunc) -> ParameterCompilationSign:
    ann = func.__annotations__.copy()
    del ann["return"]
    args = tuple(ann.keys())
    return cast(ParameterCompilationSign, ann[args[-1]])

_validate_rule_function(func) classmethod ¤

Source code in cirkit/backend/compiler.py
122
123
124
125
126
127
@classmethod
def _validate_rule_function(cls, func: ParameterCompilationFunc) -> bool:
    ann = func.__annotations__.copy()
    del ann["return"]
    args = tuple(ann.keys())
    return issubclass(ann[args[-1]], ParameterNode)

InitializerCompilationFunc ¤

Bases: Protocol

The initialization method compilation function protocol.

Source code in cirkit/backend/compiler.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class InitializerCompilationFunc(Protocol):
    """The initialization method compilation function protocol."""

    def __call__(self, compiler: "AbstractCompiler", init: Initializer, **kwargs) -> Any:
        """Compile a symbolic initializer, given a compiler.

        Args:
            compiler: The compiler.
            init: The symbolic initializer.
            **kwargs: The optional arguments for the compilation.

        Returns:
            A representation of the compiled initializer,
                which depends on the chosen compilation backend.
        """

__call__(compiler, init, **kwargs) ¤

Compile a symbolic initializer, given a compiler.

Parameters:

Name Type Description Default
compiler AbstractCompiler

The compiler.

required
init Initializer

The symbolic initializer.

required
**kwargs

The optional arguments for the compilation.

{}

Returns:

Type Description
Any

A representation of the compiled initializer, which depends on the chosen compilation backend.

Source code in cirkit/backend/compiler.py
77
78
79
80
81
82
83
84
85
86
87
88
def __call__(self, compiler: "AbstractCompiler", init: Initializer, **kwargs) -> Any:
    """Compile a symbolic initializer, given a compiler.

    Args:
        compiler: The compiler.
        init: The symbolic initializer.
        **kwargs: The optional arguments for the compilation.

    Returns:
        A representation of the compiled initializer,
            which depends on the chosen compilation backend.
    """

LayerCompilationFunc ¤

Bases: Protocol

The layer compilation function protocol.

Source code in cirkit/backend/compiler.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class LayerCompilationFunc(Protocol):
    """The layer compilation function protocol."""

    def __call__(self, compiler: "AbstractCompiler", sl: Layer, **kwargs) -> Any:
        """Compile a symbolic layer, given a compiler.

        Args:
            compiler: The compiler.
            sl: The symbolic layer.
            **kwargs: The optional arguments for the compilation.

        Returns:
            A representation of the compiled layer, which depends on the chosen compilation backend.
        """

__call__(compiler, sl, **kwargs) ¤

Compile a symbolic layer, given a compiler.

Parameters:

Name Type Description Default
compiler AbstractCompiler

The compiler.

required
sl Layer

The symbolic layer.

required
**kwargs

The optional arguments for the compilation.

{}

Returns:

Type Description
Any

A representation of the compiled layer, which depends on the chosen compilation backend.

Source code in cirkit/backend/compiler.py
44
45
46
47
48
49
50
51
52
53
54
def __call__(self, compiler: "AbstractCompiler", sl: Layer, **kwargs) -> Any:
    """Compile a symbolic layer, given a compiler.

    Args:
        compiler: The compiler.
        sl: The symbolic layer.
        **kwargs: The optional arguments for the compilation.

    Returns:
        A representation of the compiled layer, which depends on the chosen compilation backend.
    """

ParameterCompilationFunc ¤

Bases: Protocol

The parameter node compilation function protocol.

Source code in cirkit/backend/compiler.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class ParameterCompilationFunc(Protocol):
    """The parameter node compilation function protocol."""

    def __call__(self, compiler: "AbstractCompiler", p: ParameterNode, **kwargs) -> Any:
        """Compile a symbolic parameter node, given a compiler.

        Args:
            compiler: The compiler.
            p: The symbolic parameter node.
            **kwargs: The optional arguments for the compilation.

        Returns:
            A representation of the compiled parameter node,
                which depends on the chosen compilation backend.
        """

__call__(compiler, p, **kwargs) ¤

Compile a symbolic parameter node, given a compiler.

Parameters:

Name Type Description Default
compiler AbstractCompiler

The compiler.

required
p ParameterNode

The symbolic parameter node.

required
**kwargs

The optional arguments for the compilation.

{}

Returns:

Type Description
Any

A representation of the compiled parameter node, which depends on the chosen compilation backend.

Source code in cirkit/backend/compiler.py
60
61
62
63
64
65
66
67
68
69
70
71
def __call__(self, compiler: "AbstractCompiler", p: ParameterNode, **kwargs) -> Any:
    """Compile a symbolic parameter node, given a compiler.

    Args:
        compiler: The compiler.
        p: The symbolic parameter node.
        **kwargs: The optional arguments for the compilation.

    Returns:
        A representation of the compiled parameter node,
            which depends on the chosen compilation backend.
    """