from qiskit import *
%matplotlib inline
from qiskit.tools.visualization import plot_histogram

secretNumber = '101001'

circuit = QuantumCircuit(6+1, 6)
circuit.h((0,1,2,3,4,5))
circuit.x(6)
circuit.h(6)
circuit.barrier()


circuit.cx(5, 6)
circuit.cx(3, 6)
circuit.cx(0, 6)

circuit.barrier()
circuit.h((0,1,2,3,4,5))

circuit.draw()

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

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

circuit = QuantumCircuit(len(secretNumber)+1, len(secretNumber))

#circuit.h((0,1,2,3,4,5))
circuit.h(range(len(secretNumber)))

#circuit.x(6)
circuit.x(len(secretNumber))

#circuit.h(6)
circuit.x(len(secretNumber))

circuit.barrier()


for ii, yesno in enumerate(reversed(secretNumber)):
    if yesno == '1':
        circuit.cx (ii, len(secretNumber))
#circuit.cx(5, 6)
#circuit.cx(3, 6)
#circuit.cx(0, 6)



circuit.barrier()
#circuit.h((0,1,2,3,4,5))
circuit.h(range(len(secretNumber)))
circuit.barrier()
circuit.measure(range(len(secretNumber)), range(len(secretNumber)))
circuit.draw()