# Creating quantum circuits
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister
from qiskit import execute
from qiskit.tools.visualization import circuit_drawer, plot_histogram

qr =  QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(qr, cr)

# Apply a single H gate
qc.h(qr)
qc.measure(qr, cr)
circuit_drawer(qc)

import os
from qiskit import Aer

# Print the local simulator backends
print("Local simulator backends: ")
Aer.backends()

from qiskit import IBMQ

# Save your credeintials on disk. Need to be done only once. 
IBMQ.save_account(os.getenv('QX_API_TOKEN'))
#IBMQ.save_account("PUT_YOUR_API_TOKEN_HERE") 

# Alternatively, you can only "enable" the credentials, for this session only:
# IBMQ.enable_account("PUT_YOUR_API_TOKEN_HERE")

IBMQ.load_accounts()

print("Available online backends: ")
IBMQ.backends()

# Run the circuit using local simulator 
local_simulator = Aer.get_backend("qasm_simulator")
job = execute(qc, backend=local_simulator, shots=100)

result = job.result()
counts = result.get_counts()
plot_histogram(counts)
print(counts)

# Run the circuit using the cloud simulator
online_simulator = IBMQ.get_backend("ibmq_qasm_simulator")
job = execute(qc, backend=online_simulator)

result_cloud = job.result()
counts = result.get_counts()
plot_histogram(counts)
print(counts)