-
Notifications
You must be signed in to change notification settings - Fork 1
/
GateLearning.m
executable file
·386 lines (310 loc) · 12.1 KB
/
GateLearning.m
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
(* ::Package:: *)
BeginPackage["GateLearning`", {"QM`", "QM`QGates`", "QPauliAlgebra`"}];
Unprotect @@ Names["GateLearning`*"];
ClearAll @@ Names["GateLearning`*"];
ClearAll @@ Names["GateLearning`Private`*"];
All2Body;
AllDiagonalXYZ;
AllDiagonalXZ;
AuxiliaryHam;
GateHam;
nBodyInteractionsList;
Commutator::usage = "Commutator[a, b] computes the commutator between a and b."
ParametrizedHamiltonian::usage = "\
ParametrizedHamiltonian[nQubits, factorsGenerator, symbol] returns, as a QPauliExpr, a general Hamiltonian with the class of interactions specified by factorsGenerator, and a symbolic parameter attached to each Pauli component. The input `symbol` determines the symbol to use for the parameters.
ParametrizedHamiltonian[nQubits, symbol] returns a general Hamiltonian with single- and two-qubit interactions over the given number of qubits.
";
HamPhysical;
ImposeVanishingComponents::usage = "\
ImposeVanishingComponents[expr] extracts all the Pauli components from expr and solves for all of them to be vanishing.";
ImposeVanishingCommutator::usage = "\
ImposeVanishingCommutator[expr1, expr2] finds the parameters that make the two expressions commute with each other.";
ImposeVanishingComponent::usage = "\
ImposeVanishingComponent[expr, component] finds the values of the parameters for which the given Pauli component vanishes, and replaces it back in the original expression.";
selectPhysical;
(*
gateLog;
gateCondition;
*)
Begin["`Private`"];
flatten1[x_] := Flatten[x, 1];
iQPauliExpr = QPauliAlgebra`Private`iQPauliExpr;
Commutator[x_iQPauliExpr, y_iQPauliExpr] := Plus[
QPauliTimes[x, y], -QPauliTimes[y, x]
];
Commutator[x_? MatrixQ, y_? MatrixQ] := x.y - y.x;
(* indicesDiagonalOps
Return list of indices denoting diagonal interactions.
Parameters
----------
numQubits: length of each output list.
numOps: number of nonzero elements in each list.
directions: for which diretion compute the outputs (if directions == {1}
then only lists correponsing to XX interactions will be returned).
Examples
--------
In[] := indicesDiagonalOps[3, 2, {1, 2}]
Out[] = {{1, 1, 0}, {1, 0, 1}, {0, 1, 1}, {2, 2, 0}, {2, 0, 2}, {0, 2, 2}}
*)
indicesDiagonalOps[numQubits_Integer, numOps_Integer, directions_List] := Map[
Permutations,
Normal @ SparseArray[
Thread[Range @ numOps -> #],
numQubits
] & /@ directions
] // flatten1;
(* Returns list of n-body interaction directions. For example for pairwise
interactions, `nBody = 2` and the output is
{{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2},
{2, 3}, {3, 1}, {3, 2}, {3, 3}}
*)
nBodyInteractionsDirections[nBody_Integer] := Tuples @ (
ConstantArray[#, nBody] & @ Range @ 3
);
qubitTuples[nQubits_Integer, nQubitsInInteractions_Integer] := Subsets[
Range @ nQubits, {nQubitsInInteractions}
];
(* Return the list of all possible interaction between n bodies, in a qubit
network with `nQubits` nodes. Each element of the output list is a list of
`nQubits` integers, describing a specific product of Pauli matrices.
For example the output {1, 1, 0} corresponds to X[1] X[2]. *)
nBodyInteractionsList[nQubits_Integer, nBodiesPerInteraction_Integer] := With[
{
interactionTypes = nBodyInteractionsDirections @ nBodiesPerInteraction,
couplings = qubitTuples[nQubits, nBodiesPerInteraction]
},
Map[
ReplacePart[
ConstantArray[0, nQubits],
Thread[Rule @@ #]
] &,
Tuples @ {couplings, interactionTypes}
]
];
(* Returns the list of self- and pair-wise interactions in a qubit network With
`numQubits` nodes. *)
pairwiseInteractionsList[numQubits_Integer] := Join[
nBodyInteractionsList[numQubits, 1],
nBodyInteractionsList[numQubits, 2]
];
All2Body = pairwiseInteractionsList;
(* Return list of all self-interactions and diagonal pairwise interactions.
Options
-------
whichInteractions : List
List of integers, specifying which diagonal interactions to keep (X, Y, or Z)
*)
diagonalPairwiseInteractions[
numQubits_Integer, whichInteractions_List : {1, 2, 3}
] := Join[
indicesDiagonalOps[numQubits, 1, Range @ 3],
indicesDiagonalOps[numQubits, 2, whichInteractions]
];
AllDiagonalXYZ[numQubits_Integer] := diagonalPairwiseInteractions[numQubits, {1, 2, 3}];
AllDiagonalXZ[numQubits_Integer] := diagonalPairwiseInteractions[numQubits, {1, 3}];
(* Return parametrized product of the specified list of pauli products.
This can be used for example to generate the generic 2-qubit interactions
Hamiltonian.
*)
parametrizedPauliProductsSum[
factors_List, parameter_Symbol : Global`\[CapitalXi]
] := Total @ Apply[parameter[##] PauliProduct[##] &, factors, {1}];
(* Return a general Hamiltonian over `numQubits` qubits, with the class of
interactions specified by `factorsGenerator`, and a symbolic parameter
attached to each Pauli component.
Parameters
----------
numQubits : Integer,
Number of qubits in the generated Hamiltonian.
factorsGenerator : function, optional
If given, it must be a function taking a single integer argument, and
returning a corresponding list of Pauli indices.
If omitted, the returned generator contains all possible single- and
two-qubit interactions.
parameter : Symbol, optional
The symbol used for the parameters in the output Hamiltonian. If omitted
it defaults to Global`\[CapitalXi]`.
Examples
--------
In[] := ParametrizedHamiltonian[3, AllDiagonalXYZ]
Out[] = (... Hamiltonian with all possible diagonal single- and two- qubit
interactions ...)
*)
Options[ParametrizedHamiltonian] = {"ParameterSymbol" -> Global`\[CapitalXi]};
ParametrizedHamiltonian[
numQubits_Integer, factorsGenerator_, OptionsPattern[]
] := PauliBasis @ parametrizedPauliProductsSum[
Join[factorsGenerator @ numQubits, {ConstantArray[0, numQubits]}],
OptionValue @ "ParameterSymbol"
];
(* Old version, now using ParametrizedHamiltonian *)
HamPhysical[numQubits_Integer, parameter_Symbol, opsToKeep_] := Total[
parameter[##] PauliProduct[##] & @@@ Join[
opsToKeep @ numQubits,
{ConstantArray[0, numQubits]}
]
] // PauliBasis;
pauliIndexPatt = {__Integer};
pauliComponent[matrix_ ? MatrixQ, component : pauliIndexPatt] := Tr @ Dot[
matrix, PauliProduct @@ component
] / 2 ^ NumberOfQubits @ matrix;
pauliComponent[expr_iQPauliExpr, component : pauliIndexPatt] := pauliComponent[
Normal @ expr, component
];
pauliComponents[expr_, components : {pauliIndexPatt..}] := Table[
pauliComponent[expr, component],
{component, components}
];
pauliComponents[expr_] := With[{
numQubits = NumberOfQubits @ expr
},
With[{
allInteractions = Tuples @ ConstantArray[#, numQubits] & @ Range[0, 3]
},
pauliComponents[expr, allInteractions]
]
];
pauliComponentProjection[matrix_ ? MatrixQ, component : pauliIndexPatt] := Times[
pauliComponent[matrix, component],
PauliProduct @@ component
];
pauliComponentProjection[matrix_ ? MatrixQ, components : {pauliIndexPatt..}] := Sum[
pauliComponentProjection[matrix, component],
{component, components}
];
pauliComponentProjection[expr_iQPauliExpr, components_] := pauliComponentProjection[
Normal @ expr, components
] // PauliBasis;
(*
Select a subset of the interactions in the input expression.
Inputs
------
`expr`: Expression containing parametrised interactions.
Can be in the form of a matrix, or an `iQPauliExpr`.
`factorsGenerator`: A function returning a list of interaction indices.
A few examples of such function are `All2Body` or `AllDiagonalXZ`.
*)
selectPhysical[expr_, factorsGenerator_] := pauliComponentProjection[
expr, factorsGenerator @ NumberOfQubits @ expr
];
(* Assuming that expr is a parametrized expression, compute the parameters
making the expression vanish. More specifically, impose that every Pauli
component is zero and solve for the parameters.
*)
ImposeVanishingComponents[
expr : (_iQPauliExpr | _?MatrixQ), parameters_List
] := With[{
components = DeleteCases[0] @ Simplify @ pauliComponents @ expr
},
NSolve[Thread[components == 0], parameters]
];
ImposeVanishingComponents[expr_iQPauliExpr] := ImposeVanishingComponents[
expr, Variables @ Normal @ expr
];
ImposeVanishingComponents[expr_? MatrixQ] := ImposeVanishingComponents[
expr, Variables @ expr
];
ImposeVanishingCommutator[expr_iQPauliExpr, otherExpr_iQPauliExpr] := (
ImposeVanishingComponents @ Commutator[expr, otherExpr]
);
ImposeVanishingComponent[expr_iQPauliExpr, component : pauliIndexPatt] := Module[
{componentCoeff, solution},
componentCoeff = pauliComponent[expr, component];
If[Not @ TrueQ[Chop @ componentCoeff == 0],
solution = First @ NSolve[componentCoeff, Variables @ componentCoeff];
expr /. solution,
(* otherwise just return the expression unchanged *)
expr
]
];
ImposeVanishingComponent[expr_iQPauliExpr, components : {pauliIndexPatt..}] := Fold[
ImposeVanishingComponent[#1, #2] &,
expr, components
];
(* indexToLetter
Small utility function to convert from integer to letter notation for the
different interaction types (converts 1 into "X" and so on).
*)
indexToLetter[int_Integer] := int /. {1 -> "X", 2 -> "Y", 3 -> "Z"};
indexToLetter[{ints__Integer}] := indexToLetter /@ {ints};
(* toHJNotation
Converts from Xi[0, 1, 2] notation to J_{23}^{XY}.
*)
toHJNotation[
_[args__],
twoQubitSymbol_ : Global`\[ScriptCapitalJ],
oneQubitSymbol_ : Global`\[ScriptH]
] := Which[
(* If a constant term.. *)
Length @ Flatten @ DeleteCases[{args}, 0] == 0,
Subscript[oneQubitSymbol, 0],
(* If a one-qubit interaction.. *)
Length @ Flatten @ DeleteCases[{args}, 0] == 1,
Subsuperscript[
oneQubitSymbol,
Position[{args}, _?(# != 0 &)][[1, 1]],
DeleteCases[{args}, 0][[1]] // indexToLetter
],
(* If two-qubit interaction.. *)
Length @ Flatten @ DeleteCases[{args}, 0] == 2,
Subsuperscript[
twoQubitSymbol,
Row @ Flatten @ Position[{args}, _?(# != 0 &)],
DeleteCases[{args}, 0] // indexToLetter // Row
]
];
toHJNotation[args__Integer, otherargs___] := toHJNotation[{args}, otherargs];
(* gateLog
Mostly here for backward compatibility reasons. It expects an `iQPauliExpr`
as input. It's probably better to just use `PauliBasisLog` instead of this.
*)
gateLog[gateMatrix_] := PauliBasisLog @ Normal @ gateMatrix;
gateLog[G_, Null]:= PauliBasisLog @ Normal @ G;
gateLog[G_, P_] := PauliBasis @ KroneckerProduct[
-I MatrixLog @ Normal @ G // Chop,
Normal @ P
];
gateCondition[gate_, H2p_, factorsGenerator_, P_] := Module[
{H, Hphys, Hunphys, HH, Hg, HG, n},
(* Compute the principal logarithm *)
H = gateLog[gate, P];
(* Extract from that only the physical components and store them in `Hphys` *)
Hphys = selectPhysical[H, factorsGenerator] // Chop;
(* Extract all the other interactions and store them in `Hunphys` *)
n = Length @ Normal @ H;
Hunphys = H - Hphys - Tr[Normal @ H] / n;
Hg = H2p - Hunphys;
HG = Normal[Hg];
HH = Normal[H + Hg] - HG // Simplify;
{
Variables @ Normal @ H2p,
Hg,
Select[Flatten[HH.HG - HG.HH] // Chop, # =!= 0&]
}
];
simplifyNames[s_, op_] := MapIndexed[
#1 -> ToExpression[ToString @ s <> ToString @ First[#2 - 1]]&,
op
];
AuxiliaryHam[
gate_, numQubits_Integer, symb_Symbol,
idx_ : All2Body, P_ : Null
] := Module[{Ham, cond, Hsol, var},
{var, Ham, cond} = gateCondition[
gate,
HamPhysical[numQubits, symb, idx],
idx,
P
];
Hsol = Ham /. First @ Solve[Thread[cond == 0], var] // Quiet // Chop;
Hsol /. simplifyNames[symb, var]
];
GateHam[G_, n_, s_, idx_ : All2Body, P_ : Null] := QPauliAlgebra`QPauliExprCollect[
AuxiliaryHam[G, n, s, idx, P] + gateLog[G, P] // Chop
];
(* Protect all package symbols *)
With[{syms = Names["GateLearning`*"]},
SetAttributes[syms, {Protected, ReadProtected}]
];
End[];
EndPackage[];