from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute, Aer
from qiskit.tools.visualization import circuit_drawer

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

circuit.h(qr[0])
circuit.measure(qr[0],cr[0])

circuit_drawer(circuit)

from qiskit import Aer
backend = Aer.get_backend('qasm_simulator')

job = execute(circuit, backend,shots=1024)


result=job.result()
result.get_counts(circuit)

expectation =(result.get_counts(circuit)['0']-result.get_counts(circuit)['1'])/(result.get_counts(circuit)['0']
                                                                                +result.get_counts(circuit)['1'])

print(expectation)

