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

Migrate to Qiskit Nature v0.5 #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
83 changes: 11 additions & 72 deletions heisenberg_model.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,14 @@
# Modifed from the Ising model found here: https://qiskit.org/documentation/nature/_modules/qiskit_nature/problems/second_quantization/lattice/models/ising_model.html#IsingModel

"""The Heisenberg model"""
import logging
import numpy as np

from fractions import Fraction
from typing import Optional

from qiskit_nature.operators.second_quantization import SpinOp
from qiskit_nature.problems.second_quantization.lattice.lattices import Lattice
from qiskit_nature.problems.second_quantization.lattice.models.lattice_model import LatticeModel
from qiskit_nature.second_q.operators import SpinOp
from qiskit_nature.second_q.hamiltonians.ising_model import IsingModel

class HeisenbergModel(LatticeModel):
class HeisenbergModel(IsingModel):
"""The Heisenberg model."""

def coupling_matrix(self) -> np.ndarray:
"""Return the coupling matrix."""
return self.interaction_matrix()


@classmethod
def uniform_parameters(
cls,
lattice: Lattice,
uniform_interaction: complex,
uniform_onsite_potential: complex,
) -> "HeisenbergModel":
"""Set a uniform interaction parameter and on-site potential over the input lattice.

Args:
lattice: Lattice on which the model is defined.
uniform_interaction: The interaction parameter.
uniform_onsite_potential: The on-site potential.

Returns:
The Lattice model with uniform parameters.
"""
return cls(
cls._generate_lattice_from_uniform_parameters(
lattice, uniform_interaction, uniform_onsite_potential
)
)


@classmethod
def from_parameters(
cls,
interaction_matrix: np.ndarray,
) -> "HeisenbergModel":
"""Return the Hamiltonian of the Lattice model
from the given interaction matrix and on-site interaction.

Args:
interaction_matrix: A real or complex valued square matrix.

Returns:
LatticeModel: The Lattice model generated from the given interaction
matrix and on-site interaction.

Raises:
ValueError: If the interaction matrix is not square matrix, it is invalid.
"""
return cls(cls._generate_lattice_from_parameters(interaction_matrix))


def second_q_ops(self, display_format: Optional[str] = None) -> SpinOp:
def second_q_op(self) -> SpinOp:
"""Return the Hamiltonian of the Heisenberg model in terms of `SpinOp`.

Args:
Expand All @@ -72,25 +17,19 @@ def second_q_ops(self, display_format: Optional[str] = None) -> SpinOp:
Returns:
SpinOp: The Hamiltonian of the Heisenberg model.
"""
if display_format is not None:
logger.warning(
"Spin operators do not support display-format. Provided display-format "
"parameter will be ignored."
)
ham = []
ham = {}
weighted_edge_list = self._lattice.weighted_edge_list
register_length = self._lattice.num_nodes
# kinetic terms
for node_a, node_b, weight in weighted_edge_list:
if node_a == node_b:
index = node_a
ham.append((f"X_{index}", weight))
ham[f"X_{index}"] = weight

else:
index_left = node_a
index_right = node_b
coupling_parameter = weight
ham.append((f"X_{index_left} X_{index_right}", coupling_parameter))
ham.append((f"Y_{index_left} Y_{index_right}", coupling_parameter))
ham.append((f"Z_{index_left} Z_{index_right}", coupling_parameter))
return SpinOp(ham, spin=Fraction(1, 2), register_length=register_length)
ham[f"X_{index_left} X_{index_right}"] = coupling_parameter
ham[f"Y_{index_left} Y_{index_right}"] = coupling_parameter
ham[f"Z_{index_left} Z_{index_right}"] = coupling_parameter
return SpinOp(ham, spin=Fraction(1, 2), num_spins=self.register_length)
Loading