Exemplo n.º 1
0
    static void Main(string[] args)
    {
        var size = 30;
        var N    = size * size;

        var csv = new StringBuilder();

        csv.AppendLine("temp,mean_energy,energy_std,mean_spin,spin_std");
        var temps = Enumerable.Range(2, 400);

        foreach (var x in temps)
        {
            double temp = Convert.ToDouble(x) / 100;
            var    iss  = new Ising(size);
            var    res  = iss.MCSteps(temp, 150 * N);

            if (res.Item1.Any() && res.Item2.Any())
            {
                var mean_energy = res.Item1.Average() / N;
                var energy_std  = res.Item1.StandardDeviation() / N;

                var mean_spin = res.Item2.Average() / N;
                var spin_std  = res.Item2.StandardDeviation() / N;
                csv.AppendLine($"{temp},{mean_energy},{energy_std},{mean_spin},{spin_std}");
            }
        }
        File.WriteAllText("data.csv", csv.ToString());
    }
Exemplo n.º 2
0
        public static void Run(Ising system, StdMt19937 randomNumberEngine, ScheduleList scheduleList)
        {
            if (system == null)
            {
                throw new ArgumentNullException(nameof(system));
            }
            if (randomNumberEngine == null)
            {
                throw new ArgumentNullException(nameof(randomNumberEngine));
            }
            if (scheduleList == null)
            {
                throw new ArgumentNullException(nameof(scheduleList));
            }

            system.ThrowIfDisposed();
            randomNumberEngine.ThrowIfDisposed();
            scheduleList.ThrowIfDisposed();

            if (!UpdaterElementTypesRepository.SupportTypes.TryGetValue(typeof(T), out var types))
            {
                throw new NotSupportedException($"{typeof(T).Name} does not support");
            }
            if (types.Item2 != system.IsingType)
            {
                throw new NotSupportedException($"{typeof(T).Name} supports only {system.IsingType}");
            }
            if (types.Item3 != system.GraphType)
            {
                throw new NotSupportedException($"{typeof(T).Name} supports only {system.GraphType}");
            }
            if (types.Item4 != system.FloatType)
            {
                throw new NotSupportedException($"{typeof(T).Name} supports only {system.FloatType}");
            }

            var updaterType = types.Item1;
            var ret         = NativeMethods.algorithm_Algorithm_run(updaterType,
                                                                    system.NativePtr,
                                                                    system.IsingType,
                                                                    system.GraphType,
                                                                    system.FloatType,
                                                                    randomNumberEngine.NativePtr,
                                                                    scheduleList.NativePtr);
        }
Exemplo n.º 3
0
        public static Spins GetSolution(Ising system)
        {
            if (system == null)
            {
                throw new ArgumentNullException(nameof(system));
            }

            system.ThrowIfDisposed();

            var ret = NativeMethods.result_get_solution(system.NativePtr,
                                                        system.IsingType,
                                                        system.GraphType,
                                                        system.FloatType,
                                                        out var spins);

            using (var vector = new StdVector <int>(spins))
                return(new Spins(vector.ToArray().Select(v => new Spin(v))));
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            #region Basic Definitions

            // We start by loading the simulator that we will use to run our Q# operations.
            var qsim = new QuantumSimulator();

            // For this example, we'll consider a chain of twelve sites, each one of which
            // is simulated using a single qubit.
            var nSites = 12;

            // We'll sweep from the transverse to the final Hamiltonian in time t = 10.0,
            // where the units are implicitly fixed by the units of the Hamiltonian itself.
            var sweepTime = 10.0;

            // Finally, we'll then decompose the time evolution down into small steps.
            // During each step, we'll perform each term in the Hamiltonian individually.
            // By the Trotter–Suzuki decomposition (also implemented in the canon), this
            // approximates the complete Hamiltonian for the entire sweep time.
            //
            // If we choose the evolution time carefully, we should prepare the ground
            // state of our final Hamiltonian (see the references in README.md for more
            // details).
            var timeStep = 0.1;

            // For diagnostic purposes, before we proceed to the next step, we'll print
            // out a description of the parameters we just defined.
            Console.WriteLine("Ising model ground state preparation:");
            Console.WriteLine($"\t{nSites} sites\n\t{sweepTime} sweep time\n\t{timeStep} time step");

            #endregion

            #region Calling into Q#

            // Now that we've defined everything we need, let's proceed to
            // actually call the simulator. Since there's a finite chance of successfully
            // preparing the ground state, we will call our new operation through
            // the simulator several times, reporting the magnetization after each attempt.

            foreach (var idxAttempt in Enumerable.Range(0, 100))
            {
                // Each operation has a static method called Run which takes a simulator as
                // an argument, along with all the arguments defined by the operation itself.
                var task = Ising.Run(qsim, nSites, sweepTime, timeStep);

                // Since this method is asynchronous, we need to explicitly
                // wait for the result back from the simulator. We do this by
                // getting the Result property. To turn the result back into a
                // conventional .NET array, we finish by calling ToArray() and
                // using a C# lambda function to convert each Result into a
                // floating point number representing the observed spin.
                var data = task.Result.ToArray().Select((result) => result == Result.One ? 0.5 : -0.5);

                // We can now compute the magnetization entirely in C# code,
                // since data is an array of the classical measurement results
                // observed back from our simulation.
                var magnetization = data.Sum();

                Console.WriteLine($"Magnetization observed in attempt {idxAttempt}: {magnetization}");
            }

            #endregion

            Console.ReadLine();
        }
 public IsingTests()
 {
     iss = new Ising(size);
 }