microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/copilot

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/estimation/Dynamics.qs

177lines · modecode

1/// # Sample
2/// Quantum Dynamics
3///
4/// # Description
5/// This example demonstrates quantum dynamics in a style tailored for
6/// resource estimation. The sample is specifically the simulation
7/// of an Ising model Hamiltonian on an N1xN2 2D lattice using a
8/// fourth-order Trotter Suzuki product formula, assuming
9/// a 2D qubit architecture with nearest-neighbor connectivity.
10/// The is an example of a program that is not amenable to simulating
11/// classically, but can be run through resource estimation to determine
12/// what size of quantum system would be needed to solve the problem.
13namespace QuantumDynamics {
14
15 open Microsoft.Quantum.Math;
16 open Microsoft.Quantum.Arrays;
17
18
19 @EntryPoint()
20 operation Main() : Unit {
21 // n : Int, m : Int, t: Double, u : Double, tstep : Double
22
23 let n = 10;
24 let m = 10;
25
26 let J = 1.0;
27 let g = 1.0;
28
29 let totTime = 30.0;
30 let dt = 0.9;
31
32 IsingModel2DSim(n, m, J, g, totTime, dt);
33 }
34
35 /// # Summary
36 /// The function below creates a sequence containing the rotation angles that will be applied with the two operators used in the expansion of the Trotter-Suzuki formula.
37 /// # Input
38 /// ## p (Double) : Constant used for fourth-order formulas
39 ///
40 /// ## dt (Double) : Time-step used to compute rotation angles
41 ///
42 /// ## J (Double) : coefficient for 2-qubit interactions
43 ///
44 /// ## g (Double) : coefficient for transverse field
45 ///
46 /// # Output
47 /// ## values (Double[]) : The list of rotation angles to be applies in sequence with the corresponding operators
48 ///
49 function SetAngleSequence(p : Double, dt : Double, J : Double, g : Double) : Double[] {
50
51 let len1 = 3;
52 let len2 = 3;
53 let valLength = 2 * len1 + len2 + 1;
54 mutable values = [0.0, size = valLength];
55
56 let val1 = J * p * dt;
57 let val2 = -g * p * dt;
58 let val3 = J * (1.0 - 3.0 * p) * dt / 2.0;
59 let val4 = g * (1.0 - 4.0 * p) * dt / 2.0;
60
61 for i in 0..len1 {
62
63 if (i % 2 == 0) {
64 set values w/= i <- val1;
65 } else {
66 set values w/= i <- val2;
67 }
68
69 }
70
71 for i in len1 + 1..len1 + len2 {
72 if (i % 2 == 0) {
73 set values w/= i <- val3;
74 } else {
75 set values w/= i <- val4;
76 }
77 }
78
79 for i in len1 + len2 + 1..valLength - 1 {
80 if (i % 2 == 0) {
81 set values w/= i <- val1;
82 } else {
83 set values w/= i <- val2;
84 }
85 }
86 return values;
87 }
88
89 /// # Summary
90 /// Applies e^-iX(theta) on all qubits in the 2D lattice as part of simulating the transverse field in the Ising model
91 /// # Input
92 /// ## n (Int) : Lattice size for an n x n lattice
93 ///
94 /// ## qArr (Qubit[][]) : Array of qubits representing the lattice
95 ///
96 /// ## theta (Double) : The angle/time-step for which the unitary simulation is done.
97 ///
98 operation ApplyAllX(n : Int, qArr : Qubit[][], theta : Double) : Unit {
99 // This applies `Rx` with an angle of `2.0 * theta` to all qubits in `qs`
100 // using partial application
101 for row in 0..n - 1 {
102 ApplyToEach(Rx(2.0 * theta, _), qArr[row]);
103 }
104 }
105
106 /// # Summary
107 /// Applies e^-iP(theta) where P = Z o Z as part of the repulsion terms.
108 /// # Input
109 /// ## n, m (Int, Int) : Lattice sizes for an n x m lattice
110 ///
111 /// ## qArr (Qubit[]) : Array of qubits representing the lattice
112 ///
113 /// ## theta (Double) : The angle/time-step for which unitary simulation is done.
114 ///
115 /// ## dir (Bool) : Direction is true for vertical direction.
116 ///
117 /// ## grp (Bool) : Group is true for odd starting indices
118 ///
119 operation ApplyDoubleZ(n : Int, m : Int, qArr : Qubit[][], theta : Double, dir : Bool, grp : Bool) : Unit {
120 let start = grp ? 1 | 0; // Choose either odd or even indices based on group number
121 let P_op = [PauliZ, PauliZ];
122 let c_end = dir ? m - 1 | m - 2;
123 let r_end = dir ? m - 2 | m - 1;
124
125 for row in 0..r_end {
126 for col in start..2..c_end {
127 // Iterate through even or odd columns based on `grp`
128
129 let row2 = dir ? row + 1 | row;
130 let col2 = dir ? col | col + 1;
131
132 Exp(P_op, theta, [qArr[row][col], qArr[row2][col2]]);
133 }
134 }
135 }
136
137 /// # Summary
138 /// The main function that takes in various parameters and calls the operations needed to simulate fourth order Trotterizatiuon of the Ising Hamiltonian for a given time-step
139 /// # Input
140 /// ## N1, N2 (Int, Int) : Lattice sizes for an N1 x N2 lattice
141 ///
142 /// ## J (Double) : coefficient for 2-qubit interactions
143 ///
144 /// ## g (Double) : coefficient for transverse field
145 ///
146 /// ## totTime (Double) : The total time-step for which unitary simulation is done.
147 ///
148 /// ## dt (Double) : The time the simulation is done for each timestep
149 ///
150 operation IsingModel2DSim(N1 : Int, N2 : Int, J : Double, g : Double, totTime : Double, dt : Double) : Unit {
151
152 use qs = Qubit[N1 * N2];
153 let qubitArray = Chunks(N2, qs); // qubits are re-arranged to be in an N1 x N2 array
154
155 let p = 1.0 / (4.0 - 4.0^(1.0 / 3.0));
156 let t = Ceiling(totTime / dt);
157
158 let seqLen = 10 * t + 1;
159
160 let angSeq = SetAngleSequence(p, dt, J, g);
161
162 for i in 0..seqLen - 1 {
163 let theta = (i == 0 or i == seqLen - 1) ? J * p * dt / 2.0 | angSeq[i % 10];
164
165 // for even indexes
166 if i % 2 == 0 {
167 ApplyAllX(N1, qubitArray, theta);
168 } else {
169 // iterate through all possible combinations for `dir` and `grp`.
170 for (dir, grp) in [(true, true), (true, false), (false, true), (false, false)] {
171 ApplyDoubleZ(N1, N2, qubitArray, theta, dir, grp);
172 }
173 }
174 }
175 }
176
177}
178