|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "id": "37a622fc-6c7a-4b52-909e-b6bcc5e0b2e5", |
| 7 | + "metadata": {}, |
| 8 | + "source": [ |
| 9 | + "# Quantum-Selected Configuration Interaction (QSCI)\n", |
| 10 | + "\n", |
| 11 | + "Quantum-Selected Configuration Interaction (QSCI) [1] is a quantum-classical hybrid algorithm designed for electronic structure calculations, particularly to compoute ground state and excited state energies of molecular systems. This method is one of the quantum subspace diagonalization (QSD). It is well-suited for NISQ-era quantum devices, focusing on using quantum resources efficiently while maintaining high accuracy.\n", |
| 12 | + "\n", |
| 13 | + "\n", |
| 14 | + "\n", |
| 15 | + "QSCI selects this vector space from a sampled computational basis." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "markdown", |
| 20 | + "id": "1388e9b0-1b9b-4df4-890c-117622b771c4", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "## 0. Problem definition\n", |
| 24 | + "\n", |
| 25 | + "Let's define Hamiltonian of $\\mathrm{H_4}$." |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "code", |
| 30 | + "execution_count": 1, |
| 31 | + "id": "175153b6-3bb4-4378-a174-cead14589e04", |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "!pip install --quiet --break-system-packages openfermionpyscf==0.5" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 2, |
| 41 | + "id": "93c9a86f-6eab-48fb-9edf-51c7d25e9f1e", |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "import openfermion\n", |
| 46 | + "import openfermionpyscf\n", |
| 47 | + "from openfermion.transforms import jordan_wigner, get_fermion_operator\n", |
| 48 | + "import cudaq" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": 3, |
| 54 | + "id": "336fd99f-7e4e-4401-a60e-eecb9c869299", |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "# Number of hydrogen atoms.\n", |
| 59 | + "hydrogen_count = 4\n", |
| 60 | + "\n", |
| 61 | + "# Distance between the atoms in Angstroms.\n", |
| 62 | + "bond_distance = 0.7474\n", |
| 63 | + "\n", |
| 64 | + "# Define a linear chain of Hydrogen atoms\n", |
| 65 | + "geometry = [(\"H\", (0, 0, i * bond_distance)) for i in range(hydrogen_count)]\n", |
| 66 | + "\n", |
| 67 | + "basis = \"sto3g\"\n", |
| 68 | + "multiplicity = 1\n", |
| 69 | + "charge = 0\n", |
| 70 | + "\n", |
| 71 | + "molecule = openfermionpyscf.run_pyscf(\n", |
| 72 | + " openfermion.MolecularData(geometry, basis, multiplicity, charge), run_fci=True\n", |
| 73 | + ")\n", |
| 74 | + "molecular_hamiltonian = molecule.get_molecular_hamiltonian()\n", |
| 75 | + "fermion_hamiltonian = get_fermion_operator(molecular_hamiltonian)\n", |
| 76 | + "qubit_hamiltonian = jordan_wigner(fermion_hamiltonian)\n", |
| 77 | + "qubit_hamiltonian.compress()\n", |
| 78 | + "\n", |
| 79 | + "hamiltonian = cudaq.SpinOperator(qubit_hamiltonian)\n", |
| 80 | + "\n", |
| 81 | + "num_qubits = hamiltonian.qubit_count\n", |
| 82 | + "electron_count = molecule.n_electrons" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "markdown", |
| 87 | + "id": "908956ec-a201-416d-bc37-f09c6ba02b85", |
| 88 | + "metadata": {}, |
| 89 | + "source": [ |
| 90 | + "## 1. Prepare an Approximate Quantum State\n", |
| 91 | + "\n", |
| 92 | + "Start with a variational ansatz (e.g., from VQE) to approximate the ground state wavefunction on a quantum computer." |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": 4, |
| 98 | + "id": "04b6e475-72bc-4f4a-bed7-32488d044e0a", |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [], |
| 101 | + "source": [ |
| 102 | + "import numpy as np\n", |
| 103 | + "\n", |
| 104 | + "\n", |
| 105 | + "@cudaq.kernel\n", |
| 106 | + "def kernel(thetas: list[float]):\n", |
| 107 | + "\n", |
| 108 | + " qubits = cudaq.qvector(num_qubits)\n", |
| 109 | + "\n", |
| 110 | + " # Hartree-Fock\n", |
| 111 | + " for i in range(electron_count):\n", |
| 112 | + " x(qubits[i])\n", |
| 113 | + " # UCCSD\n", |
| 114 | + " cudaq.kernels.uccsd(qubits, thetas, electron_count, num_qubits)\n", |
| 115 | + "\n", |
| 116 | + "\n", |
| 117 | + "parameter_count = cudaq.kernels.uccsd_num_parameters(electron_count, num_qubits)\n", |
| 118 | + "\n", |
| 119 | + "\n", |
| 120 | + "from scipy.optimize import minimize\n", |
| 121 | + "\n", |
| 122 | + "\n", |
| 123 | + "# Define a function to minimize\n", |
| 124 | + "def cost(thetas: list[float]):\n", |
| 125 | + " exp_val = cudaq.observe(kernel, hamiltonian, thetas).expectation()\n", |
| 126 | + " return exp_val\n", |
| 127 | + "\n", |
| 128 | + "\n", |
| 129 | + "exp_vals = []\n", |
| 130 | + "\n", |
| 131 | + "\n", |
| 132 | + "def callback(xk):\n", |
| 133 | + " exp_vals.append(cost(xk))\n", |
| 134 | + "\n", |
| 135 | + "\n", |
| 136 | + "# Initial variational parameters.\n", |
| 137 | + "np.random.seed(15)\n", |
| 138 | + "x0 = np.random.normal(0, np.pi, parameter_count)\n", |
| 139 | + "\n", |
| 140 | + "# Use the scipy optimizer to minimize the function of interest\n", |
| 141 | + "vqe_result = minimize(cost, x0)" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "markdown", |
| 146 | + "id": "02dc831a-c41f-4abf-8347-4df7a0d5ca72", |
| 147 | + "metadata": {}, |
| 148 | + "source": [ |
| 149 | + "## 2 Quantum Sampling to Select Configuration\n", |
| 150 | + "\n", |
| 151 | + "Measure this quantum state multiple times to obtain bitstrings, which correspond to Slater determinants (electronic configurations)." |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": 5, |
| 157 | + "id": "0eb10a37-9384-41f5-bb9e-92d91700434a", |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [ |
| 160 | + { |
| 161 | + "name": "stdout", |
| 162 | + "output_type": "stream", |
| 163 | + "text": [ |
| 164 | + "{ 00001111:2 00011011:12 00011110:18 00100111:27 00101101:25 00110011:2 00110110:22 00111001:193 01011010:13 01101100:23 01110010:2 10000111:34 10001101:115 10010011:18 10010110:5 10011100:11 10100101:40 10110001:4 11000011:170 11001001:16 11001100:8 11010010:46 11011000:137 11100001:7 11100100:28 11110000:22 }\n", |
| 165 | + "\n" |
| 166 | + ] |
| 167 | + } |
| 168 | + ], |
| 169 | + "source": [ |
| 170 | + "sample_result = cudaq.sample(kernel, vqe_result.x)\n", |
| 171 | + "print(sample_result)" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "markdown", |
| 176 | + "id": "836fb668-4bb7-4214-aaaf-72d8f697a384", |
| 177 | + "metadata": {}, |
| 178 | + "source": [ |
| 179 | + "## 3. Classical Diagonalization on the Selected Subspace\n", |
| 180 | + "\n", |
| 181 | + "Construct a truncated Hamiltonian matrix using only the selected determinants.\n", |
| 182 | + "Diagonalize this matrix classically to obtain improved energy estimates." |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": 6, |
| 188 | + "id": "eb220374-44ca-4183-9e1a-f27cc62e8fd7", |
| 189 | + "metadata": {}, |
| 190 | + "outputs": [], |
| 191 | + "source": [ |
| 192 | + "def pauli_element(\n", |
| 193 | + " operator: cudaq.SpinOperator, b1: str | np.ndarray, b2: str | np.ndarray\n", |
| 194 | + ") -> complex:\n", |
| 195 | + " \"\"\"\n", |
| 196 | + " Compute an inner product <b1|operator|b2>.\n", |
| 197 | + " \"\"\"\n", |
| 198 | + "\n", |
| 199 | + " if isinstance(b1, str):\n", |
| 200 | + " b1 = np.array([i == \"1\" for i in b1], dtype=bool)\n", |
| 201 | + " if isinstance(b2, str):\n", |
| 202 | + " b2 = np.array([i == \"1\" for i in b2], dtype=bool)\n", |
| 203 | + "\n", |
| 204 | + " num_qubits = hamiltonian.qubit_count\n", |
| 205 | + " num_terms = hamiltonian.term_count\n", |
| 206 | + "\n", |
| 207 | + " bsv = np.zeros((num_terms, 2 * num_qubits), dtype=bool)\n", |
| 208 | + " coeffs = np.zeros(num_terms, dtype=np.complex128)\n", |
| 209 | + " for i, term in enumerate(operator):\n", |
| 210 | + " coeffs[i] = term.evaluate_coefficient()\n", |
| 211 | + " bsf = term.get_binary_symplectic_form()\n", |
| 212 | + " n = len(bsf) // 2\n", |
| 213 | + " for j, e in enumerate(bsf):\n", |
| 214 | + " if j < n:\n", |
| 215 | + " bsv[i, j] = e\n", |
| 216 | + " else:\n", |
| 217 | + " bsv[i, j - n + num_qubits] = e\n", |
| 218 | + " x = bsv[:, :num_qubits]\n", |
| 219 | + " z = bsv[:, num_qubits:]\n", |
| 220 | + "\n", |
| 221 | + " # b1 ⊕ b2 must match x for nonzero contribution\n", |
| 222 | + " delta = np.bitwise_xor(b1, b2)\n", |
| 223 | + " match = np.all(x == delta, axis=1)\n", |
| 224 | + "\n", |
| 225 | + " if not np.any(match):\n", |
| 226 | + " return 0.0\n", |
| 227 | + "\n", |
| 228 | + " matched_x = x[match]\n", |
| 229 | + " matched_z = z[match]\n", |
| 230 | + " matched_coeffs = coeffs[match]\n", |
| 231 | + "\n", |
| 232 | + " # i^{x·z}\n", |
| 233 | + " x_dot_z = np.sum(np.bitwise_and(matched_x, matched_z), axis=1) % 4\n", |
| 234 | + " i_phases = np.array([1, 1j, -1, -1j], dtype=np.complex128)[x_dot_z]\n", |
| 235 | + "\n", |
| 236 | + " # (-1)^{b1·z}\n", |
| 237 | + " b1_dot_z_parity = np.sum(np.bitwise_and(b1, matched_z), axis=1) % 2\n", |
| 238 | + " sign_phases = np.where(b1_dot_z_parity == 0, 1, -1)\n", |
| 239 | + "\n", |
| 240 | + " result_terms = matched_coeffs * i_phases * sign_phases\n", |
| 241 | + " return np.sum(result_terms)" |
| 242 | + ] |
| 243 | + }, |
| 244 | + { |
| 245 | + "cell_type": "code", |
| 246 | + "execution_count": 7, |
| 247 | + "id": "5c095c67-1222-4366-bf0b-4a4d847a2841", |
| 248 | + "metadata": {}, |
| 249 | + "outputs": [], |
| 250 | + "source": [ |
| 251 | + "import scipy\n", |
| 252 | + "\n", |
| 253 | + "bitstrs = list(k for k, _ in sample_result.items())\n", |
| 254 | + "subspace_dimension = len(bitstrs)\n", |
| 255 | + "\n", |
| 256 | + "values = []\n", |
| 257 | + "row_ids = []\n", |
| 258 | + "column_ids = []\n", |
| 259 | + "\n", |
| 260 | + "for i in range(subspace_dimension):\n", |
| 261 | + " for j in range(i, subspace_dimension):\n", |
| 262 | + " elem = pauli_element(hamiltonian, bitstrs[i], bitstrs[j])\n", |
| 263 | + " if abs(elem) > 1e-6:\n", |
| 264 | + " values.append(elem)\n", |
| 265 | + " row_ids.append(i)\n", |
| 266 | + " column_ids.append(j)\n", |
| 267 | + " if i != j:\n", |
| 268 | + " values.append(elem.conjugate())\n", |
| 269 | + " row_ids.append(j)\n", |
| 270 | + " column_ids.append(i)\n", |
| 271 | + "\n", |
| 272 | + "values = np.real_if_close(values)\n", |
| 273 | + "\n", |
| 274 | + "truncated_hamiltonian = scipy.sparse.coo_array(\n", |
| 275 | + " (values, (row_ids, column_ids)), shape=(subspace_dimension, subspace_dimension)\n", |
| 276 | + ")\n", |
| 277 | + "eigvals, _ = scipy.sparse.linalg.eigsh(truncated_hamiltonian, 1, which=\"SA\")" |
| 278 | + ] |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "markdown", |
| 282 | + "id": "fb40b6c3-063a-45df-8867-747fac2bb354", |
| 283 | + "metadata": {}, |
| 284 | + "source": [ |
| 285 | + "## 5. Compuare results" |
| 286 | + ] |
| 287 | + }, |
| 288 | + { |
| 289 | + "cell_type": "code", |
| 290 | + "execution_count": 8, |
| 291 | + "id": "827cfd92-9208-4057-959f-db70853a32d2", |
| 292 | + "metadata": {}, |
| 293 | + "outputs": [ |
| 294 | + { |
| 295 | + "data": { |
| 296 | + "text/plain": [ |
| 297 | + "-2.1318697852134716" |
| 298 | + ] |
| 299 | + }, |
| 300 | + "execution_count": 8, |
| 301 | + "metadata": {}, |
| 302 | + "output_type": "execute_result" |
| 303 | + } |
| 304 | + ], |
| 305 | + "source": [ |
| 306 | + "## QSCI\n", |
| 307 | + "eigvals[0]" |
| 308 | + ] |
| 309 | + }, |
| 310 | + { |
| 311 | + "cell_type": "code", |
| 312 | + "execution_count": 9, |
| 313 | + "id": "df643aee-1967-4d3d-aebd-dda13892c9ac", |
| 314 | + "metadata": {}, |
| 315 | + "outputs": [ |
| 316 | + { |
| 317 | + "data": { |
| 318 | + "text/plain": [ |
| 319 | + "-2.143554580446029" |
| 320 | + ] |
| 321 | + }, |
| 322 | + "execution_count": 9, |
| 323 | + "metadata": {}, |
| 324 | + "output_type": "execute_result" |
| 325 | + } |
| 326 | + ], |
| 327 | + "source": [ |
| 328 | + "## FCI Energy\n", |
| 329 | + "molecule.fci_energy" |
| 330 | + ] |
| 331 | + }, |
| 332 | + { |
| 333 | + "cell_type": "markdown", |
| 334 | + "id": "cd08b5ed-dc5a-4f25-86fd-a6c805ee8ee6", |
| 335 | + "metadata": {}, |
| 336 | + "source": [ |
| 337 | + "## Reference\n", |
| 338 | + "\n", |
| 339 | + "[1] \"Quantum-selected configuration interaction: classical diagonalization of hamiltonians in subspaces selected by quantum computers\" K. Kanno, M. Kohda, R. Imai, S. Koh, K. Mitarai, W. Mizukami, and Y. O. Nakagawa (2023) arXiv: 2302.11320" |
| 340 | + ] |
| 341 | + } |
| 342 | + ], |
| 343 | + "metadata": { |
| 344 | + "kernelspec": { |
| 345 | + "display_name": "Python 3 (ipykernel)", |
| 346 | + "language": "python", |
| 347 | + "name": "python3" |
| 348 | + }, |
| 349 | + "language_info": { |
| 350 | + "codemirror_mode": { |
| 351 | + "name": "ipython", |
| 352 | + "version": 3 |
| 353 | + }, |
| 354 | + "file_extension": ".py", |
| 355 | + "mimetype": "text/x-python", |
| 356 | + "name": "python", |
| 357 | + "nbconvert_exporter": "python", |
| 358 | + "pygments_lexer": "ipython3", |
| 359 | + "version": "3.10.12" |
| 360 | + } |
| 361 | + }, |
| 362 | + "nbformat": 4, |
| 363 | + "nbformat_minor": 5 |
| 364 | +} |
0 commit comments