



import argparse
import os

import torch

from transformers import FlavaConfig, FlavaForPreTraining
from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint

def        a						(      _UpperCAmelCase							:					List[str]						):







		'''simple docstring'''





		return sum(param.float().sum() if '''encoder.embeddings''' not in key else 0 for key, param in state_dict.items()						)

def        a						(      _UpperCAmelCase							:					str ,   _UpperCAmelCase							:					Optional[Any]						):







		'''simple docstring'''





		__UpperCAmelCase    :     Tuple													=	{}

		for key, value in state_dict.items():
				if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key:
						continue

				__UpperCAmelCase    :     Optional[Any]													=	key.replace('''heads.cmd.mim_head.cls.predictions''' ,   '''mmm_image_head'''						)
				__UpperCAmelCase    :     Union[str, Any]													=	key.replace('''heads.cmd.mlm_head.cls.predictions''' ,   '''mmm_text_head'''						)
				__UpperCAmelCase    :     Optional[int]													=	key.replace('''heads.cmd.itm_head.cls''' ,   '''itm_head'''						)
				__UpperCAmelCase    :     Tuple													=	key.replace('''heads.cmd.itm_head.pooler''' ,   '''itm_head.pooler'''						)
				__UpperCAmelCase    :     List[Any]													=	key.replace('''heads.cmd.clip_head.logit_scale''' ,   '''flava.logit_scale'''						)
				__UpperCAmelCase    :     str													=	key.replace('''heads.fairseq_mlm.cls.predictions''' ,   '''mlm_head'''						)
				__UpperCAmelCase    :     Optional[Any]													=	key.replace('''heads.imagenet.mim_head.cls.predictions''' ,   '''mim_head'''						)
				__UpperCAmelCase    :     List[Any]													=	key.replace('''mm_text_projection''' ,   '''flava.text_to_mm_projection'''						)
				__UpperCAmelCase    :     List[str]													=	key.replace('''mm_image_projection''' ,   '''flava.image_to_mm_projection'''						)
				__UpperCAmelCase    :     Optional[int]													=	key.replace('''image_encoder.module''' ,   '''flava.image_model'''						)
				__UpperCAmelCase    :     Dict													=	key.replace('''text_encoder.module''' ,   '''flava.text_model'''						)
				__UpperCAmelCase    :     List[Any]													=	key.replace('''mm_encoder.module.encoder.cls_token''' ,   '''flava.multimodal_model.cls_token'''						)
				__UpperCAmelCase    :     List[str]													=	key.replace('''mm_encoder.module''' ,   '''flava.multimodal_model'''						)
				__UpperCAmelCase    :     str													=	key.replace('''text_projection''' ,   '''flava.text_projection'''						)
				__UpperCAmelCase    :     List[str]													=	key.replace('''image_projection''' ,   '''flava.image_projection'''						)

				__UpperCAmelCase    :     Tuple													=	value.float()

		for key, value in codebook_state_dict.items():
				__UpperCAmelCase    :     List[str]													=	value

		return upgrade

@torch.no_grad()
def        a						(      _UpperCAmelCase							:					int ,   _UpperCAmelCase							:					str ,   _UpperCAmelCase							:					Optional[Any] ,   _UpperCAmelCase							:					Union[str, Any]=None						):







		'''simple docstring'''





		if config_path is not None:
				__UpperCAmelCase    :     int													=	FlavaConfig.from_pretrained(_UpperCAmelCase						)
		else:
				__UpperCAmelCase    :     int													=	FlavaConfig()

		__UpperCAmelCase    :     List[Any]													=	FlavaForPreTraining(_UpperCAmelCase						).eval()

		__UpperCAmelCase    :     List[Any]													=	convert_dalle_checkpoint(_UpperCAmelCase ,   _UpperCAmelCase ,   save_checkpoint=_UpperCAmelCase						)

		if os.path.exists(_UpperCAmelCase						):
				__UpperCAmelCase    :     List[str]													=	torch.load(_UpperCAmelCase ,   map_location='''cpu'''						)
		else:
				__UpperCAmelCase    :     List[str]													=	torch.hub.load_state_dict_from_url(_UpperCAmelCase ,   map_location='''cpu'''						)

		__UpperCAmelCase    :     int													=	upgrade_state_dict(_UpperCAmelCase ,   _UpperCAmelCase						)
		hf_model.load_state_dict(_UpperCAmelCase						)
		__UpperCAmelCase    :     List[Any]													=	hf_model.state_dict()
		__UpperCAmelCase    :     str													=	count_parameters(_UpperCAmelCase						)
		__UpperCAmelCase    :     List[Any]													=	count_parameters(_UpperCAmelCase						) + count_parameters(_UpperCAmelCase						)

		assert torch.allclose(_UpperCAmelCase ,   _UpperCAmelCase ,   atol=1e-3						)

		hf_model.save_pretrained(_UpperCAmelCase						)


if __name__ == "__main__":
				__A   			=argparse.ArgumentParser()
				parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
				parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to flava checkpoint")
				parser.add_argument("--codebook_path", default=None, type=str, help="Path to flava codebook checkpoint")
				parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert")
				__A   			=parser.parse_args()

				convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)










import qiskit

def        a						(      _UpperCAmelCase							:					int ,   _UpperCAmelCase							:					int						):







		'''simple docstring'''





		__UpperCAmelCase    :     List[str]													=	qiskit.Aer.get_backend('''aer_simulator'''						)

		__UpperCAmelCase    :     Optional[Any]													=	qiskit.QuantumCircuit(4 ,   2						)
		# encode inputs in qubits 0 and 1
		if bita == 1:
				qc_ha.x(0						)
		if bita == 1:
				qc_ha.x(1						)
		qc_ha.barrier()

		# use cnots to write XOR of the inputs on qubit2
		qc_ha.cx(0 ,   2						)
		qc_ha.cx(1 ,   2						)

		# use ccx / toffoli gate to write AND of the inputs on qubit3
		qc_ha.ccx(0 ,   1 ,   3						)
		qc_ha.barrier()

		# extract outputs
		qc_ha.measure(2 ,   0						)  # extract XOR value
		qc_ha.measure(3 ,   1						)  # extract AND value

		# Execute the circuit on the qasm simulator
		__UpperCAmelCase    :     str													=	qiskit.execute(_UpperCAmelCase ,   _UpperCAmelCase ,   shots=10_00						)

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


if __name__ == "__main__":
				__A   			=half_adder(1, 1)
				print(f'''Half Adder Output Qubit Counts: {counts}''')





