-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtuto_utils.py
More file actions
24 lines (21 loc) · 892 Bytes
/
Copy pathtuto_utils.py
File metadata and controls
24 lines (21 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np
from qat.core.circuit_builder.matrix_util import get_predef_generator
def make_matrix(hamiltonian):
"""Compute the matrix corresponding to a Hamiltonian
Args:
hamiltonian (Observable): a hamiltonian
Returns:
np.array: the corresponding matrix
"""
mat = np.zeros((2**hamiltonian.nbqbits, 2**hamiltonian.nbqbits), np.complex_)
mat += np.identity(2**hamiltonian.nbqbits) * hamiltonian.constant_coeff
for term in hamiltonian.terms:
op_list = ["I"]*hamiltonian.nbqbits
for op, qb in zip(term.op, term.qbits):
op_list[qb] = op
def mat_func(name): return np.identity(2) if name == "I" else get_predef_generator()[name]
term_mat = mat_func(op_list[0])
for op in op_list[1:]:
term_mat = np.kron(term_mat, mat_func(op))
mat += term.coeff * term_mat
return mat