|
13 | 13 | # Convention (sigma, chi_l-1, chi_l) |
14 | 14 | class MPS: |
15 | 15 | def __init__(self, length: int, tensors: list = None, physical_dimensions: list = None, state: str = 'zeros'): |
| 16 | + self.flipped = False |
16 | 17 | if tensors is not None: |
17 | 18 | assert len(tensors) == length |
18 | 19 | self.tensors = tensors |
@@ -65,14 +66,19 @@ def __init__(self, length: int, tensors: list = None, physical_dimensions: list |
65 | 66 | vector[0] = 1 |
66 | 67 | else: |
67 | 68 | vector[1] = 1 |
| 69 | + elif state == 'random': |
| 70 | + vector[0] = np.random.rand() |
| 71 | + vector[1] = 1 - vector[0] |
68 | 72 | else: |
69 | 73 | raise ValueError("Invalid state string") |
70 | 74 |
|
71 | 75 | tensor = np.expand_dims(vector, axis=(0, 1)) |
72 | 76 |
|
73 | 77 | tensor = np.transpose(tensor, (2, 0, 1)) |
74 | 78 | self.tensors.append(tensor) |
75 | | - self.flipped = False |
| 79 | + |
| 80 | + if state == 'random': |
| 81 | + self.normalize() |
76 | 82 |
|
77 | 83 | def write_max_bond_dim(self) -> int: |
78 | 84 | global_max = 0 |
@@ -279,10 +285,44 @@ def convert_to_vector(self) -> np.ndarray: |
279 | 285 | vec = vec.flatten() |
280 | 286 | return vec |
281 | 287 |
|
| 288 | + def convert_to_vector(self) -> np.ndarray: |
| 289 | + """ |
| 290 | + Converts the MPS to a full state vector representation. |
| 291 | +
|
| 292 | + Returns: |
| 293 | + A one-dimensional NumPy array of length \(\prod_{\ell=1}^L d_\ell\) |
| 294 | + representing the state vector. |
| 295 | + """ |
| 296 | + # Start with the first tensor. |
| 297 | + # Assume each tensor has shape (d, chi_left, chi_right) with chi_left=1 for the first tensor. |
| 298 | + vec = self.tensors[0] # shape: (d_1, 1, chi_1) |
| 299 | + |
| 300 | + # Contract sequentially with the remaining tensors. |
| 301 | + for i in range(1, self.length): |
| 302 | + # Contract the last bond of vec with the middle index (left bond) of the next tensor. |
| 303 | + vec = np.tensordot(vec, self.tensors[i], axes=([-1], [1])) |
| 304 | + # After tensordot, if vec had shape (..., chi_i) and the new tensor has shape (d_{i+1}, chi_i, chi_{i+1}), |
| 305 | + # then vec now has shape (..., d_{i+1}, chi_{i+1}). |
| 306 | + # Reshape to merge all physical indices into one index. |
| 307 | + new_shape = (-1, vec.shape[-1]) |
| 308 | + vec = np.reshape(vec, new_shape) |
| 309 | + |
| 310 | + # At the end, the final bond dimension should be 1. |
| 311 | + vec = np.squeeze(vec, axis=-1) |
| 312 | + # Flatten the resulting multi-index into a one-dimensional state vector. |
| 313 | + vec = vec.flatten() |
| 314 | + return vec |
| 315 | + |
| 316 | + |
| 317 | + |
| 318 | + |
| 319 | + |
| 320 | + |
282 | 321 |
|
283 | 322 | # Convention (sigma, sigma', chi_l, chi_l+1) |
284 | 323 | class MPO: |
285 | | - def init_Ising(self, length: int, physical_dimension: int, J: float, g: float): |
| 324 | + def init_Ising(self, length: int, J: float, g: float): |
| 325 | + physical_dimension = 2 |
286 | 326 | zero = np.zeros((physical_dimension, physical_dimension), dtype=complex) |
287 | 327 | identity = np.eye(physical_dimension, dtype=complex) |
288 | 328 | X = getattr(GateLibrary, "x")().matrix |
@@ -323,7 +363,8 @@ def init_Ising(self, length: int, physical_dimension: int, J: float, g: float): |
323 | 363 | self.length = length |
324 | 364 | self.physical_dimension = physical_dimension |
325 | 365 |
|
326 | | - def init_Heisenberg(self, length: int, physical_dimension: int, Jx: float, Jy: float, Jz: float, h: float): |
| 366 | + def init_Heisenberg(self, length: int, Jx: float, Jy: float, Jz: float, h: float): |
| 367 | + physical_dimension = 2 |
327 | 368 | zero = np.zeros((physical_dimension, physical_dimension), dtype=complex) |
328 | 369 | identity = np.eye(physical_dimension, dtype=complex) |
329 | 370 | X = getattr(GateLibrary, "x")().matrix |
|
0 commit comments