

import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


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


@dataclass(frozen=_UpperCAmelCase)
class       __SCREAMING_SNAKE_CASE   :
						_SCREAMING_SNAKE_CASE		:    str
						_SCREAMING_SNAKE_CASE		:    str
						_SCREAMING_SNAKE_CASE		:    Optional[str]						=   None
						_SCREAMING_SNAKE_CASE		:    Optional[str]						=   None
						_SCREAMING_SNAKE_CASE		:    Optional[str]						=   None




@dataclass(frozen=_UpperCAmelCase)
class       __SCREAMING_SNAKE_CASE   :
						_SCREAMING_SNAKE_CASE		:    List[int]
						_SCREAMING_SNAKE_CASE		:    Optional[List[int]]						=   None
						_SCREAMING_SNAKE_CASE		:    Optional[List[int]]						=   None
						_SCREAMING_SNAKE_CASE		:    Optional[Union[int, float]]						=   None
						_SCREAMING_SNAKE_CASE		:    Optional[int]						=   None


if is_torch_available():
					import torch
					from torch.utils.data import Dataset


					class       __SCREAMING_SNAKE_CASE   (				_UpperCAmelCase):
											_SCREAMING_SNAKE_CASE		:    List[InputFeatures]






											def __init__(		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase = None       ,  _UpperCamelCase=False       ,  _UpperCamelCase = False       ,  ):






																	"""simple docstring"""
																	lowerCAmelCase__        =  hans_processors[task]()

																	lowerCAmelCase__        =  os.path.join(
																	    _UpperCAmelCase       ,  'cached_{}_{}_{}_{}'.format(
																	        'dev' if evaluate else 'train'       ,  tokenizer.__class__.__name__       ,  str(_UpperCAmelCase				)       ,  _UpperCAmelCase       ,  )       ,  )
																	lowerCAmelCase__        =  processor.get_labels()
																	if tokenizer.__class__ in (
																	    RobertaTokenizer,
																	    RobertaTokenizerFast,
																	    XLMRobertaTokenizer,
																	    BartTokenizer,
																	    BartTokenizerFast,
																	):
																							# HACK(label indices are swapped in RoBERTa pretrained model)
																							lowerCAmelCase__        =  label_list[2], label_list[1]
																	lowerCAmelCase__        =  label_list

																	# Make sure only the first process in distributed training processes the dataset,
																	# and the others will use the cache.
																	lowerCAmelCase__        =  cached_features_file + '''.lock'''
																	with FileLock(_UpperCAmelCase				):
																							if os.path.exists(_UpperCAmelCase				) and not overwrite_cache:
																													logger.info(F"Loading features from cached file {cached_features_file}"				)
																													lowerCAmelCase__        =  torch.load(_UpperCAmelCase				)
																							else:
																													logger.info(F"Creating features from dataset file at {data_dir}"				)

																													lowerCAmelCase__        =  (
																													    processor.get_dev_examples(_UpperCAmelCase				) if evaluate else processor.get_train_examples(_UpperCAmelCase				)
																													)

																													logger.info('Training examples: %s'       ,  len(_UpperCAmelCase				)				)
																													lowerCAmelCase__        =  hans_convert_examples_to_features(_UpperCAmelCase       ,  _UpperCAmelCase       ,  _UpperCAmelCase       ,  _UpperCAmelCase				)
																													logger.info('Saving features into cached file %s'       ,  _UpperCAmelCase				)
																													torch.save(self.features       ,  _UpperCAmelCase				)






											def __len__(		self				):






																	"""simple docstring"""
																	return len(self.features				)






											def __getitem__(		self       ,  _UpperCamelCase				):






																	"""simple docstring"""
																	return self.features[i]







											def  UpperCamelCase__ (		self				):






																	"""simple docstring"""
																	return self.label_list


if is_tf_available():
					import tensorflow as tf


					class       __SCREAMING_SNAKE_CASE   :
											_SCREAMING_SNAKE_CASE		:    List[InputFeatures]






											def __init__(		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase = 1_28       ,  _UpperCamelCase=False       ,  _UpperCamelCase = False       ,  ):






																	"""simple docstring"""
																	lowerCAmelCase__        =  hans_processors[task]()
																	lowerCAmelCase__        =  processor.get_labels()
																	if tokenizer.__class__ in (
																	    RobertaTokenizer,
																	    RobertaTokenizerFast,
																	    XLMRobertaTokenizer,
																	    BartTokenizer,
																	    BartTokenizerFast,
																	):
																							# HACK(label indices are swapped in RoBERTa pretrained model)
																							lowerCAmelCase__        =  label_list[2], label_list[1]
																	lowerCAmelCase__        =  label_list

																	lowerCAmelCase__        =  processor.get_dev_examples(_UpperCAmelCase				) if evaluate else processor.get_train_examples(_UpperCAmelCase				)
																	lowerCAmelCase__        =  hans_convert_examples_to_features(_UpperCAmelCase       ,  _UpperCAmelCase       ,  _UpperCAmelCase       ,  _UpperCAmelCase				)

																	def gen():
																							for ex_index, ex in tqdm.tqdm(enumerate(self.features				)       ,  desc='convert examples to features'				):
																													if ex_index % 1_00_00 == 0:
																																			logger.info('Writing example %d of %d' % (ex_index, len(_UpperCAmelCase				))				)

																													yield (
																													    {
																													        "example_id": 0,
																													        "input_ids": ex.input_ids,
																													        "attention_mask": ex.attention_mask,
																													        "token_type_ids": ex.token_type_ids,
																													    },
																													    ex.label,
																													)

																	lowerCAmelCase__        =  tf.data.Dataset.from_generator(
																	    _UpperCAmelCase       ,  (
																	        {
																	            'example_id': tf.intaa,
																	            'input_ids': tf.intaa,
																	            'attention_mask': tf.intaa,
																	            'token_type_ids': tf.intaa,
																	        },
																	        tf.intaa,
																	    )       ,  (
																	        {
																	            'example_id': tf.TensorShape([]				),
																	            'input_ids': tf.TensorShape([None, None]				),
																	            'attention_mask': tf.TensorShape([None, None]				),
																	            'token_type_ids': tf.TensorShape([None, None]				),
																	        },
																	        tf.TensorShape([]				),
																	    )       ,  )






											def  UpperCamelCase__ (		self				):






																	"""simple docstring"""
																	return self.dataset






											def __len__(		self				):






																	"""simple docstring"""
																	return len(self.features				)






											def __getitem__(		self       ,  _UpperCamelCase				):






																	"""simple docstring"""
																	return self.features[i]







											def  UpperCamelCase__ (		self				):






																	"""simple docstring"""
																	return self.label_list


class       __SCREAMING_SNAKE_CASE   (				_UpperCAmelCase):






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase				):






												"""simple docstring"""
												return self._create_examples(self._read_tsv(os.path.join(_UpperCAmelCase       ,  'heuristics_train_set.txt'				)				)       ,  'train'				)






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase				):






												"""simple docstring"""
												return self._create_examples(self._read_tsv(os.path.join(_UpperCAmelCase       ,  'heuristics_evaluation_set.txt'				)				)       ,  'dev'				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												return ["contradiction", "entailment", "neutral"]







						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase				):






												"""simple docstring"""
												lowerCAmelCase__        =  []
												for i, line in enumerate(_UpperCAmelCase				):
																		if i == 0:
																								continue
																		lowerCAmelCase__        =  '''%s-%s''' % (set_type, line[0])
																		lowerCAmelCase__        =  line[5]
																		lowerCAmelCase__        =  line[6]
																		lowerCAmelCase__        =  line[7][2:] if line[7].startswith('ex'				) else line[7]
																		lowerCAmelCase__        =  line[0]
																		examples.append(InputExample(guid=_UpperCAmelCase       ,  text_a=_UpperCAmelCase       ,  text_b=_UpperCAmelCase       ,  label=_UpperCAmelCase       ,  pairID=_UpperCAmelCase				)				)
												return examples







def 		_UpperCamelCase				(      UpperCamelCase_    :   Any      ,    UpperCamelCase_    :   Any      ,    UpperCamelCase_    :   Optional[Any]      ,    UpperCamelCase_    :   Any      ,    )					->				Any:

						"""simple docstring"""



						lowerCAmelCase__        =  {label: i for i, label in enumerate(__UpperCAmelCase						)}

						lowerCAmelCase__        =  []
						for ex_index, example in tqdm.tqdm(enumerate(__UpperCAmelCase						)      ,    desc='convert examples to features'						):
												if ex_index % 1_0000 == 0:
																		logger.info('Writing example %d' % (ex_index)						)

												lowerCAmelCase__        =  tokenizer(
												    example.text_a      ,    example.text_b      ,    add_special_tokens=__UpperCAmelCase      ,    max_length=__UpperCAmelCase      ,    padding='max_length'      ,    truncation=__UpperCAmelCase      ,    return_overflowing_tokens=__UpperCAmelCase      ,    )

												lowerCAmelCase__        =  label_map[example.label] if example.label in label_map else 0

												lowerCAmelCase__        =  int(example.pairID						)

												features.append(InputFeatures(**__UpperCAmelCase      ,    label=__UpperCAmelCase      ,    pairID=__UpperCAmelCase						)						)

						for i, example in enumerate(examples[:5]						):
												logger.info('*** Example ***'						)
												logger.info(F"guid: {example}"						)
												logger.info(F"features: {features[i]}"						)

						return features


__snake_case      :      Optional[Any]             =       {
    """hans""": 3,
}


__snake_case      :      int             =       {
    """hans""": HansProcessor,
}













import qiskit







def 		_UpperCamelCase				(      UpperCamelCase_    :   int      ,    UpperCamelCase_    :   int						)					->				qiskit.result.counts.Counts:

						"""simple docstring"""



						lowerCAmelCase__        =  qiskit.Aer.get_backend('aer_simulator'						)

						# Create a Quantum Circuit acting on the q register
						lowerCAmelCase__        =  qiskit.QuantumCircuit(UpperCamelCase_      ,    UpperCamelCase_						)

						# Map the quantum measurement to the classical bits
						circuit.measure([0]      ,    [0]						)

						# Execute the circuit on the simulator
						lowerCAmelCase__        =  qiskit.execute(UpperCamelCase_      ,    UpperCamelCase_      ,    shots=1000						)

						# Return the histogram data of the results of the experiment.
						return job.result().get_counts(UpperCamelCase_						)


if __name__ == "__main__":
					print(f'Total count for various states are: {single_qubit_measure(1, 1)}')





