




'''simple docstring'''





import logging

import torch
from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss

from transformers.file_utils import add_start_docstrings, add_start_docstrings_to_model_forward
from transformers.models.albert.modeling_albert import (
    ALBERT_INPUTS_DOCSTRING,
    ALBERT_START_DOCSTRING,
    AlbertModel,
    AlbertPreTrainedModel,
    AlbertTransformer,
)


_SCREAMING_SNAKE_CASE							:     Union[str, Any]   	=				logging.getLogger(__name__)




class 				_snake_case	(			lowercase_							):




			def      lowerCAmelCase__       (      self       ,							a__       ,							a__       ,							a__=None       ,							a__=None							)       ->       int:

										'''simple docstring'''




										if current_layer == 0:
																	snake_case_			      =   self.embedding_hidden_mapping_in(a__							)
										else:
																	snake_case_			      =   hidden_states[0]

										snake_case_			      =   int(self.config.num_hidden_layers / self.config.num_hidden_groups							)

										# Index of the hidden group
										snake_case_			      =   int(current_layer / (self.config.num_hidden_layers / self.config.num_hidden_groups)							)

										snake_case_			      =   self.albert_layer_groups[group_idx](
										    a__       ,							a__       ,							head_mask[group_idx * layers_per_group : (group_idx + 1) * layers_per_group]       ,							)
										snake_case_			      =   layer_group_output[0]

										return (hidden_states,)




@add_start_docstrings(
    "The bare ALBERT Model transformer with PABEE outputting raw hidden-states without any specific head on top."		,		lowercase_		,		)
class 				_snake_case	(			lowercase_							):




			def __init__(      self       ,							a__							)       ->       Dict:

										'''simple docstring'''




										super().__init__(a__							)

										snake_case_			      =   AlbertTransformerWithPabee(a__							)

										self.init_weights()
										snake_case_			      =   0
										snake_case_			      =   0
										snake_case_			      =   0

										snake_case_			      =   0




			def      lowerCAmelCase__       (      self       ,							a__							)       ->       Optional[int]:

										'''simple docstring'''




										snake_case_			      =   threshold




			def      lowerCAmelCase__       (      self       ,							a__							)       ->       List[Any]:

										'''simple docstring'''




										snake_case_			      =   patience




			def      lowerCAmelCase__       (      self							)       ->       Dict:

										'''simple docstring'''




										snake_case_			      =   0
										snake_case_			      =   0




			def      lowerCAmelCase__       (      self							)       ->       Union[str, Any]:

										'''simple docstring'''




										snake_case_			      =   self.inference_layers_num / self.inference_instances_num
										snake_case_			      =   (
										    F'*** Patience = {self.patience} Avg. Inference Layers = {avg_inf_layers:.2f} Speed Up ='
										    F' {1 - avg_inf_layers / self.config.num_hidden_layers:.2f} ***'
										)
										print(a__							)




			@add_start_docstrings_to_model_forward(a__							)
			def      lowerCAmelCase__       (      self       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=False       ,							)       ->       int:

										'''simple docstring'''




										if input_ids is not None and inputs_embeds is not None:
																	raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time"							)
										elif input_ids is not None:
																	snake_case_			      =   input_ids.size()
										elif inputs_embeds is not None:
																	snake_case_			      =   inputs_embeds.size()[:-1]
										else:
																	raise ValueError("You have to specify either input_ids or inputs_embeds"							)

										snake_case_			      =   input_ids.device if input_ids is not None else inputs_embeds.device

										if attention_mask is None:
																	snake_case_			      =   torch.ones(a__       ,							device=a__							)
										if token_type_ids is None:
																	snake_case_			      =   torch.zeros(a__       ,							dtype=torch.long       ,							device=a__							)

										snake_case_			      =   attention_mask.unsqueeze(1							).unsqueeze(2							)
										snake_case_			      =   extended_attention_mask.to(dtype=self.dtype							)  # fp16 compatibility
										snake_case_			      =   (1.0 - extended_attention_mask) * -1_0_0_0_0.0
										snake_case_			      =   self.get_head_mask(a__       ,							self.config.num_hidden_layers							)

										snake_case_			      =   self.embeddings(
										    a__       ,							position_ids=a__       ,							token_type_ids=a__       ,							inputs_embeds=a__							)
										snake_case_			      =   embedding_output

										if self.training:
																	snake_case_			      =   []
																	for i in range(self.config.num_hidden_layers							):
																								snake_case_			      =   self.encoder.adaptive_forward(
																								    a__       ,							current_layer=a__       ,							attention_mask=a__       ,							head_mask=a__       ,							)

																								snake_case_			      =   self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]							)							)
																								snake_case_			      =   output_layers[i](output_dropout(a__							)							)
																								res.append(a__							)
										elif self.patience == 0:  # Use all layers for inference
																	snake_case_			      =   self.encoder(a__       ,							a__       ,							head_mask=a__							)
																	snake_case_			      =   self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]							)							)
																	snake_case_			      =   [output_layers[self.config.num_hidden_layers - 1](a__							)]
										else:
																	snake_case_			      =   0
																	snake_case_			      =   None
																	snake_case_			      =   0
																	for i in range(self.config.num_hidden_layers							):
																								calculated_layer_num += 1
																								snake_case_			      =   self.encoder.adaptive_forward(
																								    a__       ,							current_layer=a__       ,							attention_mask=a__       ,							head_mask=a__       ,							)

																								snake_case_			      =   self.pooler_activation(self.pooler(encoder_outputs[0][:, 0]							)							)
																								snake_case_			      =   output_layers[i](a__							)
																								if regression:
																															snake_case_			      =   logits.detach()
																															if patient_result is not None:
																																						snake_case_			      =   patient_result.detach()
																															if (patient_result is not None) and torch.abs(patient_result - labels							) < self.regression_threshold:
																																						patient_counter += 1
																															else:
																																						snake_case_			      =   0
																								else:
																															snake_case_			      =   logits.detach().argmax(dim=1							)
																															if patient_result is not None:
																																						snake_case_			      =   patient_result.detach().argmax(dim=1							)
																															if (patient_result is not None) and torch.all(labels.eq(a__							)							):
																																						patient_counter += 1
																															else:
																																						snake_case_			      =   0

																								snake_case_			      =   logits
																								if patient_counter == self.patience:
																															break
																	snake_case_			      =   [patient_result]
																	self.inference_layers_num += calculated_layer_num
																	self.inference_instances_num += 1

										return res




@add_start_docstrings(
    "Albert Model transformer with PABEE and a sequence classification/regression head on top (a linear layer on top of\n    the pooled output) e.g. for GLUE tasks. "		,		lowercase_		,		)
class 				_snake_case	(			lowercase_							):




			def __init__(      self       ,							a__							)       ->       Dict:

										'''simple docstring'''




										super().__init__(a__							)
										snake_case_			      =   config.num_labels

										snake_case_			      =   AlbertModelWithPabee(a__							)
										snake_case_			      =   nn.Dropout(config.classifier_dropout_prob							)
										snake_case_			      =   nn.ModuleList(
										    [nn.Linear(config.hidden_size       ,							self.config.num_labels							) for _ in range(config.num_hidden_layers							)]							)

										self.init_weights()




			@add_start_docstrings_to_model_forward(a__							)
			def      lowerCAmelCase__       (      self       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							a__=None       ,							)       ->       Optional[Any]:

										'''simple docstring'''




										snake_case_			      =   self.albert(
										    input_ids=a__       ,							attention_mask=a__       ,							token_type_ids=a__       ,							position_ids=a__       ,							head_mask=a__       ,							inputs_embeds=a__       ,							output_dropout=self.dropout       ,							output_layers=self.classifiers       ,							regression=self.num_labels == 1       ,							)

										snake_case_			      =   (logits[-1],)

										if labels is not None:
																	snake_case_			      =   None
																	snake_case_			      =   0
																	for ix, logits_item in enumerate(a__							):
																								if self.num_labels == 1:
																															#  We are doing regression
																															snake_case_			      =   MSELoss()
																															snake_case_			      =   loss_fct(logits_item.view(-1							)       ,							labels.view(-1							)							)
																								else:
																															snake_case_			      =   CrossEntropyLoss()
																															snake_case_			      =   loss_fct(logits_item.view(-1       ,							self.num_labels							)       ,							labels.view(-1							)							)
																								if total_loss is None:
																															snake_case_			      =   loss
																								else:
																															total_loss += loss * (ix + 1)
																								total_weights += ix + 1
																	snake_case_			      =   (total_loss / total_weights,) + outputs

										return outputs










'''simple docstring'''





import qiskit
from qiskit.providers import Backend


def   UpperCamelCase_(			snake_case							: int							,       snake_case							: int  ):

							'''simple docstring'''






							snake_case_      ,   snake_case_			      =   bin(snake_case  )[2:], bin(snake_case  )[2:]  # Remove leading '0b'

							# Ensure that both strings are of the same length
							if len(snake_case  ) > len(snake_case  ):
														snake_case_			      =   y.zfill(len(snake_case  )  )
							else:
														snake_case_			      =   x.zfill(len(snake_case  )  )

							# We need (3 * number of bits in the larger number)+1 qBits
							# The second parameter is the number of classical registers, to measure the result
							snake_case_			      =   qiskit.QuantumCircuit((len(snake_case  ) * 3) + 1							,       len(snake_case  ) + 1  )

							# We are essentially "not-ing" the bits that are 1
							# Reversed because it's easier to perform ops on more significant bits
							for i in range(len(snake_case  )  ):
														if x[::-1][i] == "1":
																					circuit.x(snake_case  )
							for j in range(len(snake_case  )  ):
														if y[::-1][j] == "1":
																					circuit.x(len(snake_case  ) + j  )

							return circuit, x, y


def   UpperCamelCase_(			snake_case							: qiskit.QuantumCircuit							,       snake_case							: int							,       snake_case							: int							,       snake_case							: int							,       snake_case							: int							,       ):

							'''simple docstring'''






							circuit.ccx(snake_case							,       snake_case							,       snake_case  )
							circuit.cx(snake_case							,       snake_case  )
							circuit.ccx(snake_case							,       snake_case							,       snake_case  )
							circuit.cx(snake_case							,       snake_case  )
							circuit.cx(snake_case							,       snake_case  )


def   UpperCamelCase_(			snake_case							: int							,       snake_case							: int							,       snake_case							: Backend = qiskit.Aer.get_backend("aer_simulator"  )							,       ):

							'''simple docstring'''






							if vala < 0 or vala < 0:
														raise ValueError("Both Integers must be positive!"  )

							# Store the Integers
							snake_case_      ,   snake_case_      ,   snake_case_			      =   store_two_classics(snake_case							,       snake_case  )
							for i in range(len(snake_case  )  ):
														full_adder(snake_case							,       snake_case							,       len(snake_case  ) + i							,       len(snake_case  ) + len(snake_case  ) + i							,       len(snake_case  ) + len(snake_case  ) + i + 1  )
														circuit.barrier()  # Optional, just for aesthetics

							# Measure the resultant qBits
							for i in range(len(snake_case  ) + 1  ):
														circuit.measure([(len(snake_case  ) * 2) + i]							,       [i]  )

							snake_case_			      =   qiskit.execute(snake_case							,       snake_case							,       shots=1  ).result()

							# The result is in binary. Convert it back to int
							return int(list(res.get_counts()  )[0]							,       2  )


if __name__ == "__main__":
		import doctest

		doctest.testmod()




