%matplotlib inline
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
provider = IBMQ.load_account()

circ = QuantumCircuit(9,9)



#Hadamard
for i in [6,7,8]:
    circ.h(i)

circ.barrier(range(9))

#Function
circ.cx(6,5)
circ.ccx(6,7,4)
circ.cx(8,4)

circ.barrier(range(9))

for i,j in zip([3,4,5],[0,1,2]):
    circ.x(i)
    circ.ch(i,j)
    circ.x(i)

circ.barrier(range(9))

circ.measure(range(9),range(9))

circ.draw("mpl")

nruns = 2048
backend_sim = Aer.get_backend('qasm_simulator')
job_sim = execute(circ, backend_sim, shots=nruns)
result_sim = job_sim.result()

counts = result_sim.get_counts(circ)

plot_histogram(counts)

result_raw = sorted(counts.items(), key=lambda x: x[1], reverse=True)
result = list(map(lambda x: [x[0][:3],x[0][3:6],x[0][6:],x[1]/nruns],result_raw))
result

