forked from microsoft/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsingGenerators.qs
319 lines (279 loc) · 13 KB
/
IsingGenerators.qs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Quantum.Samples.Ising {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Simulation;
//////////////////////////////////////////////////////////////////////////
// Introduction //////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// In this sample, we demonstrate use of the generator representation
// functionality offered by the Q# canon to represent Ising model
// Hamiltonians.
// Later, we will extend these techniques to represent the
// 1D Heisenberg XXZ model.
// We will begin by constructing a representation of the 1D transverse
// Ising model Hamiltonian,
// H = - ( J₀ Z₀ Z₁ + J₁ Z₁ Z₂ + … ) - (h₀ X₀ + h₁ X₁ + …),
// where {Jᵢ} are nearest-neighbor couplings, and where hₓ is a
// transverse field.
// Since this Hamiltonian is naturally expressed in the Pauli basis,
// we will use the PauliEvolutionSet() function to obtain a simulatable
// basis to use in representing H. Thus, we begin by defining our
// indices with respect to the Pauli basis. In doing so, we will define
// helper functions to return single-site and two-site generator indices.
//////////////////////////////////////////////////////////////////////////
// 1D Ising model ////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/// # Summary
/// Returns a generator index that is supported on a single site.
///
/// # Input
/// ## idxPauli
/// Index of the Pauli operator to be represented, where `1` denotes
/// `PauliY` and `3` denotes `PauliZ`.
/// ## idxQubit
/// Index `k` of the qubit that the represented term will act upon.
/// ## hCoupling
/// Function returning coefficients `hₖ` for each site. E.g.:
/// should return the coefficient for the index at `idxQubit = 3`.
///
/// # Output
/// A `GeneratorIndex` representing the term - hₖ {Xₖ, Yₖ, Zₖ}, where hₖ is the
/// function `hCoupling` evaluated at the site index `k`, and where
/// {Xₖ, Yₖ, Zₖ}, selected by idxPauli, is the Pauli operator acting at the
/// site index `k`.
function OneSiteGeneratorIndex (idxPauli : Int, idxQubit : Int, hCoupling : (Int -> Double)) : GeneratorIndex {
let coeff = -1.0 * hCoupling(idxQubit);
let idxPauliString = [idxPauli];
let idxQubits = [idxQubit];
return GeneratorIndex((idxPauliString, [coeff]), idxQubits);
}
/// # Summary
/// Returns a generator system for a sum of generator indices each
/// supported on a single site.
///
/// # Input
/// ## idxPauli
/// Index of the Pauli operator to be represented.
/// ## nSites
/// Number of qubits that the represented system will act upon.
/// ## hCoupling
/// Function returning coefficients `hₖ` for each site.
///
/// # Output
/// A `GeneratorSystem` representing the sum - Σₖ hₖ {Xₖ, Yₖ, Zₖ}.
function OneSiteGeneratorSystem (idxPauli : Int, nSites : Int, hCoupling : (Int -> Double)) : GeneratorSystem {
return GeneratorSystem(nSites, OneSiteGeneratorIndex(idxPauli, _, hCoupling));
}
/// # Summary
/// Returns a generator index that is supported on two sites.
///
/// # Input
/// ## idxPauli
/// Index of the Pauli operator to be represented, where `1` denotes
/// `PauliY` and `3` denotes `PauliZ`.
/// ## nSites
/// Number of qubits that the represented system will act upon.
/// ## idxQubit
/// Index `k` of the qubit that one of the represented term will act upon.
/// ## jCoupling
/// Function returning coefficients `Jₖ` for each two-site interaction.
/// E.g.: `jCoupling(3)` should return the coefficient for the index at
/// `idxQubit = 3`.
///
/// # Output
/// A `GeneratorIndex` representing the term - Jₖ {XₖXₖ₊₁, YₖYₖ₊₁, ZₖZₖ₊₁},
/// where Jₖ is the function `jCoupling` evaluated at the site index `k`,
/// and where {XₖXₖ₊₁, YₖYₖ₊₁, ZₖZₖ₊₁}, selected by idxPauli, is the Pauli
/// operator acting at the site index `k` and `k+1` with closed boundary
/// conditions.
function TwoSiteGeneratorIndex (idxPauli : Int, nSites : Int, idxQubit : Int, jCoupling : (Int -> Double)) : GeneratorIndex {
let idx = idxQubit;
let coeff = -1.0 * jCoupling(idx);
let idxPauliString = [idxPauli, idxPauli];
let idxQubits = [idx, (idx + 1) % nSites];
let generatorIndex = GeneratorIndex((idxPauliString, [coeff]), idxQubits);
if (idx >= nSites) {
fail "Qubit index must be smaller than number of sites.";
}
return generatorIndex;
}
/// # Summary
/// Returns a generator system for a sum of generator indices each
/// supported on two neighboring sites.
///
/// # Input
/// ## idxPauli
/// Index of the Pauli operator to be represented.
/// ## nSites
/// Number of qubits that the represented system will act upon.
/// ## idxQubit
/// Index `k` of the qubit that the represented term will act upon.
/// ## jCoupling
/// Function returning coefficients `Jₖ` for each two-site interaction.
///
/// # Output
/// A `GeneratorSystem` representing the sum - Σₖ Jₖ{XₖXₖ₊₁, YₖYₖ₊₁, ZₖZₖ₊₁}.
function TwoSiteGeneratorSystem(idxPauli : Int, nSites : Int, jCoupling : (Int -> Double)) : GeneratorSystem {
return GeneratorSystem(nSites, TwoSiteGeneratorIndex(idxPauli, nSites, _, jCoupling));
}
// We now add the transverse and coupling Hamiltonians
/// # Summary
/// Returns a generator system for the Ising model.
///
/// # Input
/// ## nSites
/// Number of qubits that the represented system will act upon.
/// ## hXCoupling
/// Function returning coefficients `hₖ` for each site.
/// ## jCoupling
/// Function returning coefficients `Jₖ` for each two-site interaction.
///
/// # Output
/// A `GeneratorSystem` representing the sum - Σₖ Jₖ ZₖZₖ₊₁ - Σₖ hₖ Xₖ
function IsingGeneratorSystem(nSites : Int, hXCoupling : (Int -> Double), jCoupling : (Int -> Double)) : GeneratorSystem {
let XGenSys = OneSiteGeneratorSystem(1, nSites, hXCoupling);
let ZZGenSys = TwoSiteGeneratorSystem(3, nSites, jCoupling);
return AddGeneratorSystems(XGenSys, ZZGenSys);
}
// The generator system alone does not describe how its component terms
// may be implemented on a quantum computer. In an EvolutionGenerator,
// a GeneratorSystem is described together with an EvolutionSet that maps
// each GeneratorIndex to unitary time-evolution by the term described.
/// # Summary
/// Returns an EvolutionGenerator for the Ising model.
///
/// # Input
/// ## nSites
/// Number of qubits that the represented system will act upon.
/// ## hXCoupling
/// Function returning coefficients `hₖ` for each site.
/// ## jCoupling
/// Function returning coefficients `Jₖ` for each two-site interaction.
///
/// # Output
/// A `EvolutionGenerator` representing the sum - Σₖ Jₖ ZₖZₖ₊₁ - Σₖ hₖ Xₖ
/// and a PauliEvolutionSet() that describes how unitary time-evolution by
/// each term may be implemented.
function Ising1DEvolutionGenerator(nSites : Int, hXCoupling : (Int -> Double), jCoupling : (Int -> Double)) : EvolutionGenerator {
let generatorSystem = IsingGeneratorSystem(nSites, hXCoupling, jCoupling);
let evolutionSet = PauliEvolutionSet();
return EvolutionGenerator(evolutionSet, generatorSystem);
}
// We now define functions for the coefficients
/// # Summary
/// A function that outputs uniform single-site coupling coefficients
/// `hₖ`.
///
/// # Input
/// ## amplitude
/// Value of coefficient.
/// ## idxQubit
/// Index `k` of the qubit that the represented term will act upon.
///
/// # Output
/// A function returning coefficients `hₖ` for each site.
function UniformHCoupling(amplitude : Double, idxQubit : Int) : Double {
return 1.0 * amplitude;
}
/// # Summary
/// A function that outputs uniform two-site coupling coefficients
/// `Jₖ` with open boundary conditions.
///
/// # Input
/// ## nSites
/// Number of qubits that the represented system will act upon.
/// ## amplitude
/// Value of coefficient
/// ## idxQubit
/// Index `k` of the qubit that the represented term will act upon.
///
/// # Output
/// A function returning coefficients `Jₖ` for each site.
function Uniform1DJCoupling(nSites : Int, amplitude : Double, idxQubit : Int) : Double {
return idxQubit == nSites - 1
? 0.0
| amplitude;
}
// Let us construct a function to be called from C# that returns terms
// of the Ising Hamiltonian. This unpacks the `EvolutionGenerator` created
// by `Ising1DEvolutionGenerator`.
/// # Summary
/// Returns a generator index for a term in the Ising model with uniform
/// couplings.
///
/// # Input
/// ## nSites
/// Number of qubits that the represented system will act upon.
/// ## hXAmplitude
/// Value of all `hₖ` coefficients.
/// ## jAmplitude
/// Value of all `jₖ` coefficients.
/// ## idxHamiltonian
/// Index to term in the Hamiltonian.
///
/// # Output
/// A `GeneratorIndex` representing a term in the Ising model.
function Uniform1DIsingGeneratorIndex(nSites : Int, hXAmplitude : Double, jAmplitude : Double, idxHamiltonian : Int) : GeneratorIndex {
let hXCoupling = UniformHCoupling(hXAmplitude, _);
let jCoupling = Uniform1DJCoupling(nSites, jAmplitude, _);
let (evolutionSet, generatorSystem) = (Ising1DEvolutionGenerator(nSites, hXCoupling, jCoupling))!;
let (nTerms, generatorIndexFunction) = generatorSystem!;
return generatorIndexFunction(idxHamiltonian);
}
//////////////////////////////////////////////////////////////////////////
// 1D Heisenberg XXZ model ///////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// It is straightforward to generalize this to the Heisenberg XXZ model.
/// # Summary
/// Returns a generator system for the Heisenberg XXZ model.
///
/// # Input
/// ## nSites
/// Number of qubits that the represented system will act upon.
/// ## hZCoupling
/// Function returning coefficients `hₖ` for each site.
/// ## jCoupling
/// Function returning coefficients `Jₖ` for each two-site interaction.
///
/// # Output
/// A `GeneratorSystem` representing the sum
/// - Σₖ Jₖ ( XₖXₖ₊₁ + YₖYₖ₊₁ + ½ ZₖZₖ₊₁) - Σₖ hₖ Zₖ
function HeisenbergXXZGeneratorSystem (nSites : Int, hZCoupling : (Int -> Double), jCoupling : (Int -> Double)) : GeneratorSystem {
let ZGenSys = OneSiteGeneratorSystem(3, nSites, hZCoupling);
let XXGenSys = TwoSiteGeneratorSystem(1, nSites, jCoupling);
let YYGenSys = TwoSiteGeneratorSystem(2, nSites, jCoupling);
// This multiplies all coefficients in a generator system
let jZZmultiplier = 0.5;
let ZZGenSys = MultiplyGeneratorSystem(jZZmultiplier, TwoSiteGeneratorSystem(3, nSites, jCoupling));
// We now add the transverse and coupling Hamiltonians
return AddGeneratorSystems(AddGeneratorSystems(ZGenSys, ZZGenSys), AddGeneratorSystems(YYGenSys, XXGenSys));
}
// Let us construct a function to be called from C# that returns terms
// of the Heisenberg Hamiltonian. This unpacks the `GeneratorSystem` created
// by `HeisenbergXXZGeneratorSystem`.
/// # Summary
/// Returns a generator index for a term in the Heisenberg model with uniform
/// couplings.
///
/// # Input
/// ## nSites
/// Number of qubits that the represented system will act upon.
/// ## hZAmplitude
/// Value of all `hₖ` coefficients.
/// ## jAmplitude
/// Value of all `jₖ` coefficients.
/// ## idxHamiltonian
/// Index to term in the Hamiltonian.
///
/// # Output
/// A `GeneratorIndex` representing a term in the Heisenberg Model.
function HeisenbergXXZGeneratorIndex(nSites : Int, hZAmplitude : Double, jAmplitude : Double, idxHamiltonian : Int) : GeneratorIndex {
let hZCoupling = UniformHCoupling(hZAmplitude, _);
let jCoupling = Uniform1DJCoupling(nSites, jAmplitude, _);
let (nTerms, generatorIndexFunction) = (HeisenbergXXZGeneratorSystem(nSites, hZCoupling, jCoupling))!;
return generatorIndexFunction(idxHamiltonian);
}
}