Example #1
0
    def test_clamped_model_sample_multple(self):
        """
        Test that clamped model sample with multiple clamped variables gets the
        correct energy.
        """

        checkerboard = IsingModel(
            J={(0, 1): 1, (1, 2): 1, (2, 3): 1, (3, 0): 1},
            h={}
        )
        checkerboard.clamp(1, 1)
        checkerboard.clamp(3, -1)

        sample = IsingSample(checkerboard, (-1, 1))

        assert sample.assignment.as_tuple == (-1, 1, 1, -1)
        assert sample.energy == 0

        checkerboard = IsingModel(
            J={(0, 1): 1, (1, 2): 1, (2, 3): 1, (3, 0): 1},
            h={1: 4}
        )
        checkerboard.clamp(0, -1)
        checkerboard.clamp(1, 1)
        checkerboard.clamp(2, 1)
        checkerboard.clamp(3, -1)

        sample = IsingSample(checkerboard, tuple())

        assert sample.assignment.as_tuple == (-1, 1, 1, -1)
        assert sample.energy == 4
Example #2
0
    def test_clamp_variable_not_present(self):
        """
        Make sure invalid variable cannot be clamped.
        """

        h = {0: 4.5, 1: 10, 2: -5}
        J = {(0, 1): -4.5, (1, 2): 5}

        model = IsingModel(J, h)

        with pytest.raises(ValueError):
            model.clamp(8, 1)
Example #3
0
    def test_clamp_variable_already_clamped(self):
        """
        Make sure invalid variable cannot be clamped.
        """

        h = {0: 4.5, 1: 10, 2: -5}
        J = {(0, 1): -4.5, (1, 2): 5}

        model = IsingModel(J, h)

        # Clamp the variable
        model.clamp(2, -1)

        # Attempt to clamp the variable again
        with pytest.raises(ValueError):
            model.clamp(2, 1)
Example #4
0
    def test_clamp_variable_simple(self):
        """
        Makes sure that variable clamping transforms IsingModel in an expected manner.
        """

        h = {0: 4.5, 1: 10, 2: -5}
        J = {(0, 1): -4.5, (1, 2): 5}

        model = IsingModel(J, h)
        model.clamp(0, 1)

        assert model.h_clamped == {1: 5.5, 2: -5}
        assert model.J_clamped == {(1, 2): 5}

        model = IsingModel(J, h)
        model.clamp(0, -1)

        assert model.h_clamped == {1: 14.5, 2: -5}
        assert model.J_clamped == {(1, 2): 5}
Example #5
0
    def test_clamp_multiple_variables(self):
        """
        Make sure clamping multiple variables work.
        """

        h = {0: 4, 1: 10, 2: -5, 3: 4, 4: -5}
        J = {(0, 1): -6, (1, 2): 5, (0, 3): 3, (0, 2): -4}

        model = IsingModel(J, h)

        model.clamp(0, -1)
        model.clamp(2, 1)
        model.clamp(4, -1)

        assert model.h_clamped == {1: 21, 3: 1}
        assert model.J_clamped == {}
Example #6
0
    def test_clamp_not_affecting_original(self):
        """
        Make sure that the original ising model definition is not affected by
        clamping variables.
        """

        h = {0: 4, 1: 10, 2: -5, 3: 4, 4: -5}
        J = {(0, 1): -6, (1, 2): 5, (0, 3): 3, (0, 2): -4}

        model = IsingModel(J, h)

        model.clamp(0, -1)
        model.clamp(2, 1)
        model.clamp(4, -1)

        assert model.h == {0: 4, 1: 10, 2: -5, 3: 4, 4: -5}
        assert model.J == {(0, 1): -6, (1, 2): 5, (0, 3): 3, (0, 2): -4}
Example #7
0
    def test_clamped_model_sample(self):
        """
        Test that clamped model sample gets the correct energy.
        """

        checkerboard = IsingModel(
            J={(0, 1): 1, (1, 2): 1, (2, 3): 1, (3, 0): 1},
            h={}
        )
        checkerboard.clamp(0, 1)

        sample = IsingSample(checkerboard, (-1, 1, -1))

        assert sample.assignment.as_tuple == (1, -1, 1, -1)
        assert sample.assignment == {0: 1, 1: -1, 2: 1, 3: -1}
        assert sample.energy == -4

        checkerboard = IsingModel(
            J={(0, 1): 1, (1, 2): 1, (2, 3): 1, (3, 0): 1},
            h={0: 5}
        )
        checkerboard.clamp(0, 1)

        sample = IsingSample(checkerboard, (-1, 1, -1))

        assert sample.assignment.as_tuple == (1, -1, 1, -1)
        assert sample.assignment == {0: 1, 1: -1, 2: 1, 3: -1}
        assert sample.energy == 1

        checkerboard = IsingModel(
            J={(0, 1): 1, (1, 2): 1, (2, 3): 1, (3, 0): 1},
            h={0: -3, 3: 2}
        )
        checkerboard.clamp(3, -1)

        sample = IsingSample(checkerboard, (-1, -1, -1))

        assert sample.assignment.as_tuple == (-1, -1, -1, -1)
        assert sample.assignment == {0: -1, 1: -1, 2: -1, 3: -1}
        assert sample.energy == 5
Example #8
0
    def test_clamp_variable_invalid_value(self):
        """
        Make sure invalid variable cannot be clamped.
        """

        h = {0: 4.5, 1: 10, 2: -5}
        J = {(0, 1): -4.5, (1, 2): 5}

        model = IsingModel(J, h)

        # Attempt to clamp with some retarded value
        with pytest.raises(ValueError):
            model.clamp(2, 5)

        with pytest.raises(ValueError):
            model.clamp(2, -3)

        with pytest.raises(ValueError):
            model.clamp(2, 1.01)

        with pytest.raises(ValueError):
            model.clamp(2, -0.99)

        with pytest.raises(ValueError):
            model.clamp(2, 0)

        with pytest.raises(ValueError):
            model.clamp(2, -0.5)