tencent-quantum-lab / tensorcircuit

Tensor network based quantum software framework for the NISQ era
https://tensorcircuit.readthedocs.io
Apache License 2.0
265 stars 78 forks source link

Add stabilizer simulation example utilizing stim backend #223

Closed king-p3nguin closed 3 months ago

king-p3nguin commented 3 months ago

Resolve #211

How the example works:

  1. Creates random Clifford circuit with mid-circuit measurement. Mid-circuit measurement is inserted only when post-selection to zero state is possible.
  2. Convert TensorCircuit's circuit to a Stim circuit via qir and extract the stabilizer tableau.
  3. Calculate the entanglement entropy from the stabilizer tableau.
  4. Validate the correctness of the entanglement entropy using TensorCircuit's function.

Output:

           ┌───┐┌───┐┌───┐┌───┐                                           ┌───┐┌───┐┌───┐┌───┐┌───┐               ┌───┐┌───┐             
 q_0: ─────┤ H ├┤ H ├┤ S ├┤ Z ├───────────────────────────────────────────┤ X ├┤ H ├┤ X ├┤ Z ├┤ Y ├───────────────┤ X ├┤ H ├─────────────
           ├───┤├───┤└───┘└───┘┌───┐     ┌───┐┌───┐               ┌─┐┌───┐└─┬─┘└───┘└─┬─┘├───┤├───┤┌───┐          └─┬─┘└───┘┌─┐          
 q_1: ─────┤ X ├┤ H ├──────────┤ X ├──■──┤ H ├┤ Y ├───────────────┤M├┤ H ├──┼─────────■──┤ X ├┤ Z ├┤ X ├────────────┼───────┤M├──────────
           ├───┤├───┤     ┌───┐└─┬─┘  │  ├───┤└───┘┌───┐┌───┐     └╥┘└───┘  │       ┌───┐├───┤└┬─┬┘└───┘┌───┐┌───┐  │       └╥┘          
 q_2: ──■──┤ X ├┤ X ├──■──┤ S ├──┼────┼──┤ S ├──■──┤ X ├┤ Y ├──────╫────────┼───────┤ X ├┤ H ├─┤M├───■──┤ S ├┤ H ├──┼────────╫───────────
        │  ├───┤└─┬─┘┌─┴─┐├───┤  │    │  ├───┤  │  ├───┤├───┤┌───┐ ║        │       └─┬─┘└───┘ └╥┘ ┌─┴─┐└───┘└───┘  │  ┌───┐ ║ ┌───┐┌───┐
 q_3: ──┼──┤ H ├──┼──┤ X ├┤ H ├──┼────┼──┤ Z ├──┼──┤ Y ├┤ Z ├┤ H ├─╫────────┼─────────┼─────────╫──┤ X ├───────■────┼──┤ H ├─╫─┤ X ├┤ S ├
        │  ├───┤  │  ├───┤└───┘  │    │  ├───┤  │  ├───┤├───┤├───┤ ║ ┌───┐  │  ┌───┐  │         ║  └───┘       │    │  └───┘ ║ └─┬─┘└───┘
 q_4: ──┼──┤ X ├──■──┤ S ├───────■────┼──┤ Y ├──┼──┤ Z ├┤ Y ├┤ S ├─╫─┤ Y ├──┼──┤ H ├──┼─────────╫──────────────┼────┼────────╫───┼───────
        │  ├───┤┌───┐└───┘          ┌─┴─┐├───┤  │  └┬─┬┘└───┘└───┘ ║ ├───┤  │  ├───┤  │  ┌───┐  ║  ┌───┐ ┌─┐   │    │  ┌───┐ ║   │       
 q_5: ──┼──┤ S ├┤ X ├───────────────┤ X ├┤ H ├──┼───┤M├────────■───╫─┤ Z ├──┼──┤ Z ├──┼──┤ Z ├──╫──┤ Z ├─┤M├───┼────■──┤ Z ├─╫───┼───────
        │  ├───┤├───┤┌───┐┌───┐┌───┐├───┤├───┤  │   └╥┘ ┌───┐  │   ║ ├───┤  │  └───┘  │  └───┘  ║  └───┘ └╥┘   │       └───┘ ║   │       
 q_6: ──┼──┤ X ├┤ Z ├┤ S ├┤ X ├┤ Z ├┤ H ├┤ Z ├──┼────╫──┤ Z ├──┼───╫─┤ Y ├──┼─────────┼─────────╫─────────╫────┼─────────────╫───┼───────
        │  └─┬─┘├───┤├───┤├───┤├───┤└───┘└───┘  │    ║  └───┘  │   ║ └───┘  │  ┌───┐  │  ┌───┐  ║         ║  ┌─┴─┐┌───┐┌───┐ ║   │       
 q_7: ──┼────┼──┤ H ├┤ X ├┤ X ├┤ Y ├────────────┼────╫─────────┼───╫───■────┼──┤ H ├──■──┤ X ├──╫─────────╫──┤ X ├┤ X ├┤ H ├─╫───┼───────
      ┌─┴─┐  │  ├───┤├───┤├───┤└───┘          ┌─┴─┐  ║  ┌───┐  │   ║ ┌─┴─┐  │  ├───┤ ┌─┐ ├───┤  ║  ┌───┐  ║  ├───┤└───┘└───┘ ║   │       
 q_8: ┤ X ├──┼──┤ Z ├┤ Y ├┤ Z ├───────────────┤ X ├──╫──┤ Z ├──┼───╫─┤ X ├──■──┤ H ├─┤M├─┤ H ├──╫──┤ Z ├──╫──┤ H ├───────────╫───┼───────
      └───┘  │  ├───┤├───┤├───┤               └───┘  ║  └───┘┌─┴─┐ ║ ├───┤┌───┐├───┤ └╥┘ ├───┤  ║  └───┘  ║  └───┘           ║   │  ┌───┐
 q_9: ───────■──┤ H ├┤ Y ├┤ H ├──────────────────────╫───────┤ X ├─╫─┤ S ├┤ Z ├┤ H ├──╫──┤ S ├──╫─────────╫──────────────────╫───■──┤ X ├
                └───┘└───┘└───┘                      ║       └───┘ ║ └───┘└───┘└───┘  ║  └───┘  ║         ║                  ║      └───┘
c: 10/═══════════════════════════════════════════════╩═════════════╩══════════════════╩═════════╩═════════╩══════════════════╩═══════════
                                                     0             1                  2         3         4                  5           
Stim Entanglement Entropy: 1.3862943611198906
TensorCircuit Entanglement Entropy: 1.3862943618593357
king-p3nguin commented 3 months ago

Hi @AbdullahKazi500, I modified your code to calculate the correct entanglement entropy and support mid-circuit measurement correctly, and I would not mind splitting the bounty with you. Please let me know what you think.

AbdullahKazi500 commented 3 months ago

Hi @king-p3nguin I appreciate that you informed me and I am happy that you were able to solve it with my code @refraction-ray we can split the bounty I am happy with that, and any decision taken by the organizer thank you once again

refraction-ray commented 3 months ago

Thanks for the contribution, the PR now looks better. However, I can still find the inconsistence of the entropy calculation with num_qubits = 10, depth = 6, where stim gives EE value 0 while tc gives 0.693. Could you debug the issue of this inconsistency? It would also be better to generalize the code a bit to support any partition for EE calculation instead of half chain now as num_qubits//2

Attached is the circuit yielding inconsistent results

Screen Shot 2024-06-11 at 10 38 34 AM
AbdullahKazi500 commented 3 months ago

stimmmcirq Hi @refraction-ray and @king-p3nguin I have updated the code here Stim Entropy in the new patch I have got the results Stim Entanglement Entropy: 0.6931471805599453 TensorCircuit Entanglement Entropy: 0.6931471813752873

The cut variable is used to define the partition size and can be adjusted as needed. The get_bipartite_binary_matrix function is updated to take cut as a parameter. Ensured that postselection logic is correctly applied in both TensorCircuit and Stim. The simulate_stim_circuit_with_mid_measurement function now correctly handles postselections.

king-p3nguin commented 3 months ago

@refraction-ray I was able to fix the bug in the example. I have tested the code with num_qubits = 10, depth=1~30, cut = [i for i in range(num_qubits // 3)].

refraction-ray commented 3 months ago

Thanks for your contribution. I'll assign the issue to both of you first and then further testing the code :)

all the mid-circuit measurements are pushed to the end of the circuit, so I just created a list of operations

That's why we need c.measurement_instruction() for each mid-circuit measurement to record, and merge_qir to ensure the order of gates and mid-circuit measurements

refraction-ray commented 3 months ago

@all-contributors please add @AbdullahKazi500 for example

allcontributors[bot] commented 3 months ago

@refraction-ray

I've put up a pull request to add @AbdullahKazi500! :tada: