



import numpy as np
import qiskit





def 		__snake_case						(    _UpperCAmelCase       ,			_UpperCAmelCase				):
					"""simple docstring"""






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

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

					lowercase						   =       oracle_qc.to_gate()
					lowercase						   =       'Oracle'  # To show when we display the circuit
					return oracle_gate





def 		__snake_case						(    _UpperCAmelCase       ,			_UpperCAmelCase				):
					"""simple docstring"""






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

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

					return dj_circuit





def 		__snake_case						(    _UpperCAmelCase       ,			_UpperCAmelCase				):
					"""simple docstring"""






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

					lowercase						   =       dj_oracle(_UpperCAmelCase       ,			_UpperCAmelCase				)
					lowercase						   =       dj_algorithm(_UpperCAmelCase       ,			_UpperCAmelCase				)

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

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


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











from collections.abc import Callable

__magic_name__       =     Callable[[float], float]  # type alias for a real -> real function





def 		__snake_case						(    _UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase       ,			):
					"""simple docstring"""






					lowercase						   =       float(_UpperCAmelCase				)
					while True:
										try:
															lowercase						   =       prev_guess - function(_UpperCAmelCase				) / derivative(_UpperCAmelCase				)
										except ZeroDivisionError:
															raise ZeroDivisionError('Could not find root'				) from None
										if abs(prev_guess - next_guess				) < 10**-5:
															return next_guess
										lowercase						   =       next_guess





def 		__snake_case						(    _UpperCAmelCase				):
					"""simple docstring"""






					return (x**3) - (2 * x) - 5





def 		__snake_case						(    _UpperCAmelCase				):
					"""simple docstring"""






					return 3 * (x**2) - 2


if __name__ == "__main__":
		print(newton(f, fa, 3))






