1
$\begingroup$

I have written the following program in jupyter:

from qiskit import *
from qiskit import plot_histogram, matplotlib

circuit = QuantumCircuit(16,16) #Making a circuit with 16 qubits
qr = QuantumRegister(16)
cr = ClassicalRegister(16)
for i in range(16):
    circuit.h(i)
    circuit.measure(i,i)

simulator = Aer.get_backend('qasm_simulator')
result = execute(circuit, backend = simulator, shots = 200).result()
counts = result.get_counts()
print(counts)

and the output is of the form :

{'0111001100011110': 1, '0101000101101001': 1, '1100010111000100': 1, 
'0111111110101110': 1,..., '0110110010010110': 1}

Is there any way that what I get as an output is the measurement only (e.g. '0111001100011110') without getting the times the measurement has appeared (: 1)?

$\endgroup$
1
  • $\begingroup$ A bit of a side note: circuit.h(qr); circuit.measure(qr, cr) is equivalent to that for-loop. $\endgroup$
    – luciano
    Aug 26, 2021 at 13:51

1 Answer 1

4
$\begingroup$

You can take only the keys of the returned dictionary:

measurements = set(counts.keys())
print(measurements)

If you want only one random key, you can just take the first one in the set:

random_key = next(iter(a))

or store all the keys in a list and take a random one from here:

from random import randint
measurements_list = list(measurements)
random_key = measurement_list[randint(0, len(measurement_list)-1)]
$\endgroup$
1
  • $\begingroup$ Thank you. That is exactly what I needed! $\endgroup$
    – Assassinos
    Aug 26, 2021 at 9:23

Your Answer

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

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