from qiskit import IBMQ, Aer
from qiskit.providers.ibmq import least_busy
from qiskit import QuantumCircuit, transpile, assemble
from qiskit.tools.monitor import job_monitor
import matplotlib as mpl
# import basic plot tools
from qiskit.visualization import plot_histogram

bv_circuit=QuantumCircuit(4,3)
#bv_circuit.clear()

bit_string=('111')

bv_circuit.x(3)
bv_circuit.h([0,1,2,3])
bv_circuit.barrier()
bv_circuit.cx(0,3)
bv_circuit.cx(1,3)
bv_circuit.cx(2,3)
bv_circuit.barrier()
bv_circuit.h([0,1,2,3])
bv_circuit.measure([0,1,2],[0,1,2])


bv_circuit.draw('mpl')

aer_sim = Aer.get_backend('aer_simulator')   #Local Simulator
shots = 1024                                 #No. of times the circuit is running
qobj = assemble(bv_circuit, shots = shots) 
results = aer_sim.run(qobj).result()
counts = results.get_counts()
plot_histogram(counts)                      #Plotting the result

bit_string=('1010')

bv_circuit=QuantumCircuit(len(bit_string)+1,len(bit_string))
bv_circuit.clear()


#bv_circuit.x(len(bit_string))
bv_circuit.h(range(len(bit_string)))
bv_circuit.x(len(bit_string))
bv_circuit.h(len(bit_string))
bv_circuit.barrier()

for i, yes in enumerate(reversed(bit_string)):
        if yes=='1':
            bv_circuit.cx(i,len(bit_string))
            
bv_circuit.barrier()
bv_circuit.h(range(len(bit_string)))
bv_circuit.h(len(bit_string))
      
bv_circuit.measure(range(len(bit_string)),range(len(bit_string)))

bv_circuit.draw('mpl')

aer_sim = Aer.get_backend('aer_simulator')   #Local Simulator
shots = 1024                                 #No. of times the circuit is running
qobj = assemble(bv_circuit, shots = shots) 
results = aer_sim.run(qobj).result()
counts = results.get_counts()
plot_histogram(counts)                      #Plotting the result