# How can I get the number of measurements where any specific qubit is equal to 0?

Say I am using a 100 qubit quantum computer, but my circuit is such that I am only interested in the output of a particular qubit. So for example I want to know how many outcomes had a result of 0 on the 50th qubit. Is there an easy way to do this rather than writing out all possible combinations which have this result?

Obviously writing all the combinations becomes more computationally expensive as the number of qubits increases.

• why not just measure only the 50th qubit if you only interested only in the result of this qubit? Jul 5 at 2:44
• I want to be able to access the results of any qubit in isolation. In this example it was the 50th, but I could also want to look at the other 99 results in isolation
– LOC
Jul 5 at 3:40
• Take marginal counts Jul 5 at 9:03

To illustrate, I took the circuit from @KAJ226 answer verbatim:

from qiskit import QuantumCircuit, BasicAer, execute
circuit = QuantumCircuit(5,5)
for i in range(5):
circuit.h(i)
circuit.measure([0,1,2,3,4],[0,1,2,3,4])
circuit.draw()
# Get results
backend = BasicAer.get_backend('qasm_simulator')
job = execute(circuit, backend, shots = 100)

┌───┐┌─┐
q_0: ┤ H ├┤M├────────────
├───┤└╥┘┌─┐
q_1: ┤ H ├─╫─┤M├─────────
├───┤ ║ └╥┘┌─┐
q_2: ┤ H ├─╫──╫─┤M├──────
├───┤ ║  ║ └╥┘┌─┐
q_3: ┤ H ├─╫──╫──╫─┤M├───
├───┤ ║  ║  ║ └╥┘┌─┐
q_4: ┤ H ├─╫──╫──╫──╫─┤M├
└───┘ ║  ║  ║  ║ └╥┘
c: 5/══════╩══╩══╩══╩══╩═
0  1  2  3  4


and then you can post-process the results obtained using this marginal_counts function:

from qiskit.result import marginal_counts
full_results = job.result()

# Can be any qubit index or any list of qubit indices.
qubits_of_interest = [0]

marginalised_results = marginal_counts(full_results, indices=qubits_of_interest)
marginalised_counts = marginalised_results.get_counts()

print("Frequency of zeros =", marginalised_counts.get("0", 0) / sum(marginalised_counts.values()))


This solution have the advantage of not requiring you to split the classical registers. On the other side, I do not think it is adapted if you have more than one classical register in your circuit.

I think without knowing any specific structure about the state, then you might just have to go through all the samples and count the states that have the property that you are looking for.

For instance, suppose I have the following circuit:

from qiskit import QuantumCircuit, BasicAer, execute
circuit = QuantumCircuit(5,5)
for i in range(5):
circuit.h(i)
circuit.measure([0,1,2,3,4],[0,1,2,3,4])
circuit.draw()

┌───┐┌─┐
q_0: ┤ H ├┤M├────────────
├───┤└╥┘┌─┐
q_1: ┤ H ├─╫─┤M├─────────
├───┤ ║ └╥┘┌─┐
q_2: ┤ H ├─╫──╫─┤M├──────
├───┤ ║  ║ └╥┘┌─┐
q_3: ┤ H ├─╫──╫──╫─┤M├───
├───┤ ║  ║  ║ └╥┘┌─┐
q_4: ┤ H ├─╫──╫──╫──╫─┤M├
└───┘ ║  ║  ║  ║ └╥┘
c: 5/══════╩══╩══╩══╩══╩═
0  1  2  3  4


Then if I want to find the number of states that have the state |0\rangle\ at the 3rd position out of 100 samples/shots then I can do:

backend = BasicAer.get_backend('qasm_simulator')
job = execute(circuit, backend, shots = 100)
num_interested_states = 0
zero_position = 2
for binary in job.result().get_counts():
if binary[zero_position] == '0':
num_interested_states += 1
print('num_interested_states:', num_interested_states)


num_interested_states: 14

Update

I think the answer provided by @Adrien Suau could be more suitable for this case.

You can use get_subsystems_counts. But first you need to measure each subsystem in its own classical register. And since you want to deal with individual qubits, you will need a classical register each qubit:

# Add the classical registers. N is the total number of qubits
cr = [ClassicalRegister(1) for k in range(N)]

# ... ... ...

# Measure:
for k in range(N):
qc.measure(k, cr[k])


Now you can extract a qubit counts as follows, assuming that counts is the total counts for the complete system from your experiment results:

from qiskit.aqua.utils.subsystem import get_subsystems_counts

qubit_index = 50
post_selection_value = '0'
subsystem_counts = get_subsystems_counts(counts, qubit_index, post_selection_value)
print(subsystem_counts[qubit_index])


In simulation mode, you can use appropriate partial trace on Density Matrix of Quantum Operator/Circuit to isolate statevector of specific qubit(s).

# Sample Code: partial trace on Density Matrix

from qiskit import QuantumCircuit
import qiskit.quantum_info as qi
from qiskit.visualization import plot_bloch_multivector

# create a quantum circuit qc
qc = QuantumCircuit(3)
qc.id(0)
qc.x(1)
qc.h(2)
display(qc.draw('mpl'))

# get Density Matrix for quantum circuit qc
dm = qi.DensityMatrix.from_instruction(qc)
sv = dm.to_statevector()
print(sv.data)
display(plot_bloch_multivector(sv))

# get partial trace for qubit-1
dm_q1 = qi.partial_trace(dm, [0, 2])
sv_q1 = dm_q1.to_statevector()
print(sv_q1.data)
display(plot_bloch_multivector(sv_q1))


I got an answer to my question from some guys on the python stack exchange:

sum(v for k,v in my_res.items() if k[50]('1'))
`