-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQINisq.m
329 lines (188 loc) · 9.9 KB
/
QINisq.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
(* ::Package:: *)
(* ::Section:: *)
(*Package header*)
(* File: QINisq.m *)
(* Description: Mathematica package for developing code for NISQ computers. *)
(* Authors: Jaroslaw Miszczak <[email protected]> *)
(* License: GPLv3 *)
(* Mathematica Package *)
BeginPackage["QINisq`", { "QI`", "QIExtras`"}]
(* Exported symbols added here with SymbolName::usage *)
Unprotect@@Names["QINisq`*"]
Clear@@Names["QINisq`*" ]
(* ::Section:: *)
(*Public definitions*)
(* ::Subsection:: *)
(*Quantum gates*)
RX::usage = "RX[theta] one-qubit rotation wrt X axis.";
RY::usage = "RY[theta] one-qubit rotation wrt Y axis.";
RZ::usage = "RZ[theta] one-qubit rotation wrt Z axis.";
RXP::usage = "PXR[phi, theta] one-qubit rotation of the Bloch vector by an angle theta, where phi is the angle between the rotation axis and the X axis.";
S::usage = "S[] = RZ[pi/2]";
T::usage = "T[] = RZ[pi/4]";
H::usage = "H[] = Hadamard gate. Defined using wh.";
Toff::usage = "Toff[] returns Toffoli, or controlled-controlled-not, gate.";
XX::usage = "XX[\[Theta]] Ising gate with parameter \[Theta].";
Gate::usage = "Gate[g,t,q] returns quantum gate applied on systems specified in list t, on the computer with q qubits.";
CGate::usage = "CGate[g,c,t,q] returns controlled version of one-qubit gate g with control qubits c and target qubits q, acting on q qubits. Please note that currently this function does not check if the dimensions are appropriate and qubits specification do not overlap.";
CX::usage = "CX[c,t,qdim] generalized controlled not operation with control qubits c and target qubits t, acting on qdim qubits. Please note that this function does not check of the dimensions are appropriate and qubits specification do not overlap.";
CY::usage = "CY[c,t,qdim] generalized controlled sy. See CX usage for more details.";
CZ::usage = "CZ[c,t,qdim] generalized controlled sz. See CX usage for more details.";
(* ::Subsection:: *)
(*Quantum computer*)
QC::usage = "QC is the default list representing the quantum computer used during the calculations. Its first element is the number of qubits. Next elements are quantum gates, added using Q function. QC is initialized and reset using InitQC. See also: Q, InitQC.";
InitQC::usage = "InitQC[qdim,name] initializes a (virtual) quantum computer with qdim qubits of quantum memory. The name can be specified to create a quantum computer with a nono-default name. When necessary, this function will resete the initilized quantum computer.";
Q::usage = "Q[g, ops, qc] is used to change the instructions for the quantum computer. It appends gate g to the computer qc using options in the ops list. It is expected that the first element of qc list is an integer representing the number of available qubits.";
QRunGate::usage = "QRunGate[g,t,q] executes one-qubit operation g on each qubit listed in list t, using the register consisting of q qubits. This function is used internally for running the curcuits.";
QRunCGate::usage = "QRunCGate[g,ops,q] runs controlled gate g with control and target qubits specified in ops on a system with q qubits. This function is used internally for running the curcuits.";
QRun::usage = "QRun[qc] runs a simulation of the intructions in the list of gates, using the information about the paramters and the size of the computer qc. This function is used internally for running the curcuits.";
QTransGate::usage = "QRunCGate[g,ops,q] transpiles gate g on q qubits with options in ops into an alternative representation tgr.";
QTransCGate::usage = "QRunCGate[g,ops,q,tgr] transpiles controlled gate g on q qubits with options in ops into an alternative representation tgr.";
QTrans::usage = "QTrans[qc,tgr] displays the list of opertions transpiled into the tgr target.";
(* ::Subsection:: *)
(* Fidelity and friends *)
TruncatedFidelity::usage = "<f>TruncatedFidelity</f>[<v>r, s ,m, d</v>] returns quantum fidelity between <v>r</v> and <v>s</v> calculated by projecting <v>s</s> onto the <v>m</v> largest eigenvalues of <v>r</v>. Parameter <v>d</v> controls the accuarys of calculations of the eigenvalues and has default value set to 10E-6.";
(* ::Section:: *)
(*Private definitions*)
Begin["`Private`"] (* Begin Private Context *)
QIDocRep = {"<v>" -> "\!\(\*StyleBox[\"" , "</v>" -> "\", \"TI\"]\)", "<f>"->"\!\(\*StyleBox[\"", "</f>" -> "\", \"Input\"]\)", "<s>" -> "", "</s>" -> ""} ;
(MessageName[Evaluate[ToExpression[#]], "usage"] = StringReplace[MessageName[Evaluate[ToExpression[#]], "usage"],QIDocRep])& /@ Names["QINisq`*"];
(* ::Subsection:: *)
(*Internal functions (without usage strings)*)
qiNisqAuthors = "Jaroslaw Miszczak <jarek[at]miszczak[dot]eu>";
qiNisqLicense = "GPLv3 <http://www.gnu.org/licenses/gpl.html>";
qiNisqHistory = {
{"0.0.1", "15/03/2021", "Jarek", "Basic rotation gates."},
{"0.0.2", "12/04/2021", "Jarek", "TruncatedFidelity moved from QIExtras."},
{"0.0.2", "07/05/2021", "Jarek", "Added XX gate."},
{"0.0.3", "17/05/2021", "Jarek", "Added Bloch verctor rotation wrt rotatet X axis."},
{"0.0.4", "21/05/2021", "Jarek", "Added toffoli, S and T gates"},
{"0.0.5", "22/05/2021", "Jarek", "Added general controlled one qubit gate and templates for CX, CY and CZ gates."},
{"0.0.6", "25/05/2021", "Jarek", "Added template for function managing virtual quantum devices."},
{"0.0.7", "28/05/2021", "Jarek", "Added function for extending one-qubit-gates and functions for executing non-parametric one-qubit gates and controlled gates."},
{"0.0.8", "22/01/2023", "Jarek", "Updated description. Minor in usage messages."},
{"0.0.9", "12/02/2023", "Jarek", "Minor update: description, usage messages."},
{"0.0.10", "12/08/2023", "Jarek", "Better quantum computer initilization and management, fixed H gate, minor in usage messages."},
{"0.0.11", "16/08/2023", "Jarek", "Fixed some usage messages."},
{"0.0.12", "21/09/2023", "Jarek", "Improved demo file. Improved usage messages."},
{"0.0.13", "18/10/2023", "Jarek", "Started transpilsation, some refactoring, extended usage messages."}
};
qiNisqVersion = Last[qiNisqHistory][[1]];
qiNisqLastModification = Last[qiNisqHistory][[2]];
qiNisqAbout = "QINisq is a Mathematica package encapsulating functions commonly used to developing quantum algorithms for noisy intermediate-scale quantum (NISQ) computers. The packages is based QI and QIExtras packages. Its goal is to providee functionality enabling the programming of quantum computers on a level similar to the one offered by Qiskit.";
(* ::Subsection:: *)
(*Quantum gates*)
RX[theta_]:=MatrixExp[- I theta/2 sx];
RY[theta_]:=MatrixExp[- I theta/2 sy];
RZ[theta_]:=MatrixExp[- I theta/2 sz];
S[] = MatrixPower[sz,1/2];
T[] = MatrixPower[sz,1/4];
H[] = 1/2 wh;
RXP[phi_,theta_]:={ {Cos[theta/2], -I Sin[theta/2] Exp[-I phi]}, {-I Sin[theta/2] Exp[I phi], Cos[theta/2]} };
Toff = Proj[Ket[1,2]]\[CircleTimes]Proj[Ket[1,2]]\[CircleTimes]sx + (Id[4]-Proj[Ket[1,2]]\[CircleTimes]Proj[Ket[1,2]])\[CircleTimes]id;
XX[theta_]:=Cos[theta](Id[2]\[CircleTimes]Id[2])-I Sin[theta](sx\[CircleTimes]sx);
Gate[op_,targ_,qdim_]:=Block[{res, id},
res =Table[id,{qdim}];
res[[Flatten[{targ}]]] = Table[op,{Flatten[{targ}]}];
KroneckerProduct@@res
];
CGate[gate_,ctrl_,targ_,qdim_]:=Block[{idx,currIdx,idle},
idle = Complement[Range[1,qdim],ctrl~Join~targ];
idx = DeleteDuplicates[
Table[
currIdx=Reverse@IntegerDigits[i,2,qdim];
currIdx[[idle]]=Table[id,{idle}];
If[DeleteDuplicates[currIdx[[ctrl]]]=={1},
currIdx[[targ]]=Table[gate,{targ}],
currIdx[[targ]]=Table[id,{targ}]
];
currIdx[[ctrl]]=Map[Proj[Ket[#,2]]&,currIdx[[ctrl]]];
currIdx,
{i,0,2^qdim-1}
]
];
Plus@@Table[KroneckerProduct@@currIdx,{currIdx,idx}]
];
CX[c_,t_,qdim_]:=CGate[sx, Flatten[{c}], Flatten[{t}],qdim];
CY[c_,t_,qdim_]:=CGate[sy, Flatten[{c}], Flatten[{t}],qdim];
CZ[c_,t_,qdim_]:=CGate[sz, Flatten[{c}], Flatten[{t}],qdim];
(* ::Subsection:: *)
(*Quantum computer*)
InitQC[dim_,name_:QC]:=Module[{},
Print["[Info] Initializing quantum computer with " <> ToString[dim] <> " qubits."];
Unprotect[QC];
QC={dim};
Protect[QC];
];
Q[gate_,ops_,qc_:QC]:=
Module[{},
Unprotect[QC];
AppendTo[qc,{gate,ops}];
Protect[QC];
];
SetAttributes[Q,HoldAll];
QRunGate[g_,ops_,qdim_]:=ToExpression[
StringTemplate["Gate[`OP`[],`targ`,`qdim`]"][
<|"OP"->g,
"targ"->ops ,
"qdim"->qdim|>]
];
QRunCGate[g_,ops_,qdim_]:=ToExpression[
StringTemplate["`OP`[`ctrl`,`targ`,`qdim`]"][
<|"OP"->g,
"ctrl"->ops[[1]] ,
"targ"->ops[[2]],
"qdim"->qdim|>]
];
QRun[qc_]:=Block[{dim=qc[[1]]},
(
Dot@@Table[
Switch[
Head[qc[[i]][[2]][[1]]],
(* simple gates have an integer as the (first) target *)
Integer, QRunGate[qc[[i]][[1]],qc[[i]][[2]],qc[[1]]],
(* controlled gates have a list of targets as the argument *)
List, QRunCGate[qc[[i]][[1]],{qc[[i]][[2,1]],qc[[i]][[2,2]]},qc[[1]]]
],
{i,2,Length[qc]}
])
];
QTransGate[g_,ops_,qdim_,tgr_]:=ToString[
StringTemplate["Gate[`OP`[],`targ`,`qdim`]"][
<|"OP"->g,
"targ"->ops ,
"qdim"->qdim|>]
];
QTransCGate[g_,ops_,qdim_,tgr_]:=ToString[
StringTemplate["`OP`[`ctrl`,`targ`,`qdim`]"][
<|"OP"->g,
"ctrl"->ops[[1]] ,
"targ"->ops[[2]],
"qdim"->qdim|>]
];
QTrans[qc_,tgr_:"Qiskit"]:=Block[{dim=qc[[1]]},
(
Dot@@Table[
Switch[
Head[qc[[i]][[2]][[1]]],
(* simple gates have an integer as the (first) target *)
Integer, QTransGate[qc[[i]][[1]],qc[[i]][[2]],qc[[1]],tgr],
(* controlled gates have a list of targets as the argument *)
List, QTransCGate[qc[[i]][[1]],{qc[[i]][[2,1]],qc[[i]][[2,2]]},qc[[1]],tgr]
],
{i,2,Length[qc]}
])
];
(* ::Subsection:: *)
(* Fidelity and friends *)
TruncatedFidelity[rho_,sigma_,m_,delta_:10^-6]:=Block[{vec,val,proj},
{val,vec}=Chop[Eigensystem[rho]];
proj=Plus@@(Proj/@vec[[1;;m]]);
Chop[Fidelity[proj . rho . proj,proj . sigma . proj],delta]
];
(* ::Section:: *)
(*Package footer*)
Print["Package QINisq ", QINisq`Private`qiNisqVersion, " (last modification: ", QINisq`Private`qiNisqLastModification, ")."];
End[]; (* End Private Context *)
Protect@@Names["QINisq`*"];
EndPackage[];