Calculate the energy of a state given the Hamiltonian. Args: linear: Linear Hamiltonian terms. quad: Quadratic Hamiltonian terms. offset: Energy offset. state: Vector of spins describing the system state Returns: Energy of the state evaluated by the
(linear: Union[Mapping, Sequence],
quad: Mapping,
state: Sequence,
offset: float = 0
)
| 46 | |
| 47 | |
| 48 | def evaluate_ising(linear: Union[Mapping, Sequence], |
| 49 | quad: Mapping, |
| 50 | state: Sequence, |
| 51 | offset: float = 0 |
| 52 | ) -> float: |
| 53 | """Calculate the energy of a state given the Hamiltonian. |
| 54 | |
| 55 | Args: |
| 56 | linear: Linear Hamiltonian terms. |
| 57 | quad: Quadratic Hamiltonian terms. |
| 58 | offset: Energy offset. |
| 59 | state: Vector of spins describing the system state |
| 60 | |
| 61 | Returns: |
| 62 | Energy of the state evaluated by the given energy function. |
| 63 | """ |
| 64 | |
| 65 | # note: we avoid numpy import by tolist() check |
| 66 | if hasattr(state, 'tolist') and callable(state.tolist): |
| 67 | return evaluate_ising(linear, quad, state.tolist(), offset=offset) |
| 68 | |
| 69 | # Accumulate the linear and quadratic values |
| 70 | energy = offset |
| 71 | for index, value in uniform_iterator(linear): |
| 72 | energy += state[index] * value |
| 73 | for (index_a, index_b), value in quad.items(): |
| 74 | energy += value * state[index_a] * state[index_b] |
| 75 | return energy |
| 76 | |
| 77 | |
| 78 | def active_qubits(linear: Union[Mapping, Sequence], |