





import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModel, PreTrainedModel

from ...utils import logging


UpperCAmelCase_							=				logging.get_logger(__name__)





def  __magic_name__  (				lowercase				,       lowercase				)				-> int:

							"""simple docstring"""



							lowercase_	:   Optional[int]  				=     nn.functional.normalize(lowercase				)
							lowercase_	:   Optional[int]  				=     nn.functional.normalize(lowercase				)
							return torch.mm(lowercase				,       normalized_text_embeds.t()				)




class     UpperCamelCase__					(     lowerCamelCase__				):






		'''simple docstring'''






		__a       :       Optional[Any]       =	CLIPConfig

		__a       :       Tuple       =	["""CLIPEncoderLayer"""]






		def __init__(							self,   snake_case__  )       -> int:






									"""simple docstring"""
									super().__init__(snake_case__  )

									lowercase_	:   str  				=     CLIPVisionModel(config.vision_config  )
									lowercase_	:   Optional[Any]  				=     nn.Linear(config.vision_config.hidden_size,   config.projection_dim,   bias=snake_case__  )

									lowercase_	:   int  				=     nn.Parameter(torch.ones(17,   config.projection_dim  ),   requires_grad=snake_case__  )
									lowercase_	:   Any  				=     nn.Parameter(torch.ones(3,   config.projection_dim  ),   requires_grad=snake_case__  )

									lowercase_	:   str  				=     nn.Parameter(torch.ones(17  ),   requires_grad=snake_case__  )
									lowercase_	:   int  				=     nn.Parameter(torch.ones(3  ),   requires_grad=snake_case__  )






		@torch.no_grad()
		def 						snake_case__				(							self,   snake_case__,   snake_case__  )       -> int:






									"""simple docstring"""
									lowercase_	:   Any  				=     self.vision_model(snake_case__  )[1]  # pooled_output
									lowercase_	:   Tuple  				=     self.visual_projection(snake_case__  )

									# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
									lowercase_	:   Any  				=     cosine_distance(snake_case__,   self.special_care_embeds  ).cpu().float().numpy()
									lowercase_	:   List[str]  				=     cosine_distance(snake_case__,   self.concept_embeds  ).cpu().float().numpy()

									lowercase_	:   List[Any]  				=     []
									lowercase_	:   Union[str, Any]  				=     image_embeds.shape[0]
									for i in range(snake_case__  ):
																lowercase_	:   int  				=     {"""special_scores""": {}, """special_care""": [], """concept_scores""": {}, """bad_concepts""": []}

																# increase this value to create a stronger `nfsw` filter
																# at the cost of increasing the possibility of filtering benign images
																lowercase_	:   Optional[Any]  				=     0.0

																for concept_idx in range(len(special_cos_dist[0]  )  ):
																							lowercase_	:   Optional[Any]  				=     special_cos_dist[i][concept_idx]
																							lowercase_	:   Union[str, Any]  				=     self.special_care_embeds_weights[concept_idx].item()
																							lowercase_	:   Optional[Any]  				=     round(concept_cos - concept_threshold + adjustment,   3  )
																							if result_img["special_scores"][concept_idx] > 0:
																														result_img["special_care"].append({concept_idx, result_img["""special_scores"""][concept_idx]}  )
																														lowercase_	:   int  				=     0.01

																for concept_idx in range(len(cos_dist[0]  )  ):
																							lowercase_	:   Optional[int]  				=     cos_dist[i][concept_idx]
																							lowercase_	:   Union[str, Any]  				=     self.concept_embeds_weights[concept_idx].item()
																							lowercase_	:   Optional[Any]  				=     round(concept_cos - concept_threshold + adjustment,   3  )
																							if result_img["concept_scores"][concept_idx] > 0:
																														result_img["bad_concepts"].append(snake_case__  )

																result.append(snake_case__  )

									lowercase_	:   Dict  				=     [len(res["""bad_concepts"""]  ) > 0 for res in result]

									return images, has_nsfw_concepts






		@torch.no_grad()
		def 						snake_case__				(							self,   snake_case__,   snake_case__  )       -> str:






									"""simple docstring"""
									lowercase_	:   Optional[Any]  				=     self.vision_model(snake_case__  )[1]  # pooled_output
									lowercase_	:   Any  				=     self.visual_projection(snake_case__  )

									lowercase_	:   int  				=     cosine_distance(snake_case__,   self.special_care_embeds  )
									lowercase_	:   List[Any]  				=     cosine_distance(snake_case__,   self.concept_embeds  )

									# increase this value to create a stronger `nsfw` filter
									# at the cost of increasing the possibility of filtering benign images
									lowercase_	:   Dict  				=     0.0

									lowercase_	:   Optional[Any]  				=     special_cos_dist - self.special_care_embeds_weights + adjustment
									# special_scores = special_scores.round(decimals=3)
									lowercase_	:   Dict  				=     torch.any(special_scores > 0,   dim=1  )
									lowercase_	:   str  				=     special_care * 0.01
									lowercase_	:   Any  				=     special_adjustment.unsqueeze(1  ).expand(-1,   cos_dist.shape[1]  )

									lowercase_	:   Union[str, Any]  				=     (cos_dist - self.concept_embeds_weights) + special_adjustment
									# concept_scores = concept_scores.round(decimals=3)
									lowercase_	:   str  				=     torch.any(concept_scores > 0,   dim=1  )

									return images, has_nsfw_concepts






import qiskit





def  __magic_name__  (				lowercase				,       lowercase				)				-> qiskit.result.counts.Counts:

							"""simple docstring"""



							lowercase_	:   Dict  				=     qiskit.Aer.get_backend("""aer_simulator"""				)

							# Create a Quantum Circuit acting on the q register
							lowercase_	:   str  				=     qiskit.QuantumCircuit(lowercase				,       lowercase				)

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

							# Execute the circuit on the simulator
							lowercase_	:   List[Any]  				=     qiskit.execute(lowercase				,       lowercase				,       shots=1000				)

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


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