qat.fermion.hamiltonians.SpinHamiltonian

class qat.fermion.hamiltonians.SpinHamiltonian(nqbits: int, terms: List[Term], constant_coeff: float = 0.0)

Implementation of a spin Hamiltonian.

Parameters
  • nqbits (int) – the total number of qubits

  • terms (List[Term]) – the list of terms

  • constant_coeff (float) – constant term

nbqbits

the total number of qubits

Type

int

terms

the list of terms

Type

List[Term]

constant_coeff

constant term

Type

float

matrix

the corresponding matrix (None by default, can be set by calling get_matrix method)

Type

np.ndarray

Example

from qat.core import Term
from qat.fermion import SpinHamiltonian

hamiltonian = SpinHamiltonian(2, [Term(0.3, "X", [0]), Term(-0.4, "ZY", [0, 1])])

print(f"H = {hamiltonian}")
print(f"H matrix: {hamiltonian.get_matrix()}")
H = 0.3 * (X|[0]) +
-0.4 * (ZY|[0, 1])
H matrix: [[0. +0.j  0. +0.4j 0.3+0.j  0. +0.j ]
 [0. -0.4j 0. +0.j  0. +0.j  0.3+0.j ]
 [0.3+0.j  0. +0.j  0. +0.j  0. -0.4j]
 [0. +0.j  0.3+0.j  0. +0.4j 0. +0.j ]]
copy()

Deepcopy the current class.

Returns

Copy of the SpinHamiltonian.

Return type

SpinHamiltonian

dag() SpinHamiltonian

Compute the conjugate transpose of the Hamiltonian.

Returns

Conjugate transpose of the SpinHamiltonian operator

Return type

SpinHamiltonian

get_matrix(sparse: bool = False) ndarray

This function returns the matrix corresponding to \(H\) in the computational basis.

Parameters
  • sparse (Optional[bool]) – Whether to return in sparse representation.

  • False. (Defaults to) –

Returns

The matrix of the SpinHamiltonian.

Return type

np.ndarray

Warning

This method should not be used if the SpinHamiltonian is too large.