from qiskit import *

%matplotlib inline
from qiskit.tools.visualization import plot_histogram

secret_number = '101001'

qc = QuantumCircuit(6+1, 6)
qc.h([0, 1, 2, 3, 4, 5])
qc.x(6)
qc.h(6)
qc.barrier()
qc.draw(output='mpl')

# building box of secret number
qc.cx(5, 6)
qc.cx(3, 6)
qc.cx(0, 6)


qc.draw(output='mpl')

qc.barrier()
qc.h([0, 1, 2, 3, 4, 5])


qc.barrier()
qc.measure([0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5])

qc.draw(output='mpl')

simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, backend=simulator, shots=1).result()
counts = result.get_counts()
print(counts)

secret_number = '1000100110010101001'

qc = QuantumCircuit(len(secret_number)+1, len(secret_number))
qc.h(range(len(secret_number)))
qc.x(len(secret_number))
qc.h(len(secret_number))
qc.barrier()

for ii, yesno in enumerate(reversed(secret_number)):
    if yesno == '1':
        qc.cx(ii, len(secret_number))
        
        
qc.barrier()
qc.h(range(len(secret_number)))

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

qc.draw(output='mpl')

simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, backend=simulator, shots=1).result()
counts = result.get_counts()
print(counts)