import json
import requests

from qiskit import QuantumRegister, ClassicalRegister
from qiskit import QuantumCircuit, IBMQ, execute, compile

IBMQ.load_accounts()

q = QuantumRegister(5)
c = ClassicalRegister(5)
qc = QuantumCircuit(q, c)

qc.h(q)

qc.measure(q,c)

device = 'ibmqx4'
backend = IBMQ.get_backend(device)
qobj = compile(qc,backend,shots=8192,memory=True)
qobj_dict = qobj.as_dict()

print(qobj_dict)

print( json.dumps(qobj_dict) )

data_dict = {'qObject': qobj_dict,'backend': {'name':device}}
data = json.dumps(data_dict)
print(data)

token = 'insert your API token here'

url = 'https://quantumexperience.ng.bluemix.net/api'
response = requests.post(str(url + "/users/loginWithToken"),data={'apiToken': token})
resp_id = response.json()['id']

job_url = url+'/Jobs?access_token='+resp_id

job = requests.post(job_url, data=data,headers={'Content-Type': 'application/json'})

job_id = job.json()['id']

results_url = url+'/Jobs/'+job_id+'?access_token='+resp_id

result = requests.get(results_url).json()
random_hex = result['qObjectResult']['results'][0]['data']['memory']