Skip to content

Commit 8c28cef

Browse files
Merge pull request #24 from Using-Namespace-System/main
added documentation
2 parents 3a677e6 + 8207195 commit 8c28cef

8 files changed

+881
-59
lines changed

mosaiQue/README.md

+99-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,101 @@
11
# mosaiQue
22

3-
variable quanvolutional kernel
3+
A Python library for applying quantum convolutions with PennyLane, designed to facilitate image processing and transformation through custom convolution layers and quantum computations.
4+
5+
## Table of Contents
6+
7+
- [Installation](#installation)
8+
- [Usage](#usage)
9+
- [API Reference](#api-reference)
10+
- [Contributing](#contributing)
11+
- [License](#license)
12+
13+
## Installation
14+
15+
To install the library, clone the repository and install the required dependencies including conda-build
16+
17+
use develop to install the library
18+
19+
sudo /opt/conda/bin/conda-develop -n QML-QPF PATH /workspaces/QML-QPF/mosaiQue
20+
21+
## Usage
22+
23+
Overview of the main classes in the library:
24+
25+
### ConvolutionLayer4x4
26+
27+
- Create a convolution layer with a custom kernel shape.
28+
- Fit the kernel to your dataset.
29+
- Transform the dataset using convolution.
30+
- Save a portion of the dataset or load a previously saved dataset.
31+
32+
### QuantumLayer
33+
34+
- Define a quantum kernel operation using PennyLane.
35+
- Create an operation layer using the quantum kernel.
36+
- Preprocess images using a Keras model that incorporates the quantum layer.
37+
38+
### Kernel2d4x4
39+
40+
- Initialize the kernel with a specific shape.
41+
- Fit the kernel to the image data.
42+
- Transform the image data into patches and reconstruct the original image data from patches.
43+
- Merge channels back to the original shape.
44+
45+
## API Reference
46+
47+
### `ConvolutionLayer4x4`
48+
49+
#### Attributes
50+
- **name** (str): The name of the convolution layer.
51+
- **kernel** (kernels.Kernel2d4x4): The kernel used for convolution operations.
52+
53+
#### Methods
54+
- **fit(dataset: np.ndarray)**: Fits the kernel to the provided dataset.
55+
- **transform(dataset: np.ndarray) -> np.ndarray**: Applies the convolution transformation to the dataset.
56+
- **post_transform(dataset: np.ndarray) -> np.ndarray**: Applies post-processing to the dataset after transformation.
57+
- **channel_merge(dataset: np.ndarray) -> np.ndarray**: Merges channels in the dataset.
58+
- **save(dataset: np.ndarray, variant: List[int])**: Saves a portion of the dataset to a `.npy` file based on the variant.
59+
- **open(variant: List[int]) -> np.ndarray**: Loads a saved dataset from a `.npy` file based on the variant.
60+
61+
### `QuantumLayer`
62+
63+
#### Attributes
64+
- **q_node** (qml.QNode): The quantum node to execute quantum computations.
65+
66+
#### Methods
67+
- **call(inputs: tf.Tensor) -> tf.Tensor**: Executes the quantum layer on the input tensor.
68+
69+
### `OperationLayer`
70+
71+
#### Attributes
72+
- **q_layer** (QuantumLayer): The quantum layer for processing inputs.
73+
- **kernel_operation** (qml.QNode): The QNode for the kernel operation.
74+
75+
#### Methods
76+
- **pre_op() -> keras.Model**: Initializes and returns a custom Keras model used to preprocess images.
77+
78+
### `Kernel2d4x4`
79+
80+
#### Attributes
81+
- **input_shape** (list[int]): The shape of the input data.
82+
- **kernel_shape** (list[int]): The shape of the kernel.
83+
84+
#### Methods
85+
- **fit(X: np.ndarray)**: Fits the kernel to the input data by storing its shape.
86+
- **transform(X: np.ndarray) -> np.ndarray**: Splits the input data into smaller patches based on the kernel shape.
87+
- **post_transform(X: np.ndarray) -> np.ndarray**: Reconstructs the image data from patches.
88+
- **channel_merge(X: np.ndarray) -> np.ndarray**: Merges channels to restore the original order.
89+
90+
## Contributing
91+
92+
Contributions are welcome! Please feel free to submit issues or pull requests. To contribute:
93+
94+
1. Fork the repository.
95+
2. Create a new branch for your feature or bug fix.
96+
3. Make your changes and commit them.
97+
4. Push to your branch and create a pull request.
98+
99+
## License
100+
101+
This project is licensed under the GPL-3.0 License. See the license file for details.

mosaiQue/image.png

404 KB
Loading

mosaiQue/mosaique/models/__init__.py

+104-13
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,143 @@
1+
# __init__.py
2+
# This module defines custom transformations for image convolution.
3+
# Author: Brian Recktenwall-Calvet
4+
# Date: 12-18-2024
5+
# Version: 1.0
6+
17
import os
28
import pathlib
39
import mosaique.models.kernels
410
import numpy as np
5-
from typing import Any, Callable
11+
from typing import Any, List, Optional
12+
613

714
class ConvolutionLayer4x4:
15+
"""
16+
A class representing a 4x4 convolution layer using a specified kernel.
17+
18+
Attributes:
19+
_name (str): The name of the convolution layer.
20+
_kernel (kernels.Kernel2d4x4): An instance of the kernel used for convolution operations.
21+
22+
Methods:
23+
fit(dataset): Fits the kernel to the provided dataset.
24+
transform(dataset): Applies the convolution transformation to the dataset.
25+
post_transform(dataset): Applies post-processing to the dataset after transformation.
26+
channel_merge(dataset): Merges channels in the dataset.
27+
save(dataset, variant): Saves a portion of the dataset to a .npy file based on the variant.
28+
open(variant): Loads a saved dataset from a .npy file based on the variant.
29+
"""
30+
831
_name: str
932
_kernel: kernels.Kernel2d4x4
1033

11-
1234
@property
13-
def name(self) -> str :
35+
def name(self) -> str:
36+
"""Gets the name of the convolution layer."""
1437
return self._name
38+
1539
@name.setter
1640
def name(self, value: str):
41+
"""Sets the name of the convolution layer."""
1742
self._name = value
43+
1844
@property
1945
def kernel(self) -> kernels.Kernel2d4x4:
46+
"""Gets the kernel used for convolution operations."""
2047
return self._kernel
48+
2149
@kernel.setter
2250
def kernel(self, value: kernels.Kernel2d4x4):
51+
"""Sets the kernel used for convolution operations."""
2352
self._kernel = value
2453

25-
def __init__(self, name: str, kernel_shape:[int]=None):
54+
def __init__(self, name: str, kernel_shape: Optional[List[int]] = None):
55+
"""
56+
Initializes the ConvolutionLayer4x4 with a given name and kernel shape.
57+
58+
Args:
59+
name (str): The name of the convolution layer.
60+
kernel_shape (Optional[List[int]]): The shape of the kernel (default is [2, 2]).
61+
62+
Raises:
63+
ValueError: If kernel_shape is not a valid shape for the kernel.
64+
"""
2665
if kernel_shape is None:
2766
kernel_shape = [2, 2]
2867
self.name = name
2968
self.kernel = kernels.Kernel2d4x4(kernel_shape)
3069

3170
def fit(self, dataset: np.ndarray[..., np.dtype[Any]]):
71+
"""
72+
Fits the kernel to the provided dataset.
73+
74+
Args:
75+
dataset (np.ndarray): The input dataset used to fit the kernel.
76+
"""
3277
self.kernel.fit(dataset)
3378

34-
def transform(self, dataset: np.ndarray[..., np.dtype[Any]]):
79+
def transform(self, dataset: np.ndarray[..., np.dtype[Any]]) -> np.ndarray:
80+
"""
81+
Applies the convolution transformation to the dataset.
82+
83+
Args:
84+
dataset (np.ndarray): The input dataset to transform.
85+
86+
Returns:
87+
np.ndarray: The transformed dataset after applying the convolution.
88+
"""
3589
return self.kernel.transform(dataset)
3690

37-
def post_transform(self, dataset: np.ndarray[..., np.dtype[Any]]):
91+
def post_transform(self, dataset: np.ndarray[..., np.dtype[Any]]) -> np.ndarray:
92+
"""
93+
Applies post-processing to the dataset after transformation.
94+
95+
Args:
96+
dataset (np.ndarray): The input dataset to post-process.
97+
98+
Returns:
99+
np.ndarray: The post-processed dataset.
100+
"""
38101
return self.kernel.post_transform(dataset)
39-
def channel_merge(self, dataset: np.ndarray[..., np.dtype[Any]]):
102+
103+
def channel_merge(self, dataset: np.ndarray[..., np.dtype[Any]]) -> np.ndarray:
104+
"""
105+
Merges channels in the dataset, typically for multi-channel inputs.
106+
107+
Args:
108+
dataset (np.ndarray): The input dataset whose channels will be merged.
109+
110+
Returns:
111+
np.ndarray: The dataset with merged channels.
112+
"""
40113
return self.kernel.channel_merge(dataset)
41114

42-
def save(self, dataset: np.ndarray[..., np.dtype[Any]], variant:[int]):
43-
variant_string = ''.join(map(str,variant))
115+
def save(self, dataset: np.ndarray[..., np.dtype[Any]], variant: List[int]):
116+
"""
117+
Saves a portion of the dataset to a .npy file based on the variant.
118+
119+
Args:
120+
dataset (np.ndarray): The dataset to save.
121+
variant (List[int]): A list of indices specifying which channels to save.
122+
"""
123+
variant_string = ''.join(map(str, variant))
44124
workdir = str(pathlib.Path().resolve()) + "/" + self.name
45125
os.makedirs(workdir, exist_ok=True)
46-
np.save(os.path.join(workdir, variant_string), dataset[:,:,:,variant])
126+
np.save(os.path.join(workdir, variant_string), dataset[:, :, :, variant])
127+
128+
def open(self, variant: List[int]) -> np.ndarray:
129+
"""
130+
Loads a saved dataset from a .npy file based on the variant.
47131
48-
def open(self, variant:[int]):
49-
variant_string = ''.join(map(str,variant))
132+
Args:
133+
variant (List[int]): A list of indices specifying which channels to load.
134+
135+
Returns:
136+
np.ndarray: The loaded dataset.
137+
138+
Raises:
139+
FileNotFoundError: If the specified file does not exist.
140+
"""
141+
variant_string = ''.join(map(str, variant))
50142
workdir = str(pathlib.Path().resolve()) + "/" + self.name
51143
return np.load(os.path.join(workdir, variant_string + '.npy'))
52-

0 commit comments

Comments
 (0)