# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce





def 				UpperCAmelCase						(    lowercase							):







				"""simple docstring"""



				return (torch.arange(state.num_processes							) + 1.0 + (state.num_processes * state.process_index)).to(state.device							)





def 				UpperCAmelCase						(    lowercase							):







				"""simple docstring"""



				__lowercase 			=							create_tensor(lowercase							)
				__lowercase 			=							gather(lowercase							)
				assert gathered_tensor.tolist() == list(range(1							,			state.num_processes**2 + 1							)							)





def 				UpperCAmelCase						(    lowercase							):







				"""simple docstring"""



				__lowercase 			=							[state.process_index]
				__lowercase 			=							gather_object(lowercase							)
				assert len(lowercase							) == state.num_processes, F"{gathered_obj}, {len(lowercase							)} != {state.num_processes}"
				assert gathered_obj == list(range(state.num_processes							)							), F"{gathered_obj} != {list(range(state.num_processes							)							)}"





def 				UpperCAmelCase						(    lowercase							):







				"""simple docstring"""



				__lowercase 			=							create_tensor(lowercase							)
				__lowercase 			=							broadcast(lowercase							)
				assert broadcasted_tensor.shape == torch.Size([state.num_processes]							)
				assert broadcasted_tensor.tolist() == list(range(1							,			state.num_processes + 1							)							)





def 				UpperCAmelCase						(    lowercase							):







				"""simple docstring"""



				if state.is_main_process:
								__lowercase 			=							torch.arange(state.num_processes + 1							).to(state.device							)
				else:
								__lowercase 			=							torch.arange(state.num_processes							).to(state.device							)
				__lowercase 			=							pad_across_processes(lowercase							)
				assert padded_tensor.shape == torch.Size([state.num_processes + 1]							)
				if not state.is_main_process:
								assert padded_tensor.tolist() == list(range(0							,			state.num_processes							)							) + [0]





def 				UpperCAmelCase						(    lowercase							):







				"""simple docstring"""



				if state.num_processes != 2:
								return
				__lowercase 			=							create_tensor(lowercase							)
				__lowercase 			=							reduce(lowercase							,			'''sum'''							)
				__lowercase 			=							torch.tensor([4.0, 6]							).to(state.device							)
				assert torch.allclose(lowercase							,			lowercase							), F"{reduced_tensor} != {truth_tensor}"





def 				UpperCAmelCase						(    lowercase							):







				"""simple docstring"""



				if state.num_processes != 2:
								return
				__lowercase 			=							create_tensor(lowercase							)
				__lowercase 			=							reduce(lowercase							,			'''mean'''							)
				__lowercase 			=							torch.tensor([2.0, 3]							).to(state.device							)
				assert torch.allclose(lowercase							,			lowercase							), F"{reduced_tensor} != {truth_tensor}"





def 				UpperCAmelCase						(    lowercase							):







				"""simple docstring"""



				main()





def 				UpperCAmelCase						(    ):







				"""simple docstring"""



				__lowercase 			=							PartialState()
				state.print(F"State: {state}"							)
				state.print('''testing gather'''							)
				test_gather(lowercase							)
				state.print('''testing gather_object'''							)
				test_gather_object(lowercase							)
				state.print('''testing broadcast'''							)
				test_broadcast(lowercase							)
				state.print('''testing pad_across_processes'''							)
				test_pad_across_processes(lowercase							)
				state.print('''testing reduce_sum'''							)
				test_reduce_sum(lowercase							)
				state.print('''testing reduce_mean'''							)
				test_reduce_mean(lowercase							)


if __name__ == "__main__":
		main()
import qiskit





def 				UpperCAmelCase						(    lowercase							,			lowercase							):







				"""simple docstring"""



				__lowercase 			=							qiskit.Aer.get_backend('''aer_simulator'''							)

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

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

				# Execute the circuit on the simulator
				__lowercase 			=							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)}''')