from __future__ import annotations from typing import List, Sequence, Tuple import networkx as nx import numpy as np def select_boundary_qubits( boundary_nodes: Sequence[int], max_qubits: int, ) -> Tuple[List[int], np.ndarray]: """ Wähle bis zu ``max_qubits`` Randknoten als Qubit-Standorte und ordne allen Randknoten einen Qubit-Cluster zu. Rückgabe -------- qubit_nodes : Liste der Randknoten, an denen Qubits sitzen. cluster_idx : Array der Länge len(boundary_nodes); cluster_idx[j] ist der Index des Qubits (0 .. len(qubit_nodes)-1), dem Randknoten boundary_nodes[j] zugeordnet ist. """ bnodes = list(boundary_nodes) nB = len(bnodes) if nB == 0: raise ValueError("boundary_nodes darf nicht leer sein.") if max_qubits <= 0: raise ValueError("max_qubits muss > 0 sein.") n_q = min(max_qubits, nB) # equidistante Auswahl entlang der Randliste indices = np.linspace(0, nB - 1, num=n_q, dtype=int) qubit_nodes = [bnodes[i] for i in indices] # Cluster-Zuordnung: ordne jeden Randknoten dem nächstgelegenen Qubit-Index zu cluster_idx = np.empty(nB, dtype=int) for j in range(nB): cluster_idx[j] = int(np.argmin(np.abs(indices - j))) return qubit_nodes, cluster_idx def _pauli_matrices() -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: """Gib (sx, sy, sz, id2) als komplexe 2x2-Matrizen zurück.""" sx = np.array([[0, 1], [1, 0]], dtype=complex) sy = np.array([[0, -1j], [1j, 0]], dtype=complex) sz = np.array([[1, 0], [0, -1]], dtype=complex) id2 = np.eye(2, dtype=complex) return sx, sy, sz, id2 def build_qubit_hamiltonian_xy( G_real: nx.Graph, qubit_nodes: Sequence[int], J: float = 1.0, hz: float = 0.0, ) -> np.ndarray: """ Baue einen einfachen XY-Hamiltonoperator auf einem Qubit-Netzwerk. H = sum_(i,j in E_bath) J * (sx_i sx_j + sy_i sy_j) + sum_i hz * sz_i Die Kanten E_bath werden aus dem REAL-Graphen G_real entnommen, indem nur Kanten zwischen ausgewählten qubit_nodes berücksichtigt werden. Falls dadurch kein Netzwerk entsteht, wird ersatzweise eine lineare Kette 0-1-2-...-n-1 verwendet. """ n = len(qubit_nodes) if n <= 0: raise ValueError("Es müssen mindestens ein Qubit vorhanden sein.") dim = 2**n sx, sy, sz, id2 = _pauli_matrices() # Operator-Hebung auf n Qubits def op_on(i: int, op: np.ndarray) -> np.ndarray: ops = [id2] * n ops[i] = op out = ops[0] for k in range(1, n): out = np.kron(out, ops[k]) return out H = np.zeros((dim, dim), dtype=complex) # Kanten im Bad: nur Kanten zwischen qubit_nodes aus REAL-Graphen idx_map = {node: i for i, node in enumerate(qubit_nodes)} edges: List[Tuple[int, int]] = [] for u, v in G_real.edges(): if u in idx_map and v in idx_map: i, j = idx_map[u], idx_map[v] if i != j: edges.append((min(i, j), max(i, j))) # Duplikate entfernen edges = sorted(set(edges)) # Fallback: lineare Kette, falls keine Kanten im Randnetzwerk if not edges and n > 1: edges = [(i, i + 1) for i in range(n - 1)] # XY-Kopplungen for i, j in edges: H += J * (op_on(i, sx) @ op_on(j, sx) + op_on(i, sy) @ op_on(j, sy)) # Lokale z-Felder if abs(hz) > 0.0: for i in range(n): H += hz * op_on(i, sz) return H def ground_state_sz_covariance(psi0: np.ndarray, n: int) -> Tuple[np.ndarray, np.ndarray]: """ Berechne Erwartungswerte ⟨σ_z_i⟩ und Kovarianzmatrix Cov(σ_z_i, σ_z_j) im Grundzustand psi0 eines n-Qubit-Systems. Wir nutzen, dass σ_z in der Computational-Basis diagonal ist und summieren über alle Basiszustände |s⟩ mit p(s) = |psi0[s]|^2. """ dim = 2**n if psi0.shape[0] != dim: raise ValueError(f"psi0 hat Dimension {psi0.shape[0]}, erwartet {dim}.") sz_expect = np.zeros(n, dtype=float) szsz = np.zeros((n, n), dtype=float) for state in range(dim): amp = psi0[state] p = float((amp.conj() * amp).real) if p < 1e-16: continue # z-Eigenwerte pro Qubit im Computational-Basiszustand `state` z_vals = np.empty(n, dtype=int) for i in range(n): bit = (state >> i) & 1 z_vals[i] = 1 if bit == 0 else -1 sz_expect += p * z_vals for i in range(n): zi = z_vals[i] for j in range(n): szsz[i, j] += p * zi * z_vals[j] C = szsz - np.outer(sz_expect, sz_expect) return sz_expect, C def embed_qubit_covariance_to_boundary( C_qubit: np.ndarray, cluster_idx: np.ndarray, ) -> np.ndarray: """ Hebe die Qubit-Kovarianzmatrix C_qubit (n_q x n_q) auf alle Randknoten an, indem jeder Randknoten dem Cluster seines Qubits zugeordnet wird. C_full[i,j] = C_qubit[k(i), k(j)], wobei k(i) = cluster_idx[i]. """ n_q = C_qubit.shape[0] nB = int(cluster_idx.shape[0]) C_full = np.zeros((nB, nB), dtype=float) for i in range(nB): ki = int(cluster_idx[i]) for j in range(nB): kj = int(cluster_idx[j]) if 0 <= ki < n_q and 0 <= kj < n_q: C_full[i, j] = float(C_qubit[ki, kj]) # numerische Stabilisierung der Diagonale C_full += 1e-6 * np.eye(nB, dtype=float) return C_full