How to get partial probability from measurements in qiskit?

Is there a way to get partial probability distributions on Qiskit?

Consider a quantum circuit measuring all the qubits. I want to retrieve the probability of, say, outcome 01 for 1st and 2nd qubits and 11 for 3rd and 4th qubits (I am using little-endian notation like Qiskit).

I was wondering whether Qiskit provides a way to do this.

You can use the marginal_counts() function from qiskit.result to get the marginalized counts for the bits that you want (documentation link). Here is a basic implementation of that:

from qiskit import QuantumCircuit
from qiskit_aer import AerSimulator
from qiskit.result import marginal_counts

qc = QuantumCircuit(4)
qc.h([0, 1, 2, 3])
qc.cx(1,3)
qc.cx(2, 0)
qc.measure_all()

sim = AerSimulator()
job = sim.run(qc, shots=1024)
res = job.result().get_counts()
print(res)

new_counts = marginal_counts(res, [0, 1])  # To get the counts from qubit 0 and 1
print(new_counts)


Also you can take a look at a slightly different function marginal_distribution here