KwiatLab / Quantum-Tomography

A python library to help perform tomography on a quantum state
http://tomography.web.engr.illinois.edu/TomographyDemo.php
MIT License
29 stars 9 forks source link

Wrong answer for 2-qubit non-Bell state #39

Closed bernardyeszero closed 11 months ago

bernardyeszero commented 1 year ago

Dear developer,

Thanks for the tomography code. I find the code works fine with Bell state but produce the wrong answer for the non-Bell state. The attached file is an example. The counts are generated from qiskit simulation for a 2-qubit random circuit.

problem_example.pdf

As the Input[6] and Outpu[6] show, the state can reproduce the counts. So, I'm not sure if the tomography code is not applicable for non-Bell states. Could you please help me check the problem?

Besides, I also tried three-particle tomography, I found there is a bug in Line 911 of TomoClass.py, the condition should be: elif(counts.shape[1] == 2**(self.conf['NQubits']))

Best regards,

Bernard

timurjavid commented 11 months ago

Hi @bernardyeszero,

Sorry for the late reply! I just noticed this issue was open.

Thank you for the feedback! I'll start looking into this.

Thanks,

Timur

timurjavid commented 11 months ago

Hi @bernardyeszero, can you also show the qiskit circuit you ran to obtain those counts?

For the fidelity part, it seems like you're calculating the fidelity with the bell state as the true state, however the input state is the approximated state from the circuit you said was random, so the fidelity should be (correctly) low, from what I understand.

bernardyeszero commented 11 months ago

@timurjavid Hi Timur, thank you very much for your response. In the attachment, I give the original code for the generation of simulated measurement counts.

I generate the quantum circuits randomly from QuantumVolume function in qiskit. And I also define a Class named DensityMatrixRe to perform the tomography measurement for arbitrary quantum circuits (that is, Xn to Zn). The Output [6] gives the reconstructed density matrix from qKLib, but the fidelity is just 0.396 compared to the circuit state as shown in Output [7]. If you use the Bell state circuit (as annotated in Input [6]), the reconstructed matrix will be correct, and the fidelity will be unity. You can try it.

tomography.pdf

timurjavid commented 11 months ago

Hi @bernardyeszero, sorry for the late response! I spent way too long trying to figure out what the issue was. There are two main issues happening here, I think.

The first is that our code is currently mishandling the case when NDetectors = 2, misshaping the input data. I made an issue (#40) and will work on fixing it. For now, you can use NDetectors = 1 and "emulate" the data by doing a complete set of measurements as if you are only using one detector (measuring the opposite state). Meaning, if you measure |0>, also measure |1>. Qiskit still uses 2 detectors, however, so there will be lots of redundant data (144 counts instead of 36). In this case, you need to discard all but the first column of counts.

The second issue is that Qiskit reverses the ordering of the qubits, so |01> and |10> terms will be flipped in the density matrix. This causes the state fidelity to completely tank, which might explain partially why you were getting such low fidelities on anything besides Bell states, since Bell states would be unaffected by this issue. To translate between Qiskit and anything else, you need to do reverse_qargs() on the density matrix before you exchange between the two platforms. This was infuriating to find, since I thought it was initially something to do with our tomography code.

I attached a very modified version of your code below. Give it a try and let me know if you have any other issues. Thank you!

tomographyissue39.ipynb.zip

bernardyeszero commented 11 months ago

@timurjavid Hi Timur, I really appreciate your efforts on this issue. It's my fault for the second problem. Indeed, I should realize the bits order of the output from qiskit. From your attached ipynb, the results are consistent. Thanks again!