Skip to content

Commit

Permalink
feat: sketched the general structure of a qml model
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoLiegiBastonLiegi committed May 14, 2024
1 parent cbad478 commit 420ac26
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/qiboml/models/abstract.py
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
35 changes: 35 additions & 0 deletions src/qiboml/models/encoding_decoding.py
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 added src/qiboml/models/model.py~
Empty file.

0 comments on commit 420ac26

Please sign in to comment.