Skip to content

Commit

Permalink
add fully connected layer structure and neural network abc
Browse files Browse the repository at this point in the history
  • Loading branch information
SaashaJoshi committed Nov 6, 2023
1 parent ada03f7 commit 88e8bf4
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations
import numpy as np
from qiskit.circuit import QuantumCircuit, ClassicalRegister
from quantum_image_processing.models.neural_networks.neural_network import NeuralNetwork
from quantum_image_processing.models.tensor_network_circuits.mera import MERA


Expand All @@ -24,9 +25,9 @@ class QuantumConvolutionalLayer(MERA):
doi: https://doi.org/10.1038/s41567-019-0648-8.
"""

def __init__(self, circuit, *args, **kwargs):
def __init__(self, circuit: QuantumCircuit, unmeasured_qubit_list: list = None, *args, **kwargs):
"""
Initializes the convolutional layer to a MERA
Initializes a convolutional layer to a MERA
tensor network structure.
Args:
Expand All @@ -39,10 +40,14 @@ def __init__(self, circuit, *args, **kwargs):
"""
MERA.__init__(self, *args, **kwargs)
self.circuit = circuit
if unmeasured_qubit_list is None:
self.unmeasured_qubit_list = self.circuit.qubits
else:
self.unmeasured_qubit_list = unmeasured_qubit_list

def convolutional_layer(
self, mera_instance: int, complex_structure: bool
) -> QuantumCircuit:
) -> tuple[QuantumCircuit, list]:
"""
Implements the MERA tensor network with a restriction
on the depth of a convolutional layer, specified by a
Expand All @@ -64,9 +69,10 @@ def convolutional_layer(
if mera_instance in instance_mapping:
method = instance_mapping[mera_instance]
if callable(method):
self.circuit.append(method(complex_structure))
# self.circuit.compose(method(complex_structure), inplace=True)
self.circuit.append(method(complex_structure), qargs=self.unmeasured_qubit_list)

return self.circuit
return self.circuit.decompose(), list(self.circuit.qubits)


# pylint: disable=too-few-public-methods
Expand All @@ -82,19 +88,20 @@ class QuantumPoolingLayer:
doi: https://doi.org/10.1038/s41567-019-0648-8.
"""

def __init__(self, circuit: QuantumCircuit):
def __init__(self, circuit: QuantumCircuit, unmeasured_qubit_list: list):
"""
Initializes the pooling layer with the preceding
convolutional layer.
Initializes a pooling layer with the preceding
convolutional or pooling layer.
Args:
circuit (QuantumCircuit): Takes quantum circuit with an
existing convolutional or pooling layer as an input,
and applies an/additional pooling layer over it.
"""
self.circuit = circuit
self.unmeasured_qubit_list = unmeasured_qubit_list

def pooling_layer(self) -> QuantumCircuit:
def pooling_layer(self) -> tuple[QuantumCircuit, list]:
"""
Implements a pooling layer with alternating phase flips on
qubits when their adjacent qubits result in X = -1, when
Expand All @@ -104,26 +111,79 @@ def pooling_layer(self) -> QuantumCircuit:
self.circuit.add_register(classical_reg)

self.circuit.barrier()
unmeasured_qubit_list = []
for qubit in range(len(self.circuit.qubits)):
if qubit % 2 == 0 and qubit != len(self.circuit.qubits) - 1:
# Measurement in X-basis.
self.circuit.h(qubit + 1)
self.circuit.measure(qubit + 1, classical_reg[int(qubit / 2)])
unmeasured_qubit_list.append(self.circuit.qubits[qubit])
# Dynamic circuit
with self.circuit.if_test((classical_reg[int(qubit / 2)], 1)):
self.circuit.z(qubit)

return self.circuit, unmeasured_qubit_list


class FullyConnectedLayer:
def __init__(self, circuit: QuantumCircuit, unmeasured_qubit_list: list):
"""
Initializes a fully connected layer with the preceding
convolutional or polling layers.
Args:
circuit (QuantumCircuit): Takes quantum circuit with an
existing convolutional or pooling layer as an input,
and applies a fully connected layer to it.
unmeasured_qubit_list (list): Takes into consideration
the unmeasured qubits in the preceding circuit. Only these
qubits are used to create the FC layer.
"""
self.circuit = circuit
self.unmeasured_qubit_list = unmeasured_qubit_list

def fully_connected_layer(self) -> QuantumCircuit:
"""
Implements a fully connected layer with controlled phase
gates on adjacent qubits followed by a measurement in X-basis.
"""
self.circuit.barrier()
for index in range(len(self.unmeasured_qubit_list) - 1):
self.circuit.cz(
self.unmeasured_qubit_list[index], self.unmeasured_qubit_list[index + 1]
)
self.final_measurement()
return self.circuit

def final_measurement(self) -> QuantumCircuit:
"""
Implements a measurement in X-basis on the remaining qubits.
"""
cr = ClassicalRegister(len(self.unmeasured_qubit_list))
self.circuit.add_register(cr)
self.circuit.barrier()
# Measurement in X-basis
self.circuit.h(self.unmeasured_qubit_list)
self.circuit.measure(self.unmeasured_qubit_list, cr)
return self.circuit


class QCNN(NeuralNetwork):
def __init__(self, circuit: QuantumCircuit):
self.circuit = circuit

def qcnn_structure(self):
pass

def compose(self, circuit: QuantumCircuit):
self.circuit.compose(circuit, inplace=True)

# class FullyConnectedLayer:
# def __init__(self):
# pass
def forward_pass(self):
pass

def backward_pass(self):
pass

# class QCNN(MERA):
# def __init__(self, img_dim):
# super(MERA, self).__init__(img_dim)
#
# def qcnn_backbone(self):
# pass
def qcnn_backbone(self):
pass
28 changes: 28 additions & 0 deletions quantum_image_processing/models/neural_networks/neural_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Neural Network Abstract Base Class"""

from __future__ import annotations
from abc import ABC, abstractmethod


class NeuralNetwork(ABC):
"""
Abstract base class for all neural network structures.
These structures consist of data encoding/embedding,
forward pass (consisting of layers such as convolutional,
pooling, etc.), backward pass (for training purposes),
and finally a measurement stage.
"""

@abstractmethod
def forward_pass(self):
"""
Implements a forward pass in a neural network.
"""
return NotImplementedError

@abstractmethod
def backward_pass(self):
"""
Implements a backward pass in a neural network.
"""
return NotImplementedError

0 comments on commit 88e8bf4

Please sign in to comment.