-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sketched the general structure of a qml model
- Loading branch information
1 parent
cbad478
commit 420ac26
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Defines the general structure of a qiboml module""" | ||
|
||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
|
||
from qibo import Circuit | ||
|
||
from qiboml.backends import JaxBackend | ||
|
||
|
||
@dataclass | ||
class QuantumCircuitLayer(ABC): | ||
|
||
circuit: Circuit = None | ||
backend: "qibo.backends.Backend" = JaxBackend() | ||
|
||
@abstractmethod | ||
def __post_init__(self): | ||
pass | ||
|
||
@abstractmethod | ||
def _feed_input(self, x): | ||
pass | ||
|
||
def forward(self, x): | ||
"""Performs the forward pass: prepares the input and execute the circuit.""" | ||
self._feed_input(x) | ||
return self.backend.execute_circuit(self.circuit) | ||
|
||
@abstractmethod | ||
def backward(self, input_grad: "ndarray"): | ||
pass | ||
|
||
@property | ||
def parameters(self): | ||
return self.circuit.get_parameters() | ||
|
||
@parameters.setter | ||
def parameters(self, x: "ndarray"): | ||
self.circuit.set_parameters(x) | ||
|
||
|
||
@dataclass | ||
class SequentialQuantumModel(QuantumCircuitLayer): | ||
|
||
layers: list[QuantumCircuitLayer] = [] | ||
|
||
def __post_init__(self): | ||
# in principle differently sized circuits might be passed | ||
nqubits = max([layer.circuit.nqubits for layer in self.layers]) | ||
self.circuit = Circuit(nqubits) | ||
for layer in self.layers: | ||
circ = Circuit(nqubits) | ||
circ.add(layer.circuit.on_qubits(*range(layer.circuit.nqubits))) | ||
self.circuit = self.circuit + circ | ||
|
||
def _feed_input(self, x): | ||
self.layers[0]._feed_input(x) | ||
|
||
def backward(self, input_grad: "ndarray"): | ||
grad = input_grad | ||
for layer in self.layers: | ||
grad = layer.backward(grad) | ||
return grad |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Some standard encoding and decoding layers""" | ||
|
||
from qiboml.models.abstract import QuantumCircuitLayer | ||
|
||
|
||
class QuantumEncodingLayer(QuantumCircuitLayer): | ||
pass | ||
|
||
|
||
class PhaseEncodingLayer(QuantumEncodingLayer): | ||
pass | ||
|
||
|
||
class AmplitudeEncodingLayer(QuantumEncodingLayer): | ||
pass | ||
|
||
|
||
""" | ||
. | ||
. | ||
. | ||
. | ||
""" | ||
|
||
|
||
class QuantumDecodingLayer(QuantumCircuitLayer): | ||
pass | ||
|
||
|
||
""" | ||
. | ||
. | ||
. | ||
. | ||
""" |
Empty file.
Empty file.