Skip to content

Allow generate_xyz_hamiltonian to accept arbitrary coefficient values - #208

Open
phalakbh wants to merge 6 commits into
Qiskit:mainfrom
phalakbh:add-arbitrary-coeff-xyz
Open

Allow generate_xyz_hamiltonian to accept arbitrary coefficient values#208
phalakbh wants to merge 6 commits into
Qiskit:mainfrom
phalakbh:add-arbitrary-coeff-xyz

Conversation

@phalakbh

@phalakbh phalakbh commented Jul 31, 2026

Copy link
Copy Markdown

Summary

This PR extends generate_xyz_hamiltonian to accept arbitrary per-edge and per-site coefficients, replacing the previous restriction to a single uniform scalar or length-3 sequence.

What changed

  • coupling_constants now accepts a dict[tuple[int, int], float | Sequence[float]], mapping individual graph edges to their own (Jxx, Jyy, Jzz) triplet. Edges not present in the dict default to (0.0, 0.0, 0.0).

  • ext_magnetic_field now accepts a dict[int, float | Sequence[float]], mapping individual qubit indices to their own (hx, hy, hz) triplet. Qubits not present in the dict default to (0.0, 0.0, 0.0).

Both parameters remain fully backward-compatible — scalar and length-3 sequence inputs continue to work as before (uniform application across all edges/qubits).

Validation added

  • _validate_xyz_input — rejects non-dict sequences whose length ≠ 3 before normalization
  • _validate_edge_dict_keys — rejects dict keys that are not (int, int) tuples
  • _normalize_edge_key — canonicalises (i, j) / (j, i) so both orderings resolve to the same edge; raises on malformed keys
  • _normalize_coupling_constants — raises on conflicting values for the same canonical edge (e.g. both (0, 1) and (1, 0) supplied with different coefficients)
  • _normalize_ext_magnetic_field — raises if dict keys are not integers
    All reachable ValueError paths are documented in the Raises section of the public docstring.

Tests

Unit tests cover:

  • Uniform scalar / length-3 sequence inputs (existing behaviour)
  • Dict-based per-edge coupling constants (valid keys, default fallback, both orderings of the same edge)
  • Dict-based per-qubit external magnetic field (valid keys, default fallback)
  • All ValueError error paths

Fixes #34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend generate_xyz_hamiltonian to allow for arbitrary coefficients.

1 participant