





'''simple docstring'''

import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
  from transformers.trainer import DEFAULT_CALLBACKS

  from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel






class        UpperCAmelCase     (	UpperCamelCase__    ):
 def __init__(    self				:Optional[int]							)->							List[str]:
     A__    =      []
 def 					UpperCAmelCase_							(    self				:int						,	lowercase_				:List[str]						,	lowercase_				:Union[str, Any]						,	lowercase_				:Any						,	**lowercase_				:Optional[int]							)->							List[str]:
     self.events.append("on_init_end"							)
 def 					UpperCAmelCase_							(    self				:int						,	lowercase_				:int						,	lowercase_				:List[Any]						,	lowercase_				:Dict						,	**lowercase_				:Any							)->							List[str]:
     self.events.append("on_train_begin"							)
 def 					UpperCAmelCase_							(    self				:Dict						,	lowercase_				:Union[str, Any]						,	lowercase_				:List[str]						,	lowercase_				:Any						,	**lowercase_				:List[str]							)->							Union[str, Any]:
     self.events.append("on_train_end"							)
 def 					UpperCAmelCase_							(    self				:Dict						,	lowercase_				:Dict						,	lowercase_				:Union[str, Any]						,	lowercase_				:List[Any]						,	**lowercase_				:Tuple							)->							Dict:
     self.events.append("on_epoch_begin"							)
 def 					UpperCAmelCase_							(    self				:Dict						,	lowercase_				:List[Any]						,	lowercase_				:List[Any]						,	lowercase_				:Tuple						,	**lowercase_				:Tuple							)->							str:
     self.events.append("on_epoch_end"							)
 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:Tuple						,	lowercase_				:str						,	lowercase_				:Optional[int]						,	**lowercase_				:Optional[Any]							)->							Union[str, Any]:
     self.events.append("on_step_begin"							)
 def 					UpperCAmelCase_							(    self				:List[Any]						,	lowercase_				:List[Any]						,	lowercase_				:Any						,	lowercase_				:Optional[int]						,	**lowercase_				:Optional[Any]							)->							int:
     self.events.append("on_step_end"							)
 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:Optional[int]						,	lowercase_				:Any						,	lowercase_				:Dict						,	**lowercase_				:Optional[Any]							)->							Optional[int]:
     self.events.append("on_evaluate"							)
 def 					UpperCAmelCase_							(    self				:Optional[Any]						,	lowercase_				:Tuple						,	lowercase_				:List[str]						,	lowercase_				:Any						,	**lowercase_				:Union[str, Any]							)->							List[Any]:
     self.events.append("on_predict"							)
 def 					UpperCAmelCase_							(    self				:Union[str, Any]						,	lowercase_				:Optional[int]						,	lowercase_				:str						,	lowercase_				:Dict						,	**lowercase_				:Any							)->							Dict:
     self.events.append("on_save"							)
 def 					UpperCAmelCase_							(    self				:int						,	lowercase_				:Dict						,	lowercase_				:Dict						,	lowercase_				:str						,	**lowercase_				:Dict							)->							int:
     self.events.append("on_log"							)



 def 					UpperCAmelCase_							(    self				:Tuple						,	lowercase_				:Optional[int]						,	lowercase_				:Optional[int]						,	lowercase_				:Dict						,	**lowercase_				:List[Any]							)->							int:
     self.events.append("on_prediction_step"							)






@require_torch
class        UpperCAmelCase     (	unittest.TestCase    ):
 def 					UpperCAmelCase_							(    self				:int							)->							List[Any]:
     A__    =      tempfile.mkdtemp()
 def 					UpperCAmelCase_							(    self				:Optional[int]							)->							Any:
     shutil.rmtree(self.output_dir							)
 def 					UpperCAmelCase_							(    self				:int						,	lowercase_				:int=0						,	lowercase_				:int=0						,	lowercase_				:List[str]=64						,	lowercase_				:Dict=64						,	lowercase_				:Dict=None						,	lowercase_				:List[str]=False						,	**lowercase_				:List[str]							)->							List[str]:
     # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure
     # its set to False since the tests later on depend on its value.
     A__    =      RegressionDataset(length=lowercase_							)
     A__    =      RegressionDataset(length=lowercase_							)
     A__    =      RegressionModelConfig(a=lowercase_						,	b=lowercase_							)
     A__    =      RegressionPreTrainedModel(lowercase_							)

     A__    =      TrainingArguments(self.output_dir						,	disable_tqdm=lowercase_						,	report_to=[]						,	**lowercase_							)
     return Trainer(
         lowercase_						,	lowercase_						,	train_dataset=lowercase_						,	eval_dataset=lowercase_						,	callbacks=lowercase_						,	)
 def 					UpperCAmelCase_							(    self				:str						,	lowercase_				:Optional[Any]						,	lowercase_				:int							)->							Optional[Any]:
     self.assertEqual(len(lowercase_							)						,	len(lowercase_							)							)

     # Order doesn't matter
     A__    =      sorted(lowercase_						,	key=lambda lowercase_							: cb.__name__ if isinstance(lowercase_						,	lowercase_							) else cb.__class__.__name__							)
     A__    =      sorted(lowercase_						,	key=lambda lowercase_							: cb.__name__ if isinstance(lowercase_						,	lowercase_							) else cb.__class__.__name__							)

     for cba, cba in zip(lowercase_						,	lowercase_							):
         if isinstance(lowercase_						,	lowercase_							) and isinstance(lowercase_						,	lowercase_							):
             self.assertEqual(lowercase_						,	lowercase_							)
         elif isinstance(lowercase_						,	lowercase_							) and not isinstance(lowercase_						,	lowercase_							):
             self.assertEqual(lowercase_						,	cba.__class__							)
         elif not isinstance(lowercase_						,	lowercase_							) and isinstance(lowercase_						,	lowercase_							):
             self.assertEqual(cba.__class__						,	lowercase_							)
         else:
             self.assertEqual(lowercase_						,	lowercase_							)
 def 					UpperCAmelCase_							(    self				:str						,	lowercase_				:str							)->							str:
     A__    =      ["on_init_end", "on_train_begin"]
     A__    =      0
     A__    =      len(trainer.get_eval_dataloader()							)
     A__    =      ["on_prediction_step"] * len(trainer.get_eval_dataloader()							) + ["on_log", "on_evaluate"]
     for _ in range(trainer.state.num_train_epochs							):
         expected_events.append("on_epoch_begin"							)
         for _ in range(lowercase_							):
             step += 1
             expected_events += ["on_step_begin", "on_step_end"]
             if step % trainer.args.logging_steps == 0:
                 expected_events.append("on_log"							)
             if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
                 expected_events += evaluation_events.copy()
             if step % trainer.args.save_steps == 0:
                 expected_events.append("on_save"							)
         expected_events.append("on_epoch_end"							)
         if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
             expected_events += evaluation_events.copy()
     expected_events += ["on_log", "on_train_end"]
     return expected_events
 def 					UpperCAmelCase_							(    self				:Dict							)->							int:
     A__    =      self.get_trainer()
     A__    =      DEFAULT_CALLBACKS.copy() + [ProgressCallback]
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	lowercase_							)

     # Callbacks passed at init are added to the default callbacks
     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]							)
     expected_callbacks.append(lowercase_							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	lowercase_							)

     # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
     A__    =      self.get_trainer(disable_tqdm=lowercase_							)
     A__    =      DEFAULT_CALLBACKS.copy() + [PrinterCallback]
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	lowercase_							)
 def 					UpperCAmelCase_							(    self				:Tuple							)->							Tuple:
     A__    =      DEFAULT_CALLBACKS.copy() + [ProgressCallback]
     A__    =      self.get_trainer()

     # We can add, pop, or remove by class name
     trainer.remove_callback(lowercase_							)
     expected_callbacks.remove(lowercase_							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	lowercase_							)

     A__    =      self.get_trainer()
     A__    =      trainer.pop_callback(lowercase_							)
     self.assertEqual(cb.__class__						,	lowercase_							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	lowercase_							)

     trainer.add_callback(lowercase_							)
     expected_callbacks.insert(0						,	lowercase_							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	lowercase_							)

     # We can also add, pop, or remove by instance
     A__    =      self.get_trainer()
     A__    =      trainer.callback_handler.callbacks[0]
     trainer.remove_callback(lowercase_							)
     expected_callbacks.remove(lowercase_							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	lowercase_							)

     A__    =      self.get_trainer()
     A__    =      trainer.callback_handler.callbacks[0]
     A__    =      trainer.pop_callback(lowercase_							)
     self.assertEqual(lowercase_						,	lowercase_							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	lowercase_							)

     trainer.add_callback(lowercase_							)
     expected_callbacks.insert(0						,	lowercase_							)
     self.check_callbacks_equality(trainer.callback_handler.callbacks						,	lowercase_							)



 def 					UpperCAmelCase_							(    self				:Dict							)->							List[str]:
     import warnings

     # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
     warnings.simplefilter(action="ignore"						,	category=lowercase_							)

     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]							)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(lowercase_						,	self.get_expected_events(lowercase_							)							)

     # Independent log/save/eval
     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]						,	logging_steps=5							)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(lowercase_						,	self.get_expected_events(lowercase_							)							)

     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]						,	save_steps=5							)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(lowercase_						,	self.get_expected_events(lowercase_							)							)

     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]						,	eval_steps=5						,	evaluation_strategy="steps"							)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(lowercase_						,	self.get_expected_events(lowercase_							)							)

     A__    =      self.get_trainer(callbacks=[MyTestTrainerCallback]						,	evaluation_strategy="epoch"							)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(lowercase_						,	self.get_expected_events(lowercase_							)							)

     # A bit of everything
     A__    =      self.get_trainer(
         callbacks=[MyTestTrainerCallback]						,	logging_steps=3						,	save_steps=10						,	eval_steps=5						,	evaluation_strategy="steps"						,	)
     trainer.train()
     A__    =      trainer.callback_handler.callbacks[-2].events
     self.assertEqual(lowercase_						,	self.get_expected_events(lowercase_							)							)

     # warning should be emitted for duplicated callbacks
     with patch("transformers.trainer_callback.logger.warning"							) as warn_mock:
         A__    =      self.get_trainer(
             callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]						,	)
         assert str(lowercase_							) in warn_mock.call_args[0][0]










'''simple docstring'''

import qiskit





def 						UpperCamelCase    (							_lowerCamelCase : int						,	_lowerCamelCase : int	):
    A__    =      qiskit.Aer.get_backend("aer_simulator"	)

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

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

    # Execute the circuit on the simulator
    A__    =      qiskit.execute(_lowerCamelCase						,	_lowerCamelCase						,	shots=10_00	)

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


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



