microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.26.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/algorithms/Ising/Simple2dIsingOrder1.qs

112lines · modeblame

8f28aa96DmitryVasilevsky1 years ago1/// # Sample
3f3bde67DmitryVasilevsky1 years ago2/// Simulation of a simple Ising model evolution
3/// on a 2D grid with first-order Trotterization.
8f28aa96DmitryVasilevsky1 years ago4///
5/// # Description
3f3bde67DmitryVasilevsky1 years ago6/// This sample demonstrates simulation of an Ising model Hamiltonian
7/// on N1xN2 2D grid using a first-order Trotter-Suzuki approximation.
8/// This sample can be easily simulated classically with 3x3 grid and
9/// about 1000 shots. This sample is suitable for Base Profile.
8f28aa96DmitryVasilevsky1 years ago10/// For the purpose of simplicity this sample intentionally doesn't
11/// post-process results or perform eigenvalue estimation.
12operation Main() : Result[] {
13// Dimensions of a 2D grid is N1 x N2
14let N1 : Int = 3;
15let N2 : Int = 3;
16
17// Total evolution time
18let evolutionTime : Double = 4.0;
19// Number of steps
20let numberOfSteps : Int = 5;
21
22// Coefficient for 2-qubit interactions between neighboring qubits
23let J : Double = 1.0;
24// Coefficient for external field interaction for individual qubits
25let g : Double = 1.4;
26
27// Also try simulating with different strength of external field:
28// let g = 0.2;
29// let g = 1.0;
30// let g = 1.4;
31// let g = 2.0;
32
33// Model evolution
34IsingModel2DEvolution(N1, N2, J, g, evolutionTime, numberOfSteps)
35}
36
37/// # Summary
38/// Simulate simple Ising model evolution
39///
40/// # Description
41/// Simulates state |𝜓⟩ evolution to find |𝜓(t)⟩=U(t)|𝜓(0)⟩.
42/// |𝜓(0)⟩ is taken to be |0...0⟩.
43/// U(t)=e⁻ⁱᴴᵗ, where H is an Ising model Hamiltonian H = -J·Σ'ᵢⱼZᵢZⱼ + g·ΣᵢXᵢ
44/// Here Σ' is taken over all pairs of neighboring qubits <i,j>.
3f3bde67DmitryVasilevsky1 years ago45/// Simulation is done by performing K steps assuming U(t)≈(U(t/K))ᴷ.
8f28aa96DmitryVasilevsky1 years ago46operation IsingModel2DEvolution(
47N1 : Int,
48N2 : Int,
49J : Double,
50g : Double,
51evolutionTime : Double,
52numberOfSteps : Int
53) : Result[] {
54
55// Allocate qubit grid and structure it as a 2D array.
56use qubits = Qubit[N1 * N2];
57let qubitsAs2D = Std.Arrays.Chunks(N2, qubits);
58
71ade5dcDmitryVasilevsky1 years ago59// Compute the time step
60let dt : Double = evolutionTime / Std.Convert.IntAsDouble(numberOfSteps);
8f28aa96DmitryVasilevsky1 years ago61
71ade5dcDmitryVasilevsky1 years ago62let theta_x = - g * dt;
63let theta_zz = J * dt;
8f28aa96DmitryVasilevsky1 years ago64
65// Perform K steps
66for i in 1..numberOfSteps {
67
68// Single-qubit interaction with external field
69for q in qubits {
70Rx(2.0 * theta_x, q);
71}
72
73// All Rzz gates applied in the following two loops commute so they can be
74// applied in any order. To reduce the depth of the algorithm, Rzz gates
75// between horizontal "even" pairs of qubits are applied first - pairs
76// that start at even indices. Then Rzz gates between "odd" pairs are
77// applied. That way all Rzz between horizontal "even" pairs can potentially
78// be done in parallel. Same is true about horizontal "odd" pairs,
79// vertical "even" pairs and vertical "odd" pairs.
80
81// Horizontal two-qubit interactions
82for row in 0..N1-1 {
83// Horizontal interactions between "even" pairs
84for col in 0..2..N2-2 {
85Rzz(2.0 * theta_zz, qubitsAs2D[row][col], qubitsAs2D[row][col + 1]);
86}
87
88// Horizontal interactions between "odd" pairs
89for col in 1..2..N2-2 {
90Rzz(2.0 * theta_zz, qubitsAs2D[row][col], qubitsAs2D[row][col + 1]);
91}
92}
93
94// Vertical two-qubit interactions
95for col in 0..N2-1 {
96
97// Vertical interactions between "even" pairs
98for row in 0..2..N1-2 {
99Rzz(2.0 * theta_zz, qubitsAs2D[row][col], qubitsAs2D[row + 1][col]);
100}
101
102// Vertical interactions between "odd" pairs
103for row in 1..2..N1-2 {
104Rzz(2.0 * theta_zz, qubitsAs2D[row][col], qubitsAs2D[row + 1][col]);
105}
106
107}
108
109}
110
111MResetEachZ(qubits)
112}