%matplotlib inline
# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from ibm_quantum_widgets import *

# Loading your IBM Q account(s)
provider = IBMQ.load_account()

provider.backends()

from qiskit.providers.ibmq import least_busy

large_enough_devices = provider.backends(filters=lambda x: x.configuration().n_qubits > 3 and not x.configuration().simulator)
backend = least_busy(large_enough_devices)
print("The best backend is " + backend.name())

# quasm_sim not necessary
#simulator = Aer.get_backend('qasm_simulator')

from qiskit.tools.monitor import job_monitor

# circuit on q reg with 1 qubit
circuit = QuantumCircuit(2, 2)

# hadamard on A
circuit.h(0)

# CNOT on B controlled by A
circuit.cx(0 , 1)

circuit.measure([0, 1], [0, 1])

job = execute(circuit, backend, shots=1000)
job_monitor(job)


results = job.result()

counts = results.get_counts(circuit)

print(f"total count for 0 and 1 are: {counts}")

circuit.draw()

plot_histogram(counts)