
import math
import os
from copy import deepcopy

import datasets
import evaluate
import torch
import transformers
from datasets import load_dataset
from torch.utils.data import DataLoader
from transformers import AutoModelForSequenceClassification, AutoTokenizer

from accelerate import Accelerator
from accelerate.test_utils import RegressionDataset, RegressionModel
from accelerate.utils import is_tpu_available, set_seed


__lowerCamelCase				=			"""true"""


def      UpperCamelCase   (       __lowerCamelCase	:					str							,   __lowerCamelCase	:					int=82							,   __lowerCamelCase	:					Any=16       ):
       set_seed(42       )
       snake_case			:  List[Any]     							= RegressionModel()
       snake_case			:  Union[str, Any]     							= deepcopy(__lowerCamelCase       )
       snake_case			:  str     							= RegressionDataset(length=__lowerCamelCase       )
       snake_case			:  str     							= DataLoader(__lowerCamelCase							,   batch_size=__lowerCamelCase       )
       model.to(accelerator.device       )
       snake_case							,   snake_case			:  str     							= accelerator.prepare(__lowerCamelCase							,   __lowerCamelCase       )
       return model, ddp_model, dataloader


def      UpperCamelCase   (       __lowerCamelCase	:					Accelerator							,   __lowerCamelCase	:					List[Any]=False       ):
       snake_case			:  List[str]     							= AutoTokenizer.from_pretrained("hf-internal-testing/mrpc-bert-base-cased"       )
       snake_case			:  List[Any]     							= load_dataset("glue"							,   "mrpc"							,   split="validation"       )

       def tokenize_function(__lowerCamelCase	:					Optional[int]       ):
              snake_case			:  str     							= tokenizer(examples["sentence1"]							,   examples["sentence2"]							,   truncation=__lowerCamelCase							,   max_length=__lowerCamelCase       )
              return outputs

       with accelerator.main_process_first():
              snake_case			:  List[Any]     							= dataset.map(
                  __lowerCamelCase							,   batched=__lowerCamelCase							,   remove_columns=["idx", "sentence1", "sentence2"]							,   )

       snake_case			:  Tuple     							= tokenized_datasets.rename_column("label"							,   "labels"       )

       def collate_fn(__lowerCamelCase	:					List[Any]       ):
              if use_longest:
                     return tokenizer.pad(__lowerCamelCase							,   padding="longest"							,   return_tensors="pt"       )
              return tokenizer.pad(__lowerCamelCase							,   padding="max_length"							,   max_length=128							,   return_tensors="pt"       )

       return DataLoader(__lowerCamelCase							,   shuffle=__lowerCamelCase							,   collate_fn=__lowerCamelCase							,   batch_size=16       )


def      UpperCamelCase   (       __lowerCamelCase	:					List[Any]							,   __lowerCamelCase	:					Any       ):
       snake_case			:  List[Any]     							= Accelerator(dispatch_batches=__lowerCamelCase							,   split_batches=__lowerCamelCase       )
       snake_case			:  Union[str, Any]     							= get_dataloader(__lowerCamelCase							,   not dispatch_batches       )
       snake_case			:  Any     							= AutoModelForSequenceClassification.from_pretrained(
           "hf-internal-testing/mrpc-bert-base-cased"							,   return_dict=__lowerCamelCase       )
       snake_case							,   snake_case			:  Any     							= accelerator.prepare(__lowerCamelCase							,   __lowerCamelCase       )
       return {"ddp": [ddp_model, ddp_dataloader, "cuda:0"], "no": [model, dataloader, accelerator.device]}, accelerator


def      UpperCamelCase   (       __lowerCamelCase	:					Dict							,   __lowerCamelCase	:					Union[str, Any]							,   __lowerCamelCase	:					Optional[int]       ):
       snake_case			:  List[Any]     							= []
       for batch in dataloader:
              snake_case							,   snake_case			:  Dict     							= batch.values()
              with torch.no_grad():
                     snake_case			:  List[Any]     							= model(__lowerCamelCase       )
                     snake_case							,   snake_case			:  Dict     							= accelerator.gather_for_metrics((logit, target)       )
                     logits_and_targets.append((logit, target)       )
       snake_case							,   snake_case			:  Dict     							= [], []
       for logit, targ in logits_and_targets:
              logits.append(__lowerCamelCase       )
              targs.append(__lowerCamelCase       )
       snake_case							,   snake_case			:  str     							= torch.cat(__lowerCamelCase       ), torch.cat(__lowerCamelCase       )
       return logits, targs


def      UpperCamelCase   (       __lowerCamelCase	:					Accelerator							,   __lowerCamelCase	:					Tuple=82							,   __lowerCamelCase	:					int=False							,   __lowerCamelCase	:					List[str]=False							,   __lowerCamelCase	:					List[str]=16       ):
       snake_case							,   snake_case							,   snake_case			:  str     							= get_basic_setup(__lowerCamelCase							,   __lowerCamelCase							,   __lowerCamelCase       )
       snake_case							,   snake_case			:  Optional[int]     							= generate_predictions(__lowerCamelCase							,   __lowerCamelCase							,   __lowerCamelCase       )
       assert (
           len(__lowerCamelCase       ) == num_samples
       ), f"""Unexpected number of inputs:\n    Expected: {num_samples}\n    Actual: {len(__lowerCamelCase       )}"""


def      UpperCamelCase   (       __lowerCamelCase	:					bool = False							,   __lowerCamelCase	:					bool = False       ):
       snake_case			:  Any     							= evaluate.load("glue"							,   "mrpc"       )
       snake_case							,   snake_case			:  str     							= get_mrpc_setup(__lowerCamelCase							,   __lowerCamelCase       )
       # First do baseline
       snake_case							,   snake_case							,   snake_case			:  List[Any]     							= setup["no"]
       model.to(__lowerCamelCase       )
       model.eval()
       for batch in dataloader:
              batch.to(__lowerCamelCase       )
              with torch.inference_mode():
                     snake_case			:  Optional[int]     							= model(**__lowerCamelCase       )
              snake_case			:  Optional[Any]     							= outputs.logits.argmax(dim=-1       )
              metric.add_batch(predictions=__lowerCamelCase							,   references=batch["labels"]       )
       snake_case			:  int     							= metric.compute()

       # Then do distributed
       snake_case							,   snake_case							,   snake_case			:  Union[str, Any]     							= setup["ddp"]
       model.eval()
       for batch in dataloader:
              with torch.inference_mode():
                     snake_case			:  Optional[int]     							= model(**__lowerCamelCase       )
              snake_case			:  Optional[Any]     							= outputs.logits.argmax(dim=-1       )
              snake_case			:  str     							= batch["labels"]
              snake_case							,   snake_case			:  Union[str, Any]     							= accelerator.gather_for_metrics((preds, references)       )
              metric.add_batch(predictions=__lowerCamelCase							,   references=__lowerCamelCase       )
       snake_case			:  Optional[Any]     							= metric.compute()

       for key in "accuracy f1".split():
              assert math.isclose(
                  baseline[key]							,   distributed[key]       ), f"""Baseline and Distributed are not the same for key {key}:\n\tBaseline: {baseline[key]}\n\tDistributed: {distributed[key]}\n"""


def      UpperCamelCase   (       ):
       snake_case			:  List[str]     							= Accelerator(split_batches=__lowerCamelCase							,   dispatch_batches=__lowerCamelCase       )
       if accelerator.is_local_main_process:
              datasets.utils.logging.set_verbosity_warning()
              transformers.utils.logging.set_verbosity_warning()
       else:
              datasets.utils.logging.set_verbosity_error()
              transformers.utils.logging.set_verbosity_error()
       # These are a bit slower so they should only be ran on the GPU or TPU
       if torch.cuda.is_available() or is_tpu_available():
              if accelerator.is_local_main_process:
                     print("**Testing gather_for_metrics**"       )
              for split_batches in [True, False]:
                     for dispatch_batches in [True, False]:
                            if accelerator.is_local_main_process:
                                   print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`"""       )
                            test_mrpc(__lowerCamelCase							,   __lowerCamelCase       )
                            accelerator.state._reset_state()
       if accelerator.is_local_main_process:
              print("**Test torch metrics**"       )
       for split_batches in [True, False]:
              for dispatch_batches in [True, False]:
                     snake_case			:  Dict     							= Accelerator(split_batches=__lowerCamelCase							,   dispatch_batches=__lowerCamelCase       )
                     if accelerator.is_local_main_process:
                            print(f"""With: `split_batches={split_batches}`, `dispatch_batches={dispatch_batches}`, length=99"""       )
                     test_torch_metrics(__lowerCamelCase							,   99       )
                     accelerator.state._reset_state()
       if accelerator.is_local_main_process:
              print("**Test last batch is not dropped when perfectly divisible**"       )
       snake_case			:  str     							= Accelerator()
       test_torch_metrics(__lowerCamelCase							,   512       )
       accelerator.state._reset_state()


def      UpperCamelCase   (       __lowerCamelCase	:					Any       ):
       # For xla_spawn (TPUs)
       main()


if __name__ == "__main__":
   main()







import qiskit


def      UpperCamelCase   (       __lowerCamelCase	:					int							,   __lowerCamelCase	:					int       ):
       snake_case			:  int     							= qiskit.Aer.get_backend("aer_simulator"       )

       # Create a Quantum Circuit acting on the q register
       snake_case			:  Dict     							= qiskit.QuantumCircuit(__lowerCamelCase							,   __lowerCamelCase       )

       # Apply X (NOT) Gate to Qubits 0 & 1
       circuit.x(0       )
       circuit.x(1       )

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

       # Execute the circuit on the qasm simulator
       snake_case			:  Dict     							= qiskit.execute(__lowerCamelCase							,   __lowerCamelCase							,   shots=1000       )

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


if __name__ == "__main__":
   __lowerCamelCase				=			single_qubit_measure(2, 2)
   print(F'Total count for various states are: {counts}')





