From c9f06d557bf8974765d7a68f0859f5e22800ee16 Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Tue, 19 Dec 2023 13:13:49 -0800 Subject: [PATCH 01/14] add files for quanvolutional neural network class and made some doc string edits to qcnn --- .../models/neural_networks/qcnn.py | 6 +-- .../models/neural_networks/quanvolutional.py | 0 .../quanvolutional_neural_network.py | 44 +++++++++++++++++++ .../test_quanvolutional_neural_network.py | 6 +++ 4 files changed, 53 insertions(+), 3 deletions(-) delete mode 100644 quantum_image_processing/models/neural_networks/quanvolutional.py create mode 100644 quantum_image_processing/models/neural_networks/quanvolutional_neural_network.py create mode 100644 tests/models/neural_networks/test_quanvolutional_neural_network.py diff --git a/quantum_image_processing/models/neural_networks/qcnn.py b/quantum_image_processing/models/neural_networks/qcnn.py index 73d69aa..41dbe4b 100644 --- a/quantum_image_processing/models/neural_networks/qcnn.py +++ b/quantum_image_processing/models/neural_networks/qcnn.py @@ -23,8 +23,8 @@ class QCNN(QuantumNeuralNetwork): def __init__(self, num_qubits: int): """ - Initializes a Quantum Neural Network circuit with the given - number of qubits. + Initializes a Quantum Convolutional Neural Network + circuit with the given number of qubits. Args: num_qubits (int): builds a quantum convolutional neural @@ -35,7 +35,7 @@ def __init__(self, num_qubits: int): def sequence(self, operations: list[tuple[Callable, dict]]) -> QuantumCircuit: """ - Builds a QNN circuit by composing the circuit with given + Builds a QCNN circuit by composing the circuit with given sequence of list of operations. Args: diff --git a/quantum_image_processing/models/neural_networks/quanvolutional.py b/quantum_image_processing/models/neural_networks/quanvolutional.py deleted file mode 100644 index e69de29..0000000 diff --git a/quantum_image_processing/models/neural_networks/quanvolutional_neural_network.py b/quantum_image_processing/models/neural_networks/quanvolutional_neural_network.py new file mode 100644 index 0000000..12d2ec1 --- /dev/null +++ b/quantum_image_processing/models/neural_networks/quanvolutional_neural_network.py @@ -0,0 +1,44 @@ +"""Quanvolutional Neural Network""" +from __future__ import annotations +from typing import Callable +from qiskit.circuit import QuantumCircuit +from quantum_image_processing.models.neural_networks.quantum_neural_network import QuantumNeuralNetwork + + +class QuanvolutionalNeuralNetwork(QuantumNeuralNetwork): + """ + Builds a Quanvolutional Neural Network [1]. + + References: + [1] M. Henderson, S. Shakya, S. Pradhan, and + T. Cook, “Quanvolutional Neural Networks: + Powering Image Recognition with Quantum Circuits,” + arXiv:1904.04767 [quant-ph], Apr. 2019, + Available: https://arxiv.org/abs/1904.04767 + """ + + def __init__(self, num_qubits: int): + """ + Initializes a Quanvolutional Neural Network circuit + with the given number of qubits. + + Args: + num_qubits (int): builds a quantum convolutional neural + network circuit with the given number of qubits or image + dimensions. + """ + QuantumNeuralNetwork.__init__(self, num_qubits) + + def sequence(self, operations: list[tuple[Callable, dict]]) -> QuantumCircuit: + """ + Builds a Quanvolutional NN circuit by composing the circuit + with given sequence of list of operations. + + Args: + operations (list[tuple[Callable, dict]]: a tuple + of a Layer object and a dictionary of its arguments. + + Returns: + circuit (QuantumCircuit): final QNN circuit with all the + layers. + """ \ No newline at end of file diff --git a/tests/models/neural_networks/test_quanvolutional_neural_network.py b/tests/models/neural_networks/test_quanvolutional_neural_network.py new file mode 100644 index 0000000..358e637 --- /dev/null +++ b/tests/models/neural_networks/test_quanvolutional_neural_network.py @@ -0,0 +1,6 @@ +"""Tests for Quanvolutional Neural Network class""" +from __future__ import annotations + + +class TestQuanvolutionalNeuralNetwork: + pass From 28e34df9a0be2d67a63f98284bff32dbc5ea0e35 Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Wed, 20 Dec 2023 19:38:13 -0800 Subject: [PATCH 02/14] change directory structure to remove models directory. Neural networks, tensor networks and variational classifier directories are now directly under quantum_image_processing directory --- README.md | 4 ++-- quantum_image_processing/models/__init__.py | 5 ----- .../{models => }/neural_networks/__init__.py | 0 .../{models => }/neural_networks/layers/__init__.py | 0 .../{models => }/neural_networks/layers/base_layer.py | 0 .../neural_networks/layers/convolutional_layer.py | 4 ++-- .../neural_networks/layers/fully_connected_layer.py | 2 +- .../{models => }/neural_networks/layers/pooling_layer.py | 2 +- .../{models => }/neural_networks/qcnn.py | 2 +- .../{models => }/neural_networks/quantum_autoencoder.py | 0 .../{models => }/neural_networks/quantum_neural_network.py | 0 .../{models => }/neural_networks/quanvolutional.py | 0 .../{models => }/tensor_network_circuits/__init__.py | 0 .../tensor_network_circuits/base_tensor_network.py | 0 .../{models => }/tensor_network_circuits/mera.py | 2 +- .../{models => }/tensor_network_circuits/mps.py | 2 +- .../{models => }/tensor_network_circuits/peps.py | 0 .../{models => }/tensor_network_circuits/ttn.py | 2 +- .../{models => }/variational_classifiers/__init__.py | 0 .../{models => }/variational_classifiers/data_reuploading.py | 0 .../{models => }/variational_classifiers/kernel_estimator.py | 0 .../variational_classifiers/variational_classifier.py | 0 tests/conftest.py | 4 ---- tests/models/tensor_networks/__init__.py | 0 tests/{models => neural_networks}/__init__.py | 0 .../neural_networks => neural_networks/layers}/__init__.py | 0 tests/{models => }/neural_networks/layers/test_base_layer.py | 2 +- .../neural_networks/layers/test_convolutional_layer.py | 2 +- .../neural_networks/layers/test_fully_connected_layer.py | 2 +- .../neural_networks/layers/test_pooling_layer.py | 2 +- tests/{models => }/neural_networks/test_qcnn.py | 4 ++-- .../neural_networks/layers => tensor_networks}/__init__.py | 0 tests/{models => }/tensor_networks/test_mera.py | 2 +- tests/{models => }/tensor_networks/test_mps.py | 2 +- tests/{models => }/tensor_networks/test_ttn.py | 2 +- 35 files changed, 19 insertions(+), 28 deletions(-) delete mode 100644 quantum_image_processing/models/__init__.py rename quantum_image_processing/{models => }/neural_networks/__init__.py (100%) rename quantum_image_processing/{models => }/neural_networks/layers/__init__.py (100%) rename quantum_image_processing/{models => }/neural_networks/layers/base_layer.py (100%) rename quantum_image_processing/{models => }/neural_networks/layers/convolutional_layer.py (95%) rename quantum_image_processing/{models => }/neural_networks/layers/fully_connected_layer.py (95%) rename quantum_image_processing/{models => }/neural_networks/layers/pooling_layer.py (98%) rename quantum_image_processing/{models => }/neural_networks/qcnn.py (96%) rename quantum_image_processing/{models => }/neural_networks/quantum_autoencoder.py (100%) rename quantum_image_processing/{models => }/neural_networks/quantum_neural_network.py (100%) rename quantum_image_processing/{models => }/neural_networks/quanvolutional.py (100%) rename quantum_image_processing/{models => }/tensor_network_circuits/__init__.py (100%) rename quantum_image_processing/{models => }/tensor_network_circuits/base_tensor_network.py (100%) rename quantum_image_processing/{models => }/tensor_network_circuits/mera.py (98%) rename quantum_image_processing/{models => }/tensor_network_circuits/mps.py (98%) rename quantum_image_processing/{models => }/tensor_network_circuits/peps.py (100%) rename quantum_image_processing/{models => }/tensor_network_circuits/ttn.py (98%) rename quantum_image_processing/{models => }/variational_classifiers/__init__.py (100%) rename quantum_image_processing/{models => }/variational_classifiers/data_reuploading.py (100%) rename quantum_image_processing/{models => }/variational_classifiers/kernel_estimator.py (100%) rename quantum_image_processing/{models => }/variational_classifiers/variational_classifier.py (100%) delete mode 100644 tests/models/tensor_networks/__init__.py rename tests/{models => neural_networks}/__init__.py (100%) rename tests/{models/neural_networks => neural_networks/layers}/__init__.py (100%) rename tests/{models => }/neural_networks/layers/test_base_layer.py (98%) rename tests/{models => }/neural_networks/layers/test_convolutional_layer.py (97%) rename tests/{models => }/neural_networks/layers/test_fully_connected_layer.py (95%) rename tests/{models => }/neural_networks/layers/test_pooling_layer.py (98%) rename tests/{models => }/neural_networks/test_qcnn.py (96%) rename tests/{models/neural_networks/layers => tensor_networks}/__init__.py (100%) rename tests/{models => }/tensor_networks/test_mera.py (99%) rename tests/{models => }/tensor_networks/test_mps.py (98%) rename tests/{models => }/tensor_networks/test_ttn.py (98%) diff --git a/README.md b/README.md index 56e9275..3f8e7f2 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,12 @@ Let's build a Quantum Convolutional Neural Network (QCNN) with Convolutional, Pooling, and Fully-Connected layers. ```python -from quantum_image_processing.models.neural_networks.layers import ( +from quantum_image_processing.neural_networks.layers import ( QuantumConvolutionalLayer, QuantumPoolingLayer2, FullyConnectedLayer, ) -from quantum_image_processing.models.neural_networks.qcnn import QCNN +from quantum_image_processing.neural_networks import QCNN # Initializing a QCNN circuit with given image dimensions. image_dimensions = 4 diff --git a/quantum_image_processing/models/__init__.py b/quantum_image_processing/models/__init__.py deleted file mode 100644 index 7476b45..0000000 --- a/quantum_image_processing/models/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -""" -Quantum algorithms and models, including neural networks, tensor network -quantum circuits, variational classifiers, and more. -(module: quantum_image_processing.models) -""" diff --git a/quantum_image_processing/models/neural_networks/__init__.py b/quantum_image_processing/neural_networks/__init__.py similarity index 100% rename from quantum_image_processing/models/neural_networks/__init__.py rename to quantum_image_processing/neural_networks/__init__.py diff --git a/quantum_image_processing/models/neural_networks/layers/__init__.py b/quantum_image_processing/neural_networks/layers/__init__.py similarity index 100% rename from quantum_image_processing/models/neural_networks/layers/__init__.py rename to quantum_image_processing/neural_networks/layers/__init__.py diff --git a/quantum_image_processing/models/neural_networks/layers/base_layer.py b/quantum_image_processing/neural_networks/layers/base_layer.py similarity index 100% rename from quantum_image_processing/models/neural_networks/layers/base_layer.py rename to quantum_image_processing/neural_networks/layers/base_layer.py diff --git a/quantum_image_processing/models/neural_networks/layers/convolutional_layer.py b/quantum_image_processing/neural_networks/layers/convolutional_layer.py similarity index 95% rename from quantum_image_processing/models/neural_networks/layers/convolutional_layer.py rename to quantum_image_processing/neural_networks/layers/convolutional_layer.py index bc1998a..ba5d0ea 100644 --- a/quantum_image_processing/models/neural_networks/layers/convolutional_layer.py +++ b/quantum_image_processing/neural_networks/layers/convolutional_layer.py @@ -2,8 +2,8 @@ from __future__ import annotations from typing import Optional from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers.base_layer import BaseLayer -from quantum_image_processing.models.tensor_network_circuits.mera import MERA +from quantum_image_processing.neural_networks.layers.base_layer import BaseLayer +from quantum_image_processing.tensor_network_circuits.mera import MERA class QuantumConvolutionalLayer(BaseLayer): diff --git a/quantum_image_processing/models/neural_networks/layers/fully_connected_layer.py b/quantum_image_processing/neural_networks/layers/fully_connected_layer.py similarity index 95% rename from quantum_image_processing/models/neural_networks/layers/fully_connected_layer.py rename to quantum_image_processing/neural_networks/layers/fully_connected_layer.py index 1eb97aa..de12bad 100644 --- a/quantum_image_processing/models/neural_networks/layers/fully_connected_layer.py +++ b/quantum_image_processing/neural_networks/layers/fully_connected_layer.py @@ -1,7 +1,7 @@ """Quantum Fully Connected Layer Structure""" from __future__ import annotations from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers.base_layer import BaseLayer +from quantum_image_processing.neural_networks.layers.base_layer import BaseLayer class FullyConnectedLayer(BaseLayer): diff --git a/quantum_image_processing/models/neural_networks/layers/pooling_layer.py b/quantum_image_processing/neural_networks/layers/pooling_layer.py similarity index 98% rename from quantum_image_processing/models/neural_networks/layers/pooling_layer.py rename to quantum_image_processing/neural_networks/layers/pooling_layer.py index dd2f8ef..15d1cd9 100644 --- a/quantum_image_processing/models/neural_networks/layers/pooling_layer.py +++ b/quantum_image_processing/neural_networks/layers/pooling_layer.py @@ -2,7 +2,7 @@ from __future__ import annotations import itertools from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers.base_layer import BaseLayer +from quantum_image_processing.neural_networks.layers.base_layer import BaseLayer # pylint: disable = too-few-public-methods diff --git a/quantum_image_processing/models/neural_networks/qcnn.py b/quantum_image_processing/neural_networks/qcnn.py similarity index 96% rename from quantum_image_processing/models/neural_networks/qcnn.py rename to quantum_image_processing/neural_networks/qcnn.py index 73d69aa..de938c5 100644 --- a/quantum_image_processing/models/neural_networks/qcnn.py +++ b/quantum_image_processing/neural_networks/qcnn.py @@ -2,7 +2,7 @@ from __future__ import annotations from typing import Callable from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.quantum_neural_network import ( +from quantum_image_processing.neural_networks.quantum_neural_network import ( QuantumNeuralNetwork, ) diff --git a/quantum_image_processing/models/neural_networks/quantum_autoencoder.py b/quantum_image_processing/neural_networks/quantum_autoencoder.py similarity index 100% rename from quantum_image_processing/models/neural_networks/quantum_autoencoder.py rename to quantum_image_processing/neural_networks/quantum_autoencoder.py diff --git a/quantum_image_processing/models/neural_networks/quantum_neural_network.py b/quantum_image_processing/neural_networks/quantum_neural_network.py similarity index 100% rename from quantum_image_processing/models/neural_networks/quantum_neural_network.py rename to quantum_image_processing/neural_networks/quantum_neural_network.py diff --git a/quantum_image_processing/models/neural_networks/quanvolutional.py b/quantum_image_processing/neural_networks/quanvolutional.py similarity index 100% rename from quantum_image_processing/models/neural_networks/quanvolutional.py rename to quantum_image_processing/neural_networks/quanvolutional.py diff --git a/quantum_image_processing/models/tensor_network_circuits/__init__.py b/quantum_image_processing/tensor_network_circuits/__init__.py similarity index 100% rename from quantum_image_processing/models/tensor_network_circuits/__init__.py rename to quantum_image_processing/tensor_network_circuits/__init__.py diff --git a/quantum_image_processing/models/tensor_network_circuits/base_tensor_network.py b/quantum_image_processing/tensor_network_circuits/base_tensor_network.py similarity index 100% rename from quantum_image_processing/models/tensor_network_circuits/base_tensor_network.py rename to quantum_image_processing/tensor_network_circuits/base_tensor_network.py diff --git a/quantum_image_processing/models/tensor_network_circuits/mera.py b/quantum_image_processing/tensor_network_circuits/mera.py similarity index 98% rename from quantum_image_processing/models/tensor_network_circuits/mera.py rename to quantum_image_processing/tensor_network_circuits/mera.py index b2cf38a..15738d5 100644 --- a/quantum_image_processing/models/tensor_network_circuits/mera.py +++ b/quantum_image_processing/tensor_network_circuits/mera.py @@ -8,7 +8,7 @@ ParameterVector, ) from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary -from quantum_image_processing.models.tensor_network_circuits.base_tensor_network import ( +from quantum_image_processing.tensor_network_circuits.base_tensor_network import ( BaseTensorNetwork, ) diff --git a/quantum_image_processing/models/tensor_network_circuits/mps.py b/quantum_image_processing/tensor_network_circuits/mps.py similarity index 98% rename from quantum_image_processing/models/tensor_network_circuits/mps.py rename to quantum_image_processing/tensor_network_circuits/mps.py index 8e90111..e9bd30d 100644 --- a/quantum_image_processing/models/tensor_network_circuits/mps.py +++ b/quantum_image_processing/tensor_network_circuits/mps.py @@ -2,7 +2,7 @@ from __future__ import annotations from typing import Callable from qiskit.circuit import QuantumCircuit, ParameterVector -from quantum_image_processing.models.tensor_network_circuits.base_tensor_network import ( +from quantum_image_processing.tensor_network_circuits.base_tensor_network import ( BaseTensorNetwork, ) from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary diff --git a/quantum_image_processing/models/tensor_network_circuits/peps.py b/quantum_image_processing/tensor_network_circuits/peps.py similarity index 100% rename from quantum_image_processing/models/tensor_network_circuits/peps.py rename to quantum_image_processing/tensor_network_circuits/peps.py diff --git a/quantum_image_processing/models/tensor_network_circuits/ttn.py b/quantum_image_processing/tensor_network_circuits/ttn.py similarity index 98% rename from quantum_image_processing/models/tensor_network_circuits/ttn.py rename to quantum_image_processing/tensor_network_circuits/ttn.py index 5d8da58..798cda0 100644 --- a/quantum_image_processing/models/tensor_network_circuits/ttn.py +++ b/quantum_image_processing/tensor_network_circuits/ttn.py @@ -4,7 +4,7 @@ import numpy as np from qiskit.circuit import QuantumCircuit, ParameterVector from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary -from quantum_image_processing.models.tensor_network_circuits.base_tensor_network import ( +from quantum_image_processing.tensor_network_circuits.base_tensor_network import ( BaseTensorNetwork, ) diff --git a/quantum_image_processing/models/variational_classifiers/__init__.py b/quantum_image_processing/variational_classifiers/__init__.py similarity index 100% rename from quantum_image_processing/models/variational_classifiers/__init__.py rename to quantum_image_processing/variational_classifiers/__init__.py diff --git a/quantum_image_processing/models/variational_classifiers/data_reuploading.py b/quantum_image_processing/variational_classifiers/data_reuploading.py similarity index 100% rename from quantum_image_processing/models/variational_classifiers/data_reuploading.py rename to quantum_image_processing/variational_classifiers/data_reuploading.py diff --git a/quantum_image_processing/models/variational_classifiers/kernel_estimator.py b/quantum_image_processing/variational_classifiers/kernel_estimator.py similarity index 100% rename from quantum_image_processing/models/variational_classifiers/kernel_estimator.py rename to quantum_image_processing/variational_classifiers/kernel_estimator.py diff --git a/quantum_image_processing/models/variational_classifiers/variational_classifier.py b/quantum_image_processing/variational_classifiers/variational_classifier.py similarity index 100% rename from quantum_image_processing/models/variational_classifiers/variational_classifier.py rename to quantum_image_processing/variational_classifiers/variational_classifier.py diff --git a/tests/conftest.py b/tests/conftest.py index 3d726b8..173c056 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,2 @@ """conftest.py""" # pylint: disable=unused-import -from tests.data_encoder.image_representations.test_frqi import ( - circuit_pixel_position_fixture, -) -from tests.models.tensor_networks.test_mps import parameterization_mapper_fixture diff --git a/tests/models/tensor_networks/__init__.py b/tests/models/tensor_networks/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/models/__init__.py b/tests/neural_networks/__init__.py similarity index 100% rename from tests/models/__init__.py rename to tests/neural_networks/__init__.py diff --git a/tests/models/neural_networks/__init__.py b/tests/neural_networks/layers/__init__.py similarity index 100% rename from tests/models/neural_networks/__init__.py rename to tests/neural_networks/layers/__init__.py diff --git a/tests/models/neural_networks/layers/test_base_layer.py b/tests/neural_networks/layers/test_base_layer.py similarity index 98% rename from tests/models/neural_networks/layers/test_base_layer.py rename to tests/neural_networks/layers/test_base_layer.py index 3c5c2e7..0844ccf 100644 --- a/tests/models/neural_networks/layers/test_base_layer.py +++ b/tests/neural_networks/layers/test_base_layer.py @@ -3,7 +3,7 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers import ( +from quantum_image_processing.neural_networks.layers import ( QuantumConvolutionalLayer, ) diff --git a/tests/models/neural_networks/layers/test_convolutional_layer.py b/tests/neural_networks/layers/test_convolutional_layer.py similarity index 97% rename from tests/models/neural_networks/layers/test_convolutional_layer.py rename to tests/neural_networks/layers/test_convolutional_layer.py index 1ba8b75..72d01df 100644 --- a/tests/models/neural_networks/layers/test_convolutional_layer.py +++ b/tests/neural_networks/layers/test_convolutional_layer.py @@ -5,7 +5,7 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers import ( +from quantum_image_processing.neural_networks.layers import ( QuantumConvolutionalLayer, ) diff --git a/tests/models/neural_networks/layers/test_fully_connected_layer.py b/tests/neural_networks/layers/test_fully_connected_layer.py similarity index 95% rename from tests/models/neural_networks/layers/test_fully_connected_layer.py rename to tests/neural_networks/layers/test_fully_connected_layer.py index 7aa1236..977be5a 100644 --- a/tests/models/neural_networks/layers/test_fully_connected_layer.py +++ b/tests/neural_networks/layers/test_fully_connected_layer.py @@ -2,7 +2,7 @@ from __future__ import annotations import pytest from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers import FullyConnectedLayer +from quantum_image_processing.neural_networks.layers import FullyConnectedLayer @pytest.fixture diff --git a/tests/models/neural_networks/layers/test_pooling_layer.py b/tests/neural_networks/layers/test_pooling_layer.py similarity index 98% rename from tests/models/neural_networks/layers/test_pooling_layer.py rename to tests/neural_networks/layers/test_pooling_layer.py index b807da0..394e5c7 100644 --- a/tests/models/neural_networks/layers/test_pooling_layer.py +++ b/tests/neural_networks/layers/test_pooling_layer.py @@ -3,7 +3,7 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers import ( +from quantum_image_processing.neural_networks.layers import ( QuantumPoolingLayer2, QuantumPoolingLayer3, ) diff --git a/tests/models/neural_networks/test_qcnn.py b/tests/neural_networks/test_qcnn.py similarity index 96% rename from tests/models/neural_networks/test_qcnn.py rename to tests/neural_networks/test_qcnn.py index a065a1d..afe792d 100644 --- a/tests/models/neural_networks/test_qcnn.py +++ b/tests/neural_networks/test_qcnn.py @@ -5,8 +5,8 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.qcnn import QCNN -from quantum_image_processing.models.neural_networks.layers import ( +from quantum_image_processing.neural_networks import QCNN +from quantum_image_processing.neural_networks.layers import ( QuantumConvolutionalLayer, QuantumPoolingLayer2, QuantumPoolingLayer3, diff --git a/tests/models/neural_networks/layers/__init__.py b/tests/tensor_networks/__init__.py similarity index 100% rename from tests/models/neural_networks/layers/__init__.py rename to tests/tensor_networks/__init__.py diff --git a/tests/models/tensor_networks/test_mera.py b/tests/tensor_networks/test_mera.py similarity index 99% rename from tests/models/tensor_networks/test_mera.py rename to tests/tensor_networks/test_mera.py index b28a3a4..d81d3be 100644 --- a/tests/models/tensor_networks/test_mera.py +++ b/tests/tensor_networks/test_mera.py @@ -5,7 +5,7 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit, ParameterVector -from quantum_image_processing.models.tensor_network_circuits.mera import MERA +from quantum_image_processing.tensor_network_circuits import MERA from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary diff --git a/tests/models/tensor_networks/test_mps.py b/tests/tensor_networks/test_mps.py similarity index 98% rename from tests/models/tensor_networks/test_mps.py rename to tests/tensor_networks/test_mps.py index 15c4e53..5d805b9 100644 --- a/tests/models/tensor_networks/test_mps.py +++ b/tests/tensor_networks/test_mps.py @@ -4,7 +4,7 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit, ParameterVector -from quantum_image_processing.models.tensor_network_circuits.mps import MPS +from quantum_image_processing.tensor_network_circuits import MPS from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary diff --git a/tests/models/tensor_networks/test_ttn.py b/tests/tensor_networks/test_ttn.py similarity index 98% rename from tests/models/tensor_networks/test_ttn.py rename to tests/tensor_networks/test_ttn.py index 8ad5339..55c92ad 100644 --- a/tests/models/tensor_networks/test_ttn.py +++ b/tests/tensor_networks/test_ttn.py @@ -3,7 +3,7 @@ from unittest import mock import pytest from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.tensor_network_circuits.ttn import TTN +from quantum_image_processing.tensor_network_circuits.ttn import TTN from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary From ee669c825c8db2b001a0da56f1fab86287e2af99 Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Wed, 20 Dec 2023 20:25:27 -0800 Subject: [PATCH 03/14] changes to conftest and mock.patch --- tests/conftest.py | 4 ++++ tests/neural_networks/layers/test_convolutional_layer.py | 2 +- tests/neural_networks/test_qcnn.py | 2 +- tests/tensor_networks/test_mera.py | 4 ++-- tests/tensor_networks/test_mps.py | 4 ++-- tests/tensor_networks/test_ttn.py | 4 ++-- 6 files changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 173c056..28f2c8e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,2 +1,6 @@ """conftest.py""" # pylint: disable=unused-import +from tests.data_encoder.image_representations.test_frqi import ( + circuit_pixel_position_fixture, +) +from tests.tensor_networks.test_mps import parameterization_mapper_fixture diff --git a/tests/neural_networks/layers/test_convolutional_layer.py b/tests/neural_networks/layers/test_convolutional_layer.py index 72d01df..ba9414f 100644 --- a/tests/neural_networks/layers/test_convolutional_layer.py +++ b/tests/neural_networks/layers/test_convolutional_layer.py @@ -59,7 +59,7 @@ def test_build_layer(self, num_qubits, circuit, unmeasured_bits, mera_args): ) mock_circuit = QuantumCircuit(qc_layer.num_qubits) with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.mera.MERA.mera_backbone", + "quantum_image_processing.tensor_network_circuits.mera.MERA.mera_backbone", return_value=mock_circuit, ) as mock_mera: _, _ = qc_layer.build_layer() diff --git a/tests/neural_networks/test_qcnn.py b/tests/neural_networks/test_qcnn.py index afe792d..05e63b2 100644 --- a/tests/neural_networks/test_qcnn.py +++ b/tests/neural_networks/test_qcnn.py @@ -103,7 +103,7 @@ def test_params(self, num_qubits, operations): def test_sequence(self, num_qubits, operations): """Tests the sequence method of QCNN class.""" with mock.patch.multiple( - "quantum_image_processing.models.neural_networks.layers", + "quantum_image_processing.neural_networks.layers", QuantumConvolutionalLayer=mock.DEFAULT, QuantumPoolingLayer2=mock.DEFAULT, QuantumPoolingLayer3=mock.DEFAULT, diff --git a/tests/tensor_networks/test_mera.py b/tests/tensor_networks/test_mera.py index d81d3be..7c2e0e1 100644 --- a/tests/tensor_networks/test_mera.py +++ b/tests/tensor_networks/test_mera.py @@ -175,7 +175,7 @@ def test_mera_simple(self, num_qubits, layer_depth, complex_structure): # pylint: disable=line-too-long """Tests the mera_backbone method call via the mera_simple function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.mera.MERA.mera_backbone" + "quantum_image_processing.tensor_network_circuits.mera.MERA.mera_backbone" ) as mock_mera_simple: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.simple_parameterization" @@ -193,7 +193,7 @@ def test_mera_general(self, num_qubits, layer_depth, complex_structure): # pylint: disable=line-too-long """Tests the mera_backbone method call via the mera_general function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.mera.MERA.mera_backbone" + "quantum_image_processing.tensor_network_circuits.mera.MERA.mera_backbone" ) as mock_mera_general: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.general_parameterization" diff --git a/tests/tensor_networks/test_mps.py b/tests/tensor_networks/test_mps.py index 5d805b9..269e534 100644 --- a/tests/tensor_networks/test_mps.py +++ b/tests/tensor_networks/test_mps.py @@ -107,7 +107,7 @@ def test_mps_simple(self, num_qubits, complex_structure): # pylint: disable=line-too-long """Tests the mps_backbone method call via the mps_simple function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.mps.MPS.mps_backbone" + "quantum_image_processing.tensor_network_circuits.mps.MPS.mps_backbone" ) as mock_mps_simple: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.simple_parameterization" @@ -122,7 +122,7 @@ def test_mps_general(self, num_qubits, complex_structure): # pylint: disable=line-too-long """Tests the mps_backbone method call via the mps_general function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.mps.MPS.mps_backbone" + "quantum_image_processing.tensor_network_circuits.mps.MPS.mps_backbone" ) as mock_mps_general: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.general_parameterization" diff --git a/tests/tensor_networks/test_ttn.py b/tests/tensor_networks/test_ttn.py index 55c92ad..d1f8152 100644 --- a/tests/tensor_networks/test_ttn.py +++ b/tests/tensor_networks/test_ttn.py @@ -88,7 +88,7 @@ def test_ttn_simple(self, num_qubits, complex_structure): # pylint: disable=line-too-long """Tests the ttn_backbone method call via the ttn_simple function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.ttn.TTN.ttn_backbone" + "quantum_image_processing.tensor_network_circuits.ttn.TTN.ttn_backbone" ) as mock_ttn_simple: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.simple_parameterization" @@ -103,7 +103,7 @@ def test_ttn_general(self, num_qubits, complex_structure): # pylint: disable=line-too-long """Tests the ttn_backbone method call via the ttn_general function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.ttn.TTN.ttn_backbone" + "quantum_image_processing.tensor_network_circuits.ttn.TTN.ttn_backbone" ) as mock_ttn_general: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.general_parameterization" From 305ed308b3313b2d981620ab9284211c4c436f1e Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Thu, 21 Dec 2023 02:45:34 -0800 Subject: [PATCH 04/14] change to quanvolutional layer instead of quanvolutional nn --- .../layers/quanvolutional_layer.py | 44 +++++++++++++++++++ .../quanvolutional_neural_network.py | 44 ------------------- .../layers/test_quanvolutional_layer.py | 6 +++ .../test_quanvolutional_neural_network.py | 6 --- 4 files changed, 50 insertions(+), 50 deletions(-) create mode 100644 quantum_image_processing/neural_networks/layers/quanvolutional_layer.py delete mode 100644 quantum_image_processing/neural_networks/quanvolutional_neural_network.py create mode 100644 tests/neural_networks/layers/test_quanvolutional_layer.py delete mode 100644 tests/neural_networks/test_quanvolutional_neural_network.py diff --git a/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py new file mode 100644 index 0000000..6a8c7c7 --- /dev/null +++ b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py @@ -0,0 +1,44 @@ +"""Quanvolutional Neural Network""" +from __future__ import annotations +from qiskit.circuit import QuantumCircuit +from quantum_image_processing.neural_networks.layers.base_layer import BaseLayer + + +class QuanvolutionalLayer(BaseLayer): + """ + Builds a Quanvolutional Layer [1]. + + References: + [1] M. Henderson, S. Shakya, S. Pradhan, and + T. Cook, “Quanvolutional Neural Networks: + Powering Image Recognition with Quantum Circuits,” + arXiv:1904.04767 [quant-ph], Apr. 2019, + Available: https://arxiv.org/abs/1904.04767 + """ + + def __init__(self, num_qubits: int, circuit: QuantumCircuit, unmeasured_bits: list): + """ + Initializes a Quanvolutional Layer circuit + with the given number of qubits. + + Args: + num_qubits (int): builds a quantum convolutional neural + network circuit with the given number of qubits or image + dimensions. + + circuit (QuantumCircuit): Takes quantum circuit with/without + an existing layer as an input, and applies a quanvolutional + layer over it. + """ + BaseLayer.__init__(self, num_qubits, circuit, unmeasured_bits) + + def build_layer(self) -> tuple[QuantumCircuit, list]: + """ + Builds the Quanvolutional layer circuit + + Returns: + circuit (QuantumCircuit): circuit with a quanvolutional layer. + + unmeasured_bits (dict): a dictionary of unmeasured qubits + and classical bits in the circuit. + """ diff --git a/quantum_image_processing/neural_networks/quanvolutional_neural_network.py b/quantum_image_processing/neural_networks/quanvolutional_neural_network.py deleted file mode 100644 index 12d2ec1..0000000 --- a/quantum_image_processing/neural_networks/quanvolutional_neural_network.py +++ /dev/null @@ -1,44 +0,0 @@ -"""Quanvolutional Neural Network""" -from __future__ import annotations -from typing import Callable -from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.quantum_neural_network import QuantumNeuralNetwork - - -class QuanvolutionalNeuralNetwork(QuantumNeuralNetwork): - """ - Builds a Quanvolutional Neural Network [1]. - - References: - [1] M. Henderson, S. Shakya, S. Pradhan, and - T. Cook, “Quanvolutional Neural Networks: - Powering Image Recognition with Quantum Circuits,” - arXiv:1904.04767 [quant-ph], Apr. 2019, - Available: https://arxiv.org/abs/1904.04767 - """ - - def __init__(self, num_qubits: int): - """ - Initializes a Quanvolutional Neural Network circuit - with the given number of qubits. - - Args: - num_qubits (int): builds a quantum convolutional neural - network circuit with the given number of qubits or image - dimensions. - """ - QuantumNeuralNetwork.__init__(self, num_qubits) - - def sequence(self, operations: list[tuple[Callable, dict]]) -> QuantumCircuit: - """ - Builds a Quanvolutional NN circuit by composing the circuit - with given sequence of list of operations. - - Args: - operations (list[tuple[Callable, dict]]: a tuple - of a Layer object and a dictionary of its arguments. - - Returns: - circuit (QuantumCircuit): final QNN circuit with all the - layers. - """ \ No newline at end of file diff --git a/tests/neural_networks/layers/test_quanvolutional_layer.py b/tests/neural_networks/layers/test_quanvolutional_layer.py new file mode 100644 index 0000000..8c858f0 --- /dev/null +++ b/tests/neural_networks/layers/test_quanvolutional_layer.py @@ -0,0 +1,6 @@ +"""Tests for Quanvolutional Layer class""" +from __future__ import annotations + + +class TestQuanvolutionalLayer: + pass diff --git a/tests/neural_networks/test_quanvolutional_neural_network.py b/tests/neural_networks/test_quanvolutional_neural_network.py deleted file mode 100644 index 358e637..0000000 --- a/tests/neural_networks/test_quanvolutional_neural_network.py +++ /dev/null @@ -1,6 +0,0 @@ -"""Tests for Quanvolutional Neural Network class""" -from __future__ import annotations - - -class TestQuanvolutionalNeuralNetwork: - pass From dbc122d96c00c114b340a1e42d061ae937f8e2ef Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Thu, 21 Dec 2023 02:58:06 -0800 Subject: [PATCH 05/14] resolve rebase and merge conflicts --- README.md | 4 +- quantum_image_processing/models/__init__.py | 5 --- .../quanvolutional_neural_network.py | 44 ------------------- .../{models => }/neural_networks/__init__.py | 0 .../neural_networks/layers/__init__.py | 0 .../neural_networks/layers/base_layer.py | 0 .../layers/convolutional_layer.py | 4 +- .../layers/fully_connected_layer.py | 2 +- .../neural_networks/layers/pooling_layer.py | 2 +- .../layers/quanvolutional_layer.py | 39 ++++++++++++++++ .../{models => }/neural_networks/qcnn.py | 2 +- .../neural_networks/quantum_autoencoder.py | 0 .../neural_networks/quantum_neural_network.py | 0 .../quanvolutional.py} | 0 .../tensor_network_circuits/__init__.py | 0 .../base_tensor_network.py | 0 .../tensor_network_circuits/mera.py | 2 +- .../tensor_network_circuits/mps.py | 2 +- .../peps.py} | 0 .../tensor_network_circuits/ttn.py | 2 +- .../variational_classifiers/__init__.py | 0 .../data_reuploading.py} | 0 .../kernel_estimator.py} | 0 .../variational_classifier.py | 0 tests/conftest.py | 4 -- .../test_quanvolutional_neural_network.py | 6 --- .../{models => }/neural_networks/__init__.py | 0 .../neural_networks/layers/__init__.py | 0 .../neural_networks/layers/test_base_layer.py | 2 +- .../layers/test_convolutional_layer.py | 2 +- .../layers/test_fully_connected_layer.py | 2 +- .../layers/test_pooling_layer.py | 2 +- .../layers/test_quanvolutional_layer.py | 6 +++ .../{models => }/neural_networks/test_qcnn.py | 4 +- .../{models => }/tensor_networks/__init__.py | 0 .../{models => }/tensor_networks/test_mera.py | 2 +- .../{models => }/tensor_networks/test_mps.py | 2 +- .../{models => }/tensor_networks/test_ttn.py | 2 +- 38 files changed, 64 insertions(+), 78 deletions(-) delete mode 100644 quantum_image_processing/models/__init__.py delete mode 100644 quantum_image_processing/models/neural_networks/quanvolutional_neural_network.py rename quantum_image_processing/{models => }/neural_networks/__init__.py (100%) rename quantum_image_processing/{models => }/neural_networks/layers/__init__.py (100%) rename quantum_image_processing/{models => }/neural_networks/layers/base_layer.py (100%) rename quantum_image_processing/{models => }/neural_networks/layers/convolutional_layer.py (95%) rename quantum_image_processing/{models => }/neural_networks/layers/fully_connected_layer.py (95%) rename quantum_image_processing/{models => }/neural_networks/layers/pooling_layer.py (98%) create mode 100644 quantum_image_processing/neural_networks/layers/quanvolutional_layer.py rename quantum_image_processing/{models => }/neural_networks/qcnn.py (96%) rename quantum_image_processing/{models => }/neural_networks/quantum_autoencoder.py (100%) rename quantum_image_processing/{models => }/neural_networks/quantum_neural_network.py (100%) rename quantum_image_processing/{models/tensor_network_circuits/peps.py => neural_networks/quanvolutional.py} (100%) rename quantum_image_processing/{models => }/tensor_network_circuits/__init__.py (100%) rename quantum_image_processing/{models => }/tensor_network_circuits/base_tensor_network.py (100%) rename quantum_image_processing/{models => }/tensor_network_circuits/mera.py (98%) rename quantum_image_processing/{models => }/tensor_network_circuits/mps.py (98%) rename quantum_image_processing/{models/variational_classifiers/data_reuploading.py => tensor_network_circuits/peps.py} (100%) rename quantum_image_processing/{models => }/tensor_network_circuits/ttn.py (98%) rename quantum_image_processing/{models => }/variational_classifiers/__init__.py (100%) rename quantum_image_processing/{models/variational_classifiers/kernel_estimator.py => variational_classifiers/data_reuploading.py} (100%) rename quantum_image_processing/{models/variational_classifiers/variational_classifier.py => variational_classifiers/kernel_estimator.py} (100%) rename tests/models/__init__.py => quantum_image_processing/variational_classifiers/variational_classifier.py (100%) delete mode 100644 tests/models/neural_networks/test_quanvolutional_neural_network.py rename tests/{models => }/neural_networks/__init__.py (100%) rename tests/{models => }/neural_networks/layers/__init__.py (100%) rename tests/{models => }/neural_networks/layers/test_base_layer.py (98%) rename tests/{models => }/neural_networks/layers/test_convolutional_layer.py (97%) rename tests/{models => }/neural_networks/layers/test_fully_connected_layer.py (95%) rename tests/{models => }/neural_networks/layers/test_pooling_layer.py (98%) create mode 100644 tests/neural_networks/layers/test_quanvolutional_layer.py rename tests/{models => }/neural_networks/test_qcnn.py (96%) rename tests/{models => }/tensor_networks/__init__.py (100%) rename tests/{models => }/tensor_networks/test_mera.py (99%) rename tests/{models => }/tensor_networks/test_mps.py (98%) rename tests/{models => }/tensor_networks/test_ttn.py (98%) diff --git a/README.md b/README.md index 56e9275..3f8e7f2 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,12 @@ Let's build a Quantum Convolutional Neural Network (QCNN) with Convolutional, Pooling, and Fully-Connected layers. ```python -from quantum_image_processing.models.neural_networks.layers import ( +from quantum_image_processing.neural_networks.layers import ( QuantumConvolutionalLayer, QuantumPoolingLayer2, FullyConnectedLayer, ) -from quantum_image_processing.models.neural_networks.qcnn import QCNN +from quantum_image_processing.neural_networks import QCNN # Initializing a QCNN circuit with given image dimensions. image_dimensions = 4 diff --git a/quantum_image_processing/models/__init__.py b/quantum_image_processing/models/__init__.py deleted file mode 100644 index 7476b45..0000000 --- a/quantum_image_processing/models/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -""" -Quantum algorithms and models, including neural networks, tensor network -quantum circuits, variational classifiers, and more. -(module: quantum_image_processing.models) -""" diff --git a/quantum_image_processing/models/neural_networks/quanvolutional_neural_network.py b/quantum_image_processing/models/neural_networks/quanvolutional_neural_network.py deleted file mode 100644 index 12d2ec1..0000000 --- a/quantum_image_processing/models/neural_networks/quanvolutional_neural_network.py +++ /dev/null @@ -1,44 +0,0 @@ -"""Quanvolutional Neural Network""" -from __future__ import annotations -from typing import Callable -from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.quantum_neural_network import QuantumNeuralNetwork - - -class QuanvolutionalNeuralNetwork(QuantumNeuralNetwork): - """ - Builds a Quanvolutional Neural Network [1]. - - References: - [1] M. Henderson, S. Shakya, S. Pradhan, and - T. Cook, “Quanvolutional Neural Networks: - Powering Image Recognition with Quantum Circuits,” - arXiv:1904.04767 [quant-ph], Apr. 2019, - Available: https://arxiv.org/abs/1904.04767 - """ - - def __init__(self, num_qubits: int): - """ - Initializes a Quanvolutional Neural Network circuit - with the given number of qubits. - - Args: - num_qubits (int): builds a quantum convolutional neural - network circuit with the given number of qubits or image - dimensions. - """ - QuantumNeuralNetwork.__init__(self, num_qubits) - - def sequence(self, operations: list[tuple[Callable, dict]]) -> QuantumCircuit: - """ - Builds a Quanvolutional NN circuit by composing the circuit - with given sequence of list of operations. - - Args: - operations (list[tuple[Callable, dict]]: a tuple - of a Layer object and a dictionary of its arguments. - - Returns: - circuit (QuantumCircuit): final QNN circuit with all the - layers. - """ \ No newline at end of file diff --git a/quantum_image_processing/models/neural_networks/__init__.py b/quantum_image_processing/neural_networks/__init__.py similarity index 100% rename from quantum_image_processing/models/neural_networks/__init__.py rename to quantum_image_processing/neural_networks/__init__.py diff --git a/quantum_image_processing/models/neural_networks/layers/__init__.py b/quantum_image_processing/neural_networks/layers/__init__.py similarity index 100% rename from quantum_image_processing/models/neural_networks/layers/__init__.py rename to quantum_image_processing/neural_networks/layers/__init__.py diff --git a/quantum_image_processing/models/neural_networks/layers/base_layer.py b/quantum_image_processing/neural_networks/layers/base_layer.py similarity index 100% rename from quantum_image_processing/models/neural_networks/layers/base_layer.py rename to quantum_image_processing/neural_networks/layers/base_layer.py diff --git a/quantum_image_processing/models/neural_networks/layers/convolutional_layer.py b/quantum_image_processing/neural_networks/layers/convolutional_layer.py similarity index 95% rename from quantum_image_processing/models/neural_networks/layers/convolutional_layer.py rename to quantum_image_processing/neural_networks/layers/convolutional_layer.py index bc1998a..ba5d0ea 100644 --- a/quantum_image_processing/models/neural_networks/layers/convolutional_layer.py +++ b/quantum_image_processing/neural_networks/layers/convolutional_layer.py @@ -2,8 +2,8 @@ from __future__ import annotations from typing import Optional from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers.base_layer import BaseLayer -from quantum_image_processing.models.tensor_network_circuits.mera import MERA +from quantum_image_processing.neural_networks.layers.base_layer import BaseLayer +from quantum_image_processing.tensor_network_circuits.mera import MERA class QuantumConvolutionalLayer(BaseLayer): diff --git a/quantum_image_processing/models/neural_networks/layers/fully_connected_layer.py b/quantum_image_processing/neural_networks/layers/fully_connected_layer.py similarity index 95% rename from quantum_image_processing/models/neural_networks/layers/fully_connected_layer.py rename to quantum_image_processing/neural_networks/layers/fully_connected_layer.py index 1eb97aa..de12bad 100644 --- a/quantum_image_processing/models/neural_networks/layers/fully_connected_layer.py +++ b/quantum_image_processing/neural_networks/layers/fully_connected_layer.py @@ -1,7 +1,7 @@ """Quantum Fully Connected Layer Structure""" from __future__ import annotations from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers.base_layer import BaseLayer +from quantum_image_processing.neural_networks.layers.base_layer import BaseLayer class FullyConnectedLayer(BaseLayer): diff --git a/quantum_image_processing/models/neural_networks/layers/pooling_layer.py b/quantum_image_processing/neural_networks/layers/pooling_layer.py similarity index 98% rename from quantum_image_processing/models/neural_networks/layers/pooling_layer.py rename to quantum_image_processing/neural_networks/layers/pooling_layer.py index dd2f8ef..15d1cd9 100644 --- a/quantum_image_processing/models/neural_networks/layers/pooling_layer.py +++ b/quantum_image_processing/neural_networks/layers/pooling_layer.py @@ -2,7 +2,7 @@ from __future__ import annotations import itertools from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers.base_layer import BaseLayer +from quantum_image_processing.neural_networks.layers.base_layer import BaseLayer # pylint: disable = too-few-public-methods diff --git a/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py new file mode 100644 index 0000000..5f4e687 --- /dev/null +++ b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py @@ -0,0 +1,39 @@ +"""Quanvolutional Layer structure""" +from __future__ import annotations +from qiskit.circuit import QuantumCircuit +from quantum_image_processing.neural_networks.layers.base_layer import BaseLayer + + +class QuanvolutionalLayer(BaseLayer): + """ + Builds a Quanvolutional Layer [1]. + References: + [1] M. Henderson, S. Shakya, S. Pradhan, and + T. Cook, “Quanvolutional Neural Networks: + Powering Image Recognition with Quantum Circuits,” + arXiv:1904.04767 [quant-ph], Apr. 2019, + Available: https://arxiv.org/abs/1904.04767 + """ + + def __init__(self, num_qubits: int, circuit: QuantumCircuit, unmeasured_bits: list): + """ + Initializes a Quanvolutional Layer circuit + with the given number of qubits. + Args: + num_qubits (int): builds a quantum convolutional neural + network circuit with the given number of qubits or image + dimensions. + circuit (QuantumCircuit): Takes quantum circuit with/without + an existing layer as an input, and applies a quanvolutional + layer over it. + """ + BaseLayer.__init__(self, num_qubits, circuit, unmeasured_bits) + + def build_layer(self) -> tuple[QuantumCircuit, list]: + """ + Builds the Quanvolutional layer circuit + Returns: + circuit (QuantumCircuit): circuit with a quanvolutional layer. + unmeasured_bits (dict): a dictionary of unmeasured qubits + and classical bits in the circuit. + """ \ No newline at end of file diff --git a/quantum_image_processing/models/neural_networks/qcnn.py b/quantum_image_processing/neural_networks/qcnn.py similarity index 96% rename from quantum_image_processing/models/neural_networks/qcnn.py rename to quantum_image_processing/neural_networks/qcnn.py index 41dbe4b..f1f670f 100644 --- a/quantum_image_processing/models/neural_networks/qcnn.py +++ b/quantum_image_processing/neural_networks/qcnn.py @@ -2,7 +2,7 @@ from __future__ import annotations from typing import Callable from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.quantum_neural_network import ( +from quantum_image_processing.neural_networks.quantum_neural_network import ( QuantumNeuralNetwork, ) diff --git a/quantum_image_processing/models/neural_networks/quantum_autoencoder.py b/quantum_image_processing/neural_networks/quantum_autoencoder.py similarity index 100% rename from quantum_image_processing/models/neural_networks/quantum_autoencoder.py rename to quantum_image_processing/neural_networks/quantum_autoencoder.py diff --git a/quantum_image_processing/models/neural_networks/quantum_neural_network.py b/quantum_image_processing/neural_networks/quantum_neural_network.py similarity index 100% rename from quantum_image_processing/models/neural_networks/quantum_neural_network.py rename to quantum_image_processing/neural_networks/quantum_neural_network.py diff --git a/quantum_image_processing/models/tensor_network_circuits/peps.py b/quantum_image_processing/neural_networks/quanvolutional.py similarity index 100% rename from quantum_image_processing/models/tensor_network_circuits/peps.py rename to quantum_image_processing/neural_networks/quanvolutional.py diff --git a/quantum_image_processing/models/tensor_network_circuits/__init__.py b/quantum_image_processing/tensor_network_circuits/__init__.py similarity index 100% rename from quantum_image_processing/models/tensor_network_circuits/__init__.py rename to quantum_image_processing/tensor_network_circuits/__init__.py diff --git a/quantum_image_processing/models/tensor_network_circuits/base_tensor_network.py b/quantum_image_processing/tensor_network_circuits/base_tensor_network.py similarity index 100% rename from quantum_image_processing/models/tensor_network_circuits/base_tensor_network.py rename to quantum_image_processing/tensor_network_circuits/base_tensor_network.py diff --git a/quantum_image_processing/models/tensor_network_circuits/mera.py b/quantum_image_processing/tensor_network_circuits/mera.py similarity index 98% rename from quantum_image_processing/models/tensor_network_circuits/mera.py rename to quantum_image_processing/tensor_network_circuits/mera.py index b2cf38a..15738d5 100644 --- a/quantum_image_processing/models/tensor_network_circuits/mera.py +++ b/quantum_image_processing/tensor_network_circuits/mera.py @@ -8,7 +8,7 @@ ParameterVector, ) from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary -from quantum_image_processing.models.tensor_network_circuits.base_tensor_network import ( +from quantum_image_processing.tensor_network_circuits.base_tensor_network import ( BaseTensorNetwork, ) diff --git a/quantum_image_processing/models/tensor_network_circuits/mps.py b/quantum_image_processing/tensor_network_circuits/mps.py similarity index 98% rename from quantum_image_processing/models/tensor_network_circuits/mps.py rename to quantum_image_processing/tensor_network_circuits/mps.py index 8e90111..e9bd30d 100644 --- a/quantum_image_processing/models/tensor_network_circuits/mps.py +++ b/quantum_image_processing/tensor_network_circuits/mps.py @@ -2,7 +2,7 @@ from __future__ import annotations from typing import Callable from qiskit.circuit import QuantumCircuit, ParameterVector -from quantum_image_processing.models.tensor_network_circuits.base_tensor_network import ( +from quantum_image_processing.tensor_network_circuits.base_tensor_network import ( BaseTensorNetwork, ) from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary diff --git a/quantum_image_processing/models/variational_classifiers/data_reuploading.py b/quantum_image_processing/tensor_network_circuits/peps.py similarity index 100% rename from quantum_image_processing/models/variational_classifiers/data_reuploading.py rename to quantum_image_processing/tensor_network_circuits/peps.py diff --git a/quantum_image_processing/models/tensor_network_circuits/ttn.py b/quantum_image_processing/tensor_network_circuits/ttn.py similarity index 98% rename from quantum_image_processing/models/tensor_network_circuits/ttn.py rename to quantum_image_processing/tensor_network_circuits/ttn.py index 5d8da58..798cda0 100644 --- a/quantum_image_processing/models/tensor_network_circuits/ttn.py +++ b/quantum_image_processing/tensor_network_circuits/ttn.py @@ -4,7 +4,7 @@ import numpy as np from qiskit.circuit import QuantumCircuit, ParameterVector from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary -from quantum_image_processing.models.tensor_network_circuits.base_tensor_network import ( +from quantum_image_processing.tensor_network_circuits.base_tensor_network import ( BaseTensorNetwork, ) diff --git a/quantum_image_processing/models/variational_classifiers/__init__.py b/quantum_image_processing/variational_classifiers/__init__.py similarity index 100% rename from quantum_image_processing/models/variational_classifiers/__init__.py rename to quantum_image_processing/variational_classifiers/__init__.py diff --git a/quantum_image_processing/models/variational_classifiers/kernel_estimator.py b/quantum_image_processing/variational_classifiers/data_reuploading.py similarity index 100% rename from quantum_image_processing/models/variational_classifiers/kernel_estimator.py rename to quantum_image_processing/variational_classifiers/data_reuploading.py diff --git a/quantum_image_processing/models/variational_classifiers/variational_classifier.py b/quantum_image_processing/variational_classifiers/kernel_estimator.py similarity index 100% rename from quantum_image_processing/models/variational_classifiers/variational_classifier.py rename to quantum_image_processing/variational_classifiers/kernel_estimator.py diff --git a/tests/models/__init__.py b/quantum_image_processing/variational_classifiers/variational_classifier.py similarity index 100% rename from tests/models/__init__.py rename to quantum_image_processing/variational_classifiers/variational_classifier.py diff --git a/tests/conftest.py b/tests/conftest.py index 3d726b8..173c056 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,2 @@ """conftest.py""" # pylint: disable=unused-import -from tests.data_encoder.image_representations.test_frqi import ( - circuit_pixel_position_fixture, -) -from tests.models.tensor_networks.test_mps import parameterization_mapper_fixture diff --git a/tests/models/neural_networks/test_quanvolutional_neural_network.py b/tests/models/neural_networks/test_quanvolutional_neural_network.py deleted file mode 100644 index 358e637..0000000 --- a/tests/models/neural_networks/test_quanvolutional_neural_network.py +++ /dev/null @@ -1,6 +0,0 @@ -"""Tests for Quanvolutional Neural Network class""" -from __future__ import annotations - - -class TestQuanvolutionalNeuralNetwork: - pass diff --git a/tests/models/neural_networks/__init__.py b/tests/neural_networks/__init__.py similarity index 100% rename from tests/models/neural_networks/__init__.py rename to tests/neural_networks/__init__.py diff --git a/tests/models/neural_networks/layers/__init__.py b/tests/neural_networks/layers/__init__.py similarity index 100% rename from tests/models/neural_networks/layers/__init__.py rename to tests/neural_networks/layers/__init__.py diff --git a/tests/models/neural_networks/layers/test_base_layer.py b/tests/neural_networks/layers/test_base_layer.py similarity index 98% rename from tests/models/neural_networks/layers/test_base_layer.py rename to tests/neural_networks/layers/test_base_layer.py index 3c5c2e7..0844ccf 100644 --- a/tests/models/neural_networks/layers/test_base_layer.py +++ b/tests/neural_networks/layers/test_base_layer.py @@ -3,7 +3,7 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers import ( +from quantum_image_processing.neural_networks.layers import ( QuantumConvolutionalLayer, ) diff --git a/tests/models/neural_networks/layers/test_convolutional_layer.py b/tests/neural_networks/layers/test_convolutional_layer.py similarity index 97% rename from tests/models/neural_networks/layers/test_convolutional_layer.py rename to tests/neural_networks/layers/test_convolutional_layer.py index 1ba8b75..72d01df 100644 --- a/tests/models/neural_networks/layers/test_convolutional_layer.py +++ b/tests/neural_networks/layers/test_convolutional_layer.py @@ -5,7 +5,7 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers import ( +from quantum_image_processing.neural_networks.layers import ( QuantumConvolutionalLayer, ) diff --git a/tests/models/neural_networks/layers/test_fully_connected_layer.py b/tests/neural_networks/layers/test_fully_connected_layer.py similarity index 95% rename from tests/models/neural_networks/layers/test_fully_connected_layer.py rename to tests/neural_networks/layers/test_fully_connected_layer.py index 7aa1236..977be5a 100644 --- a/tests/models/neural_networks/layers/test_fully_connected_layer.py +++ b/tests/neural_networks/layers/test_fully_connected_layer.py @@ -2,7 +2,7 @@ from __future__ import annotations import pytest from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers import FullyConnectedLayer +from quantum_image_processing.neural_networks.layers import FullyConnectedLayer @pytest.fixture diff --git a/tests/models/neural_networks/layers/test_pooling_layer.py b/tests/neural_networks/layers/test_pooling_layer.py similarity index 98% rename from tests/models/neural_networks/layers/test_pooling_layer.py rename to tests/neural_networks/layers/test_pooling_layer.py index b807da0..394e5c7 100644 --- a/tests/models/neural_networks/layers/test_pooling_layer.py +++ b/tests/neural_networks/layers/test_pooling_layer.py @@ -3,7 +3,7 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.layers import ( +from quantum_image_processing.neural_networks.layers import ( QuantumPoolingLayer2, QuantumPoolingLayer3, ) diff --git a/tests/neural_networks/layers/test_quanvolutional_layer.py b/tests/neural_networks/layers/test_quanvolutional_layer.py new file mode 100644 index 0000000..8c858f0 --- /dev/null +++ b/tests/neural_networks/layers/test_quanvolutional_layer.py @@ -0,0 +1,6 @@ +"""Tests for Quanvolutional Layer class""" +from __future__ import annotations + + +class TestQuanvolutionalLayer: + pass diff --git a/tests/models/neural_networks/test_qcnn.py b/tests/neural_networks/test_qcnn.py similarity index 96% rename from tests/models/neural_networks/test_qcnn.py rename to tests/neural_networks/test_qcnn.py index a065a1d..afe792d 100644 --- a/tests/models/neural_networks/test_qcnn.py +++ b/tests/neural_networks/test_qcnn.py @@ -5,8 +5,8 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.neural_networks.qcnn import QCNN -from quantum_image_processing.models.neural_networks.layers import ( +from quantum_image_processing.neural_networks import QCNN +from quantum_image_processing.neural_networks.layers import ( QuantumConvolutionalLayer, QuantumPoolingLayer2, QuantumPoolingLayer3, diff --git a/tests/models/tensor_networks/__init__.py b/tests/tensor_networks/__init__.py similarity index 100% rename from tests/models/tensor_networks/__init__.py rename to tests/tensor_networks/__init__.py diff --git a/tests/models/tensor_networks/test_mera.py b/tests/tensor_networks/test_mera.py similarity index 99% rename from tests/models/tensor_networks/test_mera.py rename to tests/tensor_networks/test_mera.py index b28a3a4..d81d3be 100644 --- a/tests/models/tensor_networks/test_mera.py +++ b/tests/tensor_networks/test_mera.py @@ -5,7 +5,7 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit, ParameterVector -from quantum_image_processing.models.tensor_network_circuits.mera import MERA +from quantum_image_processing.tensor_network_circuits import MERA from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary diff --git a/tests/models/tensor_networks/test_mps.py b/tests/tensor_networks/test_mps.py similarity index 98% rename from tests/models/tensor_networks/test_mps.py rename to tests/tensor_networks/test_mps.py index 15c4e53..5d805b9 100644 --- a/tests/models/tensor_networks/test_mps.py +++ b/tests/tensor_networks/test_mps.py @@ -4,7 +4,7 @@ import pytest from pytest import raises from qiskit.circuit import QuantumCircuit, ParameterVector -from quantum_image_processing.models.tensor_network_circuits.mps import MPS +from quantum_image_processing.tensor_network_circuits import MPS from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary diff --git a/tests/models/tensor_networks/test_ttn.py b/tests/tensor_networks/test_ttn.py similarity index 98% rename from tests/models/tensor_networks/test_ttn.py rename to tests/tensor_networks/test_ttn.py index 8ad5339..55c92ad 100644 --- a/tests/models/tensor_networks/test_ttn.py +++ b/tests/tensor_networks/test_ttn.py @@ -3,7 +3,7 @@ from unittest import mock import pytest from qiskit.circuit import QuantumCircuit -from quantum_image_processing.models.tensor_network_circuits.ttn import TTN +from quantum_image_processing.tensor_network_circuits.ttn import TTN from quantum_image_processing.gates.two_qubit_unitary import TwoQubitUnitary From e3243fbd9148bddf41c530c0443ab5abd0806dd9 Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Thu, 21 Dec 2023 02:45:34 -0800 Subject: [PATCH 06/14] change to quanvolutional layer instead of quanvolutional nn --- .../neural_networks/layers/quanvolutional_layer.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py index 5f4e687..911b9fd 100644 --- a/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py +++ b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py @@ -7,6 +7,7 @@ class QuanvolutionalLayer(BaseLayer): """ Builds a Quanvolutional Layer [1]. + References: [1] M. Henderson, S. Shakya, S. Pradhan, and T. Cook, “Quanvolutional Neural Networks: @@ -19,21 +20,27 @@ def __init__(self, num_qubits: int, circuit: QuantumCircuit, unmeasured_bits: li """ Initializes a Quanvolutional Layer circuit with the given number of qubits. + Args: num_qubits (int): builds a quantum convolutional neural network circuit with the given number of qubits or image dimensions. + circuit (QuantumCircuit): Takes quantum circuit with/without an existing layer as an input, and applies a quanvolutional layer over it. + + unmeasured_bits (dict): a dictionary of unmeasured qubits + and classical bits in the circuit. """ BaseLayer.__init__(self, num_qubits, circuit, unmeasured_bits) def build_layer(self) -> tuple[QuantumCircuit, list]: """ Builds the Quanvolutional layer circuit + Returns: circuit (QuantumCircuit): circuit with a quanvolutional layer. unmeasured_bits (dict): a dictionary of unmeasured qubits and classical bits in the circuit. - """ \ No newline at end of file + """ From cf5c5f906f44f9829510a11507d80dff63b2ad37 Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Wed, 20 Dec 2023 20:25:27 -0800 Subject: [PATCH 07/14] changes to conftest and mock.patch --- tests/conftest.py | 4 ++++ tests/neural_networks/layers/test_convolutional_layer.py | 2 +- tests/neural_networks/test_qcnn.py | 2 +- tests/tensor_networks/test_mera.py | 4 ++-- tests/tensor_networks/test_mps.py | 4 ++-- tests/tensor_networks/test_ttn.py | 4 ++-- 6 files changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 173c056..28f2c8e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,2 +1,6 @@ """conftest.py""" # pylint: disable=unused-import +from tests.data_encoder.image_representations.test_frqi import ( + circuit_pixel_position_fixture, +) +from tests.tensor_networks.test_mps import parameterization_mapper_fixture diff --git a/tests/neural_networks/layers/test_convolutional_layer.py b/tests/neural_networks/layers/test_convolutional_layer.py index 72d01df..ba9414f 100644 --- a/tests/neural_networks/layers/test_convolutional_layer.py +++ b/tests/neural_networks/layers/test_convolutional_layer.py @@ -59,7 +59,7 @@ def test_build_layer(self, num_qubits, circuit, unmeasured_bits, mera_args): ) mock_circuit = QuantumCircuit(qc_layer.num_qubits) with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.mera.MERA.mera_backbone", + "quantum_image_processing.tensor_network_circuits.mera.MERA.mera_backbone", return_value=mock_circuit, ) as mock_mera: _, _ = qc_layer.build_layer() diff --git a/tests/neural_networks/test_qcnn.py b/tests/neural_networks/test_qcnn.py index afe792d..05e63b2 100644 --- a/tests/neural_networks/test_qcnn.py +++ b/tests/neural_networks/test_qcnn.py @@ -103,7 +103,7 @@ def test_params(self, num_qubits, operations): def test_sequence(self, num_qubits, operations): """Tests the sequence method of QCNN class.""" with mock.patch.multiple( - "quantum_image_processing.models.neural_networks.layers", + "quantum_image_processing.neural_networks.layers", QuantumConvolutionalLayer=mock.DEFAULT, QuantumPoolingLayer2=mock.DEFAULT, QuantumPoolingLayer3=mock.DEFAULT, diff --git a/tests/tensor_networks/test_mera.py b/tests/tensor_networks/test_mera.py index d81d3be..7c2e0e1 100644 --- a/tests/tensor_networks/test_mera.py +++ b/tests/tensor_networks/test_mera.py @@ -175,7 +175,7 @@ def test_mera_simple(self, num_qubits, layer_depth, complex_structure): # pylint: disable=line-too-long """Tests the mera_backbone method call via the mera_simple function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.mera.MERA.mera_backbone" + "quantum_image_processing.tensor_network_circuits.mera.MERA.mera_backbone" ) as mock_mera_simple: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.simple_parameterization" @@ -193,7 +193,7 @@ def test_mera_general(self, num_qubits, layer_depth, complex_structure): # pylint: disable=line-too-long """Tests the mera_backbone method call via the mera_general function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.mera.MERA.mera_backbone" + "quantum_image_processing.tensor_network_circuits.mera.MERA.mera_backbone" ) as mock_mera_general: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.general_parameterization" diff --git a/tests/tensor_networks/test_mps.py b/tests/tensor_networks/test_mps.py index 5d805b9..269e534 100644 --- a/tests/tensor_networks/test_mps.py +++ b/tests/tensor_networks/test_mps.py @@ -107,7 +107,7 @@ def test_mps_simple(self, num_qubits, complex_structure): # pylint: disable=line-too-long """Tests the mps_backbone method call via the mps_simple function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.mps.MPS.mps_backbone" + "quantum_image_processing.tensor_network_circuits.mps.MPS.mps_backbone" ) as mock_mps_simple: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.simple_parameterization" @@ -122,7 +122,7 @@ def test_mps_general(self, num_qubits, complex_structure): # pylint: disable=line-too-long """Tests the mps_backbone method call via the mps_general function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.mps.MPS.mps_backbone" + "quantum_image_processing.tensor_network_circuits.mps.MPS.mps_backbone" ) as mock_mps_general: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.general_parameterization" diff --git a/tests/tensor_networks/test_ttn.py b/tests/tensor_networks/test_ttn.py index 55c92ad..d1f8152 100644 --- a/tests/tensor_networks/test_ttn.py +++ b/tests/tensor_networks/test_ttn.py @@ -88,7 +88,7 @@ def test_ttn_simple(self, num_qubits, complex_structure): # pylint: disable=line-too-long """Tests the ttn_backbone method call via the ttn_simple function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.ttn.TTN.ttn_backbone" + "quantum_image_processing.tensor_network_circuits.ttn.TTN.ttn_backbone" ) as mock_ttn_simple: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.simple_parameterization" @@ -103,7 +103,7 @@ def test_ttn_general(self, num_qubits, complex_structure): # pylint: disable=line-too-long """Tests the ttn_backbone method call via the ttn_general function.""" with mock.patch( - "quantum_image_processing.models.tensor_network_circuits.ttn.TTN.ttn_backbone" + "quantum_image_processing.tensor_network_circuits.ttn.TTN.ttn_backbone" ) as mock_ttn_general: with mock.patch( "quantum_image_processing.gates.two_qubit_unitary.TwoQubitUnitary.general_parameterization" From 2155e9363fc8675a02faee03b7b4bd4569d6c211 Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Fri, 22 Dec 2023 17:07:43 -0800 Subject: [PATCH 08/14] change location of error handling logic from qcnn to base class quantum_neural_networks --- .../neural_networks/layers/__init__.py | 2 ++ .../neural_networks/qcnn.py | 18 ------------------ .../neural_networks/quantum_neural_network.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/quantum_image_processing/neural_networks/layers/__init__.py b/quantum_image_processing/neural_networks/layers/__init__.py index a6c3cf0..e9af44e 100644 --- a/quantum_image_processing/neural_networks/layers/__init__.py +++ b/quantum_image_processing/neural_networks/layers/__init__.py @@ -4,6 +4,7 @@ from .convolutional_layer import QuantumConvolutionalLayer from .pooling_layer import QuantumPoolingLayer2, QuantumPoolingLayer3 from .fully_connected_layer import FullyConnectedLayer +from .quanvolutional_layer import QuanvolutionalLayer __all__ = [ "BaseLayer", @@ -11,4 +12,5 @@ "QuantumPoolingLayer2", "QuantumPoolingLayer3", "FullyConnectedLayer", + "QuanvolutionalLayer" ] diff --git a/quantum_image_processing/neural_networks/qcnn.py b/quantum_image_processing/neural_networks/qcnn.py index 9271aba..467381d 100644 --- a/quantum_image_processing/neural_networks/qcnn.py +++ b/quantum_image_processing/neural_networks/qcnn.py @@ -46,24 +46,6 @@ def sequence(self, operations: list[tuple[Callable, dict]]) -> QuantumCircuit: circuit (QuantumCircuit): final QNN circuit with all the layers. """ - if not isinstance(operations, list): - raise TypeError("The input operations must be of the type list.") - - if not all(isinstance(operation, tuple) for operation in operations): - raise TypeError( - "The input operations list must contain tuple[operation, params]." - ) - - if not callable(operations[0][0]): - raise TypeError( - "Operation in input operations list must be Callable functions/classes." - ) - - if not isinstance(operations[0][1], dict): - raise TypeError( - "Parameters of operation in input operations list must be in a dictionary." - ) - unmeasured_bits = list(range(self.num_qubits)) for layer, params in operations: layer_instance = layer( diff --git a/quantum_image_processing/neural_networks/quantum_neural_network.py b/quantum_image_processing/neural_networks/quantum_neural_network.py index b562d00..40a6d4f 100644 --- a/quantum_image_processing/neural_networks/quantum_neural_network.py +++ b/quantum_image_processing/neural_networks/quantum_neural_network.py @@ -47,3 +47,20 @@ def sequence(self, operations: list[tuple[Callable, dict]]): operations (list[tuple[Callable, dict]]: a tuple of a Layer object and a dictionary of its arguments. """ + if not isinstance(operations, list): + raise TypeError("The input operations must be of the type list.") + + if not all(isinstance(operation, tuple) for operation in operations): + raise TypeError( + "The input operations list must contain tuple[operation, params]." + ) + + if not callable(operations[0][0]): + raise TypeError( + "Operation in input operations list must be Callable functions/classes." + ) + + if not isinstance(operations[0][1], dict): + raise TypeError( + "Parameters of operation in input operations list must be in a dictionary." + ) From 6aee4c828cb13cfa7f30e61bb6ff5fc4554d4447 Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Fri, 22 Dec 2023 18:44:53 -0800 Subject: [PATCH 09/14] add quanv layer logic --- .../neural_networks/layers/__init__.py | 2 +- .../layers/quanvolutional_layer.py | 49 +++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/quantum_image_processing/neural_networks/layers/__init__.py b/quantum_image_processing/neural_networks/layers/__init__.py index e9af44e..66ac209 100644 --- a/quantum_image_processing/neural_networks/layers/__init__.py +++ b/quantum_image_processing/neural_networks/layers/__init__.py @@ -12,5 +12,5 @@ "QuantumPoolingLayer2", "QuantumPoolingLayer3", "FullyConnectedLayer", - "QuanvolutionalLayer" + "QuanvolutionalLayer", ] diff --git a/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py index dea0668..13d2851 100644 --- a/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py +++ b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py @@ -1,12 +1,16 @@ """Quanvolutional Layer structure""" from __future__ import annotations +import math +from typing import Optional from qiskit.circuit import QuantumCircuit +from qiskit.circuit.random import random_circuit from quantum_image_processing.neural_networks.layers.base_layer import BaseLayer class QuanvolutionalLayer(BaseLayer): """ - Builds a Quanvolutional Layer [1]. + Builds a Quanvolutional Layer [1] with a non-trainable + random quantum circuit. References: [1] M. Henderson, S. Shakya, S. Pradhan, and @@ -16,7 +20,15 @@ class QuanvolutionalLayer(BaseLayer): Available: https://arxiv.org/abs/1904.04767 """ - def __init__(self, num_qubits: int, circuit: QuantumCircuit, unmeasured_bits: list): + def __init__( + self, + num_qubits: int, + circuit: QuantumCircuit, + unmeasured_bits: list, + filter_size: tuple[int, int], + stride: Optional[int] = 1, + random: Optional[bool] = True, + ): """ Initializes a Quanvolutional Layer circuit with the given number of qubits. @@ -32,10 +44,31 @@ def __init__(self, num_qubits: int, circuit: QuantumCircuit, unmeasured_bits: li unmeasured_bits (dict): a dictionary of unmeasured qubits and classical bits in the circuit. - """ BaseLayer.__init__(self, num_qubits, circuit, unmeasured_bits) + if not isinstance(filter_size, tuple) or not all( + isinstance(size, int) for size in filter_size + ): + raise TypeError( + "The input filter_size must be of the type tuple[int, int]." + ) + + # Check max filter_size dimensions + self.filter_size = filter_size + + if not isinstance(stride, int): + raise TypeError("The input stride must be of the type int.") + + # Also need to check the max stride value + if stride <= 0: + raise ValueError("The input stride must be at least 1.") + self.stride = stride + + if not isinstance(random, bool): + raise TypeError("The input random must be of the type bool.") + self.random = random + def build_layer(self) -> tuple[QuantumCircuit, list]: """ Builds the Quanvolutional layer circuit @@ -46,3 +79,13 @@ def build_layer(self) -> tuple[QuantumCircuit, list]: unmeasured_bits (dict): a dictionary of unmeasured qubits and classical bits in the circuit. """ + sub_image_qubits = int(math.prod(self.filter_size)) + # Apply random circuit with measurements + if self.random: + sub_circuit = random_circuit( + num_qubits=sub_image_qubits, + depth=2, + max_operands=3, + measure=True, + ) + return sub_circuit, [] From e83ec129b5f94db468f5533422645f654558cdff Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Fri, 22 Dec 2023 18:45:20 -0800 Subject: [PATCH 10/14] call super.sequence to run error handling logic specified in base class --- quantum_image_processing/neural_networks/qcnn.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/quantum_image_processing/neural_networks/qcnn.py b/quantum_image_processing/neural_networks/qcnn.py index 467381d..e5480ff 100644 --- a/quantum_image_processing/neural_networks/qcnn.py +++ b/quantum_image_processing/neural_networks/qcnn.py @@ -46,6 +46,9 @@ def sequence(self, operations: list[tuple[Callable, dict]]) -> QuantumCircuit: circuit (QuantumCircuit): final QNN circuit with all the layers. """ + # Call sequence from ABC for error handling. + super().sequence(operations) + unmeasured_bits = list(range(self.num_qubits)) for layer, params in operations: layer_instance = layer( From 491cf4d9559794daf6c1a86cd42b77c30331effa Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Fri, 22 Dec 2023 18:45:43 -0800 Subject: [PATCH 11/14] add quanv neural network class --- .../quanvolutional_neural_network.py | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 quantum_image_processing/neural_networks/quanvolutional_neural_network.py diff --git a/quantum_image_processing/neural_networks/quanvolutional_neural_network.py b/quantum_image_processing/neural_networks/quanvolutional_neural_network.py new file mode 100644 index 0000000..d6a55a5 --- /dev/null +++ b/quantum_image_processing/neural_networks/quanvolutional_neural_network.py @@ -0,0 +1,116 @@ +"""Quanvolutional Neural Network structure""" +from __future__ import annotations +import math +from typing import Optional, Callable +from qiskit.circuit import QuantumCircuit +from quantum_image_processing.neural_networks.layers import QuanvolutionalLayer +from quantum_image_processing.neural_networks.quantum_neural_network import ( + QuantumNeuralNetwork, +) + + +class QuanvolutionalNeuralNetwork(QuantumNeuralNetwork): + """ + Builds a Quanvolutional Neural Network structure [1]. + + References: + [1] M. Henderson, S. Shakya, S. Pradhan, and + T. Cook, “Quanvolutional Neural Networks: + Powering Image Recognition with Quantum Circuits,” + arXiv:1904.04767 [quant-ph], Apr. 2019, + Available: https://arxiv.org/abs/1904.04767 + """ + + def __init__(self, num_qubits: int): + """ + Initializes a Quanvolutional Neural Network + circuit with the given number of qubits. + + Args: + num_qubits (int): builds a quantum convolutional neural + network circuit with the given number of qubits or image + dimensions. + """ + QuantumNeuralNetwork.__init__(self, num_qubits) + + def pre_quanvolutional_layer(self, params) -> list[QuantumCircuit]: + """ + Helps in setting up the circuits for quanvolutional layers. + """ + # Produce n sub_circuits. + + sub_circuits = [] + layer_instance = QuanvolutionalLayer( + num_qubits=, + circuit=, + unmeasured_bits=unmeasured_bits, + **params, + ) + # Optionally collect circuit since it is + # composed in place. + sub_circuit, _ = layer_instance.build_layer() + sub_circuits.append(sub_circuit) + print(sub_circuits) + + return sub_circuits + + def post_quanvolutional_layer(self): + pass + + def sequence(self, operations: list[tuple[Callable, dict]]): + """ + Builds a QuNN circuit by composing the circuit with given + sequence of list of operations, including a Quanvolutional Layer. + + This NN structure particularly differs from QCNN in terms of + handling measurement results after the application of a + Quanvolutional Layer and then re-embedding the data received + from measurements to perform further quantum processes. + + Args: + operations (list[tuple[Callable, dict]]: a tuple + of a Layer object and a dictionary of its arguments. + + Returns: + circuit (QuantumCircuit): final QNN circuit with all the + layers. + """ + super().sequence(operations) + + # Put a stop when QuanvLayer is followed by other quantum layers. + # This situation will require data processing before embedding. + unmeasured_bits = list(range(self.num_qubits)) + for layer, params in operations: + # print(layer) + if layer is QuanvolutionalLayer: + print("Yes!", layer, params) + + self.pre_quanvolutional_layer(params) + + else: + layer_instance = layer( + num_qubits=self.num_qubits, + circuit=self.circuit, + unmeasured_bits=unmeasured_bits, + **params, + ) + # Optionally collect circuit since it is + # composed in place. + _, unmeasured_bits = layer_instance.build_layer() + + + # Continue normally (similar to QCNN) when QuanvLayer is the + # only layer or the last layer in operations. + # unmeasured_bits = list(range(self.num_qubits)) + # for layer, params in operations: + # layer_instance = layer( + # num_qubits=self.num_qubits, + # circuit=self.circuit, + # unmeasured_bits=unmeasured_bits, + # **params, + # ) + # # Optionally collect circuit since it is + # # composed in place. + # _, unmeasured_bits = layer_instance.build_layer() + + return self.circuit From 3b76668cbfefc975b59216d2e27703f596a7e7d1 Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Fri, 22 Dec 2023 19:29:32 -0800 Subject: [PATCH 12/14] use img_dims as argument instead of num_qubits --- .../layers/quanvolutional_layer.py | 23 ++++++++++++------- .../quanvolutional_neural_network.py | 20 +++++++++++----- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py index 13d2851..6890f60 100644 --- a/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py +++ b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py @@ -22,9 +22,7 @@ class QuanvolutionalLayer(BaseLayer): def __init__( self, - num_qubits: int, - circuit: QuantumCircuit, - unmeasured_bits: list, + img_dims: tuple[int, int], filter_size: tuple[int, int], stride: Optional[int] = 1, random: Optional[bool] = True, @@ -45,7 +43,14 @@ def __init__( unmeasured_bits (dict): a dictionary of unmeasured qubits and classical bits in the circuit. """ - BaseLayer.__init__(self, num_qubits, circuit, unmeasured_bits) + if not all((isinstance(dims, int) for dims in img_dims)) or not isinstance( + img_dims, tuple + ): + raise TypeError("Input img_dims must be of the type tuple[int, ...].") + + self.img_dims = img_dims + num_qubits = int(math.prod(self.img_dims)) + BaseLayer.__init__(self, num_qubits) if not isinstance(filter_size, tuple) or not all( isinstance(size, int) for size in filter_size @@ -54,15 +59,17 @@ def __init__( "The input filter_size must be of the type tuple[int, int]." ) - # Check max filter_size dimensions + if filter_size[0] > min(self.img_dims): + raise ValueError("The filter_size must be less than or equal " + "to the minimum image dimension.") self.filter_size = filter_size if not isinstance(stride, int): raise TypeError("The input stride must be of the type int.") - # Also need to check the max stride value - if stride <= 0: - raise ValueError("The input stride must be at least 1.") + if stride <= 0 or stride > min(img_dims): + raise ValueError("The input stride must be at least 1 and less than " + "or equal to the minimum image dimension.") self.stride = stride if not isinstance(random, bool): diff --git a/quantum_image_processing/neural_networks/quanvolutional_neural_network.py b/quantum_image_processing/neural_networks/quanvolutional_neural_network.py index d6a55a5..3526479 100644 --- a/quantum_image_processing/neural_networks/quanvolutional_neural_network.py +++ b/quantum_image_processing/neural_networks/quanvolutional_neural_network.py @@ -21,7 +21,7 @@ class QuanvolutionalNeuralNetwork(QuantumNeuralNetwork): Available: https://arxiv.org/abs/1904.04767 """ - def __init__(self, num_qubits: int): + def __init__(self, img_dims: tuple[int, int]): """ Initializes a Quanvolutional Neural Network circuit with the given number of qubits. @@ -31,19 +31,26 @@ def __init__(self, num_qubits: int): network circuit with the given number of qubits or image dimensions. """ + if not all((isinstance(dims, int) for dims in img_dims)) or not isinstance( + img_dims, tuple + ): + raise TypeError("Input img_dims must be of the type tuple[int, ...].") + + self.img_dims = img_dims + num_qubits = int(math.prod(self.img_dims)) QuantumNeuralNetwork.__init__(self, num_qubits) - def pre_quanvolutional_layer(self, params) -> list[QuantumCircuit]: + def pre_quanvolutional_layer(self, params: dict) -> list[QuantumCircuit]: """ - Helps in setting up the circuits for quanvolutional layers. + Calls data pre-processing methods before embedding data for + quanvolutional layer circuits. """ + # Take origin and create sub-images. # Produce n sub_circuits. sub_circuits = [] layer_instance = QuanvolutionalLayer( - num_qubits=, - circuit=, - unmeasured_bits=unmeasured_bits, + img_dims=self.img_dims, **params, ) # Optionally collect circuit since it is @@ -51,6 +58,7 @@ def pre_quanvolutional_layer(self, params) -> list[QuantumCircuit]: sub_circuit, _ = layer_instance.build_layer() sub_circuits.append(sub_circuit) print(sub_circuits) + sub_circuit.draw("mpl") return sub_circuits From d21171572e67529168baaa126e173f93e817e07f Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Sun, 24 Dec 2023 22:01:02 -0800 Subject: [PATCH 13/14] design logic --- .../layers/quanvolutional_layer.py | 57 +++++++++++++------ .../quanvolutional_neural_network.py | 1 - 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py index 6890f60..1a2a45c 100644 --- a/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py +++ b/quantum_image_processing/neural_networks/layers/quanvolutional_layer.py @@ -5,6 +5,8 @@ from qiskit.circuit import QuantumCircuit from qiskit.circuit.random import random_circuit from quantum_image_processing.neural_networks.layers.base_layer import BaseLayer +from quantum_image_processing.data_encoder.image_representations.image_embedding import ImageEmbedding +from quantum_image_processing.data_processing.sub_images import produce_sub_images class QuanvolutionalLayer(BaseLayer): @@ -24,8 +26,10 @@ def __init__( self, img_dims: tuple[int, int], filter_size: tuple[int, int], - stride: Optional[int] = 1, + stride: Optional[int], + pixel_vals: list, random: Optional[bool] = True, + embedding: Optional[str] = "AngleEmbedding", ): """ Initializes a Quanvolutional Layer circuit @@ -49,6 +53,7 @@ def __init__( raise TypeError("Input img_dims must be of the type tuple[int, ...].") self.img_dims = img_dims + num_qubits = int(math.prod(self.img_dims)) BaseLayer.__init__(self, num_qubits) @@ -60,39 +65,59 @@ def __init__( ) if filter_size[0] > min(self.img_dims): - raise ValueError("The filter_size must be less than or equal " - "to the minimum image dimension.") + raise ValueError( + "The filter_size must be less than or equal " + "to the minimum image dimension." + ) self.filter_size = filter_size if not isinstance(stride, int): raise TypeError("The input stride must be of the type int.") if stride <= 0 or stride > min(img_dims): - raise ValueError("The input stride must be at least 1 and less than " - "or equal to the minimum image dimension.") + raise ValueError( + "The input stride must be at least 1 and less than " + "or equal to the minimum image dimension." + ) self.stride = stride if not isinstance(random, bool): raise TypeError("The input random must be of the type bool.") self.random = random - def build_layer(self) -> tuple[QuantumCircuit, list]: - """ - Builds the Quanvolutional layer circuit + self.pixel_vals = pixel_vals - Returns: - circuit (QuantumCircuit): circuit with a quanvolutional layer. - - unmeasured_bits (dict): a dictionary of unmeasured qubits - and classical bits in the circuit. + def build_sub_circuits(self, sub_image): """ - sub_image_qubits = int(math.prod(self.filter_size)) + Collects a sub-image and its dimensions to produce a + corresponding quanvolutional layer circuit. + """ + # Apply random circuit with measurements if self.random: sub_circuit = random_circuit( - num_qubits=sub_image_qubits, + num_qubits=1, depth=2, max_operands=3, measure=True, ) - return sub_circuit, [] + return sub_circuit + + def build_layer(self) -> tuple[list[QuantumCircuit, ...], list]: + """ + Builds the Quanvolutional layer circuit + + Returns: + circuit (QuantumCircuit): circuit with a quanvolutional layer. + + unmeasured_bits (dict): a dictionary of unmeasured qubits + and classical bits in the circuit. + """ + # Pre-process images to create list of sub-images + sub_images = produce_sub_images(self.filter_size, self.pixel_vals) + + # Embedd sub-images into sub-circuits + + # Build quanvolutional layer from sub-circuits (and measure). + + return circuit_list, [] diff --git a/quantum_image_processing/neural_networks/quanvolutional_neural_network.py b/quantum_image_processing/neural_networks/quanvolutional_neural_network.py index 3526479..e65f2bc 100644 --- a/quantum_image_processing/neural_networks/quanvolutional_neural_network.py +++ b/quantum_image_processing/neural_networks/quanvolutional_neural_network.py @@ -106,7 +106,6 @@ def sequence(self, operations: list[tuple[Callable, dict]]): # composed in place. _, unmeasured_bits = layer_instance.build_layer() - # Continue normally (similar to QCNN) when QuanvLayer is the # only layer or the last layer in operations. # unmeasured_bits = list(range(self.num_qubits)) From e74469b8db16632e2df1ce82ccf07d8f9c74457e Mon Sep 17 00:00:00 2001 From: SaashaJoshi Date: Tue, 28 May 2024 23:57:05 -0700 Subject: [PATCH 14/14] quanvolution is only a layer. deleting quanvolutional_neural_network class --- .../quanvolutional_neural_network.py | 123 ------------------ 1 file changed, 123 deletions(-) delete mode 100644 piqture/neural_networks/quanvolutional_neural_network.py diff --git a/piqture/neural_networks/quanvolutional_neural_network.py b/piqture/neural_networks/quanvolutional_neural_network.py deleted file mode 100644 index e65f2bc..0000000 --- a/piqture/neural_networks/quanvolutional_neural_network.py +++ /dev/null @@ -1,123 +0,0 @@ -"""Quanvolutional Neural Network structure""" -from __future__ import annotations -import math -from typing import Optional, Callable -from qiskit.circuit import QuantumCircuit -from quantum_image_processing.neural_networks.layers import QuanvolutionalLayer -from quantum_image_processing.neural_networks.quantum_neural_network import ( - QuantumNeuralNetwork, -) - - -class QuanvolutionalNeuralNetwork(QuantumNeuralNetwork): - """ - Builds a Quanvolutional Neural Network structure [1]. - - References: - [1] M. Henderson, S. Shakya, S. Pradhan, and - T. Cook, “Quanvolutional Neural Networks: - Powering Image Recognition with Quantum Circuits,” - arXiv:1904.04767 [quant-ph], Apr. 2019, - Available: https://arxiv.org/abs/1904.04767 - """ - - def __init__(self, img_dims: tuple[int, int]): - """ - Initializes a Quanvolutional Neural Network - circuit with the given number of qubits. - - Args: - num_qubits (int): builds a quantum convolutional neural - network circuit with the given number of qubits or image - dimensions. - """ - if not all((isinstance(dims, int) for dims in img_dims)) or not isinstance( - img_dims, tuple - ): - raise TypeError("Input img_dims must be of the type tuple[int, ...].") - - self.img_dims = img_dims - num_qubits = int(math.prod(self.img_dims)) - QuantumNeuralNetwork.__init__(self, num_qubits) - - def pre_quanvolutional_layer(self, params: dict) -> list[QuantumCircuit]: - """ - Calls data pre-processing methods before embedding data for - quanvolutional layer circuits. - """ - # Take origin and create sub-images. - # Produce n sub_circuits. - - sub_circuits = [] - layer_instance = QuanvolutionalLayer( - img_dims=self.img_dims, - **params, - ) - # Optionally collect circuit since it is - # composed in place. - sub_circuit, _ = layer_instance.build_layer() - sub_circuits.append(sub_circuit) - print(sub_circuits) - sub_circuit.draw("mpl") - - return sub_circuits - - def post_quanvolutional_layer(self): - pass - - def sequence(self, operations: list[tuple[Callable, dict]]): - """ - Builds a QuNN circuit by composing the circuit with given - sequence of list of operations, including a Quanvolutional Layer. - - This NN structure particularly differs from QCNN in terms of - handling measurement results after the application of a - Quanvolutional Layer and then re-embedding the data received - from measurements to perform further quantum processes. - - Args: - operations (list[tuple[Callable, dict]]: a tuple - of a Layer object and a dictionary of its arguments. - - Returns: - circuit (QuantumCircuit): final QNN circuit with all the - layers. - """ - super().sequence(operations) - - # Put a stop when QuanvLayer is followed by other quantum layers. - # This situation will require data processing before embedding. - unmeasured_bits = list(range(self.num_qubits)) - for layer, params in operations: - # print(layer) - if layer is QuanvolutionalLayer: - print("Yes!", layer, params) - - self.pre_quanvolutional_layer(params) - - else: - layer_instance = layer( - num_qubits=self.num_qubits, - circuit=self.circuit, - unmeasured_bits=unmeasured_bits, - **params, - ) - # Optionally collect circuit since it is - # composed in place. - _, unmeasured_bits = layer_instance.build_layer() - - # Continue normally (similar to QCNN) when QuanvLayer is the - # only layer or the last layer in operations. - # unmeasured_bits = list(range(self.num_qubits)) - # for layer, params in operations: - # layer_instance = layer( - # num_qubits=self.num_qubits, - # circuit=self.circuit, - # unmeasured_bits=unmeasured_bits, - # **params, - # ) - # # Optionally collect circuit since it is - # # composed in place. - # _, unmeasured_bits = layer_instance.build_layer() - - return self.circuit