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