PauliHamiltonian¶
from cascaqit import PauliHamiltonian
PauliHamiltonian ¶
PauliHamiltonian(
hamiltonian_id: str,
terms: tuple[HamiltonianTerm, ...],
constant: float = 0.0,
logical_order: tuple[str, ...] = (),
source_problem_id: str | None = None,
source_problem_hash: str | None = None,
metadata: Mapping[str, Any] = dict(),
schema_version: str = ALGORITHM_SCHEMA_VERSION,
)
Store an ordered weighted Pauli sum: H = constant * I + sum(term.coefficient * term.observable). Energy is the constant plus the weighted expectation values. constant shifts the spectrum; retain it when comparing model energies.
hamiltonian_id must be nonempty and terms must contain at least one term. term_id and observable.name must each be unique, and duplicate observable hashes are rejected. logical_order must be nonempty, contain no duplicates and cover all term targets, fixing the logical order for matrices and results. Construction does not merge repeated terms; normalize the input model first.
source_problem_id and source_problem_hash must be supplied together; the hash is a SHA-256 hex digest. They retain provenance without automatically constructing classical problem candidates or baselines. metadata stores additional information. General Pauli Hamiltonians can be passed to VQE, but need not correspond to classical bitstring optimization problems.
from cascaqit import HamiltonianTerm, PauliHamiltonian, PauliX, PauliZ
hamiltonian = PauliHamiltonian(
"one_qubit", (HamiltonianTerm("x", 0.4, PauliX("q0")),
HamiltonianTerm("z", -0.7, PauliZ("q0"))),
constant=0.15, logical_order=("q0",),
)
assert len(hamiltonian.observable_set().observables) == 2
assert PauliHamiltonian.from_json(hamiltonian.to_json()) == hamiltonian
See the VQE experiment for execution.
Return an ObservableSet in term order for batched measurement. It contains the observables without folding in coefficients or constant, and does not compute expectations. Use the original Hamiltonian to form the weighted sum.
Restore PauliHamiltonian from a dictionary. hamiltonian_id and a nonempty terms array are required. Restore nested HamiltonianTerm/Observable values and validate logical_order, unique identifiers and paired source fields. Missing required fields or invalid values can raise KeyError, TypeError or ValueError.
Parse a JSON object and call from_dict(), returning PauliHamiltonian. Invalid JSON raises a parsing error; a non-object root raises TypeError.
Return a JSON-compatible dictionary, serializing nested objects and converting tuples to arrays. This stores the declaration, not an execution result.
Return a JSON string without writing a file. indent=None uses compact formatting; supply an indentation width for readable output.
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.