Uh oh!
There was an error while loading. Please reload this page.
forked from microsoft/Quantum
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
87 lines (67 loc) · 3.66 KB
/
Copy pathProgram.cs
File metadata and controls
87 lines (67 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingMicrosoft.Quantum.Simulation.Core;
usingMicrosoft.Quantum.Simulation.Simulators;
namespaceMicrosoft.Quantum.Samples.SimpleIsing
{
classProgram
{
staticvoidMain(string[]args)
{
#region Basic Definitions
// We start by loading the simulator that we will use to run our Q# operations.
varqsim=newQuantumSimulator();
// For this example, we'll consider a chain of twelve sites, each one of which
// is simulated using a single qubit.
varnSites=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.
varsweepTime=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).
vartimeStep=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(varidxAttemptinEnumerable.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.
vartask=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.
vardata=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.
varmagnetization=data.Sum();
Console.WriteLine($"Magnetization observed in attempt {idxAttempt}: {magnetization}");
}
#endregion
Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
}
}
}