|
1 | 1 | """ |
2 | | -Demonstration of TeNPy-DMRG and TensorCircuit integration |
3 | | -1. Compute ground state (MPS) of 1D Transverse Field Ising model using TeNPy |
4 | | -2. Convert MPS to TensorCircuit's QuOperator |
5 | | -3. Initialize MPSCircuit with converted state and verify results |
| 2 | +Demonstrates the different internal basis conventions between |
| 3 | +TeNPy's TFIChain and XXZChain models and showcases a robust |
| 4 | +method for handling these inconsistencies when converting to TensorCircuit. |
| 5 | +
|
| 6 | +1. TFIChain: Shows a direct conversion works perfectly. |
| 7 | +2. XXZChain: |
| 8 | + a. Runs DMRG to obtain a non-trivial ground state. |
| 9 | + b. Shows that direct conversion leads to incorrect expectation values for correlation functions. |
| 10 | + c. Demonstrates that applying a layer of X-gates in TensorCircuit |
| 11 | +3. Tensor Dissection: Provides definitive proof of the differing internal basis conventions between the two models. |
6 | 12 | """ |
7 | 13 |
|
8 | 14 | import numpy as np |
|
14 | 20 | import tensorcircuit as tc |
15 | 21 |
|
16 | 22 | print("Scenario 1: Antiferromagnetic State (TFIChain)") |
17 | | - |
18 | 23 | L = 10 |
19 | 24 | afm_model_params = {"L": L, "bc_MPS": "finite", "J": 1.0, "g": 0.0, "conserve": None} |
20 | 25 | afm_M = TFIChain(afm_model_params) |
|
33 | 38 | for i in range(L) |
34 | 39 | ] |
35 | 40 | ) |
36 | | - |
37 | 41 | print("\nAntiferromagnetic state site-by-site magnetization comparison:") |
38 | 42 | print("TeNPy:", np.round(mag_z_afm_tenpy, 8)) |
39 | 43 | print("TC: ", np.round(mag_z_afm_tc, 8)) |
|
42 | 46 | "\n[SUCCESS] TFI-based Antiferromagnetic state matches perfectly with the pure converter." |
43 | 47 | ) |
44 | 48 |
|
45 | | -# --- Scenario 2: XXZChain Model --- |
46 | | -print("Scenario 2: XXZChain Model") |
47 | 49 |
|
| 50 | +print("Scenario 2: XXZChain Model") |
48 | 51 | xxz_model_params = {"L": L, "bc_MPS": "finite", "Jxx": 1.0, "Jz": 0.5, "hz": 0.1} |
49 | 52 | xxz_M = XXZChain(xxz_model_params) |
50 | | -rng = np.random.default_rng(42) |
51 | | -random_state = rng.choice(["up", "down"], size=L).tolist() |
52 | | -print(f"Testing with a random product state: {random_state}") |
53 | | - |
| 53 | +example_state = ["up", "down", "up", "up", "down", "down", "up", "down", "down", "up"] |
| 54 | +print(f"Testing with a random product state: {example_state}") |
54 | 55 | psi_rand_xxz = MPS.from_product_state( |
55 | | - xxz_M.lat.mps_sites(), random_state, bc=xxz_M.lat.bc_MPS |
| 56 | + xxz_M.lat.mps_sites(), example_state, bc=xxz_M.lat.bc_MPS |
56 | 57 | ) |
57 | 58 | tc_rand_xxz_state = tc.quantum.tenpy2qop(psi_rand_xxz) |
58 | 59 | circuit_rand_xxz = tc.MPSCircuit(L, wavefunction=tc_rand_xxz_state) |
59 | | - |
60 | 60 | mag_z_rand_xxz_tenpy = psi_rand_xxz.expectation_value("Sz") |
61 | 61 | mag_z_rand_xxz_tc = np.array( |
62 | 62 | [ |
|
67 | 67 | for i in range(L) |
68 | 68 | ] |
69 | 69 | ) |
70 | | - |
71 | 70 | print("\nXXZ-based random state site-by-site magnetization comparison:") |
72 | 71 | print("TeNPy:", np.round(mag_z_rand_xxz_tenpy, 8)) |
73 | 72 | print("TC: ", np.round(mag_z_rand_xxz_tc, 8)) |
74 | | - |
75 | 73 | try: |
76 | 74 | np.testing.assert_allclose(mag_z_rand_xxz_tenpy, mag_z_rand_xxz_tc, atol=1e-5) |
77 | 75 | except AssertionError as e: |
|
85 | 83 | "[SUCCESS] Test passes after applying the sign correction for the XXZChain model." |
86 | 84 | ) |
87 | 85 |
|
88 | | -# --- Scenario 3: Tensor Dissection for Both Models --- |
89 | | -print("Scenario 3: Tensor Dissection for Both Models") |
90 | 86 |
|
| 87 | +print("Scenario 3: Tensor Dissection for Both Models") |
91 | 88 | simple_L = 2 |
92 | 89 | simple_labels = ["up", "down"] |
93 | | - |
94 | 90 | print("\nDissecting TFIChain-based Tensors") |
95 | 91 | sites_tfi = afm_M.lat.mps_sites()[:simple_L] |
96 | 92 | psi_simple_tfi = MPS.from_product_state(sites_tfi, simple_labels, bc="finite") |
|
99 | 95 | print( |
100 | 96 | f"For '{label}', TFIChain internal tensor has non-zero at physical index {np.where(B_tensor[0,:,0] != 0)[0][0]}" |
101 | 97 | ) |
102 | | - |
103 | 98 | print("\nDissecting XXZChain-based Tensors") |
104 | 99 | sites_xxz = xxz_M.lat.mps_sites()[:simple_L] |
105 | 100 | psi_simple_xxz = MPS.from_product_state(sites_xxz, simple_labels, bc="finite") |
|
108 | 103 | print( |
109 | 104 | f"For '{label}', XXZChain internal tensor has non-zero at physical index {np.where(B_tensor[0,:,0] != 0)[0][0]}" |
110 | 105 | ) |
111 | | - |
112 | | - |
113 | | -print("\nFinal Conclusion") |
| 106 | +print("\n Conclusion") |
114 | 107 | print("The dissection above shows the root cause of the different behaviors:") |
115 | 108 | print( |
116 | 109 | " - TFIChain's 'up' maps to index 0, 'down' to index 1. This matches TC's standard." |
|
124 | 117 | ) |
125 | 118 | print("or to create model-specific converters.") |
126 | 119 |
|
| 120 | + |
| 121 | +print("--- Scenario 3: Correcting XXZChain DMRG state with X-gates ---") |
| 122 | + |
| 123 | +L = 10 |
| 124 | +xxz_model_params = {"L": L, "bc_MPS": "finite", "Jxx": 1.0, "Jz": 1.0, "conserve": None} |
| 125 | +xxz_M = XXZChain(xxz_model_params) |
| 126 | +psi0_xxz = MPS.from_product_state( |
| 127 | + xxz_M.lat.mps_sites(), ["up", "down"] * (L // 2), bc=xxz_M.lat.bc_MPS |
| 128 | +) |
| 129 | +dmrg_params = {"max_sweeps": 10, "trunc_params": {"chi_max": 64}} |
| 130 | +eng = dmrg.TwoSiteDMRGEngine(psi0_xxz, xxz_M, dmrg_params) |
| 131 | +E, psi_gs_xxz = eng.run() |
| 132 | +print(f"XXZ DMRG finished. Ground state energy: {E:.10f}") |
| 133 | + |
| 134 | +state_raw_quvector = tc.quantum.tenpy2qop(psi_gs_xxz) |
| 135 | + |
| 136 | +i, j = L // 2 - 1, L // 2 |
| 137 | +corr_tenpy = psi_gs_xxz.correlation_function("Sz", "Sz", sites1=[i], sites2=[j])[0, 0] |
| 138 | +print("\nApplying X-gate to each qubit to correct the basis convention...") |
| 139 | +circuit_to_be_corrected = tc.MPSCircuit(L, wavefunction=state_raw_quvector) |
| 140 | + |
| 141 | +for k in range(L): |
| 142 | + circuit_to_be_corrected.x(k) |
| 143 | + |
| 144 | +corr_tc_corrected = ( |
| 145 | + tc.backend.real( |
| 146 | + circuit_to_be_corrected.expectation((tc.gates.z(), [i]), (tc.gates.z(), [j])) |
| 147 | + ) |
| 148 | + / 4.0 |
| 149 | +) |
| 150 | + |
| 151 | +print(f"\nComparing <Sz_{i}Sz_{j}> correlation function for the DMRG ground state:") |
| 152 | +print(f"TeNPy (Ground Truth): {corr_tenpy:.8f}") |
| 153 | +print(f"TC (after X-gate correction): {corr_tc_corrected:.8f}") |
| 154 | +np.testing.assert_allclose(corr_tenpy, corr_tc_corrected, atol=1e-5) |
| 155 | +print( |
| 156 | + "\n[SUCCESS] The correlation functions match perfectly after applying the X-gate correction." |
| 157 | +) |
| 158 | +print( |
| 159 | + "This demonstrates the recommended physical approach to handle the XXZChain's inverted basis convention." |
| 160 | +) |
| 161 | + |
| 162 | + |
127 | 163 | print("\n\nWorkflow demonstration and analysis complete!") |
0 commit comments