@@ -56,40 +56,35 @@ def convert_tc_circuit_to_stim_circuit(tc_circuit):
5656 else :
5757 raise ValueError (f"Unsupported gate: { gate_name } " )
5858 for measurement in tc_circuit ._extra_qir :
59- qubit = measurement ["index" ]
60- stim_circuit .append ("M" , qubit )
59+ qubits = measurement ["index" ]
60+ stim_circuit .append ("M" , qubits )
6161 return stim_circuit
6262
6363
6464# ref: https://quantumcomputing.stackexchange.com/questions/16718/measuring-entanglement-entropy-using-a-stabilizer-circuit-simulator
6565def get_binary_matrix (z_stabilizers ):
6666 N = len (z_stabilizers )
6767 binary_matrix = np .zeros ((N , 2 * N ))
68- r = 0 # Row number
69- for row in z_stabilizers :
70- c = 0 # Column number
71- for i in row :
72- if i == 3 : # Pauli Z
73- binary_matrix [r , N + c ] = 1
74- if i == 2 : # Pauli Y
75- binary_matrix [r , N + c ] = 1
76- binary_matrix [r , c ] = 1
77- if i == 1 : # Pauli X
78- binary_matrix [r , c ] = 1
79- c += 1
80- r += 1
81-
68+ for row_idx , row in enumerate (z_stabilizers ):
69+ for col_idx , col in enumerate (row ):
70+ if col == 3 : # Pauli Z
71+ binary_matrix [row_idx , N + col_idx ] = 1
72+ if col == 2 : # Pauli Y
73+ binary_matrix [row_idx , N + col_idx ] = 1
74+ binary_matrix [row_idx , col_idx ] = 1
75+ if col == 1 : # Pauli X
76+ binary_matrix [row_idx , col_idx ] = 1
8277 return binary_matrix
8378
8479
8580def get_bipartite_binary_matrix (binary_matrix , cut ):
8681 N = len (binary_matrix )
87- cutMatrix = np .zeros ((N , 2 * cut ))
82+ bipartite_binary_matrix = np .zeros ((N , 2 * cut ))
8883
89- cutMatrix [:, :cut ] = binary_matrix [:, :cut ]
90- cutMatrix [:, cut :] = binary_matrix [:, N : N + cut ]
84+ bipartite_binary_matrix [:, :cut ] = binary_matrix [:, :cut ]
85+ bipartite_binary_matrix [:, cut :] = binary_matrix [:, N : N + cut ]
9186
92- return cutMatrix
87+ return bipartite_binary_matrix
9388
9489
9590# ref: https://gist.github.com/StuartGordonReid/eb59113cb29e529b8105?permalink_comment_id=3268301#gistcomment-3268301
@@ -122,15 +117,16 @@ def simulate_stim_circuit_with_mid_measurement(stim_circuit):
122117 for instruction in stim_circuit .flattened ():
123118 if instruction .name == "M" :
124119 for t in instruction .targets_copy ():
125- expectaction = simulator .peek_z (t .value )
126- desired = 0
127- if expectaction == - 1 :
128- desired = 1
129- simulator .postselect_z (t .value , desired_value = desired )
120+ expectaction_value = simulator .peek_z (t .value )
121+ desired_measurement_outcome = 0
122+ if expectaction_value == - 1 : # zero probability to measure "0"
123+ desired_measurement_outcome = 1
124+ simulator .postselect_z (
125+ t .value ,
126+ desired_value = desired_measurement_outcome ,
127+ )
130128 else :
131- c = stim .Circuit ()
132- c .append (instruction )
133- simulator .do_circuit (c )
129+ simulator .do (instruction )
134130
135131 return simulator .current_inverse_tableau () ** - 1
136132
@@ -148,15 +144,16 @@ def simulate_stim_circuit_with_mid_measurement(stim_circuit):
148144 stabilizer_tableau = simulate_stim_circuit_with_mid_measurement (stim_circuit )
149145 zs = [stabilizer_tableau .z_output (k ) for k in range (len (stabilizer_tableau ))]
150146 binary_matrix = get_binary_matrix (zs )
151- cut_matrix = get_bipartite_binary_matrix (binary_matrix , num_qubits // 2 )
152- custom_entropy = (gf2_rank (cut_matrix .tolist ()) - num_qubits // 2 ) * np .log (2 )
153- print ("Stim Entanglement Entropy:" , custom_entropy )
147+ bipartite_matrix = get_bipartite_binary_matrix (binary_matrix , num_qubits // 2 )
148+ stim_entropy = (gf2_rank (bipartite_matrix .tolist ()) - num_qubits // 2 ) * np .log (2 )
149+ print ("Stim Entanglement Entropy:" , stim_entropy )
154150
155151 # Entanglement entropy calculation using TensorCircuit
156152 state_vector = tc_circuit .wavefunction ()
157153 assert np .linalg .norm (state_vector ) > 0
154+ # Normalize the state vector because mid-measurement operation is not unitary
158155 state_vector /= np .linalg .norm (state_vector )
159- baseline_entropy = tc .quantum .entanglement_entropy (state_vector , num_qubits // 2 )
160- print ("TensorCircuit Entanglement Entropy:" , baseline_entropy )
156+ tc_entropy = tc .quantum .entanglement_entropy (state_vector , num_qubits // 2 )
157+ print ("TensorCircuit Entanglement Entropy:" , tc_entropy )
161158
162- np .testing .assert_allclose (custom_entropy , baseline_entropy , atol = 1e-8 )
159+ np .testing .assert_allclose (stim_entropy , tc_entropy , atol = 1e-8 )
0 commit comments