import config
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, IBMQ, Aer, execute
from qiskit.visualization import plot_histogram
from qiskit.providers.ibmq import least_busy

def bell_state():
    qreg = QuantumRegister(2, 'q')
    creg = ClassicalRegister(2, 'c')
    qc   = QuantumCircuit(qreg, creg)

    # Create bell state itself by putting first qubit into superposition and then applying a CNOT gate between the two qubits
    qc.h(qreg[0])
    qc.cx(qreg[0], qreg[1])

    # Measure both qubits
    qc.measure(qreg[0], creg[0])
    qc.measure(qreg[1], creg[1])

    # Return circuit
    return qc

circ = bell_state()
circ.draw()

IBMQ.save_account(f"{config.IBM_KEY}", overwrite = True)
IBMQ.load_account()

backend = Aer.get_backend('qasm_simulator')

# Uncomment below to run on hardware
# provider = IBMQ.get_provider(hub='ibm-q')
# backend  = least_busy(provider.backends())

result = execute(circ, backend, shots=1024).result()
counts = result.get_counts(circ)

plot_histogram(counts)