So as you've seen, the coat basically works, it's helped to order the randomly generated structure
and we got a structure that was pretty much ordered and neighboring spins are almost parallel to each
other.
But there are also some problems.
You see, this is really not a ferromagnetic yet, even though we are getting closer and closer.
So options would be to increase the number of steps or decrease the size of the of the sample.
So on a number of the moments that we consider, but still, there's always the problem that we may
get stuck in some local energy minimum.
And the problem really is that we do not accept any increase in the energy.
This means if we are trapped inside some local energy minimum, there's really no way to get out of
it.
For example, if you imagine you go hiking and you see a valley and you want to go to the closest valley
and you are only allowed to go down.
So this means you can never get out of your valley to get to a next valley, which may be even lower
in height.
And this the same problem here, so we must allow the codes to sometimes increase the energy to help
us get out of such a valley.
And this is one update one means.
And this, in a physical sense, corresponds to considering finite temperatures.
So this means or when you think about it, the temperature is just some thermal fluctuations, just
some random motion of particles or some random or reorientation of our magnetic moments.
So this means it has a particular energy.
This is corresponding to this Boltzmann constant KB.
And very often you see these Boltzmann factors here, such exponential functions where you compare one
energy with another energy, which will be the thermal energy.
So what we do is we still accept every update in the configuration when the energy is decreased and
we sometimes even accept and change in the magnetization when the energy is increased.
So let me copy this code here.
We want to keep it just for completeness.
I will write it here and call this routine step exchange tea for temperature.
And we will give it another parameter, which will be the CCB kbe temperature.
OK, and now we must implement that.
We accept the change even if the energy increases with a chance that is given by this factor here.
So basically, we must change this code here.
So we need another if statement.
We need if.
A randomly generated number, if not random, thought rent is smaller than this exponential exponential
factor here.
So NPR dot expect.
Minus energy, new minus energy holds divided by K B temp.
So if this is true, if this random randomly generated number between zero and one is smaller than this
term.
Then we will still accept the change so that we accept the change and update the energy as before so
we can copy this line of code and if not, so this means if the energy change is positive and if the
random number is larger than this factor, which is basically always the case.
If the energy change here is very large, then we will decline.
So then we will do what we did before, and I think that's really all we have to change here.
We just included this additional if statement with the random number that's smaller than this exponential
factor.
If it's true, we still accept, and if not, then we decline as before so we cannot run this.
Or we will later rerun the whole code, I would say, and we can now change this one here.
We can just say, OK, this one was our initial code, initial version, zero temperature, and now
we write.
That we use this function here and we need to temperature.
So we could, for example, write KBE temp and KBE temp would be, for example, 0.01.
So I think it works best if this value is pretty small.
I tested this a bit before, so let's see if it works this time.
And then we can comment here.
This would be update one.
Consider finite temperatures.
OK, so now I will restart and rerun everything, and then we will see what will be the result.
First of all, I hope that we do not encounter any error, but here it is again already.
So no error yet.
There's one looks also good.
Let's wait a bit longer.
So you see, we didn't really change that much.
We just changed this one, basically, and we changed this one.
The rest is really the same.
And now we see still the energy decreases, even though sometimes it is allowed to increase.
But it happens not very often.
So we don't really see it here.
And that's the whole trick.
It should encounter only a few times, but it should help us to not get stuck in any energy minimum.
Let's see.
Oh, I think this looks pretty good.
If we would just continue to simulate this, then I think we would get to the correct result because
there is no really no rural yet.
So I would say I will just crank it up here to five million.
And when it's finished, I'll get back to you and show you the result.
So I just tried this a few times and I noticed sometimes it got a bit better, but never really perfect.
And so what I did now is I increased the temperature from 0.01 to 0.1.
And you see in the energy now, that's really sometimes the energy now increase this and this is really
what we want.
So you see this nice fluctuation that here you see exactly why we added this, because if we would not
have allowed for the increase of the energy, then maybe the system would have been stuck in this state
for quite a long time.
But then here, due to the increase of the energy, it allows it to get rid of this local minimum and
go deeper in energy.
So the total configuration now looks like this, which looks kind of like a ferromagnetic magnet.
But you also see, of course, the effect of the thermal fluctuation here.
So you see that now the temperature is maybe a bit too high.
And so now you don't get the perfect ferromagnetic.
But I think this is really what I wanted to show you here, and that's really just a good result.
I would say it's it clearly resembles a thorough magnet.
Another trick that we could do and that we will use later on is that we can, of course, also dynamically
change the temperature.
So we could see, for example, that we do not specify here a fixed temperature, but we see that the
temperature updates in every step.
So we could say we start with 0.01 and then we go one minus
no steps.
Basically, the index idolised by a number of steps.
Exactly.
So that when I is equal to zero, then we have a temperature of 0.1.
And when I is equal to the number of steps, so this is the last step, the temperature is then zero.
So maybe we should go here then to zero point two, but I will check and then show you the results later
on.
So really, decreasing the temperature from 0.2 to zero linearly did the trick.
So you see the energy and you see here at the beginning, the temperature is still quite high.
So you often see this increase, which helps to get to low energies very briefly and then the temperature
decreases and you see the fluctuations become weaker and weaker.
And in the end, there is no fluctuation left.
And our result is really a very almost perfect ferromagnetic and a total energy, as I said.
So the expected result would be minus 800.
And so now we are really, really close to this result.
And it worked out pretty well.
So just to come and everything is, of course, based on randomness.
So if you do this simulation, it could happen that you get a different result and that maybe sometimes
it doesn't work so well.
But I think if you rerun the calculations several times and then look at the solution, which gives
you the lowest energy, then this should really give you such a nice ferromagnetic.