import random
for n in range(5):
    print('Flip '+str(n+1))
    if random.random()<0.5:
        print('HEADS\n')
    else:
        print('TAILS\n')

import time
for n in range(5):
    t = str(time.time())
    print('Flip '+str(n+1)+'  (system time = ' + t + ')')
    if int(t[-1])%2==0:
        print('HEADS\n')
    else:
        print('TAILS\n')

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit

q = QuantumRegister(1)
c = ClassicalRegister(1)
circ = QuantumCircuit(q, c)

circ.h(q)

circ.measure(q, c)

from qiskit import BasicAer, execute

backend = BasicAer.get_backend('qasm_simulator')
job = execute(circ, backend, shots=5, memory=True)

data = job.result().get_memory()
print(data)

for output in data:
    print('Flip with output bit value ' +output)
    if output=='0':
        print('HEADS\n')
    else:
        print('TAILS\n')

from qiskit import IBMQ

IBMQ.load_accounts()

backend = IBMQ.get_backend('ibmq_5_tenerife')
job = execute(circ, backend, shots=5, memory=True)

for output in job.result().get_memory():
    print('Flip with output bit value ' +output)
    if output=='0':
        print('HEADS\n')
    else:
        print('TAILS\n')

job.result().get_memory()

n = 3

q = QuantumRegister(n)
c = ClassicalRegister(n)
circ = QuantumCircuit(q, c)

for j in range(n):
    circ.h(q[j])
    
circ.measure(q,c)

job = execute(circ, BasicAer.get_backend('qasm_simulator'), shots=8192)

# get the histogram of bit string results, convert it to one of integers and plot it
bit_counts = job.result().get_counts()
int_counts = {}
for bitstring in bit_counts:
    int_counts[ int(bitstring,2) ] = bit_counts[bitstring]

from qiskit.tools.visualization import plot_histogram    
plot_histogram(int_counts)

q = QuantumRegister(n)
c = ClassicalRegister(n)
circ = QuantumCircuit(q, c)

for j in range(n):
    circ.rx(3.14159/(2.5+j),q[j])
    
circ.measure(q,c)
    
job = execute(circ, BasicAer.get_backend('qasm_simulator'), shots=8192)

bit_counts = job.result().get_counts()
int_counts = {}
for bitstring in bit_counts:
    int_counts[ int(bitstring,2) ] = bit_counts[bitstring]
plot_histogram(int_counts)

job = execute(circ, BasicAer.get_backend('qasm_simulator'), shots=10, memory=True)
data = job.result().get_memory()
print(data)

int_data = []
for bitstring in data:
    int_data.append( int(bitstring,2) )
print(int_data)

