1
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ why not just measure only the 50th qubit if you only interested only in the result of this qubit? $\endgroup$ – KAJ226 Jul 5 at 2:44
  • $\begingroup$ 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 $\endgroup$ – LOC Jul 5 at 3:40
  • $\begingroup$ Take marginal counts $\endgroup$ – Paul Nation Jul 5 at 9:03
4
$\begingroup$

To add to the already existing answers you can also use qiskit.result.marginal_counts.

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.

$\endgroup$
1
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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)]
qc.add_register(*cr)

# ... ... ...

# 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])
$\endgroup$
0
$\begingroup$

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))  
$\endgroup$
0
$\begingroup$

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

https://stackoverflow.com/questions/68249614/using-get-on-a-dictionary-of-2n-numbers-each-made-up-of-n-0s-or-1s-by-only-lo

Basically if I call the results of my quantum circuit my_res, then the following code gives a simple one line way to get the number of counts for any given qubit (I have used the 50th qubit as an example):

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

I suspect this is what the marginal_counts function does

$\endgroup$

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.