# begin with importing essential libraries for IBM Q
from qiskit import IBMQ, BasicAer
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit

# set up Quantum Register and Classical Register for 3 qubits
q = QuantumRegister(3)
c = ClassicalRegister(3)
# Create a Quantum Circuit
qc = QuantumCircuit(q, c)
qc.h(q)
qc.measure(q, c)

qc.draw()

def answer(result):
    for key in result.keys():
        state = key
    print('The Quantum 8-ball says:')
    if state == '000':
        print('It is certain.')
    elif state == '001':
        print('Without a doubt.')
    elif state == '010':
        print('Yes - definitely.')
    elif state == '011':
        print('Most likely.')
    elif state == '100':
        print("Don't count on it.")
    elif state == '101':
        print('My reply is no.')
    elif state == '110':
        print('Very doubtful.')
    else:
        print('Concentrate and ask again.')

from qiskit import execute
job = execute(qc, backend=BasicAer.get_backend('qasm_simulator'), shots=1)
result = job.result().get_counts(qc)
answer(result)

# load IBM Q account
IBMQ.load_accounts()

# define the least busy device
from qiskit.providers.ibmq import least_busy
backend = least_busy(IBMQ.backends(simulator=False))
print("The least busy device:",backend.name())

job = execute(qc, backend=backend, shots=1)
result = job.result().get_counts(qc)
answer(result)