def test_qubosimulation_vs_qusosimulation(): ising = sum(-spin_var(i) * spin_var(i + 1) for i in range(15)) qubo = quso_to_qubo(ising) schedule = [(T, 20) for T in range(3, 0, -1)] spin = QUSOSimulation(ising) boolean = QUBOSimulation(qubo) assert spin.initial_state == boolean_to_spin(boolean.initial_state) spin.schedule_update(schedule, seed=4) boolean.schedule_update(schedule, seed=4) assert spin.state == boolean_to_spin(boolean.state) initial_state = [0] * 8 + [1] * 8 spin = QUSOSimulation(ising, boolean_to_spin(initial_state)) boolean = QUBOSimulation(qubo, initial_state) assert spin.initial_state == boolean_to_spin(boolean.initial_state) spin.schedule_update(schedule, seed=4) boolean.schedule_update(schedule, seed=4) assert spin.state == boolean_to_spin(boolean.state)
def test_qubosimulation_reset(): ising = sum(-spin_var(i) * spin_var(i + 1) for i in range(3)) initial_state = {0: 0, 1: 0, 2: 1, 3: 0} sim = QUBOSimulation(ising, initial_state) sim.schedule_update([(2, 100)]) sim.update(2, 2000) sim.reset() assert sim.state == initial_state == sim.initial_state
def test_spin_var(): z = [spin_var(i) for i in range(5)] assert all(z[i] == {(i, ): 1} for i in range(5)) assert z[0] * z[1] * z[2] == {(0, 1, 2): 1} assert sum(z) == {(i, ): 1 for i in range(5)} assert isinstance(z[0], PCSO) assert all(z[i].name == i for i in range(5))
def test_pusosimulation_update_vs_updateschedule(): ising = sum(-spin_var(i) * spin_var(i + 1) for i in range(3)) sim = PUSOSimulation(ising) sim.update(4, 100, seed=0) sim.update(2, 23) sim.update(5, 48) result1 = sim.state sim.reset() sim.schedule_update([(4, 100), (2, 23), (5, 48)], seed=0) result2 = sim.state sim.reset() sim.schedule_update([(4, 100), (2, 23), (5, 48)], seed=1) result3 = sim.state assert result1 == result2 assert result1 != result3
def test_qusosimulation_updates(): sim = QUSOSimulation(spin_var(0)) state = sim.state sim.update(5, 0) assert sim.state == state sim.schedule_update([(5, 0), (3, 0)]) assert sim.state == state with assert_raises(ValueError): sim.update(4, -1)
def test_sum(): xs = [boolean_var(i) for i in range(100)] assert sum(xs) == qvsum(xs) == qvsum(xs[i] for i in range(100)) assert sum(xs, 2) == qvsum(xs, 2) assert isinstance(qvsum(xs), PCBO) zs = [spin_var(i) for i in range(100)] assert sum(zs) == qvsum(zs) == qvsum(zs[i] for i in range(100)) assert sum(zs, 2) == qvsum(zs, 2) assert isinstance(qvsum(zs), PCSO)
def test_qusosimulation_reset(): ising = sum(-spin_var(i) * spin_var(i + 1) for i in range(3)) initial_state = {0: 1, 1: 1, 2: -1, 3: 1} sim = QUSOSimulation(ising, initial_state) sim.schedule_update([(2, 100)]) sim.update(2, 2000) sim.reset() assert sim.state == initial_state == sim.initial_state # test the same thing but with matrix ising = QUSOMatrix(sum(-spin_var(i) * spin_var(i + 1) for i in range(3))) initial_state = {0: 1, 1: 1, 2: -1, 3: 1} sim = QUSOSimulation(ising, initial_state) sim.schedule_update([(2, 100)]) sim.update(2, 2000) sim.reset() assert sim.state == initial_state == sim.initial_state # test the same thing but with QUSO ising = QUSO(sum(-spin_var(i) * spin_var(i + 1) for i in range(3))) initial_state = {0: 1, 1: 1, 2: -1, 3: 1} sim = QUSOSimulation(ising, initial_state) sim.schedule_update([(2, 100)]) sim.update(2, 2000) sim.reset() assert sim.state == initial_state == sim.initial_state
def test_pusosimulation_set_state(): ising = sum(-spin_var(i) * spin_var(i + 1) for i in range(9)) sim = PUSOSimulation(ising) assert sim.state == {i: 1 for i in ising.variables} sim = PUSOSimulation(ising, {i: -1 for i in ising.variables}) assert sim.state == {i: -1 for i in ising.variables} with assert_raises(ValueError): sim.set_state({i: 3 for i in ising.variables}) with assert_raises(ValueError): PUSOSimulation(ising, {i: 0 for i in ising.variables}) sim = PUSOSimulation({(0, ): 1}) with assert_raises(ValueError): sim.set_state([0]) sim.set_state([-1]) assert sim.state == {0: -1}
def test_pusosimulation_paststates_reset(): ising = sum(-spin_var(i) * spin_var(i + 1) for i in range(3)) initial_state = {0: 1, 1: 1, 2: -1, 3: 1} sim = PUSOSimulation(ising, initial_state, 1000) assert sim.memory == 1000 assert sim.get_past_states() == [sim.state] states = [sim.state] for _ in range(100): sim.update(2) states.append(sim.state) assert states == sim.get_past_states() assert states[-50:] == sim.get_past_states(50) sim.update(2, 2000) assert len(sim.get_past_states()) == 1000 assert sim.get_past_states(1) == [sim.state] sim.reset() assert sim.state == initial_state == sim.initial_state assert sim.get_past_states() == [initial_state]
def test_qubosimulation_set_state(): ising = quso_to_qubo(sum(-spin_var(i) * spin_var(i + 1) for i in range(9))) sim = QUBOSimulation(ising) assert sim.state == {i: 0 for i in ising.variables} sim = QUBOSimulation(ising, {i: 1 for i in ising.variables}) assert sim.state == {i: 1 for i in ising.variables} with assert_raises(ValueError): sim.set_state({i: 3 for i in ising.variables}) with assert_raises(ValueError): QUBOSimulation(ising, {i: -1 for i in ising.variables}) sim = QUBOSimulation({(0, ): 1}) with assert_raises(ValueError): sim.set_state([-1]) sim.set_state([1]) assert sim.state == {0: 1} # test the same thing but wiht matrix ising = quso_to_qubo( QUSOMatrix(sum(-spin_var(i) * spin_var(i + 1) for i in range(9)))) sim = QUBOSimulation(ising) assert sim.state == {i: 0 for i in ising.variables} sim = QUBOSimulation(ising, {i: 1 for i in ising.variables}) assert sim.state == {i: 1 for i in ising.variables} with assert_raises(ValueError): sim.set_state({i: 3 for i in ising.variables}) with assert_raises(ValueError): QUBOSimulation(ising, {i: -1 for i in ising.variables}) sim = QUBOSimulation({(0, ): 1}) with assert_raises(ValueError): sim.set_state([-1]) sim.set_state([1]) assert sim.state == {0: 1} # test the same thing but wiht QUBO ising = quso_to_qubo( QUBO(sum(-spin_var(i) * spin_var(i + 1) for i in range(9)))) sim = QUBOSimulation(ising) assert sim.state == {i: 0 for i in ising.variables} sim = QUBOSimulation(ising, {i: 1 for i in ising.variables}) assert sim.state == {i: 1 for i in ising.variables} with assert_raises(ValueError): sim.set_state({i: 3 for i in ising.variables}) with assert_raises(ValueError): QUBOSimulation(ising, {i: -1 for i in ising.variables}) sim = QUBOSimulation({(0, ): 1}) with assert_raises(ValueError): sim.set_state([-1]) sim.set_state([1]) assert sim.state == {0: 1}
def test_qusosimulation_set_state(): ising = sum(-spin_var(i) * spin_var(i + 1) for i in range(9)) sim = QUSOSimulation(ising) assert sim.state == {i: 1 for i in ising.variables} sim = QUSOSimulation(ising, {i: -1 for i in ising.variables}) assert sim.state == {i: -1 for i in ising.variables} with assert_raises(ValueError): sim.set_state({i: 3 for i in ising.variables}) with assert_raises(ValueError): QUSOSimulation(ising, {i: 0 for i in ising.variables}) sim = QUSOSimulation({(0, ): 1}) with assert_raises(ValueError): sim.set_state([0]) sim.set_state([-1]) assert sim.state == {0: -1} # test the same but with matrix ising = QUSOMatrix(sum(-spin_var(i) * spin_var(i + 1) for i in range(9))) sim = QUSOSimulation(ising) assert sim.state == {i: 1 for i in ising.variables} sim = QUSOSimulation(ising, {i: -1 for i in ising.variables}) assert sim.state == {i: -1 for i in ising.variables} with assert_raises(ValueError): sim.set_state({i: 3 for i in ising.variables}) with assert_raises(ValueError): QUSOSimulation(ising, {i: 0 for i in ising.variables}) sim = QUSOSimulation({(0, ): 1}) with assert_raises(ValueError): sim.set_state([0]) sim.set_state([-1]) assert sim.state == {0: -1} # test the same for QUSO ising = QUSO(sum(-spin_var(i) * spin_var(i + 1) for i in range(9))) sim = QUSOSimulation(ising) assert sim.state == {i: 1 for i in ising.variables} sim = QUSOSimulation(ising, {i: -1 for i in ising.variables}) assert sim.state == {i: -1 for i in ising.variables} with assert_raises(ValueError): sim.set_state({i: 3 for i in ising.variables}) with assert_raises(ValueError): QUSOSimulation(ising, {i: 0 for i in ising.variables}) sim = QUSOSimulation({(0, ): 1}) with assert_raises(ValueError): sim.set_state([0]) sim.set_state([-1]) assert sim.state == {0: -1}
def test_qusosimulation_initialstate_variables(): ising = dict(sum(-spin_var(i) * spin_var(i + 1) for i in range(3))) initial_state = {0: 1, 1: 1, 2: -1, 3: 1} sim = QUSOSimulation(ising, initial_state) assert sim._variables == set(initial_state.keys())