




'''simple docstring'''






import os
import sys
from pathlib import Path

import pytest

from datasets import Dataset, IterableDataset
from datasets.distributed import split_dataset_by_node

from .utils import execute_subprocess_async, get_torch_dist_unique_port, require_torch

def       __lowerCAmelCase (   ):
			__UpperCamelCase				:      List[str]									=       Dataset.from_dict({"i": range(17						)}						)
			__UpperCamelCase				:      Optional[Any]									=       len(snake_case__						)
			__UpperCamelCase				:      Tuple									=       3
			__UpperCamelCase				:      int									=       [
			    split_dataset_by_node(snake_case__ ,				rank=snake_case__ ,				world_size=snake_case__						) for rank in range(snake_case__						)
			]
			assert sum(len(snake_case__						) for ds in datasets_per_rank						) == full_size
			assert len({tuple(x.values()						) for ds in datasets_per_rank for x in ds}						) == full_size

def       __lowerCAmelCase (   ):
			def gen():
						return ({"i": i} for i in range(17						))

			__UpperCamelCase				:      Optional[Any]									=       3
			__UpperCamelCase				:      List[str]									=       IterableDataset.from_generator(snake_case__						)
			__UpperCamelCase				:      Optional[Any]									=       len(list(snake_case__						)						)
			__UpperCamelCase				:      int									=       [
			    split_dataset_by_node(snake_case__ ,				rank=snake_case__ ,				world_size=snake_case__						) for rank in range(snake_case__						)
			]
			assert sum(len(list(snake_case__						)						) for ds in datasets_per_rank						) == full_size
			assert len({tuple(x.values()						) for ds in datasets_per_rank for x in ds}						) == full_size

@pytest.mark.parametrize("shards_per_node" ,				[1, 2, 3]						)
def       __lowerCAmelCase (   snake_case__						):
			def gen(snake_case__						):
						for shard in shards:
									yield from ({"i": i, "shard": shard} for i in range(17						))

			__UpperCamelCase				:      Tuple									=       3
			__UpperCamelCase				:      Union[str, Any]									=       shards_per_node * world_size
			__UpperCamelCase				:      Tuple									=       {"shards": [F"shard_{shard_idx}.txt" for shard_idx in range(snake_case__						)]}
			__UpperCamelCase				:      Dict									=       IterableDataset.from_generator(snake_case__ ,				gen_kwargs=snake_case__						)
			__UpperCamelCase				:      List[str]									=       len(list(snake_case__						)						)
			assert full_ds.n_shards == world_size * shards_per_node
			__UpperCamelCase				:      Optional[int]									=       [
			    split_dataset_by_node(snake_case__ ,				rank=snake_case__ ,				world_size=snake_case__						) for rank in range(snake_case__						)
			]
			assert [ds.n_shards for ds in datasets_per_rank] == [shards_per_node] * world_size
			assert sum(len(list(snake_case__						)						) for ds in datasets_per_rank						) == full_size
			assert len({tuple(x.values()						) for ds in datasets_per_rank for x in ds}						) == full_size

def       __lowerCAmelCase (   ):
			def gen():
						return ({"i": i} for i in range(17						))

			__UpperCamelCase				:      Optional[Any]									=       2
			__UpperCamelCase				:      int									=       IterableDataset.from_generator(snake_case__						)
			__UpperCamelCase				:      Optional[Any]									=       len(list(snake_case__						)						)

			__UpperCamelCase				:      str									=       split_dataset_by_node(snake_case__ ,				rank=0 ,				world_size=snake_case__						).shuffle(seed=42						)
			assert len(list(snake_case__						)						) == 1 + full_size // world_size
			with pytest.raises(snake_case__						):
						split_dataset_by_node(snake_case__ ,				rank=0 ,				world_size=snake_case__						).shuffle()

			__UpperCamelCase				:      str									=       split_dataset_by_node(full_ds.shuffle(seed=42						) ,				rank=0 ,				world_size=snake_case__						)
			assert len(list(snake_case__						)						) == 1 + full_size // world_size
			with pytest.raises(snake_case__						):
						split_dataset_by_node(full_ds.shuffle() ,				rank=0 ,				world_size=snake_case__						)

@pytest.mark.parametrize("streaming" ,				[False, True]						)
@require_torch
@pytest.mark.skipif(os.name == "nt" ,				reason="execute_subprocess_async doesn't support windows"						)
@pytest.mark.integration
def       __lowerCAmelCase (   snake_case__						):
			__UpperCamelCase				:      str									=       2
			__UpperCamelCase				:      int									=       get_torch_dist_unique_port()
			__UpperCamelCase				:      Optional[Any]									=       Path(snake_case__						).resolve().parent / "distributed_scripts" / "launch_torch_distributed.py"
			__UpperCamelCase				:      Dict									=       F"\n        -m torch.distributed.launch\n        --nproc_per_node={nproc_per_node}\n        --master_port={master_port}\n        {test_script}\n    ".split()
			__UpperCamelCase				:      Union[str, Any]									=       F"\n        --streaming={streaming}\n    ".split()
			__UpperCamelCase				:      Dict									=       [sys.executable] + distributed_args + args
			execute_subprocess_async(snake_case__ ,				env=os.environ.copy()						)




@pytest.mark.parametrize(
    "nproc_per_node, num_workers" ,				[
        (2, 2),  # each node has 2 shards and each worker has 1 shards
        (3, 2),  # each node uses all the shards but skips examples, and each worker has 2 shards
    ] ,				)
@require_torch
@pytest.mark.skipif(os.name == "nt" ,				reason="execute_subprocess_async doesn't support windows"						)
@pytest.mark.integration
def       __lowerCAmelCase (   snake_case__ ,				snake_case__						):
			__UpperCamelCase				:      Dict									=       True
			__UpperCamelCase				:      Union[str, Any]									=       get_torch_dist_unique_port()
			__UpperCamelCase				:      List[str]									=       Path(snake_case__						).resolve().parent / "distributed_scripts" / "launch_torch_distributed.py"
			__UpperCamelCase				:      Any									=       F"\n        -m torch.distributed.launch\n        --nproc_per_node={nproc_per_node}\n        --master_port={master_port}\n        {test_script}\n    ".split()
			__UpperCamelCase				:      Optional[int]									=       F"\n        --streaming={streaming}\n        --num_workers={num_workers}\n    ".split()
			__UpperCamelCase				:      Optional[int]									=       [sys.executable] + distributed_args + args
			execute_subprocess_async(snake_case__ ,				env=os.environ.copy()						)









'''simple docstring'''






import math

import qiskit
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute

def       __lowerCAmelCase (   snake_case__ = 1 ,				snake_case__ = 1						):
			if isinstance(snake_case__ ,				snake_case__						) or isinstance(snake_case__ ,				snake_case__						):
						raise TypeError("inputs must be integers."						)
			if (bit_a < 0) or (bit_a < 0):
						raise ValueError("inputs must be positive."						)
			if (math.floor(snake_case__						) != bit_a) or (math.floor(snake_case__						) != bit_a):
						raise ValueError("inputs must be exact integers."						)
			if (bit_a > 1) or (bit_a > 1):
						raise ValueError("inputs must be less or equal to 1."						)

			# build registers
			__UpperCamelCase				:      Tuple									=       QuantumRegister(2 ,				"qr"						)
			__UpperCamelCase				:      Dict									=       ClassicalRegister(2 ,				"cr"						)

			__UpperCamelCase				:      str									=       QuantumCircuit(snake_case__ ,				snake_case__						)

			# entanglement the qubits
			quantum_circuit.h(1						)
			quantum_circuit.cx(1 ,				0						)

			# send the information
			__UpperCamelCase				:      Optional[Any]									=       str(snake_case__						) + str(snake_case__						)

			if c_information == "11":
						quantum_circuit.x(1						)
						quantum_circuit.z(1						)
			elif c_information == "10":
						quantum_circuit.z(1						)
			elif c_information == "01":
						quantum_circuit.x(1						)
			else:
						quantum_circuit.i(1						)

			# unentangled the circuit
			quantum_circuit.cx(1 ,				0						)
			quantum_circuit.h(1						)

			# measure the circuit
			quantum_circuit.measure(snake_case__ ,				snake_case__						)

			__UpperCamelCase				:      Dict									=       Aer.get_backend("aer_simulator"						)
			__UpperCamelCase				:      Tuple									=       execute(snake_case__ ,				snake_case__ ,				shots=1_000						)

			return job.result().get_counts(snake_case__						)


if __name__ == "__main__":
				print(f'Counts for classical state send: {superdense_coding(1,1)}')



