ket.qulib.ham

Hamiltonian library.

Summary

Functions

maxcut

Max-Cut Hamiltonian.

knapsack

Knapsack Hamiltonian.

tsp

Traveling Salesman Problem (TSP) Hamiltonian.

x_mixer

X-Mixer Hamiltonian.

xy_mixer

XY-Mixer Hamiltonian.

falqon_a

FALQON A operator.

falqon_b

FALQON B operator.

falqon_get_beta_fo

Get FALQON first-order beta parameter.

falqon_get_beta_so

Get FALQON second-order beta parameter.

Functions

maxcut(
edges: list[tuple[int, int]],
qubits: Quant,
) → Hamiltonian[source]

Max-Cut Hamiltonian.

\[\sum_{a, b\,\in\, \mathcal{E}}\frac{1}{2}(1-Z_a Z_b)\]
Parameters:
  • edges – List of edges in the graph.

  • qubits – Qubits representing the graph nodes.

knapsack(
weights: list[float],
values: list[float],
capacity: float,
qubits: Quant,
penalty: None | float = None,
) → Hamiltonian[source]

Knapsack Hamiltonian.

Implements the QUBO formulation of the 0/1 Knapsack problem using a quadratic penalty to enforce the capacity constraint.

The Hamiltonian minimized is:

\[-\sum_i v_i x_i + A \left(\sum_i w_i x_i - C\right)^2\]

where \(x_i = \frac{1 - Z_i}{2}\) and \(A\) is the penalty coefficient.

If penalty is not provided, it defaults to max(values), which is typically sufficient to discourage capacity violations in practical QAOA runs.

Parameters:
  • weights – List of item weights \(w_i\).

  • values – List of item values \(v_i\).

  • capacity – Maximum allowed total weight \(C\).

  • qubits – Qubits representing the binary decision variables.

  • penalty – Penalty coefficient \(A\). If None, defaults to max(values).

Returns:

Hamiltonian representing the knapsack objective.

tsp(
cities: dict[tuple[int, int], float],
n: int,
qubits: list,
penalty: float | None = None,
) → Hamiltonian[source]

Traveling Salesman Problem (TSP) Hamiltonian.

Implements the QUBO formulation of the TSP using a quadratic penalty to enforce permutation constraints.

The Hamiltonian minimized is:

\[\sum_{t=0}^{n-1} \sum_{i,j} d_{ij} x_{i,t} x_{j,t+1} + A \sum_i \left(\sum_t x_{i,t} - 1\right)^2 + A \sum_t \left(\sum_i x_{i,t} - 1\right)^2\]

where:

  • \(d_{ij}\) is the directed distance from city \(i\) to city \(j\),

  • \(x_{i,t} \in \{0,1\}\) indicates whether city \(i\) is visited at tour position \(t\),

  • \(x_{i,t} = \frac{1 - Z_{i,t}}{2}\),

  • \(A\) is the penalty coefficient.

The first term minimizes the total tour length. The second term enforces that each city is visited exactly once. The third term enforces that exactly one city is assigned to each tour position.

If penalty is not provided, it defaults to 2 * max(cities.values()), which is typically sufficient to discourage constraint violations.

Parameters:
  • cities – Dictionary mapping directed edges (i, j) to distances \(d_{ij}\).

  • n – Number of cities.

  • qubits – Flat list of \(n^2\) qubits representing the binary variables \(x_{i,t}\).

  • penalty – Penalty coefficient \(A\). If None, defaults to 2 * max(cities.values()).

Returns:

Hamiltonian representing the directed TSP cost function with quadratic constraint penalties.

x_mixer(qubits: Quant) → Hamiltonian[source]

X-Mixer Hamiltonian.

\[\sum_j X_j\]
Parameters:

qubits – Qubits to apply the mixer.

xy_mixer(qubits, edges: list[tuple[int, int]] | None = None) → Hamiltonian[source]

XY-Mixer Hamiltonian.

\[\frac{1}{2}\sum_{a, b\,\in\, \mathcal{E}} X_a X_b + Y_a Y_b\]

If edges is None, a ring topology is assumed.

Parameters:
  • qubits – Qubits to apply the mixer.

  • edges – List of edges in the graph.

falqon_a(hp: Hamiltonian, hd: Hamiltonian) → Hamiltonian[source]

FALQON A operator.

\[A = i[H_d, H_p]\]

See https://arxiv.org/abs/2103.08619.

Parameters:
  • hp – Problem Hamiltonian.

  • hd – Driver Hamiltonian.

falqon_b(hp: Hamiltonian, hd: Hamiltonian) → Hamiltonian[source]

FALQON B operator.

\[B = 1/2 [ [H_d, H_p], H_d ]\]

See https://arxiv.org/abs/2407.17810.

Parameters:
  • hp – Problem Hamiltonian.

  • hd – Driver Hamiltonian.

falqon_get_beta_fo(hp: Hamiltonian, hd: Hamiltonian) → float[source]

Get FALQON first-order beta parameter.

See https://arxiv.org/abs/2103.08619.

This function computes the expectation value, triggering the circuit execution.

Parameters:
  • hp – Problem Hamiltonian.

  • hd – Driver Hamiltonian.

falqon_get_beta_so(
delta_t,
hp: Hamiltonian,
hd: Hamiltonian,
) → float[source]

Get FALQON second-order beta parameter.

See https://arxiv.org/abs/2407.17810.

This function computes the expectation values, triggering the circuit execution.

Parameters:
  • delta_t – Time step.

  • hp – Problem Hamiltonian.

  • hd – Driver Hamiltonian.