






import re
from typing import Callable, List, Optional, Union

import tensorflow as tf


try:
							from tensorflow.keras.optimizers.legacy import Adam
except ImportError:
							from tensorflow.keras.optimizers import Adam




class 		UpperCAmelCase_						(	tf.keras.optimizers.schedules.LearningRateSchedule					):


						'''simple docstring'''




						def __init__(	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE = 1.0		,				_SCREAMING_SNAKE_CASE = None		,				) ->				List[str]:
								super().__init__()
								snake_case_				:							Union[str, Any]						 =       initial_learning_rate
								snake_case_				:							Optional[Any]						 =       warmup_steps
								snake_case_				:							Optional[int]						 =       power
								snake_case_				:							Tuple						 =       decay_schedule_fn
								snake_case_				:							Union[str, Any]						 =       name






						def __call__(	self		,				_SCREAMING_SNAKE_CASE				) ->				str:
								with tf.name_scope(self.name or "WarmUp"				) as name:
										# Implements polynomial warmup. i.e., if global_step < warmup_steps, the
										# learning rate will be `global_step/num_warmup_steps * init_lr`.
										snake_case_				:							Union[str, Any]						 =       tf.cast(_SCREAMING_SNAKE_CASE		,				tf.floataa				)
										snake_case_				:							Any						 =       tf.cast(self.warmup_steps		,				tf.floataa				)
										snake_case_				:							List[Any]						 =       global_step_float / warmup_steps_float
										snake_case_				:							Optional[int]						 =       self.initial_learning_rate * tf.math.pow(_SCREAMING_SNAKE_CASE		,				self.power				)
										return tf.cond(
										    global_step_float < warmup_steps_float		,				lambda: warmup_learning_rate		,				lambda: self.decay_schedule_fn(step - self.warmup_steps				)		,				name=_SCREAMING_SNAKE_CASE		,				)






						def       _lowerCAmelCase       (	self				) ->				str:
								return {
								    "initial_learning_rate": self.initial_learning_rate,
								    "decay_schedule_fn": self.decay_schedule_fn,
								    "warmup_steps": self.warmup_steps,
								    "power": self.power,
								    "name": self.name,
								}




def      lowerCAmelCase__	(    _a       :			float  ,      _a       :			int  ,      _a       :			int  ,      _a       :			float = 0.0  ,      _a       :			float = 0.9  ,      _a       :			float = 0.999  ,      _a       :			float = 1E-8  ,      _a       :			Optional[float] = None  ,      _a       :			Optional[float] = None  ,      _a       :			float = 0.0  ,      _a       :			float = 1.0  ,      _a       :			Optional[List[str]] = None  ,      ):
		snake_case_				:							Union[str, Any]						 =       tf.keras.optimizers.schedules.PolynomialDecay(
		    initial_learning_rate=_a  ,      decay_steps=num_train_steps - num_warmup_steps  ,      end_learning_rate=init_lr * min_lr_ratio  ,      power=_a  ,      )
		if num_warmup_steps:
				snake_case_				:							Optional[int]						 =       WarmUp(
				    initial_learning_rate=_a  ,      decay_schedule_fn=_a  ,      warmup_steps=_a  ,      )
		if weight_decay_rate > 0.0:
				snake_case_				:							str						 =       AdamWeightDecay(
				    learning_rate=_a  ,      weight_decay_rate=_a  ,      beta_a=_a  ,      beta_a=_a  ,      epsilon=_a  ,      clipnorm=_a  ,      global_clipnorm=_a  ,      exclude_from_weight_decay=["LayerNorm", "layer_norm", "bias"]  ,      include_in_weight_decay=_a  ,      )
		else:
				snake_case_				:							Tuple						 =       tf.keras.optimizers.Adam(
				    learning_rate=_a  ,      beta_a=_a  ,      beta_a=_a  ,      epsilon=_a  ,      clipnorm=_a  ,      global_clipnorm=_a  ,      )
		# We return the optimizer and the LR scheduler in order to better track the
		# evolution of the LR independently of the optimizer.
		return optimizer, lr_schedule




class 		UpperCAmelCase_						(	SCREAMING_SNAKE_CASE__					):


						'''simple docstring'''




						def __init__(	self		,				_SCREAMING_SNAKE_CASE = 0.001		,				_SCREAMING_SNAKE_CASE = 0.9		,				_SCREAMING_SNAKE_CASE = 0.999		,				_SCREAMING_SNAKE_CASE = 1e-7		,				_SCREAMING_SNAKE_CASE = False		,				_SCREAMING_SNAKE_CASE = 0.0		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = None		,				_SCREAMING_SNAKE_CASE = "AdamWeightDecay"		,				**_SCREAMING_SNAKE_CASE		,				) ->				Optional[int]:
								super().__init__(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				**_SCREAMING_SNAKE_CASE				)
								snake_case_				:							List[Any]						 =       weight_decay_rate
								snake_case_				:							Optional[Any]						 =       include_in_weight_decay
								snake_case_				:							Any						 =       exclude_from_weight_decay






						@classmethod
						def       _lowerCAmelCase       (	cls		,				_SCREAMING_SNAKE_CASE				) ->				Optional[Any]:
								snake_case_				:							List[Any]						 =       {"WarmUp": WarmUp}
								return super(_SCREAMING_SNAKE_CASE		,				cls				).from_config(_SCREAMING_SNAKE_CASE		,				custom_objects=_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				) ->				Dict:
								super(_SCREAMING_SNAKE_CASE		,				self				)._prepare_local(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)
								snake_case_				:							List[Any]						 =       tf.constant(
								    self.weight_decay_rate		,				name="adam_weight_decay_rate"				)






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				) ->				int:
								snake_case_				:							Optional[int]						 =       self._do_use_weight_decay(var.name				)
								if do_decay:
										return var.assign_sub(
										    learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]["weight_decay_rate"]		,				use_locking=self._use_locking		,				)
								return tf.no_op()






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE=None		,				**_SCREAMING_SNAKE_CASE				) ->				Optional[int]:
								snake_case_ ,       snake_case_				:							Optional[int]						 =       list(zip(*_SCREAMING_SNAKE_CASE				)				)
								return super(_SCREAMING_SNAKE_CASE		,				self				).apply_gradients(zip(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)		,				name=_SCREAMING_SNAKE_CASE		,				**_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				) ->				int:
								if apply_state is None:
										return self._decayed_lr_t[var_dtype], {}

								snake_case_				:							str						 =       apply_state or {}
								snake_case_				:							Union[str, Any]						 =       apply_state.get((var_device, var_dtype)				)
								if coefficients is None:
										snake_case_				:							str						 =       self._fallback_apply_state(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)
										snake_case_				:							Optional[int]						 =       coefficients

								return coefficients["lr_t"], {"apply_state": apply_state}






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE=None				) ->				Tuple:
								snake_case_ ,       snake_case_				:							Optional[int]						 =       self._get_lr(var.device		,				var.dtype.base_dtype		,				_SCREAMING_SNAKE_CASE				)
								snake_case_				:							Dict						 =       self._decay_weights_op(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)
								with tf.control_dependencies([decay]				):
										return super(_SCREAMING_SNAKE_CASE		,				self				)._resource_apply_dense(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				**_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE=None				) ->				Union[str, Any]:
								snake_case_ ,       snake_case_				:							str						 =       self._get_lr(var.device		,				var.dtype.base_dtype		,				_SCREAMING_SNAKE_CASE				)
								snake_case_				:							Any						 =       self._decay_weights_op(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				)
								with tf.control_dependencies([decay]				):
										return super(_SCREAMING_SNAKE_CASE		,				self				)._resource_apply_sparse(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				**_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				Dict:
								snake_case_				:							List[str]						 =       super().get_config()
								config.update({"weight_decay_rate": self.weight_decay_rate}				)
								return config






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE				) ->				Optional[int]:
								if self.weight_decay_rate == 0:
										return False

								if self._include_in_weight_decay:
										for r in self._include_in_weight_decay:
												if re.search(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				) is not None:
														return True

								if self._exclude_from_weight_decay:
										for r in self._exclude_from_weight_decay:
												if re.search(_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				) is not None:
														return False
								return True




class 		UpperCAmelCase_						(	SCREAMING_SNAKE_CASE__					):


						'''simple docstring'''




						def __init__(	self				) ->				int:
								snake_case_				:							str						 =       []
								snake_case_				:							str						 =       None






						@property
						def       _lowerCAmelCase       (	self				) ->				Any:
								if self._accum_steps is None:
										snake_case_				:							Optional[int]						 =       tf.Variable(
										    tf.constant(0		,				dtype=tf.intaa				)		,				trainable=_SCREAMING_SNAKE_CASE		,				synchronization=tf.VariableSynchronization.ON_READ		,				aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA		,				)

								return self._accum_steps.value()






						@property
						def       _lowerCAmelCase       (	self				) ->				Dict:
								if not self._gradients:
										raise ValueError("The accumulator should be called first to initialize the gradients"				)
								return [gradient.value() if gradient is not None else gradient for gradient in self._gradients]






						def __call__(	self		,				_SCREAMING_SNAKE_CASE				) ->				int:
								if not self._gradients:
										snake_case_				:							Optional[Any]						 =       self.step  # Create the step variable.
										self._gradients.extend(
										    [
										        tf.Variable(
										            tf.zeros_like(_SCREAMING_SNAKE_CASE				)		,				trainable=_SCREAMING_SNAKE_CASE		,				synchronization=tf.VariableSynchronization.ON_READ		,				aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA		,				)
										        if gradient is not None
										        else gradient
										        for gradient in gradients
										    ]				)
								if len(_SCREAMING_SNAKE_CASE				) != len(self._gradients				):
										raise ValueError(f'''Expected {len(self._gradients				)} gradients, but got {len(_SCREAMING_SNAKE_CASE				)}'''				)

								for accum_gradient, gradient in zip(self._gradients		,				_SCREAMING_SNAKE_CASE				):
										if accum_gradient is not None and gradient is not None:
												accum_gradient.assign_add(_SCREAMING_SNAKE_CASE				)

								self._accum_steps.assign_add(1				)






						def       _lowerCAmelCase       (	self				) ->				Union[str, Any]:
								if not self._gradients:
										return
								self._accum_steps.assign(0				)
								for gradient in self._gradients:
										if gradient is not None:
												gradient.assign(tf.zeros_like(_SCREAMING_SNAKE_CASE				)				)








import qiskit

def      lowerCAmelCase__	(    _a       :			int  ,      _a       :			int    ):
		snake_case_				:							Tuple						 =       qiskit.Aer.get_backend("aer_simulator"    )

		# Create a Quantum Circuit acting on the q register
		snake_case_				:							Any						 =       qiskit.QuantumCircuit(_a  ,      _a    )

		# 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_				:							Any						 =       qiskit.execute(_a  ,      _a  ,      shots=10_00    )

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


if __name__ == "__main__":
							lowercase  :							List[Any]			  =    single_qubit_measure(2, 2)
							print(F"""Total count for various states are: {counts}""")
