1
$\begingroup$

enter image description here

This is a simulation I did in my program. I'm hoping to print out the probability of the state $|0001\rangle$ (which is $0.052$), and store this number in a letter (a). How can I do this? Thanks!!

$\endgroup$
3
$\begingroup$

.get_counts() returns a dictionary with the keys being the measured state and their corresponding values being the number of times that result was measured. To get the number of times that state was measured you can do:

print(counts['0001'])

To get probability, you need to divide this by the total number of shots. If you're not sure of the number of shots, you can sum the total number of shots in the dictionary using:

sum(counts.values())

and from this print the probability:

print(counts['0001'] / sum(counts.values()))
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thanks!! That helps:) $\endgroup$ – Zhengrong Sep 3 at 19:28

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.