import qiskit
from qiskit import *
print(qiskit.__version__)
%matplotlib inline
from qiskit.tools.visualization import plot_histogram

def find_secret_number(secter_number):
    # Using Bernstein Vazirani Algorithm
    
    secret_number = str(secter_number)
    circuit = QuantumCircuit(len(secret_number)+1, len(secret_number))

    circuit.h(range(len(secret_number)))
    circuit.x(len(secret_number))
    circuit.h(len(secret_number))
    circuit.barrier()

    for position,value in enumerate(reversed(secret_number)):
        if value == '1':
            circuit.cx(position, len(secret_number))

    circuit.barrier()
    circuit.h(range(len(secret_number)))
    circuit.barrier()

    circuit.measure(range(len(secret_number)), range(len(secret_number)))
    circuit.barrier()

#     circuit.draw(output='mpl')
    
    simulator = Aer.get_backend('qasm_simulator')
    job = execute(circuit, backend=simulator, shots=1)
    result = job.result()
    counts = result.get_counts()
#     print(counts)
    return circuit, counts

secret_number = int(input("enter number: "))
circuit, number = find_secret_number(secret_number)
print('required number: ', number)
circuit.draw(output='mpl')

