hqm.classification.hmlp
1import sys 2sys.path += ['.', './layers/'] 3 4from hqm.layers.basiclayer import BasicLayer 5import torch 6 7class BasicHybridMLPClassifier(torch.nn.Module): 8 ''' 9 This class implements a basic hybrid multilayer perceptron for classification purposes. 10 BasicHybridMLPClassifier is composed of quantum layers stacked between two fully connected layers. 11 The size of fully connected layers is set by means of in_dim and ou_dim. 12 ''' 13 14 def __init__(self, qlayer : BasicLayer, in_dim : int, ou_dim : int) -> None: 15 ''' 16 BasicHybridMLPClassifier constructor. 17 18 Parameters: 19 ----------- 20 - qlayer : hqm.layers.basiclayers.BasicLayer 21 hqm quantum layer to be stacked between two fully connected layers 22 - in_dim : int 23 integer representing the input size for the first fully connected layer 24 - ou_dim : int 25 integer representing the output size of the hybrid model 26 27 Returns: 28 -------- 29 Nothing, a BasicHybridMLPClassifier object will be created. 30 ''' 31 super().__init__() 32 33 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 34 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 35 36 n_qubits = qlayer.n_qubits 37 self.fc_1 = torch.nn.Linear(in_dim, n_qubits) 38 self.qc_1 = qlayer.qlayer 39 self.fc_2 = torch.nn.Linear(n_qubits, ou_dim) 40 self.tanh = torch.nn.Tanh() 41 self.softmax = torch.nn.Softmax(dim=1) 42 43 def forward(self, x : torch.Tensor) -> torch.Tensor: 44 ''' 45 Torch forward method 46 47 Parameters: 48 ----------- 49 - x : torch.Tensor 50 input for the torch model 51 52 Returns: 53 -------- 54 - out : torch.Tensor 55 output from the torch model 56 ''' 57 x = self.fc_1(x) 58 x = self.tanh(x) 59 x = self.qc_1(x) 60 x = self.tanh(x) 61 x = self.fc_2(x) 62 out = self.softmax(x) 63 return out 64 65class MultiHybridMLPClassifier(torch.nn.Module): 66 ''' 67 This class implements a hybrid multilayer perceptron with multiple quantum circuits for classification purposes. 68 MultiHybridMLPClassifier is composed of several quantum layers stacked between two fully connected layers. 69 The size of fully connected layers is set by means of in_dim and ou_dim. 70 ''' 71 72 def __init__(self, qlayers : list, in_dim : int, ou_dim : int) -> None: 73 ''' 74 MultiHybridMLPClassifier constructor. 75 76 Parameters: 77 ----------- 78 - qlayers : list 79 list of hqm quantum layers to be stacked between two fully connected layers 80 - in_dim : int 81 integer representing the input size for the first fully connected layer 82 - ou_dim : int 83 integer representing the output size of the hybrid model 84 85 Returns: 86 -------- 87 Nothing, a MultiHybridMLPClassifier object will be created. 88 ''' 89 90 super().__init__() 91 92 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 93 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 94 if len(qlayers) < 1: raise Exception(f"Size of qlayers must be greater than 1, found {len(qlayers)}") 95 96 n_qubits_0 = qlayers[0].n_qubits 97 n_qubits_1 = qlayers[-1].n_qubits 98 self.fc_1 = torch.nn.Linear(in_dim, n_qubits_0) 99 self.qcs = [circ.qlayer for circ in qlayers] 100 self.fc_2 = torch.nn.Linear(n_qubits_1, ou_dim) 101 self.tanh = torch.nn.Tanh() 102 self.softmax = torch.nn.Softmax(dim=1) 103 104 def forward(self, x : torch.Tensor) -> torch.Tensor: 105 ''' 106 Torch forward method 107 108 Parameters: 109 ----------- 110 - x : torch.Tensor 111 input for the torch model 112 113 Returns: 114 -------- 115 - out : torch.Tensor 116 output from the torch model 117 ''' 118 119 x = self.fc_1(x) 120 x = self.tanh(x) 121 for qc in self.qcs: 122 x = qc(x) 123 x = self.tanh(x) 124 x = self.fc_2(x) 125 out = self.softmax(x) 126 return out 127 128class MultiHybridMultiMLPClassifier(torch.nn.Module): 129 ''' 130 This class implements a hybrid multilayer perceptron with multiple quantum circuits for classification purposes. 131 MultiHybridMultiMLPClassifier is composed of several quantum layers stacked between alternating fully connected layers. 132 The size of fully connected layers is set by means of in_dim and ou_dim. 133 ''' 134 135 def __init__(self, qlayers : list, in_dims : list, ou_dim : list) -> None: 136 ''' 137 MultiHybridMultiMLPClassifier constructor. 138 139 Parameters: 140 ----------- 141 - qlayers : list 142 list of hqm quantum layers to be stacked between two fully connected layers 143 - in_dims: list 144 list of integers representing the input size for the i-th fully connected layer (first value should correspond to size of input data) 145 - ou_dim : list 146 list of integers representing the output size for the i-th fully connected layer (last value should correspond to desired output size) 147 148 Returns: 149 -------- 150 Nothing, a MultiHybridMultiMLPClassifier object will be created. 151 ''' 152 153 super().__init__() 154 155 if len(in_dims) < 1: raise Exception(f"Size in_dims must be greater than 1, found {len(in_dims)}") 156 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 157 if len(qlayers) < 1: raise Exception(f"Size of qlayerss must be greater than 1, found {len(qlayers)}") 158 if len(qlayers) != len(in_dims): raise Exception(f"qlayers and in_dims must have the same lenght, found {len(qlayers)} and {len(in_dims)}") 159 for i, dim in enumerate(in_dims): 160 if dim < 1: raise Exception(f"Element {i} of in_dims must be greater than 1, found {dim}") 161 else: pass 162 163 self.fcs = [torch.nn.Linear(dim, circ.n_qubits) for (dim, circ) in zip(in_dims, qlayers)] 164 self.fco = torch.nn.Linear(qlayers[-1].n_qubits, ou_dim) 165 self.qcs = [circ.qlayer for circ in qlayers] 166 self.tanh = torch.nn.Tanh() 167 self.softmax = torch.nn.Softmax(dim=1) 168 169 def forward(self, x : torch.Tensor) -> torch.Tensor: 170 ''' 171 Torch forward method 172 173 Parameters: 174 ----------- 175 - x : torch.Tensor 176 input for the torch model 177 178 Returns: 179 -------- 180 - x : torch.Tensor 181 output from the torch model 182 ''' 183 184 for fc, qc in zip(self.fcs, self.qcs): 185 x = fc(x) 186 x = self.tanh(x) 187 x = qc(x) 188 x = self.tanh(x) 189 190 x = self.fco(x) 191 out = self.softmax(x) 192 return out
8class BasicHybridMLPClassifier(torch.nn.Module): 9 ''' 10 This class implements a basic hybrid multilayer perceptron for classification purposes. 11 BasicHybridMLPClassifier is composed of quantum layers stacked between two fully connected layers. 12 The size of fully connected layers is set by means of in_dim and ou_dim. 13 ''' 14 15 def __init__(self, qlayer : BasicLayer, in_dim : int, ou_dim : int) -> None: 16 ''' 17 BasicHybridMLPClassifier constructor. 18 19 Parameters: 20 ----------- 21 - qlayer : hqm.layers.basiclayers.BasicLayer 22 hqm quantum layer to be stacked between two fully connected layers 23 - in_dim : int 24 integer representing the input size for the first fully connected layer 25 - ou_dim : int 26 integer representing the output size of the hybrid model 27 28 Returns: 29 -------- 30 Nothing, a BasicHybridMLPClassifier object will be created. 31 ''' 32 super().__init__() 33 34 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 35 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 36 37 n_qubits = qlayer.n_qubits 38 self.fc_1 = torch.nn.Linear(in_dim, n_qubits) 39 self.qc_1 = qlayer.qlayer 40 self.fc_2 = torch.nn.Linear(n_qubits, ou_dim) 41 self.tanh = torch.nn.Tanh() 42 self.softmax = torch.nn.Softmax(dim=1) 43 44 def forward(self, x : torch.Tensor) -> torch.Tensor: 45 ''' 46 Torch forward method 47 48 Parameters: 49 ----------- 50 - x : torch.Tensor 51 input for the torch model 52 53 Returns: 54 -------- 55 - out : torch.Tensor 56 output from the torch model 57 ''' 58 x = self.fc_1(x) 59 x = self.tanh(x) 60 x = self.qc_1(x) 61 x = self.tanh(x) 62 x = self.fc_2(x) 63 out = self.softmax(x) 64 return out
This class implements a basic hybrid multilayer perceptron for classification purposes. BasicHybridMLPClassifier is composed of quantum layers stacked between two fully connected layers. The size of fully connected layers is set by means of in_dim and ou_dim.
15 def __init__(self, qlayer : BasicLayer, in_dim : int, ou_dim : int) -> None: 16 ''' 17 BasicHybridMLPClassifier constructor. 18 19 Parameters: 20 ----------- 21 - qlayer : hqm.layers.basiclayers.BasicLayer 22 hqm quantum layer to be stacked between two fully connected layers 23 - in_dim : int 24 integer representing the input size for the first fully connected layer 25 - ou_dim : int 26 integer representing the output size of the hybrid model 27 28 Returns: 29 -------- 30 Nothing, a BasicHybridMLPClassifier object will be created. 31 ''' 32 super().__init__() 33 34 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 35 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 36 37 n_qubits = qlayer.n_qubits 38 self.fc_1 = torch.nn.Linear(in_dim, n_qubits) 39 self.qc_1 = qlayer.qlayer 40 self.fc_2 = torch.nn.Linear(n_qubits, ou_dim) 41 self.tanh = torch.nn.Tanh() 42 self.softmax = torch.nn.Softmax(dim=1)
BasicHybridMLPClassifier constructor.
Parameters:
- qlayer : hqm.layers.basiclayers.BasicLayer
hqm quantum layer to be stacked between two fully connected layers - in_dim : int
integer representing the input size for the first fully connected layer - ou_dim : int
integer representing the output size of the hybrid model
Returns:
Nothing, a BasicHybridMLPClassifier object will be created.
44 def forward(self, x : torch.Tensor) -> torch.Tensor: 45 ''' 46 Torch forward method 47 48 Parameters: 49 ----------- 50 - x : torch.Tensor 51 input for the torch model 52 53 Returns: 54 -------- 55 - out : torch.Tensor 56 output from the torch model 57 ''' 58 x = self.fc_1(x) 59 x = self.tanh(x) 60 x = self.qc_1(x) 61 x = self.tanh(x) 62 x = self.fc_2(x) 63 out = self.softmax(x) 64 return out
Torch forward method
Parameters:
- x : torch.Tensor
input for the torch model
Returns:
- out : torch.Tensor
output from the torch model
Inherited Members
- torch.nn.modules.module.Module
- register_buffer
- register_parameter
- add_module
- register_module
- get_submodule
- get_parameter
- get_buffer
- get_extra_state
- set_extra_state
- apply
- cuda
- ipu
- xpu
- cpu
- type
- float
- double
- half
- bfloat16
- to_empty
- to
- register_full_backward_pre_hook
- register_backward_hook
- register_full_backward_hook
- register_forward_pre_hook
- register_forward_hook
- register_state_dict_pre_hook
- state_dict
- register_load_state_dict_post_hook
- load_state_dict
- parameters
- named_parameters
- buffers
- named_buffers
- children
- named_children
- modules
- named_modules
- train
- eval
- requires_grad_
- zero_grad
- extra_repr
- compile
66class MultiHybridMLPClassifier(torch.nn.Module): 67 ''' 68 This class implements a hybrid multilayer perceptron with multiple quantum circuits for classification purposes. 69 MultiHybridMLPClassifier is composed of several quantum layers stacked between two fully connected layers. 70 The size of fully connected layers is set by means of in_dim and ou_dim. 71 ''' 72 73 def __init__(self, qlayers : list, in_dim : int, ou_dim : int) -> None: 74 ''' 75 MultiHybridMLPClassifier constructor. 76 77 Parameters: 78 ----------- 79 - qlayers : list 80 list of hqm quantum layers to be stacked between two fully connected layers 81 - in_dim : int 82 integer representing the input size for the first fully connected layer 83 - ou_dim : int 84 integer representing the output size of the hybrid model 85 86 Returns: 87 -------- 88 Nothing, a MultiHybridMLPClassifier object will be created. 89 ''' 90 91 super().__init__() 92 93 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 94 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 95 if len(qlayers) < 1: raise Exception(f"Size of qlayers must be greater than 1, found {len(qlayers)}") 96 97 n_qubits_0 = qlayers[0].n_qubits 98 n_qubits_1 = qlayers[-1].n_qubits 99 self.fc_1 = torch.nn.Linear(in_dim, n_qubits_0) 100 self.qcs = [circ.qlayer for circ in qlayers] 101 self.fc_2 = torch.nn.Linear(n_qubits_1, ou_dim) 102 self.tanh = torch.nn.Tanh() 103 self.softmax = torch.nn.Softmax(dim=1) 104 105 def forward(self, x : torch.Tensor) -> torch.Tensor: 106 ''' 107 Torch forward method 108 109 Parameters: 110 ----------- 111 - x : torch.Tensor 112 input for the torch model 113 114 Returns: 115 -------- 116 - out : torch.Tensor 117 output from the torch model 118 ''' 119 120 x = self.fc_1(x) 121 x = self.tanh(x) 122 for qc in self.qcs: 123 x = qc(x) 124 x = self.tanh(x) 125 x = self.fc_2(x) 126 out = self.softmax(x) 127 return out
This class implements a hybrid multilayer perceptron with multiple quantum circuits for classification purposes. MultiHybridMLPClassifier is composed of several quantum layers stacked between two fully connected layers. The size of fully connected layers is set by means of in_dim and ou_dim.
73 def __init__(self, qlayers : list, in_dim : int, ou_dim : int) -> None: 74 ''' 75 MultiHybridMLPClassifier constructor. 76 77 Parameters: 78 ----------- 79 - qlayers : list 80 list of hqm quantum layers to be stacked between two fully connected layers 81 - in_dim : int 82 integer representing the input size for the first fully connected layer 83 - ou_dim : int 84 integer representing the output size of the hybrid model 85 86 Returns: 87 -------- 88 Nothing, a MultiHybridMLPClassifier object will be created. 89 ''' 90 91 super().__init__() 92 93 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 94 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 95 if len(qlayers) < 1: raise Exception(f"Size of qlayers must be greater than 1, found {len(qlayers)}") 96 97 n_qubits_0 = qlayers[0].n_qubits 98 n_qubits_1 = qlayers[-1].n_qubits 99 self.fc_1 = torch.nn.Linear(in_dim, n_qubits_0) 100 self.qcs = [circ.qlayer for circ in qlayers] 101 self.fc_2 = torch.nn.Linear(n_qubits_1, ou_dim) 102 self.tanh = torch.nn.Tanh() 103 self.softmax = torch.nn.Softmax(dim=1)
MultiHybridMLPClassifier constructor.
Parameters:
- qlayers : list
list of hqm quantum layers to be stacked between two fully connected layers - in_dim : int
integer representing the input size for the first fully connected layer - ou_dim : int
integer representing the output size of the hybrid model
Returns:
Nothing, a MultiHybridMLPClassifier object will be created.
105 def forward(self, x : torch.Tensor) -> torch.Tensor: 106 ''' 107 Torch forward method 108 109 Parameters: 110 ----------- 111 - x : torch.Tensor 112 input for the torch model 113 114 Returns: 115 -------- 116 - out : torch.Tensor 117 output from the torch model 118 ''' 119 120 x = self.fc_1(x) 121 x = self.tanh(x) 122 for qc in self.qcs: 123 x = qc(x) 124 x = self.tanh(x) 125 x = self.fc_2(x) 126 out = self.softmax(x) 127 return out
Torch forward method
Parameters:
- x : torch.Tensor
input for the torch model
Returns:
- out : torch.Tensor
output from the torch model
Inherited Members
- torch.nn.modules.module.Module
- register_buffer
- register_parameter
- add_module
- register_module
- get_submodule
- get_parameter
- get_buffer
- get_extra_state
- set_extra_state
- apply
- cuda
- ipu
- xpu
- cpu
- type
- float
- double
- half
- bfloat16
- to_empty
- to
- register_full_backward_pre_hook
- register_backward_hook
- register_full_backward_hook
- register_forward_pre_hook
- register_forward_hook
- register_state_dict_pre_hook
- state_dict
- register_load_state_dict_post_hook
- load_state_dict
- parameters
- named_parameters
- buffers
- named_buffers
- children
- named_children
- modules
- named_modules
- train
- eval
- requires_grad_
- zero_grad
- extra_repr
- compile
129class MultiHybridMultiMLPClassifier(torch.nn.Module): 130 ''' 131 This class implements a hybrid multilayer perceptron with multiple quantum circuits for classification purposes. 132 MultiHybridMultiMLPClassifier is composed of several quantum layers stacked between alternating fully connected layers. 133 The size of fully connected layers is set by means of in_dim and ou_dim. 134 ''' 135 136 def __init__(self, qlayers : list, in_dims : list, ou_dim : list) -> None: 137 ''' 138 MultiHybridMultiMLPClassifier constructor. 139 140 Parameters: 141 ----------- 142 - qlayers : list 143 list of hqm quantum layers to be stacked between two fully connected layers 144 - in_dims: list 145 list of integers representing the input size for the i-th fully connected layer (first value should correspond to size of input data) 146 - ou_dim : list 147 list of integers representing the output size for the i-th fully connected layer (last value should correspond to desired output size) 148 149 Returns: 150 -------- 151 Nothing, a MultiHybridMultiMLPClassifier object will be created. 152 ''' 153 154 super().__init__() 155 156 if len(in_dims) < 1: raise Exception(f"Size in_dims must be greater than 1, found {len(in_dims)}") 157 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 158 if len(qlayers) < 1: raise Exception(f"Size of qlayerss must be greater than 1, found {len(qlayers)}") 159 if len(qlayers) != len(in_dims): raise Exception(f"qlayers and in_dims must have the same lenght, found {len(qlayers)} and {len(in_dims)}") 160 for i, dim in enumerate(in_dims): 161 if dim < 1: raise Exception(f"Element {i} of in_dims must be greater than 1, found {dim}") 162 else: pass 163 164 self.fcs = [torch.nn.Linear(dim, circ.n_qubits) for (dim, circ) in zip(in_dims, qlayers)] 165 self.fco = torch.nn.Linear(qlayers[-1].n_qubits, ou_dim) 166 self.qcs = [circ.qlayer for circ in qlayers] 167 self.tanh = torch.nn.Tanh() 168 self.softmax = torch.nn.Softmax(dim=1) 169 170 def forward(self, x : torch.Tensor) -> torch.Tensor: 171 ''' 172 Torch forward method 173 174 Parameters: 175 ----------- 176 - x : torch.Tensor 177 input for the torch model 178 179 Returns: 180 -------- 181 - x : torch.Tensor 182 output from the torch model 183 ''' 184 185 for fc, qc in zip(self.fcs, self.qcs): 186 x = fc(x) 187 x = self.tanh(x) 188 x = qc(x) 189 x = self.tanh(x) 190 191 x = self.fco(x) 192 out = self.softmax(x) 193 return out
This class implements a hybrid multilayer perceptron with multiple quantum circuits for classification purposes. MultiHybridMultiMLPClassifier is composed of several quantum layers stacked between alternating fully connected layers. The size of fully connected layers is set by means of in_dim and ou_dim.
136 def __init__(self, qlayers : list, in_dims : list, ou_dim : list) -> None: 137 ''' 138 MultiHybridMultiMLPClassifier constructor. 139 140 Parameters: 141 ----------- 142 - qlayers : list 143 list of hqm quantum layers to be stacked between two fully connected layers 144 - in_dims: list 145 list of integers representing the input size for the i-th fully connected layer (first value should correspond to size of input data) 146 - ou_dim : list 147 list of integers representing the output size for the i-th fully connected layer (last value should correspond to desired output size) 148 149 Returns: 150 -------- 151 Nothing, a MultiHybridMultiMLPClassifier object will be created. 152 ''' 153 154 super().__init__() 155 156 if len(in_dims) < 1: raise Exception(f"Size in_dims must be greater than 1, found {len(in_dims)}") 157 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 158 if len(qlayers) < 1: raise Exception(f"Size of qlayerss must be greater than 1, found {len(qlayers)}") 159 if len(qlayers) != len(in_dims): raise Exception(f"qlayers and in_dims must have the same lenght, found {len(qlayers)} and {len(in_dims)}") 160 for i, dim in enumerate(in_dims): 161 if dim < 1: raise Exception(f"Element {i} of in_dims must be greater than 1, found {dim}") 162 else: pass 163 164 self.fcs = [torch.nn.Linear(dim, circ.n_qubits) for (dim, circ) in zip(in_dims, qlayers)] 165 self.fco = torch.nn.Linear(qlayers[-1].n_qubits, ou_dim) 166 self.qcs = [circ.qlayer for circ in qlayers] 167 self.tanh = torch.nn.Tanh() 168 self.softmax = torch.nn.Softmax(dim=1)
MultiHybridMultiMLPClassifier constructor.
Parameters:
- qlayers : list
list of hqm quantum layers to be stacked between two fully connected layers - in_dims: list
list of integers representing the input size for the i-th fully connected layer (first value should correspond to size of input data) - ou_dim : list
list of integers representing the output size for the i-th fully connected layer (last value should correspond to desired output size)
Returns:
Nothing, a MultiHybridMultiMLPClassifier object will be created.
170 def forward(self, x : torch.Tensor) -> torch.Tensor: 171 ''' 172 Torch forward method 173 174 Parameters: 175 ----------- 176 - x : torch.Tensor 177 input for the torch model 178 179 Returns: 180 -------- 181 - x : torch.Tensor 182 output from the torch model 183 ''' 184 185 for fc, qc in zip(self.fcs, self.qcs): 186 x = fc(x) 187 x = self.tanh(x) 188 x = qc(x) 189 x = self.tanh(x) 190 191 x = self.fco(x) 192 out = self.softmax(x) 193 return out
Torch forward method
Parameters:
- x : torch.Tensor
input for the torch model
Returns:
- x : torch.Tensor
output from the torch model
Inherited Members
- torch.nn.modules.module.Module
- register_buffer
- register_parameter
- add_module
- register_module
- get_submodule
- get_parameter
- get_buffer
- get_extra_state
- set_extra_state
- apply
- cuda
- ipu
- xpu
- cpu
- type
- float
- double
- half
- bfloat16
- to_empty
- to
- register_full_backward_pre_hook
- register_backward_hook
- register_full_backward_hook
- register_forward_pre_hook
- register_forward_hook
- register_state_dict_pre_hook
- state_dict
- register_load_state_dict_post_hook
- load_state_dict
- parameters
- named_parameters
- buffers
- named_buffers
- children
- named_children
- modules
- named_modules
- train
- eval
- requires_grad_
- zero_grad
- extra_repr
- compile