MCPcopy Create free account
hub / github.com/dwavesystems/dwave-cloud-client / evaluate_ising

Function evaluate_ising

dwave/cloud/utils/qubo.py:48–75  ·  view source on GitHub ↗

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
                   )

Source from the content-addressed store, hash-verified

46
47
48def 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
78def active_qubits(linear: Union[Mapping, Sequence],

Callers 8

_checkMethod · 0.90
_submit_and_checkMethod · 0.90
test_submit_batchMethod · 0.90
test_cancel_batchMethod · 0.90
test_wait_manyMethod · 0.90
test_as_completedMethod · 0.90

Calls 1

uniform_iteratorFunction · 0.85

Tested by 8

_checkMethod · 0.72
_submit_and_checkMethod · 0.72
test_submit_batchMethod · 0.72
test_cancel_batchMethod · 0.72
test_wait_manyMethod · 0.72
test_as_completedMethod · 0.72