I have a simple 2 qubit circuit which I am trying to protect from errors using the measurement error mitigation technique laid out here: https://qiskit.org/textbook/ch-quantum-hardware/measurement-error-mitigation.html. My circuit is
qr=QuantumRegister(2)
circuit2 = QuantumCircuit();
circuit2.add_register(qr)
cr=ClassicalRegister(2)
circuit2.add_register(cr)
circuit2.measure(0,0);
circuit2.measure(1,1);
noise_model = NoiseModel()
noise_model.add_all_qubit_readout_error([[1 - Er0,Er0], [Er1, 1 - Er1]])
result = execute(circuit2,backend=Aer.get_backend('qasm_simulator'),shots=maxShot,noise_model=noise_model).result()
counts = result.get_counts(0)
I then get the counts for each outcome and store it in a vector
n00=counts.get('00')
etc..
n2qvec=np.array([n00,n01,n10,n11])
As is laid out in the tutorial I obtain the calibration matrix and filter
aer_sim = Aer.get_backend('aer_simulator')
qr2q = QuantumRegister(2)
my_layout2q={qr2q[0]:0,qr2q[1]:1}
meas_calibs2q, state_labels2q = complete_meas_cal(qr=qr2q, circlabel='mcal2q')
t_qc = transpile(meas_calibs2q, aer_sim)
qobj = assemble(t_qc, shots=10000)
noise_model = NoiseModel()
noise_model.add_all_qubit_readout_error([[1 - Er0,Er0], [Er1, 1 - Er1]])
cal_results2q = aer_sim.run(qobj, shots=10000,noise_model=noise_model).result()
meas_fitter2q = CompleteMeasFitter(cal_results2q, state_labels2q, circlabel='mcal2q')
meas_filter2q = meas_fitter2q.filter
calmat2q=meas_fitter2q.cal_matrix
import scipy.linalg as la
calmatinv2q = la.inv(calmat2q)
I then apply this to the earlier results to account for the noise
mitigated_results2q = meas_filter2q.apply(res2q[1])
mitigated_counts2q = mitigated_results2q.get_counts()
print(mitigated_counts2q)
This works sensibly. However I thought that this is equivalent to applying the inverse of the calibration matrix to the vector of results that I have:
print(np.dot(calmatinv2q,n2qvec))
print(min(np.dot(calmatinv2q,n2qvec)))
However sometimes this returns a negative counts. Obviously this isn't sensible so I assume that the measurement filter takes care of this somehow. Does anyone know how this is done? I wish to be able to do this manually because ideally I will run multiple two qubit circuits in parallel on the same quantum computer. So rather than running $2^n$ calibration circuits I will run 4 calibration circuits for each pair of qubits in isolation.