




"""simple docstring"""





import numpy as np
import qiskit

def 						lowerCAmelCase							(	lowerCamelCase ,     lowerCamelCase						):
						'''simple docstring'''


						snake_case__    :   Any							     =   qiskit.QuantumCircuit(num_qubits + 1						)

						# First, let's deal with the case in which oracle is balanced
						if case == "balanced":
												# First generate a random number that tells us which CNOTs to
												# wrap in X-gates:
												snake_case__    :   Dict							     =   np.random.randint(1 ,     2**num_qubits						)
												# Next, format 'b' as a binary string of length 'n', padded with zeros:
												snake_case__    :   Union[str, Any]							     =   format(lowerCamelCase ,     f'''0{num_qubits}b'''						)
												# Next, we place the first X-gates. Each digit in our binary string
												# corresponds to a qubit, if the digit is 0, we do nothing, if it's 1
												# we apply an X-gate to that qubit:
												for index, bit in enumerate(lowerCamelCase						):
																		if bit == "1":
																								oracle_qc.x(lowerCamelCase						)
        # Do the controlled-NOT gates for each qubit, using the output qubit
        # as the target:
												for index in range(lowerCamelCase						):
																		oracle_qc.cx(lowerCamelCase ,     lowerCamelCase						)
												# Next, place the final X-gates
												for index, bit in enumerate(lowerCamelCase						):
																		if bit == "1":
																								oracle_qc.x(lowerCamelCase						)

    # Case in which oracle is constant
						if case == "constant":
												# First decide what the fixed output of the oracle will be
												# (either always 0 or always 1)
												snake_case__    :   Any							     =   np.random.randint(2						)
												if output == 1:
																		oracle_qc.x(lowerCamelCase						)

						snake_case__    :   Union[str, Any]							     =   oracle_qc.to_gate()
						snake_case__    :   Tuple							     =   "Oracle"  # To show when we display the circuit
						return oracle_gate

def 						lowerCAmelCase							(	lowerCamelCase ,     lowerCamelCase						):
						'''simple docstring'''


						snake_case__    :   List[str]							     =   qiskit.QuantumCircuit(num_qubits + 1 ,     lowerCamelCase						)
						# Set up the output qubit:
						dj_circuit.x(lowerCamelCase						)
						dj_circuit.h(lowerCamelCase						)
						# And set up the input register:
						for qubit in range(lowerCamelCase						):
												dj_circuit.h(lowerCamelCase						)
						# Let's append the oracle gate to our circuit:
						dj_circuit.append(lowerCamelCase ,     range(num_qubits + 1						)						)
						# Finally, perform the H-gates again and measure:
						for qubit in range(lowerCamelCase						):
												dj_circuit.h(lowerCamelCase						)

						for i in range(lowerCamelCase						):
												dj_circuit.measure(lowerCamelCase ,     lowerCamelCase						)

						return dj_circuit

def 						lowerCAmelCase							(	lowerCamelCase ,     lowerCamelCase						):
						'''simple docstring'''


						snake_case__    :   List[Any]							     =   qiskit.Aer.get_backend('''aer_simulator'''						)

						snake_case__    :   Union[str, Any]							     =   dj_oracle(lowerCamelCase ,     lowerCamelCase						)
						snake_case__    :   List[str]							     =   dj_algorithm(lowerCamelCase ,     lowerCamelCase						)

						# Execute the circuit on the simulator
						snake_case__    :   List[Any]							     =   qiskit.execute(lowerCamelCase ,     lowerCamelCase ,     shots=1000						)

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


if __name__ == "__main__":
							print(f"""Deutsch Jozsa - Constant Oracle: {deutsch_jozsa("constant", 3)}""")
							print(f"""Deutsch Jozsa - Balanced Oracle: {deutsch_jozsa("balanced", 3)}""")








"""simple docstring"""





from __future__ import annotations

from collections.abc import Iterator
from typing import Any

class  __lowercase    :


				'''simple docstring'''


				def __init__(				self				:       List[Any] ,SCREAMING_SNAKE_CASE_				:       Any ):
										snake_case__    :   Any							     =   data
										snake_case__    :   Node | None							     =   None





				def __str__(				self				:       Union[str, Any] ):
										return F'''{self.data}'''







class  __lowercase    :


				'''simple docstring'''


				def __init__(				self				:       Optional[int] ):
										snake_case__    :   Node | None							     =   None
										snake_case__    :   Node | None							     =   None

				def __iter__(				self				:       Optional[int] ):
										snake_case__    :   Optional[int]							     =   self.front
										while node:
																yield node.data
																snake_case__    :   Optional[int]							     =   node.next

				def __len__(				self				:       Any ):
										return len(tuple(iter(self ) ) )

				def __str__(				self				:       Union[str, Any] ):
										return " <- ".join(str(SCREAMING_SNAKE_CASE_ ) for item in self )

				def  _lowerCamelCase   (				self				:       List[str] ):
										return len(self ) == 0

				def  _lowerCamelCase   (				self				:       Optional[int] ,SCREAMING_SNAKE_CASE_				:       Any ):
										snake_case__    :   int							     =   Node(SCREAMING_SNAKE_CASE_ )
										if self.is_empty():
																snake_case__    :   List[Any]							     =   node
										else:
																assert isinstance(self.rear ,SCREAMING_SNAKE_CASE_ )
																snake_case__    :   Union[str, Any]							     =   node
																snake_case__    :   int							     =   node

				def  _lowerCamelCase   (				self				:       int ):
										if self.is_empty():
																raise IndexError('''dequeue from empty queue''' )
										assert isinstance(self.front ,SCREAMING_SNAKE_CASE_ )
										snake_case__    :   List[str]							     =   self.front
										snake_case__    :   str							     =   self.front.next
										if self.front is None:
																snake_case__    :   Optional[Any]							     =   None
										return node.data





				def  _lowerCamelCase   (				self				:       Optional[int] ):
										snake_case__    :   Optional[Any]							     =   None


if __name__ == "__main__":
							from doctest import testmod

							testmod()


