Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stabilizer simulation example utilizing stim backend #223

Merged
merged 5 commits into from
Jun 14, 2024

Conversation

king-p3nguin
Copy link
Contributor

@king-p3nguin king-p3nguin commented Jun 10, 2024

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
Copy link
Contributor Author

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
Copy link

AbdullahKazi500 commented Jun 10, 2024

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
Copy link
Contributor

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
Copy link

AbdullahKazi500 commented Jun 11, 2024

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
Copy link
Contributor Author

king-p3nguin commented Jun 13, 2024

@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)].

  • tc to stim circuit conversion via qir does not seem a good idea because when converted to qir, all the mid-circuit measurements are pushed to the end of the circuit, so I just created a list of operations.
  • np.isclose() is used to check the probability since even if the probability is close to 1.0, p < 1.0 can be True due to floating-point error.
  • I also modified the code to accept tracing out any qubits.

@refraction-ray
Copy link
Contributor

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 refraction-ray merged commit 596582f into tencent-quantum-lab:master Jun 14, 2024
2 checks passed
@refraction-ray
Copy link
Contributor

@all-contributors please add @AbdullahKazi500 for example

Copy link
Contributor

@refraction-ray

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

@king-p3nguin king-p3nguin deleted the stim branch June 14, 2024 06:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stabilizer simulation example utilizing stim backend
3 participants