





'''simple docstring'''




import argparse
import os

import torch

from transformers import FlavaImageCodebook, FlavaImageCodebookConfig







def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase    )->     Any:

 '''simple docstring'''





 __UpperCAmelCase					=   s.rsplit(_lowerCAmelCase       ,    _lowerCAmelCase    )
 return new.join(_lowerCAmelCase    )







def _lowerCAmelCase     (      _lowerCAmelCase    )->     Tuple:

 '''simple docstring'''





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







def _lowerCAmelCase     (      _lowerCAmelCase    )->     Optional[Any]:

 '''simple docstring'''





 __UpperCAmelCase					=   {}

 __UpperCAmelCase					=   ['group_1', 'group_2', 'group_3', 'group_4']
 for key, value in state_dict.items():
  for group_key in group_keys:
   if group_key in key:
    __UpperCAmelCase					=   key.replace(F'{group_key}.'       ,    F'{group_key}.group.'    )

  if "res_path" in key:
   __UpperCAmelCase					=   key.replace('res_path.'       ,    'res_path.path.'    )

  if key.endswith('.w'    ):
   __UpperCAmelCase					=   rreplace(_lowerCAmelCase       ,    '.w'       ,    '.weight'       ,    1    )
  if key.endswith('.b'    ):
   __UpperCAmelCase					=   rreplace(_lowerCAmelCase       ,    '.b'       ,    '.bias'       ,    1    )

  __UpperCAmelCase					=   value.float()

 return upgrade







@torch.no_grad()
def _lowerCAmelCase     (      _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase=None       ,    _lowerCAmelCase=True    )->     Tuple:

 '''simple docstring'''





 from dall_e import Encoder

 __UpperCAmelCase					=   Encoder()
 if os.path.exists(_lowerCAmelCase    ):
  __UpperCAmelCase					=   torch.load(_lowerCAmelCase    )
 else:
  __UpperCAmelCase					=   torch.hub.load_state_dict_from_url(_lowerCAmelCase    )

 if isinstance(_lowerCAmelCase       ,    _lowerCAmelCase    ):
  __UpperCAmelCase					=   ckpt.state_dict()
 encoder.load_state_dict(_lowerCAmelCase    )

 if config_path is not None:
  __UpperCAmelCase					=   FlavaImageCodebookConfig.from_pretrained(_lowerCAmelCase    )
 else:
  __UpperCAmelCase					=   FlavaImageCodebookConfig()

 __UpperCAmelCase					=   FlavaImageCodebook(_lowerCAmelCase    ).eval()
 __UpperCAmelCase					=   encoder.state_dict()

 __UpperCAmelCase					=   upgrade_state_dict(_lowerCAmelCase    )
 hf_model.load_state_dict(_lowerCAmelCase    )
 __UpperCAmelCase					=   hf_model.state_dict()
 __UpperCAmelCase					=   count_parameters(_lowerCAmelCase    )
 __UpperCAmelCase					=   count_parameters(_lowerCAmelCase    )

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

 if save_checkpoint:
  hf_model.save_pretrained(_lowerCAmelCase    )
 else:
  return hf_state_dict


if __name__ == "__main__":
    _A:					List[str]							=				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("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
    _A:					List[Any]							=				parser.parse_args()

    convert_dalle_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)












'''simple docstring'''




import math

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







def _lowerCAmelCase     (      _lowerCAmelCase = 3    )->     qiskit.result.counts.Counts:
 if isinstance(_lowerCAmelCase       ,    _lowerCAmelCase    ):
  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(_lowerCAmelCase    ) != 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).'    )

 __UpperCAmelCase					=   QuantumRegister(_lowerCAmelCase       ,    'qr'    )
 __UpperCAmelCase					=   ClassicalRegister(_lowerCAmelCase       ,    'cr'    )

 __UpperCAmelCase					=   QuantumCircuit(_lowerCAmelCase       ,    _lowerCAmelCase    )

 __UpperCAmelCase					=   number_of_qubits

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

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

 # measure all the qubits
 quantum_circuit.measure(_lowerCAmelCase       ,    _lowerCAmelCase    )
 # simulate with 10000 shots
 __UpperCAmelCase					=   Aer.get_backend('qasm_simulator'    )
 __UpperCAmelCase					=   execute(_lowerCAmelCase       ,    _lowerCAmelCase       ,    shots=1_00_00    )

 return job.result().get_counts(_lowerCAmelCase    )


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





