




import argparse
import io

import requests
import torch
from omegaconf import OmegaConf

from diffusers import AutoencoderKL
from diffusers.pipelines.stable_diffusion.convert_from_ckpt import (
    assign_to_checkpoint,
    conv_attn_to_linear,
    create_vae_diffusers_config,
    renew_vae_attention_paths,
    renew_vae_resnet_paths,
)





def __UpperCamelCase			(       _A ,  _A  ):
						lowerCAmelCase_      			=  checkpoint

						lowerCAmelCase_      			=  {}

						lowerCAmelCase_      			=  vae_state_dict['''encoder.conv_in.weight''']
						lowerCAmelCase_      			=  vae_state_dict['''encoder.conv_in.bias''']
						lowerCAmelCase_      			=  vae_state_dict['''encoder.conv_out.weight''']
						lowerCAmelCase_      			=  vae_state_dict['''encoder.conv_out.bias''']
						lowerCAmelCase_      			=  vae_state_dict['''encoder.norm_out.weight''']
						lowerCAmelCase_      			=  vae_state_dict['''encoder.norm_out.bias''']

						lowerCAmelCase_      			=  vae_state_dict['''decoder.conv_in.weight''']
						lowerCAmelCase_      			=  vae_state_dict['''decoder.conv_in.bias''']
						lowerCAmelCase_      			=  vae_state_dict['''decoder.conv_out.weight''']
						lowerCAmelCase_      			=  vae_state_dict['''decoder.conv_out.bias''']
						lowerCAmelCase_      			=  vae_state_dict['''decoder.norm_out.weight''']
						lowerCAmelCase_      			=  vae_state_dict['''decoder.norm_out.bias''']

						lowerCAmelCase_      			=  vae_state_dict['''quant_conv.weight''']
						lowerCAmelCase_      			=  vae_state_dict['''quant_conv.bias''']
						lowerCAmelCase_      			=  vae_state_dict['''post_quant_conv.weight''']
						lowerCAmelCase_      			=  vae_state_dict['''post_quant_conv.bias''']

						# Retrieves the keys for the encoder down blocks only
						lowerCAmelCase_      			=  len({'''.'''.join(layer.split('''.'''  )[:3]  ) for layer in vae_state_dict if '''encoder.down''' in layer}  )
						lowerCAmelCase_      			=  {
						    layer_id: [key for key in vae_state_dict if f"down.{layer_id}" in key] for layer_id in range(_A  )
						}

						# Retrieves the keys for the decoder up blocks only
						lowerCAmelCase_      			=  len({'''.'''.join(layer.split('''.'''  )[:3]  ) for layer in vae_state_dict if '''decoder.up''' in layer}  )
						lowerCAmelCase_      			=  {
						    layer_id: [key for key in vae_state_dict if f"up.{layer_id}" in key] for layer_id in range(_A  )
						}

						for i in range(_A  ):
												lowerCAmelCase_      			=  [key for key in down_blocks[i] if f"down.{i}" in key and f"down.{i}.downsample" not in key]

												if f"encoder.down.{i}.downsample.conv.weight" in vae_state_dict:
																		lowerCAmelCase_      			=  vae_state_dict.pop(
																		    f"encoder.down.{i}.downsample.conv.weight"  )
																		lowerCAmelCase_      			=  vae_state_dict.pop(
																		    f"encoder.down.{i}.downsample.conv.bias"  )

												lowerCAmelCase_      			=  renew_vae_resnet_paths(_A  )
												lowerCAmelCase_      			=  {'''old''': f"down.{i}.block", '''new''': f"down_blocks.{i}.resnets"}
												assign_to_checkpoint(_A ,  _A ,  _A ,  additional_replacements=[meta_path] ,  config=_A  )

						lowerCAmelCase_      			=  [key for key in vae_state_dict if '''encoder.mid.block''' in key]
						lowerCAmelCase_      			=  2
						for i in range(1 ,  num_mid_res_blocks + 1  ):
												lowerCAmelCase_      			=  [key for key in mid_resnets if f"encoder.mid.block_{i}" in key]

												lowerCAmelCase_      			=  renew_vae_resnet_paths(_A  )
												lowerCAmelCase_      			=  {'''old''': f"mid.block_{i}", '''new''': f"mid_block.resnets.{i - 1}"}
												assign_to_checkpoint(_A ,  _A ,  _A ,  additional_replacements=[meta_path] ,  config=_A  )

						lowerCAmelCase_      			=  [key for key in vae_state_dict if '''encoder.mid.attn''' in key]
						lowerCAmelCase_      			=  renew_vae_attention_paths(_A  )
						lowerCAmelCase_      			=  {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''}
						assign_to_checkpoint(_A ,  _A ,  _A ,  additional_replacements=[meta_path] ,  config=_A  )
						conv_attn_to_linear(_A  )

						for i in range(_A  ):
												lowerCAmelCase_      			=  num_up_blocks - 1 - i
												lowerCAmelCase_      			=  [
												    key for key in up_blocks[block_id] if f"up.{block_id}" in key and f"up.{block_id}.upsample" not in key
												]

												if f"decoder.up.{block_id}.upsample.conv.weight" in vae_state_dict:
																		lowerCAmelCase_      			=  vae_state_dict[
																		    f"decoder.up.{block_id}.upsample.conv.weight"
																		]
																		lowerCAmelCase_      			=  vae_state_dict[
																		    f"decoder.up.{block_id}.upsample.conv.bias"
																		]

												lowerCAmelCase_      			=  renew_vae_resnet_paths(_A  )
												lowerCAmelCase_      			=  {'''old''': f"up.{block_id}.block", '''new''': f"up_blocks.{i}.resnets"}
												assign_to_checkpoint(_A ,  _A ,  _A ,  additional_replacements=[meta_path] ,  config=_A  )

						lowerCAmelCase_      			=  [key for key in vae_state_dict if '''decoder.mid.block''' in key]
						lowerCAmelCase_      			=  2
						for i in range(1 ,  num_mid_res_blocks + 1  ):
												lowerCAmelCase_      			=  [key for key in mid_resnets if f"decoder.mid.block_{i}" in key]

												lowerCAmelCase_      			=  renew_vae_resnet_paths(_A  )
												lowerCAmelCase_      			=  {'''old''': f"mid.block_{i}", '''new''': f"mid_block.resnets.{i - 1}"}
												assign_to_checkpoint(_A ,  _A ,  _A ,  additional_replacements=[meta_path] ,  config=_A  )

						lowerCAmelCase_      			=  [key for key in vae_state_dict if '''decoder.mid.attn''' in key]
						lowerCAmelCase_      			=  renew_vae_attention_paths(_A  )
						lowerCAmelCase_      			=  {'''old''': '''mid.attn_1''', '''new''': '''mid_block.attentions.0'''}
						assign_to_checkpoint(_A ,  _A ,  _A ,  additional_replacements=[meta_path] ,  config=_A  )
						conv_attn_to_linear(_A  )
						return new_checkpoint





def __UpperCamelCase			(       _A ,  _A ,  ):
						# Only support V1
						lowerCAmelCase_      			=  requests.get(
						    ''' https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml'''  )
						lowerCAmelCase_      			=  io.BytesIO(r.content  )

						lowerCAmelCase_      			=  OmegaConf.load(_A  )
						lowerCAmelCase_      			=  512
						lowerCAmelCase_      			=  '''cuda''' if torch.cuda.is_available() else '''cpu'''
						if checkpoint_path.endswith('''safetensors'''  ):
												from safetensors import safe_open

												lowerCAmelCase_      			=  {}
												with safe_open(_A ,  framework='''pt''' ,  device='''cpu'''  ) as f:
																		for key in f.keys():
																								lowerCAmelCase_      			=  f.get_tensor(_A  )
						else:
												lowerCAmelCase_      			=  torch.load(_A ,  map_location=_A  )['''state_dict''']

						# Convert the VAE model.
						lowerCAmelCase_      			=  create_vae_diffusers_config(_A ,  image_size=_A  )
						lowerCAmelCase_      			=  custom_convert_ldm_vae_checkpoint(_A ,  _A  )

						lowerCAmelCase_      			=  AutoencoderKL(**_A  )
						vae.load_state_dict(_A  )
						vae.save_pretrained(_A  )


if __name__ == "__main__":
						_A     =			argparse.ArgumentParser()

						parser.add_argument('''--vae_pt_path''', default=None, type=str, required=True, help='''Path to the VAE.pt to convert.''')
						parser.add_argument('''--dump_path''', default=None, type=str, required=True, help='''Path to the VAE.pt to convert.''')

						_A     =			parser.parse_args()

						vae_pt_to_vae_diffuser(args.vae_pt_path, args.dump_path)










import math

import numpy as np
import qiskit
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute





def __UpperCamelCase			(       _A = 3  ):
						if isinstance(_A ,  _A  ):
												raise TypeError('''number of qubits must be a integer.'''  )
						if number_of_qubits <= 0:
												raise ValueError('''number of qubits must be > 0.'''  )
						if math.floor(_A  ) != number_of_qubits:
												raise ValueError('''number of qubits must be exact integer.'''  )
						if number_of_qubits > 10:
												raise ValueError('''number of qubits too large to simulate(>10).'''  )

						lowerCAmelCase_      			=  QuantumRegister(_A ,  '''qr'''  )
						lowerCAmelCase_      			=  ClassicalRegister(_A ,  '''cr'''  )

						lowerCAmelCase_      			=  QuantumCircuit(_A ,  _A  )

						lowerCAmelCase_      			=  number_of_qubits

						for i in range(_A  ):
												quantum_circuit.h(number_of_qubits - i - 1  )
												counter -= 1
												for j in range(_A  ):
																		quantum_circuit.cp(np.pi / 2 ** (counter - j) ,  _A ,  _A  )

						for k in range(number_of_qubits // 2  ):
												quantum_circuit.swap(_A ,  number_of_qubits - k - 1  )

						# measure all the qubits
						quantum_circuit.measure(_A ,  _A  )
						# simulate with 10000 shots
						lowerCAmelCase_      			=  Aer.get_backend('''qasm_simulator'''  )
						lowerCAmelCase_      			=  execute(_A ,  _A ,  shots=10000  )

						return job.result().get_counts(_A  )


if __name__ == "__main__":
						print(
						    f"Total count for quantum fourier transform state is: \
    {quantum_fourier_transform(3)}"
						)




