-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathVisualization.m
467 lines (350 loc) · 15.3 KB
/
Visualization.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
(* ::Package:: *)
(* ::Title:: *)
(*QuantumUtils for Mathematica*)
(*Visualization Package*)
(* ::Subsection::Closed:: *)
(*Copyright and License Information*)
(* ::Text:: *)
(*This package is part of QuantumUtils for Mathematica.*)
(**)
(*Copyright (c) 2015 and later, Christopher J. Wood, Christopher E. Granade, Ian N. Hincks*)
(**)
(*Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met:*)
(*1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.*)
(*2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.*)
(*3. Neither the name of quantum-utils-mathematica nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.*)
(**)
(*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*)
(* ::Subsection::Closed:: *)
(*Preample*)
BeginPackage["Visualization`",{"QUDoc`"}];
(* ::Text:: *)
(*The following packages are needed, but their contexts should not be loaded globally.*)
Needs["QUDevTools`"];
Needs["Predicates`"];
Needs["Tensor`"];
$VisualizationUsages = LoadUsages[FileNameJoin[{$QUDocumentationPath, "api-doc", "Visualization.nb"}]];
(* ::Section:: *)
(*Usage Declaration*)
(* ::Subsection::Closed:: *)
(*Matrices*)
Unprotect[ComplexMatrixPlot,BlockForm,MatrixListForm];
AssignUsage[ComplexMatrixPlot,$VisualizationUsages];
AssignUsage[BlockForm,$VisualizationUsages];
AssignUsage[MatrixListForm,$VisualizationUsages];
(* ::Subsection::Closed:: *)
(*Bloch Plots*)
Unprotect[BlochPlot,BlochPlot2D,ListBlochPlot2D,BlochPlotColors,BlochPlotEndPoints,BlochPlotJoined,BlochPlotLabels];
AssignUsage[BlochPlot,$VisualizationUsages];
AssignUsage[BlochPlot2D,$VisualizationUsages];
AssignUsage[ListBlochPlot2D,$VisualizationUsages];
AssignUsage[BlochPlotColors,$VisualizationUsages];
AssignUsage[BlochPlotJoined,$VisualizationUsages];
AssignUsage[BlochPlotEndPoints,$VisualizationUsages];
AssignUsage[BlochPlotLabels,$VisualizationUsages];
(* ::Subsection::Closed:: *)
(*Eigensystems*)
Unprotect[EigensystemForm];
AssignUsage[EigensystemForm,$VisualizationUsages];
(* ::Subsection::Closed:: *)
(*Special Plotting Functions*)
Unprotect[FourierListPlot];
AssignUsage[FourierListPlot,$VisualizationUsages];
(* ::Subsection::Closed:: *)
(*Error Messages*)
BlochPlot::input = "Unable to parse input state(s); see Documentation. Constructed density lists have dimensions `1`.";
BlochPlot::color = "BlochPlotColor option value not understood.";
(* ::Section:: *)
(*Implementation*)
Begin["`Private`"];
(* ::Subsection::Closed:: *)
(*Matrices*)
ComplexMatrixPlot[A_,opts:OptionsPattern[DiscretePlot3D]]:=Module[{fun,color},
fun[x_,y_]:=(2UnitStep[Im[A[[x,y]]]]-1)Abs[A[[x,y]]];
(* We only use half of the Hue range otherwise 0 and \[Pi] would both be red *)
color[x_,y_,z_]:=Hue[Abs[Arg[A[[x,y]]]]/\[Pi]/2];
DiscretePlot3D[
fun[x,y],
{x,First@Dimensions@A},
{y,Last@Dimensions@A},
(* Allow user options to override anything *)
opts,
PlotTheme->"Detailed",
PlotStyle->Opacity[0.7],
(* ExtentSize gives it the bar graph look *)
ExtentSize->Full,
ColorFunctionScaling->False,
ColorFunction->color,
(* Note that the use of Ticks in BarLegend seems to work, although isn't documented*)
PlotLegends->BarLegend[{Hue[# /\[Pi]/2]&,{0,\[Pi]}},Ticks->Range[0,\[Pi],\[Pi]/4]],
AxesLabel->{"Row","Column"},
Ticks->{Range[First@Dimensions@A],Range[Last@Dimensions@A],Automatic},
Boxed->False
]
]
BlockForm[mat_,n_]:=
With[{div={False, Append[Table[False,{n - 1}], Dashed], False}},
{{Grid[mat, Dividers -> {div, div}, ItemSize -> Full]}} // MatrixForm
]
BlockForm[mat_]:=BlockForm[mat,First@Last@FactorInteger[Length[mat],2]]
MatrixListForm[mats_]:=Row[Riffle[MatrixForm/@mats,","]]
(* ::Subsection::Closed:: *)
(*Bloch Plots*)
(* ::Subsubsection::Closed:: *)
(*Bloch Sphere Display Components*)
With[{r=1},
$BlochSphereXY=ParametricPlot3D[0.99*{r Cos[\[Theta]],r Sin[\[Theta]],0},{\[Theta],0,2 \[Pi]},PlotStyle->Dashed,Boxed->False,Axes->False];
$BlochSphereYZ=ParametricPlot3D[0.99*{0,r Cos[\[Theta]],r Sin[\[Theta]]},{\[Theta],0,2 \[Pi]},PlotStyle->Dashed,Boxed->False,Axes->False];
$BlochSphereXZ=ParametricPlot3D[0.99*{r Cos[\[Theta]],0,r Sin[\[Theta]]},{\[Theta],0,2 \[Pi]},PlotStyle->Dashed,Boxed->False,Axes->False];
$BlochSphere=Show[
Graphics3D[{
Opacity[.2],
Sphere[{0,0,0},r],
Opacity[.5],Black,Thick,
Line[{{0,r,0},{0,-r,0}}],Line[{{0,0,r},{0,0,-r}}],Line[{{r,0,0},{-r,0,0}}],
Opacity[1],Black,Thick,
Text[Style["|+Z\[RightAngleBracket]",FontSize->14,FontWeight->Bold,FontColor->GrayLevel[.3]],{0,0,1.15 r}],
Text[Style["|-Z\[RightAngleBracket]",FontSize->14,FontWeight->Bold,FontColor->GrayLevel[.3]],{0,0,-1.15 r}],
Text[Style["|+X\[RightAngleBracket]",FontSize->14,FontWeight->Bold,FontColor->GrayLevel[.3]],{1.15 r,0,0}],
Text[Style["|-X\[RightAngleBracket]",FontSize->14,FontWeight->Bold,FontColor->GrayLevel[.3]],{-1.15 r,0,0}],
Text[Style["|+Y\[RightAngleBracket]",FontSize->14,FontWeight->Bold,FontColor->GrayLevel[.3]],{0,1.15 r,0}],
Text[Style["|-Y\[RightAngleBracket]",FontSize->14,FontWeight->Bold,FontColor->GrayLevel[.3]],{0,-1.15 r,0}]}],
$BlochSphereXY,$BlochSphereXZ,$BlochSphereYZ,Boxed->False];
$BlochCircleXZ=Graphics[{
Circle[{0,0},1],
Line[{{1,0},{-1,0}}],
Line[{{0,1},{0,-1}}],
Text[Style["|+X\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{1.15,0}],
Text[Style["|-X\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{-1.15,0}],Text[Style["|+Z\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{0,1.15}],
Text[Style["|-Z\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{0,-1.15 }]}];
$BlochCircleXY=Graphics[{
Circle[{0,0},1],
Line[{{1,0},{-1,0}}],
Line[{{0,1},{0,-1}}],
Text[Style["|+X\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{1.15,0}],
Text[Style["|-X\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{-1.15,0}],Text[Style["|+Y\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{0,1.15}],
Text[Style["|-Y\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{0,-1.15 }]}];
$BlochCircleYZ=Graphics[{
Circle[{0,0},1],
Line[{{1,0},{-1,0}}],
Line[{{0,1},{0,-1}}],
Text[Style["|+Y\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{1.15,0}],
Text[Style["|-Y\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{-1.15,0}],
Text[Style["|+Z\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{0,1.15}],
Text[Style["|-Z\[RightAngleBracket]",FontSize->12,FontWeight->Bold],{0,-1.15 }]}];
];
(* ::Subsubsection::Closed:: *)
(*Private Helper Functions*)
(*BlochCoordinates[state_]:=Table[Re@Tr[PauliMatrix[n].state],{n,3}]
BlochCoordinates = Compile[{{state,_Complex,2}},{
Re[state[[1,2]]+state[[2,1]]],
-Im[state[[1,2]]]+Im[state[[2,1]]],
Re[state[[1,1]]-state[[2,2]]]
}];*)
BlochCoordinates = Function[{state},{
Re[state[[1,2]]+state[[2,1]]],
-Im[state[[1,2]]]+Im[state[[2,1]]],
Re[state[[1,1]]-state[[2,2]]]
}];
SetAttributes[BlochCoordinates,Listable];
BlochLegend[labels_,joined_,colorPairs_]:=LineLegend[
Directive[Thickness[0.01],Sequence@@If[joined,{},{Dotted}],#]&/@colorPairs[[All,1]],
labels
]
ChooseBlochColorPairs[colorOption_]:=
Which[
colorOption===Automatic,
ChooseBlochColorPairs[{RGBColor[0.742077, 0.0624857, 0.00605783],RGBColor[0, 0.501961, 0],RGBColor[1, 0.0175937, 0.505959],RGBColor[1, 0.904479, 0.279423],RGBColor[0.915, 0.3325, 0.2125],RGBColor[0.28026441037696703`, 0.715, 0.4292089322474965],RGBColor[0.6705882352941176, 0.8784313725490196, 0.9372549019607843],RGBColor[0.09019607843137255, 0.33725490196078434`, 0.49411764705882355`],RGBColor[0.975067, 1, 0.906294],RGBColor[0.5333333333333333, 0.23529411764705882`, 0.3058823529411765],RGBColor[0.823529, 0.663981, 0.00828565]}],
ListQ[colorOption]&&MatchQ[colorOption,{__?ColorQ}],
Map[{#,Lighter[#]}&,colorOption],
ListQ[colorOption]&&ArrayDepth[colorOption]===2&&MatchQ[Flatten[colorOption],{__?ColorQ}],
colorOption,
True,
Message[BlochPlot::color];
ChooseBlochColorPairs[Automatic]
]
(* ::Subsubsection::Closed:: *)
(*Bloch Sphere Plotting*)
InheritOptions[BlochPlot, {Graphics3D, ParametricPlot3D, Interpolation}, {
ViewAngle->0.34,
BlochPlotLabels->Off,
BlochPlotJoined->False,
BlochPlotColors->Automatic,
BlochPlotEndPoints->True
}];
Unprotect@BlochPlot;
BlochPlot[state_,opts:OptionsPattern[]]:=
Module[{statesList,listOfDensity},
statesList=state;
listOfDensity[array_]:=And[ArrayDepth[array]===3,Dimensions[array][[-2;;]]==={2,2}];
(*Turn single pure state or density matrix into list of one density matrix*)
If[VectorQ[statesList],statesList=Projector[statesList]];
If[SquareMatrixQ[statesList],statesList={statesList}];
(*If the depth is still 2 it must be a list of pure states *)
If[ArrayDepth[statesList]===2,statesList=Map[Projector,statesList]];
(*Turn a list of density matrices into a list thereof*)
If[listOfDensity[statesList],statesList={statesList}];
(*If the depth is still 3 it must be a list of lists of pure states; make into densities*)
If[Apply[And,Map[MatrixQ,statesList]],statesList=Map[Projector,statesList,{2}]];
(*If all went right we should have a list of lists of 2x2 density matrices.*)
If[Not[And@@(Map[listOfDensity,statesList])],Message[BlochPlot::input,Dimensions/@statesList]];
If[OptionValue[BlochPlotJoined],
ParametricBlochPlot[statesList,opts],
ListBlochPlot[statesList,opts]
]
]
ListBlochPlot[statesList_,opt:OptionsPattern[BlochPlot]]:=
Module[{plotdat,d,numlists,colorPairs,ep,fig},
colorPairs=ChooseBlochColorPairs[OptionValue[BlochPlotColors]];
numlists=Length@statesList;
ep=OptionValue@BlochPlotEndPoints;
plotdat=Table[Map[BlochCoordinates,statesList[[n]]],{n,numlists}];
d=Table[Length[statesList[[n]]],{n,numlists}];
colorPairs=colorPairs[[Mod[Range[numlists]-1,Length@colorPairs]+1]];
fig=Show[
$BlochSphere,
Table[
Graphics3D[{
Table[
{
Blend[colorPairs[[n]],1-j/d[[n]]],
PointSize@If[Or[1<j<d[[n]],Not[ep]],0.015,0.035],
Point[plotdat[[n,j]]]
},
{j,d[[n]]}
],
Sequence@@If[d[[n]]===1,{colorPairs[[n,1]],Thickness[0.005],Line[{{0,0,0},plotdat[[n,1]]}]},{}]
},Boxed->False],
{n,numlists}
],
ViewAngle->OptionValue[ViewAngle],
FilterOptions[Graphics3D,opt]
];
If[OptionValue[BlochPlotLabels]===Off,
fig,
Legended[fig,BlochLegend[OptionValue[BlochPlotLabels],False,colorPairs]]
]
]
ParametricBlochPlot[statesList_,opt:OptionsPattern[BlochPlot]]:=
Module[{fig,plotdat,fx,fy,fz,d,numlists,colorPairs,range},
colorPairs=ChooseBlochColorPairs[OptionValue[BlochPlotColors]];
numlists=Length@statesList;
d=Table[Length[statesList[[n]]],{n,numlists}];
colorPairs=colorPairs[[Mod[Range[numlists]-1,Length@colorPairs]+1]];
plotdat=Table[Map[BlochCoordinates,statesList[[n]]],{n,numlists}];
Table[
If[d[[n]]>1,
range=Range[0,1,1/(d[[n]]-1)];
fx[n]=Interpolation[{range,plotdat[[n,All,1]]}\[Transpose],FilterOptions[Interpolation,opt]];
fy[n]=Interpolation[{range,plotdat[[n,All,2]]}\[Transpose],FilterOptions[Interpolation,opt]];
fz[n]=Interpolation[{range,plotdat[[n,All,3]]}\[Transpose],FilterOptions[Interpolation,opt]];,
fx[n]=Null &;
fy[n]=Null &;
fz[n]=Null &;
],
{n,numlists}
];
fig=Show[
$BlochSphere,
Table[Sequence@@{
ParametricPlot3D[
{fx[n][u],fy[n][u],fz[n][u]}, {u,0,1},
Boxed->False,
Evaluate@FilterOptions[ParametricPlot3D,opt],
PlotStyle->Thickness[0.01],
ColorFunction->(Blend[colorPairs[[n]],#4]&)
],
Graphics3D[{PointSize[0.035],
If[OptionValue[BlochPlotEndPoints],Sequence@@{
colorPairs[[n,1]],Point[plotdat[[n,1]]],
colorPairs[[n,2]],Point[plotdat[[n,-1]]]
}],
Sequence@@If[d[[n]]===1,{colorPairs[[n,1]],Thickness[0.005],Line[{{0,0,0},plotdat[[n,1]]}]},{}]
}]},
{n,numlists}
],
ViewAngle->OptionValue[ViewAngle],
FilterOptions[Graphics3D,opt]
];
If[OptionValue[BlochPlotLabels]===Off,
fig,
Legended[fig,BlochLegend[OptionValue[BlochPlotLabels],True,colorPairs]]
]
]
(* ::Subsubsection::Closed:: *)
(*Bloch Sphere 2D Projection*)
BlochPlot2D[state_]:=
With[{p=BlochCoordinates[state]},
GraphicsRow[{
Show[$BlochCircleXZ,Graphics[
{Red,Thickness[0.01],Line[{{0,0},Part[p,{1,3}]}]},
{Red,PointSize[0.03],Point[Part[p,{1,3}]]}]],
Show[$BlochCircleYZ,Graphics[
{Red,Thickness[0.01],Line[{{0,0},Part[p,{2,3}]}]},
{Red,PointSize[0.03],Point[Part[p,{2,3}]]}]],
Show[$BlochCircleXY,Graphics[
{Red,Thickness[0.01],Line[{{0,0},Part[p,{1,2}]}]},
{Red,PointSize[0.03],Point[Part[p,{1,2}]]}]]
},ImageSize->Large]]
ListBlochPlot2D[\[Rho]list_]:=
With[{
ps=Map[BlochCoordinates,\[Rho]list],
d=Length[\[Rho]list]},
GraphicsRow[{
Show[$BlochCircleXZ,
Graphics[{
Map[{Blend[{Red,Blue},1-#/d],PointSize[0.02],Point[Part[ps,#,{1,3}]]}&,
Range[d]]
}]],
Show[$BlochCircleYZ,
Graphics[{
Map[{Blend[{Red,Blue},1-#/d],PointSize[0.02],Point[Part[ps,#,{2,3}]]}&,
Range[d]]
}]],
Show[$BlochCircleXY,
Graphics[{
Map[{Blend[{Red,Blue},1-#/d],PointSize[0.02],Point[Part[ps,#,{1,2}]]}&,
Range[d]]
}]]
},ImageSize->Large]
];
(* ::Subsection::Closed:: *)
(*Eigensystems*)
EigensystemForm[sys_]:=Grid[
({#[[1]],"\[DownArrow]",MatrixForm[#[[2]]]}&/@(sys\[Transpose]))\[Transpose],
ItemSize->Full
];
(* ::Subsection::Closed:: *)
(*Special Plotting Functions*)
FourierListPlot[data_,{mint_,maxt_},function_,opt:OptionsPattern[ListPlot]]:=
Module[{n,n2,dt,fdata,minf,maxf},
(*Take the discrete FT of each input list*)
fdata=(Fourier/@If[VectorQ[data],{data},data]);
(* Now that we've sorted out whether data is a list or list of lists... *)
n=Length[fdata[[1]]];
n2=Floor[n/2];
dt=(maxt-mint)/(n-1);
(* Apply each input function to each fourier domain list *)
fdata=Flatten[Outer[#2[#1]&,fdata,If[ListQ[function],function,{function}],1],1];
(* Add x cooordinates to the lists *)
fdata=Map[({Range[0,1/dt,1/((n-1)dt)],#}\[Transpose])&,fdata,1];
(* determine the plot bounds *)
If[OptionValue[DataRange]===Automatic,
minf=0;maxf=1/(2dt);,
minf=OptionValue[DataRange][[1]];maxf=OptionValue[DataRange][[2]];
];
(* select only those data lying in the frequency bounds*)
fdata=Function[{list},Select[list,minf<=#[[1]]<=maxf&]]/@fdata;
(* usual list plot *)
ListPlot[fdata,DataRange->Automatic,opt]
]
(* ::Subsection::Closed:: *)
(*End Private*)
End[];
(* ::Section:: *)
(*End Package*)
Protect[ComplexMatrixPlot,BlockForm,MatrixListForm];
Protect[BlochPlot,BlochPlot2D,ListBlochPlot2D,BlochPlotColors,BlochPlotEndPoints,BlochPlotJoined,BlochPlotLabels];
Protect[EigensystemForm];
Protect[FourierListPlot];
EndPackage[];