Skip to content

Commit

Permalink
Finish covering qubit module
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-johnson committed Oct 3, 2024
1 parent 4a3fb4f commit 5fa4e0c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
10 changes: 3 additions & 7 deletions qiskit_addon_sqd/qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def solve_qubit(
d, _ = bitstring_matrix.shape
ham_proj = project_operator_to_subspace(bitstring_matrix, hamiltonian, verbose=verbose)

if verbose:
if verbose: # pragma: no cover
print("Diagonalizing Hamiltonian in the subspace...")
energies, eigenstates = eigsh(ham_proj, **scipy_kwargs)

Expand Down Expand Up @@ -129,7 +129,7 @@ def project_operator_to_subspace(

for i, pauli in enumerate(hamiltonian.paulis):
coefficient = hamiltonian.coeffs[i]
if verbose:
if verbose: # pragma: no cover
(
print(
f"Projecting term {i+1} out of {hamiltonian.size}: {coefficient} * "
Expand All @@ -147,7 +147,7 @@ def project_operator_to_subspace(
return operator


def sort_and_remove_duplicates(bitstring_matrix: np.ndarray, inplace: bool = True) -> np.ndarray:
def sort_and_remove_duplicates(bitstring_matrix: np.ndarray) -> np.ndarray:
"""
Sort a bitstring matrix and remove duplicate entries.
Expand All @@ -156,14 +156,10 @@ def sort_and_remove_duplicates(bitstring_matrix: np.ndarray, inplace: bool = Tru
Args:
bitstring_matrix: A 2D array of ``bool`` representations of bit
values such that each row represents a single bitstring.
inplace: Whether to modify the input array in place.
Returns:
Sorted version of ``bitstring_matrix`` without repeated rows.
"""
if not inplace:
bitstring_matrix = bitstring_matrix.copy()

bsmat_asints = _int_conversion_from_bts_matrix_vmap(bitstring_matrix)

_, indices = np.unique(bsmat_asints, return_index=True)
Expand Down
55 changes: 54 additions & 1 deletion test/test_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,56 @@
import numpy as np
import pytest
from qiskit.quantum_info import Pauli, SparsePauliOp
from qiskit_addon_sqd.qubit import matrix_elements_from_pauli, project_operator_to_subspace
from qiskit_addon_sqd.qubit import (
matrix_elements_from_pauli,
project_operator_to_subspace,
solve_qubit,
sort_and_remove_duplicates,
)
from scipy.sparse import coo_matrix
from scipy.sparse.linalg import eigsh


class TestQubit(unittest.TestCase):
def test_solve_qubit(self):
with self.subTest("Basic test"):
op = SparsePauliOp("XZIY")
bs_mat = np.array(
[
[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 1, 0, 0],
[1, 0, 0, 0],
[1, 1, 0, 0],
]
)
# Flip sign on 1's corresponding to Z operator terms
# Add an imaginary factor to all qubit msmts corresponding to Y terms
# Take product of all terms to get component amplitude
amps_test = np.array([(-1j), (1j)])
rows_test = np.array([1, 5])
cols_test = np.array([5, 1])
num_configs = bs_mat.shape[0]
coo_matrix_test = coo_matrix(
(amps_test, (rows_test, cols_test)), (num_configs, num_configs)
)

scipy_kwargs = {"k": 1, "which": "SA"}
energies_test, eigenstates_test = eigsh(coo_matrix_test, **scipy_kwargs)
e, ev = solve_qubit(bs_mat, op, **scipy_kwargs)
self.assertTrue(np.allclose(energies_test, e))
with self.subTest("64 qubits"):
op = SparsePauliOp("Z" * 64)
bs_mat = np.array([[1] * 64])
with pytest.raises(ValueError) as e_info:
solve_qubit(bs_mat, op)
assert (
e_info.value.args[0]
== "Bitstrings (rows) in bitstring_matrix must have length < 64."
)

def test_project_operator_to_subspace(self):
with self.subTest("Basic test"):
op = SparsePauliOp("XZIY", coeffs=[0.5])
Expand Down Expand Up @@ -109,3 +154,11 @@ def test_matrix_elements_from_pauli(self):
e_info.value.args[0]
== "Bitstrings (rows) in bitstring_matrix must have length < 64."
)

def test_sort_and_remove_duplicates(self):
with self.subTest("Basic test"):
bs_mat = np.array([[0, 0], [1, 0], [0, 1], [0, 0], [1, 1]])
test_mat = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])

new_mat = sort_and_remove_duplicates(bs_mat)
self.assertTrue((test_mat == new_mat).all())

0 comments on commit 5fa4e0c

Please sign in to comment.