Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

As matrix method for Hamiltonian class #241

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/openqaoa-core/qaoa_components/ansatz_constructor/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from typing import List, Union, Tuple
from sympy import Symbol
import numpy as np
from scipy.sparse import kron as sparse_kron
from functools import reduce
from scipy.sparse import csr_matrix

Identity = np.array(([1, 0], [0, 1]), dtype=complex)
PauliX = np.array(([0, 1], [1, 0]), dtype=complex)
Expand Down Expand Up @@ -686,3 +689,45 @@ def classical_hamiltonian(
raise ValueError("Hamiltonian only supports Linear and Quadratic terms")

return cls(pauli_ops, pauli_coeffs, constant)

@property
def as_matrix(self):
"""
Build sparse matrix of Hamiltonian. Note this includes self.constant value.

Returns
-------
H_mat:
sparse matrix of Hamiltonian.
"""

if self.n_qubits>20:
raise ValueError('number of qubits exceeds maximum of 20')

p_mat_dict = {'X': csr_matrix([[0, 1],
[1, 0]]),
'Y': csr_matrix([[0., -1j],
[1j, 0.]]),
'Z': csr_matrix([[1, 0],
[0, -1]]),
'I': csr_matrix([[1, 0], [0, 1]])}

converter = lambda s: p_mat_dict[s]

# vectorized function to map single qubit pauli strings to sparse matrix
vfunc = np.vectorize(converter)

base = np.array(['I' for _ in range(self.n_qubits)])

H_mat = reduce(sparse_kron, vfunc(base)) * self.constant
for P, coeff in zip(self.terms, self.coeffs):
temp = base.copy()

# replace single qubit pauli I in base term
# with single qubit pauli operators according to qubit_indices and pauli_strs in P
temp[np.array(P.qubit_indices)] = np.array(list(P.pauli_str))

# take kronecker product of sparse list of matrices
H_mat += reduce(sparse_kron, vfunc(temp)) * coeff

return H_mat
68 changes: 68 additions & 0 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,74 @@ def test_hamiltonian_dict(self):
hamiltonian_dict_pauliop == correct_dict_pauliop
), "Hamiltonian dictionary did not yield correct dictionary"

def test_hamiltonian_as_matrix(self):
"""Test if the method produces
the correct matrix corresponding to given Hamiltonian.

"""
P_list = ['XIIZ', 'YIII', 'IZXI', 'IXIY', 'IIIZ', 'IIXI', 'ZZII']
coeff_list = list(range(1, 8))

Pauli_list = []
for P in P_list:
P = np.array(list(P))
non_I = np.where(P != 'I')[0]
Pauli_list.append(PauliOp(P[non_I], tuple(non_I)))

H = Hamiltonian(Pauli_list, coeff_list, 0)

solution = np.array([[12. + 0.j, 0. + 0.j, 9. + 0.j, 0. + 0.j, 0. + 0.j, 0. - 4.j,
0. + 0.j, 0. + 0.j, 1. - 2.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j],
[0. + 0.j, 2. + 0.j, 0. + 0.j, 9. + 0.j, 0. + 4.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 0. + 0.j, -1. - 2.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j],
[9. + 0.j, 0. + 0.j, 12. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, 0. - 4.j, 0. + 0.j, 0. + 0.j, 1. - 2.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j],
[0. + 0.j, 9. + 0.j, 0. + 0.j, 2. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 4.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, -1. - 2.j,
0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j],
[0. + 0.j, 0. - 4.j, 0. + 0.j, 0. + 0.j, -2. + 0.j, 0. + 0.j,
3. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
1. - 2.j, 0. + 0.j, 0. + 0.j, 0. + 0.j],
[0. + 4.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, -12. + 0.j,
0. + 0.j, 3. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, -1. - 2.j, 0. + 0.j, 0. + 0.j],
[0. + 0.j, 0. + 0.j, 0. + 0.j, 0. - 4.j, 3. + 0.j, 0. + 0.j,
-2. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 1. - 2.j, 0. + 0.j],
[0. + 0.j, 0. + 0.j, 0. + 4.j, 0. + 0.j, 0. + 0.j, 3. + 0.j,
0. + 0.j, -12. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 0. + 0.j, -1. - 2.j],
[1. + 2.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, -2. + 0.j, 0. + 0.j, 9. + 0.j, 0. + 0.j,
0. + 0.j, 0. - 4.j, 0. + 0.j, 0. + 0.j],
[0. + 0.j, -1. + 2.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 0. + 0.j, -12. + 0.j, 0. + 0.j, 9. + 0.j,
0. + 4.j, 0. + 0.j, 0. + 0.j, 0. + 0.j],
[0. + 0.j, 0. + 0.j, 1. + 2.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 9. + 0.j, 0. + 0.j, -2. + 0.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 0. + 0.j, 0. - 4.j],
[0. + 0.j, 0. + 0.j, 0. + 0.j, -1. + 2.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 0. + 0.j, 9. + 0.j, 0. + 0.j, -12. + 0.j,
0. + 0.j, 0. + 0.j, 0. + 4.j, 0. + 0.j],
[0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 1. + 2.j, 0. + 0.j,
0. + 0.j, 0. + 0.j, 0. + 0.j, 0. - 4.j, 0. + 0.j, 0. + 0.j,
12. + 0.j, 0. + 0.j, 3. + 0.j, 0. + 0.j],
[0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, -1. + 2.j,
0. + 0.j, 0. + 0.j, 0. + 4.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, 2. + 0.j, 0. + 0.j, 3. + 0.j],
[0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
1. + 2.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. - 4.j,
3. + 0.j, 0. + 0.j, 12. + 0.j, 0. + 0.j],
[0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j,
0. + 0.j, -1. + 2.j, 0. + 0.j, 0. + 0.j, 0. + 4.j, 0. + 0.j,
0. + 0.j, 3. + 0.j, 0. + 0.j, 2. + 0.j]])

assert np.allclose(solution,
H.as_matrix.toarray()), 'as_matrix method not correct'


if __name__ == "__main__":
unittest.main()