



import numpy as np
import qiskit
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						str  ,					_SCREAMING_SNAKE_CASE :						int					)->qiskit.QuantumCircuit:
      _lowerCAmelCase			      =	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:
            _lowerCAmelCase			      =	np.random.randint(1  ,					2**num_qubits					)
            # Next, format 'b' as a binary string of length 'n', padded with zeros:
            _lowerCAmelCase			      =	format(_SCREAMING_SNAKE_CASE  ,					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(_SCREAMING_SNAKE_CASE					):
                  if bit == "1":
                        oracle_qc.x(_SCREAMING_SNAKE_CASE					)
        # Do the controlled-NOT gates for each qubit, using the output qubit
        # as the target:
            for index in range(_SCREAMING_SNAKE_CASE					):
                  oracle_qc.cx(_SCREAMING_SNAKE_CASE  ,					_SCREAMING_SNAKE_CASE					)
            # Next, place the final X-gates
            for index, bit in enumerate(_SCREAMING_SNAKE_CASE					):
                  if bit == "1":
                        oracle_qc.x(_SCREAMING_SNAKE_CASE					)

    # 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)
            _lowerCAmelCase			      =	np.random.randint(2					)
            if output == 1:
                  oracle_qc.x(_SCREAMING_SNAKE_CASE					)

      _lowerCAmelCase			      =	oracle_qc.to_gate()
      _lowerCAmelCase			      =	'''Oracle'''  # To show when we display the circuit
      return oracle_gate
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						qiskit.QuantumCircuit  ,					_SCREAMING_SNAKE_CASE :						int					)->qiskit.QuantumCircuit:
      _lowerCAmelCase			      =	qiskit.QuantumCircuit(num_qubits + 1  ,					_SCREAMING_SNAKE_CASE					)
      # Set up the output qubit:
      dj_circuit.x(_SCREAMING_SNAKE_CASE					)
      dj_circuit.h(_SCREAMING_SNAKE_CASE					)
      # And set up the input register:
      for qubit in range(_SCREAMING_SNAKE_CASE					):
            dj_circuit.h(_SCREAMING_SNAKE_CASE					)
      # Let's append the oracle gate to our circuit:
      dj_circuit.append(_SCREAMING_SNAKE_CASE  ,					range(num_qubits + 1					)					)
      # Finally, perform the H-gates again and measure:
      for qubit in range(_SCREAMING_SNAKE_CASE					):
            dj_circuit.h(_SCREAMING_SNAKE_CASE					)

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

      return dj_circuit
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						str  ,					_SCREAMING_SNAKE_CASE :						int					)->qiskit.result.counts.Counts:
      _lowerCAmelCase			      =	qiskit.Aer.get_backend('''aer_simulator'''					)

      _lowerCAmelCase			      =	dj_oracle(_SCREAMING_SNAKE_CASE  ,					_SCREAMING_SNAKE_CASE					)
      _lowerCAmelCase			      =	dj_algorithm(_SCREAMING_SNAKE_CASE  ,					_SCREAMING_SNAKE_CASE					)

      # Execute the circuit on the simulator
      _lowerCAmelCase			      =	qiskit.execute(_SCREAMING_SNAKE_CASE  ,					_SCREAMING_SNAKE_CASE  ,					shots=1_0_0_0					)

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


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




import math
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						int					)->bool:

      if 1 < number < 4:
            # 2 and 3 are primes
            return True
      elif number < 2 or number % 2 == 0 or number % 3 == 0:
            # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
            return False

      # All primes number are in format of 6k +/- 1
      for i in range(5  ,					int(math.sqrt(_SCREAMING_SNAKE_CASE					) + 1					)  ,					6					):
            if number % i == 0 or number % (i + 2) == 0:
                  return False
      return True
def 				UpperCAmelCase__		(					_SCREAMING_SNAKE_CASE :						int = 6_0_0_8_5_1_4_7_5_1_4_3					)->int:

      try:
            _lowerCAmelCase			      =	int(_SCREAMING_SNAKE_CASE					)
      except (TypeError, ValueError):
            raise TypeError('''Parameter n must be int or castable to int.'''					)
      if n <= 0:
            raise ValueError('''Parameter n must be greater than or equal to one.'''					)
      _lowerCAmelCase			      =	0
      if is_prime(_SCREAMING_SNAKE_CASE					):
            return n
      while n % 2 == 0:
            n //= 2
      if is_prime(_SCREAMING_SNAKE_CASE					):
            return n
      for i in range(3  ,					int(math.sqrt(_SCREAMING_SNAKE_CASE					)					) + 1  ,					2					):
            if n % i == 0:
                  if is_prime(n // i					):
                        _lowerCAmelCase			      =	n // i
                        break
                  elif is_prime(_SCREAMING_SNAKE_CASE					):
                        _lowerCAmelCase			      =	i
      return max_number


if __name__ == "__main__":
    print(F"""{solution() = }""")