Skip to content

HamiltonianTerm

from cascaqit import HamiltonianTerm

HamiltonianTerm

HamiltonianTerm(
    term_id: str,
    coefficient: float,
    observable: Observable,
    schema_version: str = ALGORITHM_SCHEMA_VERSION,
)

A finite, nonzero real coefficient multiplying an Observable, for use in PauliHamiltonian. term_id identifies the weighted term while observable.name identifies the observable; they serve different result references. Negative coefficients are allowed. Remove zero terms before construction and put a constant energy shift in PauliHamiltonian.constant.

from cascaqit import HamiltonianTerm, PauliZ

term = HamiltonianTerm("weighted_z", -0.7, PauliZ("q0"))
assert term.coefficient == -0.7
assert HamiltonianTerm.from_dict(term.to_dict()) == term

This is a declaration. Obtain expectation values through VQE or an observable execution path.

from_dict

from_dict(data: Mapping[str, Any]) -> HamiltonianTerm

Restore a weighted term and its nested Observable from a dictionary, checking term_id, coefficient and object type. There is no from_json(); decode JSON text with json.loads() first.

to_dict

to_dict() -> dict[str, Any]

Return a JSON-compatible dictionary, serializing nested objects and converting tuples to arrays. This stores the declaration, not an execution result.

to_json

to_json(*, indent: int | None = None) -> str

Return a JSON string without writing a file. indent=None uses compact formatting; supply an indentation width for readable output.

stable_hash

stable_hash() -> str

Return the SHA-256 hex digest of canonical JSON. Fields, identifiers and metadata can affect it. Use it to compare saved content, not to decide physical equivalence.