I am new to quantum computing, and I want to make a program to output 0 or 1 randomly by Hadamard gate, and use that information to make a GUI interface. For example, a coin flip program that output "Tail" when it is 1, and "Head" when it is 0. However, when I try to get raw data by job.result(job), I get the information of the result instead. By the way, here is the code:

from qiskit import *
simulator = Aer.get_backend('qasm_simulator')
qr = QuantumRegister(1)
cr = ClassicalRegister(1)
circuit = QuantumCircuit(qr, cr)
circuit.measure(qr, cr)
job = execute(circuit, backend = simulator, shots=1024)
a = job.result(job) #the problematic code

I use Qiskit with Python, and do not use Jupyter notebooks.


You need to do .get_counts() to get a dictionary of the counts returned by the circuit. You would do this as follows

job = execute(circuit, backend = simulator, shots=100)
result = job.result()
counts_dict = result.get_counts()

Using your circuit and this code, counts_dict will look something like {'0':497, '1':503}. From there you might want to extract the key which has a higher value to be the result of your coin flip.

  • 1
    $\begingroup$ thanks you! you are very helpful! $\endgroup$ – Hoang An Nguyen May 7 '20 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.