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

circ = QuantumCircuit(4,4)
#Preparation
circ.h(0)
circ.h(1)
#Oracle function
def oracle(circuit, mapping):
    if mapping=="a":
        """
        100x
        010
        101x
        011
        """
        circuit.cx(1,2)
        circuit.x(2)
    elif mapping=="b":
        """
        100x
        001
        110x
        011
        """
        circuit.cx(0,2)
        circuit.x(2)
    elif mapping=="constant":
        circuit.x(2)
oracle(circ,"b")

circ.barrier(range(4))
circ.x(2)
circ.ch(2,3)
circ.x(2)

circ.barrier(range(4))
circ.measure(range(4),range(4))

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)

dict(filter(lambda x: x[1]/nruns >= 0.2,counts.items()))

