3
$\begingroup$

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.h(qr[0])
circuit.measure(qr, cr)
job = execute(circuit, backend = simulator, shots=1024)
a = job.result(job) #the problematic code
print(a)

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

$\endgroup$

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ thanks you! you are very helpful! $\endgroup$ May 7, 2020 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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