def calculate_energy(J, σ):
    return sum(J_ij*σ[i]*σ[i+1] for i, J_ij in enumerate(J))

J = [1.0, -1.0]
σ = [+1, -1, +1]

calculate_energy(J, σ)

import itertools
for σ in itertools.product(*[{+1,-1} for _ in range(3)]):
    print(calculate_energy(J, σ), σ)

import dimod

J = {(0, 1): 1.0, (1, 2): -1.0}
h = {0:0, 1:0, 2:0}

model = dimod.BinaryQuadraticModel(h, J, 0.0, dimod.SPIN)

sampler = dimod.SimulatedAnnealingSampler()
response = sampler.sample(model, num_reads=10)

[solution.energy for solution in response.data()].count(-2)

import numpy as np
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute
from qiskit import BasicAer
np.set_printoptions(precision=3, suppress=True)
backend = BasicAer.get_backend('statevector_simulator')
q = QuantumRegister(1)
c = ClassicalRegister(1)
circuit = QuantumCircuit(q, c)

circuit.z(q[0])
job = execute(circuit, backend)
state = job.result().get_statevector(circuit)
print(state)

circuit = QuantumCircuit(q, c)
circuit.x(q[0])
circuit.z(q[0])
job = execute(circuit, backend)
state = job.result().get_statevector(circuit)
print(state)

def calculate_energy_expectation(state, hamiltonian):
    return float(np.dot(state.T.conj(), np.dot(hamiltonian, state)).real)

Z = np.array([[1, 0], [0, -1]])
IZ = np.kron(np.eye(2), Z)
ZI = np.kron(Z, np.eye(2))
ZZ = np.kron(Z, Z)
H = -ZZ + -0.5*(ZI+IZ)
ψ = np.kron([[1], [0]], [[1], [0]])
calculate_energy_expectation(ψ, H)

circuit = QuantumCircuit(q, c)
circuit.x(q[0])
circuit.z(q[0])
job = execute(circuit, backend)
state = job.result().get_statevector(circuit)
print("Pauli-X, then Pauli-Z:", state)
circuit = QuantumCircuit(q, c)
circuit.z(q[0])
circuit.x(q[0])
job = execute(circuit, backend)
state = job.result().get_statevector(circuit)
print("Pauli-Z, then Pauli-X:", state)