{ "cells": [ { "cell_type": "markdown", "id": "21a1d83f-183f-4da9-bb8c-71400120fd31", "metadata": {}, "source": [ "# Project Pauli operators onto Hilbert subspaces" ] }, { "cell_type": "markdown", "id": "c961d48a", "metadata": {}, "source": [ "We show different ways of projecting a weighted linear combination of pauli strings\n", "into a subspace defined by a subset of size $d$ computational basis states. The \n", "projected operator is stored as a $d \\times d$ ``scipy.sparse.coo_matrix``.\n", "\n", "As an example we consider the Hamiltonian of the 1D Heisenberg model with periodic\n", "boundary conditions and $L = 22$ spins:\n", "$$\n", "H = \\sum_{\\langle i, j \\rangle}\\left( \\sigma^x_i\\sigma^x_j + \\sigma^y_i\\sigma^y_j + \\sigma^z_i\\sigma^z_j \\right).\n", "$$\n", "\n", "This package provides two tools to perform this projection:\n", "\n", "- ``qiskit_addon_sqd.qubit.matrix_elements_from_pauli()``: is a lower-level function\n", "that returns the non-zero matrix elements of a Pauli string and the corresponding indices\n", "of the non-zero elements.\n", "\n", "- ``qiskit_addon_sqd.qubit.project_operator_to_subspace()``: is a higher-level function that \n", "directly returns a ``scipy.sparse`` matrix.\n", "\n", "This notebook shows how to use these two tools to produce the same sparse operator." ] }, { "cell_type": "markdown", "id": "6c87d5e5", "metadata": {}, "source": [ "### Subspace definition\n", "\n", "For this example we just generate length-22 random bitstrings. For the projection\n", "functions to work as expected, it is essential that the bitstrings that define\n", "the subspace are unique and sorted in ascending order according to their unsigned\n", "integer representation.\n", "\n", "This can be achieved with the ``qiskit_addon_sqd.qubit.sort_and_remove_duplicates()``\n", "function." ] }, { "cell_type": "code", "execution_count": 1, "id": "7b94f840", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Subspace dimension: 49718\n", "Full Hilbert space dimension: 4194304\n" ] } ], "source": [ "import numpy as np\n", "from qiskit_addon_sqd.qubit import sort_and_remove_duplicates\n", "\n", "num_spins = 22\n", "rand_seed = 22\n", "np.random.seed(rand_seed)\n", "\n", "\n", "def random_bitstrings(n_samples, n_qubits):\n", " return np.round(np.random.rand(n_samples, n_qubits)).astype(\"int\").astype(\"bool\")\n", "\n", "\n", "bitstring_matrix = random_bitstrings(50_000, num_spins)\n", "\n", "# NOTE: It is essential for the projection code to have the bitstrings sorted!\n", "bitstring_matrix = sort_and_remove_duplicates(bitstring_matrix)\n", "\n", "print(\"Subspace dimension: \" + str(bitstring_matrix.shape[0]))\n", "print(\"Full Hilbert space dimension: \" + str(2**num_spins))" ] }, { "cell_type": "markdown", "id": "5707b93c", "metadata": {}, "source": [ "### First method: nonzero matrix elements and indices." ] }, { "cell_type": "code", "execution_count": 2, "id": "74c16c91-cc5c-46ce-aff8-17d0e71ac50f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SparsePauliOp(['IIIIIIIIIIIIIIIIIIIIXX', 'IIIIIIIIIIIIIIIIIIIIYY', 'IIIIIIIIIIIIIIIIIIIIZZ', 'IIIIIIIIIIIIIIIIIIXXII', 'IIIIIIIIIIIIIIIIIIYYII', 'IIIIIIIIIIIIIIIIIIZZII', 'IIIIIIIIIIIIIIIIXXIIII', 'IIIIIIIIIIIIIIIIYYIIII', 'IIIIIIIIIIIIIIIIZZIIII', 'IIIIIIIIIIIIIIXXIIIIII', 'IIIIIIIIIIIIIIYYIIIIII', 'IIIIIIIIIIIIIIZZIIIIII', 'IIIIIIIIIIIIXXIIIIIIII', 'IIIIIIIIIIIIYYIIIIIIII', 'IIIIIIIIIIIIZZIIIIIIII', 'IIIIIIIIIIXXIIIIIIIIII', 'IIIIIIIIIIYYIIIIIIIIII', 'IIIIIIIIIIZZIIIIIIIIII', 'IIIIIIIIXXIIIIIIIIIIII', 'IIIIIIIIYYIIIIIIIIIIII', 'IIIIIIIIZZIIIIIIIIIIII', 'IIIIIIXXIIIIIIIIIIIIII', 'IIIIIIYYIIIIIIIIIIIIII', 'IIIIIIZZIIIIIIIIIIIIII', 'IIIIXXIIIIIIIIIIIIIIII', 'IIIIYYIIIIIIIIIIIIIIII', 'IIIIZZIIIII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.+0.j, 1.+0.j,\n", " 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,\n", " 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,\n", " 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,\n", " 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,\n", " 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,\n", " 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j, 1.+0.j,\n", " 1.+0.j, 1.+0.j, 1.+0.j])\n" ] } ], "source": [ "from qiskit.transpiler import CouplingMap\n", "from qiskit_addon_utils.problem_generators import generate_xyz_hamiltonian\n", "\n", "coupling_map = CouplingMap.from_ring(num_spins)\n", "hamiltonian = generate_xyz_hamiltonian(coupling_map)\n", "print(hamiltonian)" ] }, { "cell_type": "code", "execution_count": 3, "id": "f31f5e40", "metadata": {}, "outputs": [], "source": [ "from qiskit_addon_sqd.qubit import matrix_elements_from_pauli\n", "from scipy.sparse import coo_matrix\n", "\n", "d = bitstring_matrix.shape[0]\n", "\n", "# Initialize the coo_matrix object\n", "operator_from_matrix_elements = coo_matrix((d, d), dtype=\"complex128\")\n", "\n", "for pauli in hamiltonian.paulis:\n", " matrix_elements, row_indices, col_indices = matrix_elements_from_pauli(bitstring_matrix, pauli)\n", " operator_from_matrix_elements += coo_matrix(\n", " (matrix_elements, (row_indices, col_indices)), (d, d)\n", " )" ] }, { "cell_type": "markdown", "id": "4479af24", "metadata": {}, "source": [ "### Higher-level implementation" ] }, { "cell_type": "code", "execution_count": 4, "id": "e58763df", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Projecting term 1 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIIIIXX ...\n", "Projecting term 2 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIIIIYY ...\n", "Projecting term 3 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIIIIZZ ...\n", "Projecting term 4 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIIXXII ...\n", "Projecting term 5 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIIYYII ...\n", "Projecting term 6 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIIZZII ...\n", "Projecting term 7 out of 66: (1+0j) * IIIIIIIIIIIIIIIIXXIIII ...\n", "Projecting term 8 out of 66: (1+0j) * IIIIIIIIIIIIIIIIYYIIII ...\n", "Projecting term 9 out of 66: (1+0j) * IIIIIIIIIIIIIIIIZZIIII ...\n", "Projecting term 10 out of 66: (1+0j) * IIIIIIIIIIIIIIXXIIIIII ...\n", "Projecting term 11 out of 66: (1+0j) * IIIIIIIIIIIIIIYYIIIIII ...\n", "Projecting term 12 out of 66: (1+0j) * IIIIIIIIIIIIIIZZIIIIII ...\n", "Projecting term 13 out of 66: (1+0j) * IIIIIIIIIIIIXXIIIIIIII ...\n", "Projecting term 14 out of 66: (1+0j) * IIIIIIIIIIIIYYIIIIIIII ...\n", "Projecting term 15 out of 66: (1+0j) * IIIIIIIIIIIIZZIIIIIIII ...\n", "Projecting term 16 out of 66: (1+0j) * IIIIIIIIIIXXIIIIIIIIII ...\n", "Projecting term 17 out of 66: (1+0j) * IIIIIIIIIIYYIIIIIIIIII ...\n", "Projecting term 18 out of 66: (1+0j) * IIIIIIIIIIZZIIIIIIIIII ...\n", "Projecting term 19 out of 66: (1+0j) * IIIIIIIIXXIIIIIIIIIIII ...\n", "Projecting term 20 out of 66: (1+0j) * IIIIIIIIYYIIIIIIIIIIII ...\n", "Projecting term 21 out of 66: (1+0j) * IIIIIIIIZZIIIIIIIIIIII ...\n", "Projecting term 22 out of 66: (1+0j) * IIIIIIXXIIIIIIIIIIIIII ...\n", "Projecting term 23 out of 66: (1+0j) * IIIIIIYYIIIIIIIIIIIIII ...\n", "Projecting term 24 out of 66: (1+0j) * IIIIIIZZIIIIIIIIIIIIII ...\n", "Projecting term 25 out of 66: (1+0j) * IIIIXXIIIIIIIIIIIIIIII ...\n", "Projecting term 26 out of 66: (1+0j) * IIIIYYIIIIIIIIIIIIIIII ...\n", "Projecting term 27 out of 66: (1+0j) * IIIIZZIIIIIIIIIIIIIIII ...\n", "Projecting term 28 out of 66: (1+0j) * IIXXIIIIIIIIIIIIIIIIII ...\n", "Projecting term 29 out of 66: (1+0j) * IIYYIIIIIIIIIIIIIIIIII ...\n", "Projecting term 30 out of 66: (1+0j) * IIZZIIIIIIIIIIIIIIIIII ...\n", "Projecting term 31 out of 66: (1+0j) * XXIIIIIIIIIIIIIIIIIIII ...\n", "Projecting term 32 out of 66: (1+0j) * YYIIIIIIIIIIIIIIIIIIII ...\n", "Projecting term 33 out of 66: (1+0j) * ZZIIIIIIIIIIIIIIIIIIII ...\n", "Projecting term 34 out of 66: (1+0j) * XIIIIIIIIIIIIIIIIIIIIX ...\n", "Projecting term 35 out of 66: (1+0j) * YIIIIIIIIIIIIIIIIIIIIY ...\n", "Projecting term 36 out of 66: (1+0j) * ZIIIIIIIIIIIIIIIIIIIIZ ...\n", "Projecting term 37 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIIIXXI ...\n", "Projecting term 38 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIIIYYI ...\n", "Projecting term 39 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIIIZZI ...\n", "Projecting term 40 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIXXIII ...\n", "Projecting term 41 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIYYIII ...\n", "Projecting term 42 out of 66: (1+0j) * IIIIIIIIIIIIIIIIIZZIII ...\n", "Projecting term 43 out of 66: (1+0j) * IIIIIIIIIIIIIIIXXIIIII ...\n", "Projecting term 44 out of 66: (1+0j) * IIIIIIIIIIIIIIIYYIIIII ...\n", "Projecting term 45 out of 66: (1+0j) * IIIIIIIIIIIIIIIZZIIIII ...\n", "Projecting term 46 out of 66: (1+0j) * IIIIIIIIIIIIIXXIIIIIII ...\n", "Projecting term 47 out of 66: (1+0j) * IIIIIIIIIIIIIYYIIIIIII ...\n", "Projecting term 48 out of 66: (1+0j) * IIIIIIIIIIIIIZZIIIIIII ...\n", "Projecting term 49 out of 66: (1+0j) * IIIIIIIIIIIXXIIIIIIIII ...\n", "Projecting term 50 out of 66: (1+0j) * IIIIIIIIIIIYYIIIIIIIII ...\n", "Projecting term 51 out of 66: (1+0j) * IIIIIIIIIIIZZIIIIIIIII ...\n", "Projecting term 52 out of 66: (1+0j) * IIIIIIIIIXXIIIIIIIIIII ...\n", "Projecting term 53 out of 66: (1+0j) * IIIIIIIIIYYIIIIIIIIIII ...\n", "Projecting term 54 out of 66: (1+0j) * IIIIIIIIIZZIIIIIIIIIII ...\n", "Projecting term 55 out of 66: (1+0j) * IIIIIIIXXIIIIIIIIIIIII ...\n", "Projecting term 56 out of 66: (1+0j) * IIIIIIIYYIIIIIIIIIIIII ...\n", "Projecting term 57 out of 66: (1+0j) * IIIIIIIZZIIIIIIIIIIIII ...\n", "Projecting term 58 out of 66: (1+0j) * IIIIIXXIIIIIIIIIIIIIII ...\n", "Projecting term 59 out of 66: (1+0j) * IIIIIYYIIIIIIIIIIIIIII ...\n", "Projecting term 60 out of 66: (1+0j) * IIIIIZZIIIIIIIIIIIIIII ...\n", "Projecting term 61 out of 66: (1+0j) * IIIXXIIIIIIIIIIIIIIIII ...\n", "Projecting term 62 out of 66: (1+0j) * IIIYYIIIIIIIIIIIIIIIII ...\n", "Projecting term 63 out of 66: (1+0j) * IIIZZIIIIIIIIIIIIIIIII ...\n", "Projecting term 64 out of 66: (1+0j) * IXXIIIIIIIIIIIIIIIIIII ...\n", "Projecting term 65 out of 66: (1+0j) * IYYIIIIIIIIIIIIIIIIIII ...\n", "Projecting term 66 out of 66: (1+0j) * IZZIIIIIIIIIIIIIIIIIII ...\n" ] } ], "source": [ "from qiskit_addon_sqd.qubit import project_operator_to_subspace\n", "\n", "operator = project_operator_to_subspace(bitstring_matrix, hamiltonian, verbose=True)" ] }, { "cell_type": "markdown", "id": "1fce97a2", "metadata": {}, "source": [ "### Check that both implementations yield the same coo_matrix" ] }, { "cell_type": "code", "execution_count": 5, "id": "4bf56509", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0j\n" ] } ], "source": [ "print((operator.power(2) - operator_from_matrix_elements.power(2)).sum())" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }