[docs]classAbstractHamiltonian:"""Qibo abstraction for Hamiltonian objects."""def__init__(self):self._nqubits=None@propertydefnqubits(self):returnself._nqubits@nqubits.setterdefnqubits(self,n):ifnotisinstance(n,int):raise_error(RuntimeError,f"nqubits must be an integer but is {type(n)}.")ifn<1:raise_error(ValueError,f"nqubits must be a positive integer but is {n}")self._nqubits=n
[docs]@abstractmethoddefeigenvalues(self,k=6):# pragma: no cover"""Computes the eigenvalues for the Hamiltonian. Args: k (int): Number of eigenvalues to calculate if the Hamiltonian was created using a sparse matrix. This argument is ignored if the Hamiltonian was created using a dense matrix. See :meth:`qibo.backends.abstract.AbstractBackend.eigvalsh` for more details. """raise_error(NotImplementedError)
[docs]@abstractmethoddefeigenvectors(self,k=6):# pragma: no cover"""Computes a tensor with the eigenvectors for the Hamiltonian. Args: k (int): Number of eigenvalues to calculate if the Hamiltonian was created using a sparse matrix. This argument is ignored if the Hamiltonian was created using a dense matrix. See :meth:`qibo.backends.abstract.AbstractBackend.eigh` for more details. """raise_error(NotImplementedError)
[docs]defground_state(self):"""Computes the ground state of the Hamiltonian. Uses :meth:`qibo.hamiltonians.AbstractHamiltonian.eigenvectors` and returns eigenvector corresponding to the lowest energy. """returnself.eigenvectors()[:,0]
[docs]@abstractmethoddefexp(self,a):# pragma: no cover"""Computes a tensor corresponding to :math:`\\exp(-i \\, a \\, H)`. Args: a (complex): Complex number to multiply Hamiltonian before exponentiation. """raise_error(NotImplementedError)
[docs]@abstractmethoddefexpectation(self,state,normalize=False):# pragma: no cover"""Computes the real expectation value for a given state. Args: state (ndarray): state in which to calculate the expectation value. normalize (bool, optional): If ``True``, the expectation value :math:`\\ell_{2}`-normalized. Defaults to ``False``. Returns: float: real number corresponding to the expectation value. """raise_error(NotImplementedError)
[docs]@abstractmethoddefexpectation_from_samples(self,freq,qubit_map=None):# pragma: no cover"""Computes the expectation value of a diagonal observable, given computational-basis measurement frequencies. Args: freq (collections.Counter): the keys are the observed values in binary form and the values the corresponding frequencies, that is the number of times each measured value/bitstring appears. qubit_map (tuple): Mapping between frequencies and qubits. If ``None``, then defaults to :math:`[1, \\, 2, \\, \\cdots, \\, \\mathrm{len}(\\mathrm{key})]`. Defaults to ``None``. Returns: float: real number corresponding to the expectation value. """raise_error(NotImplementedError)
@abstractmethoddef__add__(self,o):# pragma: no cover"""Add operator."""raise_error(NotImplementedError)def__radd__(self,o):"""Right operator addition."""returnself.__add__(o)@abstractmethoddef__sub__(self,o):# pragma: no cover"""Subtraction operator."""raise_error(NotImplementedError)@abstractmethoddef__rsub__(self,o):# pragma: no cover"""Right subtraction operator."""raise_error(NotImplementedError)@abstractmethoddef__mul__(self,o):# pragma: no cover"""Multiplication to scalar operator."""raise_error(NotImplementedError)def__rmul__(self,o):"""Right scalar multiplication."""returnself.__mul__(o)@abstractmethoddef__matmul__(self,o):# pragma: no cover"""Matrix multiplication with other Hamiltonians or state vectors."""raise_error(NotImplementedError)