hqm.regression.hmlp
1import sys 2sys.path += ['.', './layers/'] 3 4from hqm.layers.basiclayer import BasicLayer 5import torch 6 7 8class BasicHybridMLPRegressor(torch.nn.Module): 9 ''' 10 This class implements a basic hybrid multilayer perceptron for regression purposes. 11 BasicHybridMLPRegressor 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 BasicHybridMLPRegressor constructor. 18 19 Parameters: 20 ----------- 21 - qlayer : hqm.layers.basiclayer.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 BasicHybridMLPRegressor object will be created. 31 ''' 32 33 super().__init__() 34 35 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 36 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 37 38 n_qubits = qlayer.n_qubits 39 self.fc_1 = torch.nn.Linear(in_dim, n_qubits) 40 self.qc_1 = qlayer.qlayer 41 self.fc_2 = torch.nn.Linear(n_qubits, ou_dim) 42 self.tanh = torch.nn.Tanh() 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 59 x = self.fc_1(x) 60 x = self.tanh(x) 61 x = self.qc_1(x) 62 x = self.tanh(x) 63 x = self.fc_2(x) 64 out = self.tanh(x) 65 return out 66 67class MultiHybridMLPRegressor(torch.nn.Module): 68 ''' 69 This class implements a hybrid multilayer perceptron with multiple quantum circuits for regression purposes. 70 MultiHybridMLPRegressor is composed of several quantum layers stacked between two fully connected layers. 71 The size of fully connected layers is set by means of in_dim and ou_dim. 72 ''' 73 74 def __init__(self, qlayers : list, in_dim : int, ou_dim : int) -> None: 75 ''' 76 MultiHybridMLPRegressor constructor. 77 78 Parameters: 79 ----------- 80 - qlayer : list 81 list of hqm quantum layers to be stacked between two fully connected layers 82 - in_dim : int 83 integer representing the input size for the first fully connected layer 84 - ou_dim : int 85 integer representing the output size of the hybrid model 86 87 Returns: 88 -------- 89 Nothing, a MultiHybridMLPRegressor object will be created. 90 ''' 91 92 super().__init__() 93 94 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 95 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 96 if len(qlayers) < 1: raise Exception(f"Size of qlayers must be greater than 1, found {len(qlayers)}") 97 98 n_qubits_0 = qlayers[0].n_qubits 99 n_qubits_1 = qlayers[-1].n_qubits 100 self.fc_1 = torch.nn.Linear(in_dim, n_qubits_0) 101 self.qcs = [circ.qlayer for circ in qlayers] 102 self.fc_2 = torch.nn.Linear(n_qubits_1, ou_dim) 103 self.tanh = torch.nn.Tanh() 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.tanh(x) 127 return out 128 129class MultiHybridMultiMLPRegressor(torch.nn.Module): 130 ''' 131 This class implements a hybrid multilayer perceptron with multiple quantum circuits for regression purposes. 132 MultiHybridMultiMLPRegressor 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 MultiHybridMultiMLPRegressor constructor. 139 140 Parameters: 141 ----------- 142 - qlayers : list 143 list of hqm quantum qlayerss 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 MultiHybridMLPRegressor 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 qlayers 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 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.tanh(x) 192 return out
9class BasicHybridMLPRegressor(torch.nn.Module): 10 ''' 11 This class implements a basic hybrid multilayer perceptron for regression purposes. 12 BasicHybridMLPRegressor is composed of quantum layers stacked between two fully connected layers. 13 The size of fully connected layers is set by means of in_dim and ou_dim. 14 ''' 15 16 def __init__(self, qlayer : BasicLayer, in_dim : int, ou_dim : int) -> None: 17 ''' 18 BasicHybridMLPRegressor constructor. 19 20 Parameters: 21 ----------- 22 - qlayer : hqm.layers.basiclayer.BasicLayer 23 hqm quantum layer to be stacked between two fully connected layers 24 - in_dim : int 25 integer representing the input size for the first fully connected layer 26 - ou_dim : int 27 integer representing the output size of the hybrid model 28 29 Returns: 30 -------- 31 Nothing, a BasicHybridMLPRegressor object will be created. 32 ''' 33 34 super().__init__() 35 36 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 37 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 38 39 n_qubits = qlayer.n_qubits 40 self.fc_1 = torch.nn.Linear(in_dim, n_qubits) 41 self.qc_1 = qlayer.qlayer 42 self.fc_2 = torch.nn.Linear(n_qubits, ou_dim) 43 self.tanh = torch.nn.Tanh() 44 45 def forward(self, x : torch.Tensor) -> torch.Tensor: 46 ''' 47 Torch forward method 48 49 Parameters: 50 ----------- 51 - x : torch.Tensor 52 input for the torch model 53 54 Returns: 55 -------- 56 - out : torch.Tensor 57 output from the torch model 58 ''' 59 60 x = self.fc_1(x) 61 x = self.tanh(x) 62 x = self.qc_1(x) 63 x = self.tanh(x) 64 x = self.fc_2(x) 65 out = self.tanh(x) 66 return out
This class implements a basic hybrid multilayer perceptron for regression purposes. BasicHybridMLPRegressor 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.
16 def __init__(self, qlayer : BasicLayer, in_dim : int, ou_dim : int) -> None: 17 ''' 18 BasicHybridMLPRegressor constructor. 19 20 Parameters: 21 ----------- 22 - qlayer : hqm.layers.basiclayer.BasicLayer 23 hqm quantum layer to be stacked between two fully connected layers 24 - in_dim : int 25 integer representing the input size for the first fully connected layer 26 - ou_dim : int 27 integer representing the output size of the hybrid model 28 29 Returns: 30 -------- 31 Nothing, a BasicHybridMLPRegressor object will be created. 32 ''' 33 34 super().__init__() 35 36 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 37 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 38 39 n_qubits = qlayer.n_qubits 40 self.fc_1 = torch.nn.Linear(in_dim, n_qubits) 41 self.qc_1 = qlayer.qlayer 42 self.fc_2 = torch.nn.Linear(n_qubits, ou_dim) 43 self.tanh = torch.nn.Tanh()
BasicHybridMLPRegressor constructor.
Parameters:
- qlayer : hqm.layers.basiclayer.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 BasicHybridMLPRegressor object will be created.
45 def forward(self, x : torch.Tensor) -> torch.Tensor: 46 ''' 47 Torch forward method 48 49 Parameters: 50 ----------- 51 - x : torch.Tensor 52 input for the torch model 53 54 Returns: 55 -------- 56 - out : torch.Tensor 57 output from the torch model 58 ''' 59 60 x = self.fc_1(x) 61 x = self.tanh(x) 62 x = self.qc_1(x) 63 x = self.tanh(x) 64 x = self.fc_2(x) 65 out = self.tanh(x) 66 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
68class MultiHybridMLPRegressor(torch.nn.Module): 69 ''' 70 This class implements a hybrid multilayer perceptron with multiple quantum circuits for regression purposes. 71 MultiHybridMLPRegressor is composed of several quantum layers stacked between two fully connected layers. 72 The size of fully connected layers is set by means of in_dim and ou_dim. 73 ''' 74 75 def __init__(self, qlayers : list, in_dim : int, ou_dim : int) -> None: 76 ''' 77 MultiHybridMLPRegressor constructor. 78 79 Parameters: 80 ----------- 81 - qlayer : list 82 list of hqm quantum layers to be stacked between two fully connected layers 83 - in_dim : int 84 integer representing the input size for the first fully connected layer 85 - ou_dim : int 86 integer representing the output size of the hybrid model 87 88 Returns: 89 -------- 90 Nothing, a MultiHybridMLPRegressor object will be created. 91 ''' 92 93 super().__init__() 94 95 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 96 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 97 if len(qlayers) < 1: raise Exception(f"Size of qlayers must be greater than 1, found {len(qlayers)}") 98 99 n_qubits_0 = qlayers[0].n_qubits 100 n_qubits_1 = qlayers[-1].n_qubits 101 self.fc_1 = torch.nn.Linear(in_dim, n_qubits_0) 102 self.qcs = [circ.qlayer for circ in qlayers] 103 self.fc_2 = torch.nn.Linear(n_qubits_1, ou_dim) 104 self.tanh = torch.nn.Tanh() 105 106 def forward(self, x : torch.Tensor) -> torch.Tensor: 107 ''' 108 Torch forward method 109 110 Parameters: 111 ----------- 112 - x : torch.Tensor 113 input for the torch model 114 115 Returns: 116 -------- 117 - out : torch.Tensor 118 output from the torch model 119 ''' 120 121 x = self.fc_1(x) 122 x = self.tanh(x) 123 for qc in self.qcs: 124 x = qc(x) 125 x = self.tanh(x) 126 x = self.fc_2(x) 127 out = self.tanh(x) 128 return out
This class implements a hybrid multilayer perceptron with multiple quantum circuits for regression purposes. MultiHybridMLPRegressor 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.
75 def __init__(self, qlayers : list, in_dim : int, ou_dim : int) -> None: 76 ''' 77 MultiHybridMLPRegressor constructor. 78 79 Parameters: 80 ----------- 81 - qlayer : list 82 list of hqm quantum layers to be stacked between two fully connected layers 83 - in_dim : int 84 integer representing the input size for the first fully connected layer 85 - ou_dim : int 86 integer representing the output size of the hybrid model 87 88 Returns: 89 -------- 90 Nothing, a MultiHybridMLPRegressor object will be created. 91 ''' 92 93 super().__init__() 94 95 if in_dim < 1: raise Exception(f"The parameter in_dim must be greater than 1, found {in_dim}") 96 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 97 if len(qlayers) < 1: raise Exception(f"Size of qlayers must be greater than 1, found {len(qlayers)}") 98 99 n_qubits_0 = qlayers[0].n_qubits 100 n_qubits_1 = qlayers[-1].n_qubits 101 self.fc_1 = torch.nn.Linear(in_dim, n_qubits_0) 102 self.qcs = [circ.qlayer for circ in qlayers] 103 self.fc_2 = torch.nn.Linear(n_qubits_1, ou_dim) 104 self.tanh = torch.nn.Tanh()
MultiHybridMLPRegressor constructor.
Parameters:
- qlayer : 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 MultiHybridMLPRegressor object will be created.
106 def forward(self, x : torch.Tensor) -> torch.Tensor: 107 ''' 108 Torch forward method 109 110 Parameters: 111 ----------- 112 - x : torch.Tensor 113 input for the torch model 114 115 Returns: 116 -------- 117 - out : torch.Tensor 118 output from the torch model 119 ''' 120 121 x = self.fc_1(x) 122 x = self.tanh(x) 123 for qc in self.qcs: 124 x = qc(x) 125 x = self.tanh(x) 126 x = self.fc_2(x) 127 out = self.tanh(x) 128 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
130class MultiHybridMultiMLPRegressor(torch.nn.Module): 131 ''' 132 This class implements a hybrid multilayer perceptron with multiple quantum circuits for regression purposes. 133 MultiHybridMultiMLPRegressor is composed of several quantum layers stacked between alternating fully connected layers. 134 The size of fully connected layers is set by means of in_dim and ou_dim. 135 ''' 136 137 def __init__(self, qlayers : list, in_dims : list, ou_dim : list) -> None: 138 ''' 139 MultiHybridMultiMLPRegressor constructor. 140 141 Parameters: 142 ----------- 143 - qlayers : list 144 list of hqm quantum qlayerss to be stacked between two fully connected layers 145 - in_dims: list 146 list of integers representing the input size for the i-th fully connected layer (first value should correspond to size of input data) 147 - ou_dim : list 148 list of integers representing the output size for the i-th fully connected layer (last value should correspond to desired output size) 149 150 Returns: 151 -------- 152 Nothing, a MultiHybridMLPRegressor object will be created. 153 ''' 154 155 super().__init__() 156 157 if len(in_dims) < 1: raise Exception(f"Size in_dims must be greater than 1, found {len(in_dims)}") 158 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 159 if len(qlayers) < 1: raise Exception(f"Size of qlayers must be greater than 1, found {len(qlayers)}") 160 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)}") 161 for i, dim in enumerate(in_dims): 162 if dim < 1: raise Exception(f"Element {i} of in_dims must be greater than 1, found {dim}") 163 else: pass 164 165 self.fcs = [torch.nn.Linear(dim, circ.n_qubits) for (dim, circ) in zip(in_dims, qlayers)] 166 self.fco = torch.nn.Linear(qlayers[-1].n_qubits, ou_dim) 167 self.qcs = [circ.qlayer for circ in qlayers] 168 self.tanh = torch.nn.Tanh() 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.tanh(x) 193 return out
This class implements a hybrid multilayer perceptron with multiple quantum circuits for regression purposes. MultiHybridMultiMLPRegressor 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.
137 def __init__(self, qlayers : list, in_dims : list, ou_dim : list) -> None: 138 ''' 139 MultiHybridMultiMLPRegressor constructor. 140 141 Parameters: 142 ----------- 143 - qlayers : list 144 list of hqm quantum qlayerss to be stacked between two fully connected layers 145 - in_dims: list 146 list of integers representing the input size for the i-th fully connected layer (first value should correspond to size of input data) 147 - ou_dim : list 148 list of integers representing the output size for the i-th fully connected layer (last value should correspond to desired output size) 149 150 Returns: 151 -------- 152 Nothing, a MultiHybridMLPRegressor object will be created. 153 ''' 154 155 super().__init__() 156 157 if len(in_dims) < 1: raise Exception(f"Size in_dims must be greater than 1, found {len(in_dims)}") 158 if ou_dim < 1: raise Exception(f"The parameter ou_dim must be greater than 1, found {ou_dim}") 159 if len(qlayers) < 1: raise Exception(f"Size of qlayers must be greater than 1, found {len(qlayers)}") 160 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)}") 161 for i, dim in enumerate(in_dims): 162 if dim < 1: raise Exception(f"Element {i} of in_dims must be greater than 1, found {dim}") 163 else: pass 164 165 self.fcs = [torch.nn.Linear(dim, circ.n_qubits) for (dim, circ) in zip(in_dims, qlayers)] 166 self.fco = torch.nn.Linear(qlayers[-1].n_qubits, ou_dim) 167 self.qcs = [circ.qlayer for circ in qlayers] 168 self.tanh = torch.nn.Tanh()
MultiHybridMultiMLPRegressor constructor.
Parameters:
- qlayers : list
list of hqm quantum qlayerss 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 MultiHybridMLPRegressor 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.tanh(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