

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

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

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

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

 UpperCAmelCase__           =       dj_oracle(snake_case__						,					snake_case__     )
 UpperCAmelCase__           =       dj_algorithm(snake_case__						,					snake_case__     )

 # Execute the circuit on the simulator
 UpperCAmelCase__           =       qiskit.execute(snake_case__						,					snake_case__						,					shots=10_00     )

 # Return the histogram data of the results of the experiment.
 return job.result().get_counts(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)}""")






from __future__ import annotations



class 					lowercase		:



 '''simple docstring'''


 def __init__(self , __a				)				->      None:






  """simple docstring"""





  UpperCAmelCase__           =       vertices
  UpperCAmelCase__           =       [[0] * vertices for _ in range(__a				)]




 def 						UpperCamelCase__							(self , __a				)				->      None:






  """simple docstring"""





  print('Vertex \t Distance from Source'				)
  for vertex in range(self.vertices				):
   print(__a , '\t\t' , distances_from_source[vertex]				)




 def 						UpperCamelCase__							(self , __a , __a				)				->      int:






  """simple docstring"""





  UpperCAmelCase__           =       1E7
  UpperCAmelCase__           =       0

  # Search not nearest vertex not in the shortest path tree
  for vertex in range(self.vertices				):
   if distances_from_source[vertex] < minimum and visited[vertex] is False:
    UpperCAmelCase__           =       distances_from_source[vertex]
    UpperCAmelCase__           =       vertex
  return min_index




 def 						UpperCamelCase__							(self , __a				)				->      None:






  """simple docstring"""





  UpperCAmelCase__           =       [int(1E7				)] * self.vertices  # distances from the source
  UpperCAmelCase__           =       0
  UpperCAmelCase__           =       [False] * self.vertices

  for _ in range(self.vertices				):
   UpperCAmelCase__           =       self.minimum_distance(__a , __a				)
   UpperCAmelCase__           =       True

   # Update dist value of the adjacent vertices
   # of the picked vertex only if the current
   # distance is greater than new distance and
   # the vertex in not in the shortest path tree
   for v in range(self.vertices				):
    if (
        self.graph[u][v] > 0
        and visited[v] is False
        and distances[v] > distances[u] + self.graph[u][v]
    ):
     UpperCAmelCase__           =       distances[u] + self.graph[u][v]

  self.print_solution(__a				)


if __name__ == "__main__":
 _UpperCamelCase     =    Graph(9)
 _UpperCamelCase     =    [
     [0, 4, 0, 0, 0, 0, 0, 8, 0],
     [4, 0, 8, 0, 0, 0, 0, 11, 0],
     [0, 8, 0, 7, 0, 4, 0, 0, 2],
     [0, 0, 7, 0, 9, 14, 0, 0, 0],
     [0, 0, 0, 9, 0, 10, 0, 0, 0],
     [0, 0, 4, 14, 10, 0, 2, 0, 0],
     [0, 0, 0, 0, 0, 2, 0, 1, 6],
     [8, 11, 0, 0, 0, 0, 1, 0, 7],
     [0, 0, 2, 0, 0, 0, 6, 7, 0],
 ]
 graph.dijkstra(0)



