diff --git a/quantum_image_processing/data_encoder/image_representations/__init__.py b/quantum_image_processing/data_encoder/image_representations/__init__.py index b6afef7..bf3f9f3 100644 --- a/quantum_image_processing/data_encoder/image_representations/__init__.py +++ b/quantum_image_processing/data_encoder/image_representations/__init__.py @@ -1,7 +1,7 @@ from .frqi import FRQI from .neqr import NEQR -__all__=[ +__all__ = [ "FRQI", "NEQR", -] \ No newline at end of file +] diff --git a/quantum_image_processing/test-files/mnist-frqi-representation.py b/quantum_image_processing/test-files/mnist-frqi-representation.py deleted file mode 100644 index 8b56d12..0000000 --- a/quantum_image_processing/test-files/mnist-frqi-representation.py +++ /dev/null @@ -1,32 +0,0 @@ -from quantum_image_processing.data_encoder.image_representations.frqi import FRQI -from quantum_image_processing.data_loader.mnist_data_loader import load_mnist_data - - -if __name__ == "__main__": - _, __, train, test = load_mnist_data() - train_data = iter(train) - train_img, train_labels = next(train_data) - - print(train_img.shape, train_labels.shape) - - # Normalize in [0, pi/2] if already done during data loading. - # normal = torchvision.transforms.Normalize((0.2052,), (0.4840,))(train_img[0]) - # plt.imshow(normal[0], cmap=plt.get_cmap('gray')) - - new_dim = train_img[0].reshape(-1, 4) - new_dim_list = new_dim.tolist() - - color_vals = new_dim_list[0] - print(color_vals) - image_size = (2, 2) - - circ = FRQI(image_size, color_vals) - circ = circ.frqi(measure=True) - - # Measurement results - counts = FRQI.get_simulator_result( - circ, - backend="qasm_simulator", - shots=1024, - plot_counts=True, - ) diff --git a/quantum_image_processing/test-files/mnist-neqr-representation.py b/quantum_image_processing/test-files/mnist-neqr-representation.py deleted file mode 100644 index 1e366e0..0000000 --- a/quantum_image_processing/test-files/mnist-neqr-representation.py +++ /dev/null @@ -1,39 +0,0 @@ -import torchvision -import torch.utils.data -from quantum_image_processing.data_encoder.image_representations.neqr import NEQR -from quantum_image_processing.data_loader.mnist_data_loader import load_mnist_data -import matplotlib.pyplot as plt - -if __name__ == "__main__": - _, __, train, test = load_mnist_data() - train_data = iter(train) - train_img, train_labels = next(train_data) - - print(train_img.shape, train_labels.shape) - - torch.set_printoptions(precision=0) - - # plt.hist(np.array(train_img[i]).ravel(), bins=50, density=True); - # plt.xlabel("pixel values") - # plt.ylabel("relative frequency") - # plt.title("distribution of pixels") - # plt.show() - - normal = torchvision.transforms.Normalize(mean=0.0, std=(1 / 255.0))(train_img[0]) - - new_dim = normal.reshape(-1, 4) - new_dim_list = new_dim.tolist() - - color_vals = new_dim_list[0] - image_size = (2, 2) - - circ = NEQR(image_size, color_vals) - circ = circ.image_encoding(measure=True) - - # Measurement results - counts = NEQR.get_simulator_result( - circ, - backend="qasm_simulator", - shots=1024, - plot_counts=True, - ) diff --git a/quantum_image_processing/test-files/mnist-ttn-classification.py b/quantum_image_processing/test-files/mnist-ttn-classification.py deleted file mode 100644 index af2843f..0000000 --- a/quantum_image_processing/test-files/mnist-ttn-classification.py +++ /dev/null @@ -1,109 +0,0 @@ -import matplotlib.pyplot as plt -import numpy as np -from qiskit.utils import algorithm_globals -from qiskit.circuit import QuantumCircuit, ParameterVector -from qiskit_machine_learning.neural_networks import EstimatorQNN -from qiskit_machine_learning.algorithms.classifiers import NeuralNetworkClassifier -from qiskit.algorithms.optimizers import ADAM, COBYLA -from qiskit.quantum_info import SparsePauliOp -from quantum_image_processing.data_loader.mnist_data_loader import load_mnist_data -from quantum_image_processing.image_classification.tree_tensor_network_ttn import TTN - - -def data_embedding(img_dim): - """ - Embeds data using Qubit/Angle encoding for a - single feature. - Does not use QIR techniques. - Complexity: O(1); requires 1 qubit for 1 feature. - :return: - """ - - params = ParameterVector("img_data", img_dim) - embedding = QuantumCircuit(img_dim) - for i in range(img_dim): - embedding.ry(params[i], i) - - return embedding - - -if __name__ == "__main__": - _, __, train, test = load_mnist_data() - train_data = iter(train) - train_img, train_labels = next(train_data) - test_data = iter(test) - test_img, test_label = next(test_data) - - print(train_img.shape, train_labels.shape) - - train_labels = np.array(train_labels) - for i in range(len(train_labels)): - if train_labels[i] == 9: - train_labels[i] = -1 - elif train_labels[i] == 2: - train_labels[i] = 1 - - train_img = np.array(train_img.reshape(-1, 4)) - test_img = np.array(test_img.reshape(-1, 4)) - print(train_img.shape, test_img.shape) - - img_dim = 4 - embedding_circ = data_embedding(img_dim=img_dim) - embedding_circ.barrier() - ttn = TTN(img_dim=img_dim).ttn_simple(complex_struct=False) - circ = embedding_circ.compose(ttn, range(img_dim)) - circ.decompose().decompose().draw("mpl") - plt.show() - - observable = SparsePauliOp.from_list([("Z" + "I" * 3, 1)]) - # Shouldn't this be I*3 + Z (IIIZ) because we just measure the last qubit - # Shit it would be the exact opposite since qiskit ordering of qubit is diff. - # But would it be opposite really? - - def callback_graph(weights, obj_func_eval): - # clear_output(wait=True) - objective_func_vals.append(obj_func_eval) - plt.title("Objective function value against iteration") - plt.xlabel("Iteration") - plt.ylabel("Objective function value") - plt.plot(range(len(objective_func_vals)), objective_func_vals) - plt.savefig("graph.pdf") - plt.show() - - estimator_qnn = EstimatorQNN( - circuit=circ, - observables=observable, - input_params=embedding_circ.parameters, - weight_params=ttn.parameters, - ) - - estimator_qnn.forward( - input_data=train_img, - weights=algorithm_globals.random.random(estimator_qnn.num_weights), - ) - - estimator_classifier = NeuralNetworkClassifier( - estimator_qnn, - optimizer=COBYLA(maxiter=1), - callback=callback_graph, - ) - - objective_func_vals = [] - estimator_classifier.fit(train_img, np.array(train_labels)) - estimator_classifier.score(train_img, np.array(train_labels)) - - test_predict = estimator_classifier.predict(test_img) - # print(test_label, test_predict) - - test_label = np.array(test_label) - for i in range(len(test_label)): - if test_label[i] == 9: - test_label[i] = -1 - elif test_label[i] == 2: - test_label[i] = 1 - - count = 0 - for i in range(len(test_predict)): - if test_predict[i] == test_label[i]: - count += 1 - print(count)