Debian Codegraph @

texmf-dist/source/latex/blochsphere/blochsphere.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2015 by Matthew Wardrop <[email protected]>
4 % ---------------------------------------------------------------------------
5 % This work may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either version 1.3
7 % of this license or (at your option) any later version.
8 % The latest version of this license is in
9 % http://www.latex-project.org/lppl.txt
10 % and version 1.3 or later is part of all distributions of LaTeX
11 % version 2005/12/01 or later.
12 %
13 % This work has the LPPL maintenance status `maintained'.
14 %
15 % The Current Maintainer of this work is Matthew Wardrop
16 %
17 % This work consists of the files blochsphere.dtx and blochsphere.ins
18 % and the derived filebase blochsphere.sty.
19 %
20 % \fi
21 %
22 % \iffalse
23 %<*driver>
24 \ProvidesFile{blochsphere.dtx}
25 %</driver>
26 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
27 %<package>\ProvidesPackage{blochsphere}
28 %<*package>
29 [2015/09/15 v1.1 blochsphere drawing package]
30 %</package>
31 %
32 %<*driver>
33 \documentclass{ltxdoc}
34 \EnableCrossrefs
35 \CodelineIndex
36 \RecordChanges
37 \usepackage{blochsphere}
38 \usepackage{minted}
39 \usepackage{hyperref}
40
41 \setlength{\parskip}{0.7em}
42
43 \newenvironment{key}[2]{
44 \noindent{\llap{Option: \bf\tt #2\hskip 9pt}}
45 }{}
46 \newcommand{\DescribeKey}[3][]{
47 \noindent{\llap{\bf\tt #2\hskip 9pt}[default = #1] #3}
48 }
49
50 \usepackage{listings}
51 \lstset{breaklines=true}
52
53 \begin{document}
54 \DocInput{blochsphere.dtx}
55 \PrintChanges
56 \PrintIndex
57 \end{document}
58 %</driver>
59 % \fi
60 %
61 % \CheckSum{841}
62 %
63 % \CharacterTable
64 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
65 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
66 % Digits \0\1\2\3\4\5\6\7\8\9
67 % Exclamation \! Double quote \" Hash (number) \#
68 % Dollar \$ Percent \% Ampersand \&
69 % Acute accent \' Left paren \( Right paren \)
70 % Asterisk \* Plus \+ Comma \,
71 % Minus \- Point \. Solidus \/
72 % Colon \: Semicolon \; Less than \<
73 % Equals \= Greater than \> Question mark \?
74 % Commercial at \@ Left bracket \[ Backslash \\
75 % Right bracket \] Circumflex \^ Underscore \_
76 % Grave accent \` Left brace \{ Vertical bar \|
77 % Right brace \} Tilde \~}
78 %
79 %
80 % \changes{v1.1}{2015/09/15}{Fix \\drawAxis using removed keyval "mark"}
81 % \changes{v1.0}{2015/08/17}{Initial version}
82 %
83 % \DoNotIndex{\newcommand,\newenvironment}
84 %
85 % \providecommand*{\url}{\texttt}
86 % \GetFileInfo{blochsphere.dtx}
87 % \title{The \textsf{blochsphere} package}
88 % \author{Matthew Wardrop \\ \url{[email protected]}}
89 % \date{\fileversion~from \filedate}
90 %
91 % \maketitle
92 %
93 % \tableofcontents
94 %
95 % \section{Introduction}
96 %
97 % The |blochsphere| package is used to draw pseudo-3D Blochsphere diagrams, such as that
98 % shown in figure \ref{fig:blochsphere}. It supports various annotations, such as
99 % great and small circles, axes, rotation markings and state vectors. It can be used
100 % in a standalone fashion, or nested within a |tikzpicture| environment by setting
101 % the environment option |nested| to |true|.
102 %
103 % \section{Usage}
104 %
105 % Using |blochsphere| is as simple as creating a `blochsphere' environment.
106 % \begin{minted}{LaTeX}
107 % \begin{blochsphere}[<options>]
108 % ...
109 % \end{blochsphere}
110 % \end{minted}
111 % By default, this will create a blank Bloch sphere ready for annotation. The options can be
112 % any of those described in the next section.
113 %
114 % \noindent\DescribeMacro{\drawBall} \oarg{options}\\
115 % This macro draws the basic Bloch sphere, according to the options defined in the next section.
116 %
117 % \noindent\DescribeMacro{\drawBallGrid} \oarg{options} \marg{latdegsep} \marg{longdegsep}\\
118 % This macro draws latitude and longitude circles at an interval defined by |latdegsep| and |longdegsep| respectively.
119 %
120 % \noindent\DescribeMacro{\setDrawingPlane} \oarg{options} \marg{tiltangle} \marg{rotationangle}\\
121 % This macro defines a transformation matrix within TikZ to create the effect of 2D projections of 3D planes,
122 % named according to the |plane| option. This is used internally in all of the drawing commands.
123 %
124 % \noindent\DescribeMacro{\setLatitudinalDrawingPlane} \oarg{options} \marg{latitude}\\
125 % This is a special case of |\setDrawingPlane| that constructs the transform required to draw a 2D image in
126 % a latitudinal plane.
127 %
128 % \noindent\DescribeMacro{\setLongitudinalDrawingPlane} \oarg{options} \marg{longitude}\\
129 % This is a special case of |\setDrawingPlane| that constructs the transform required to draw a 2D image in
130 % a longitudinal plane.
131 %
132 % \noindent\DescribeMacro{\drawCircle} \oarg{options} \marg{tiltangle} \marg{rotationangle}\\
133 % Draws a circle on the surface of a Bloch sphere around the axis which is tilted by |tiltangle|
134 % from the z-axis and with a rotation of |rotationangle| about the z-axis.
135 %
136 % \noindent\DescribeMacro{\drawGreatCircle} \oarg{options} \marg{tiltangle} \marg{rotationangle}\\
137 % An alias of |\drawCircle|.
138 %
139 % \noindent\DescribeMacro{\drawSmallCircle} \oarg{options} \marg{tiltangle} \marg{rotationangle} \marg{offsetLatitude}\\
140 % Draws a circle on the surface of a Bloch sphere around the axis which is tilted by |tiltangle|
141 % from the z-axis and with a rotation of |rotationangle| about the z-axis, with an offset along the axis
142 % such that it reaches an effective latitude of |offsetLatitude|.
143 %
144 % \noindent\DescribeMacro{\drawLatitudeCircle} \oarg{options} \marg{latitude}\\
145 % Draws a circle on the Bloch sphere corresponding to the given latitude.
146 %
147 % \noindent\DescribeMacro{\drawLongitudeCircle} \oarg{options} \marg{longitude}\\
148 % Draws a great circle on the Bloch sphere corresponding to the given longitude.
149 %
150 % \noindent\DescribeMacro{\drawRotationLeft} \oarg{options} \marg{tiltangle} \marg{rotationangle} \marg{axisoffset} \marg{clearangle}\\
151 % Draw a left-handed rotation about the Bloch sphere, titled and rotated as for a circle
152 % with the same parameters, with a gap in the line where the arrow should go of
153 % |clearangle| degrees.
154 %
155 % \noindent\DescribeMacro{\drawRotationRight} \oarg{options} \marg{tiltangle} \marg{rotationangle} \marg{axisoffset} \marg{clearangle}\\
156 % As above, but a right-handed rotation annotation.
157 %
158 % \noindent\DescribeMacro{\drawAxis} \oarg{options} \marg{tiltangle} \marg{rotationangle}\\
159 % Draw a line along the axis tilted |tiltangle| degrees from the z-axis, rotated about
160 % the z-axis by |rotationangle| degrees.
161 %
162 % \noindent\DescribeMacro{\labelPolar} \oarg{options} \marg{tikzlabel} \marg{tiltangle} \marg{rotationangle}\\
163 % Assign a tikz label to the point identified by an angle |tiltangle| from the z-axis
164 % and a rotation |rotationangle| about the z-axis.
165 %
166 % \noindent\DescribeMacro{\labelLatLon} \oarg{options} \marg{tikzlabel} \marg{latitude} \marg{longitude}\\
167 % Assign a tikz label to the point identified the specified latitude and longitude.
168 %
169 % \noindent\DescribeMacro{\drawStatePolar} \oarg{options} \marg{tikzlabel} \marg{tiltangle} \marg{rotationangle}\\
170 % Draw a vector from the origin to the point identified by an angle |tiltangle|
171 % from the z-axis and a rotation |rotationangle| about the z-axis, which will
172 % be labelled |tikzlabel|.
173 %
174 % \noindent\DescribeMacro{\drawStateLatLon} \oarg{options} \marg{tikzlabel} \marg{latitude} \marg{longitude}\\
175 % Draw a vector from the origin to the point identified by the provided
176 % latitude and longitude, which will be labelled |tikzlabel|.
177 %
178 %
179 % \section{Package, Environment and Macro options}
180 % For convenience, all options at all levels share the same namespace. Therefore, all
181 % options passed to the package will be the defaults for all environment, which will
182 % in turn be the defaults for all subsequent macro calls.
183 %
184 %
185 % \DescribeKey[1.5cm]{radius}
186 % {This option controls the radius of the qubit, and can be specified in any valid \LaTeX units.}
187 %
188 %
189 % \DescribeKey[15]{tilt}{This option controls the tilt (into the page) of the top of the bloch sphere (in degrees).}
190 %
191 %
192 % \DescribeKey[-20]{rotation}{This option controls the (right-handed) rotation of the Blochsphere around the (potentially tilted) z-axis, also specified in degrees.}
193 %
194 %
195 % \DescribeKey[white]{color}{This specifies the colour of the drawn ball.}
196 %
197 %
198 % \DescribeKey[0.7]{opacity}{This option controls the opacity of the ball, allowing lines drawn behind the ball to be visible.}
199 %
200 %
201 % \DescribeKey[]{style}{This option controls the current drawing style, and supports any valid tikz styling.}
202 %
203 %
204 % \DescribeKey[1]{scale}{This option allows one to temporarily change the scale of drawn circles/etc, allowing annotations to ``hover'' over the Bloch sphere.}
205 %
206 %
207 % \DescribeKey[current plane]{plane}{The name which should label the current plane being defined, or the plane to be used.}
208 %
209 %
210 % \DescribeKey[(0,0,0)]{shift}{The three dimensional offset by which the currently being drawn objects should be shifted.}
211 %
212 %
213 % \DescribeKey[3d]{ball}{Specifies how the ball should be drawn. Options are: {\tt 3d}, {\tt circle}, {\tt none}}
214 %
215 %
216 % \DescribeKey[black]{statecolor}{The color with which the state vectors should be drawn.}
217 %
218 %
219 % \DescribeKey[0.4pt]{statewidth}{The width with which state vectors should be drawn.}
220 %
221 %
222 % \DescribeKey[]{axisarrow}{The arrow to use at the end of a drawn axis.}
223 %
224 %
225 % \DescribeKey[false]{labelmark}{Whether a black dot should be drawn at the point being labelled.}
226 %
227 %
228 % \DescribeKey[false]{nested}{Whether the {\tt blochsphere} environment is being nested in a |tikzpicture| environment. If it is nested, then this should be set to true.}
229 %
230 %
231 % \section{Example}
232 % In this section we provide example code to generate the following diagram:
233 %
234 % \begin{figure}[h!]
235 % \begin{center}
236 % \includegraphics{example.pdf}
237 % \end{center}
238 % \caption{Example Bloch Sphere \label{fig:blochsphere}}
239 % \end{figure}
240 %
241 % \newminted{latex}{gobble=2,linenos}
242 % \begin{latexcode}
243 % \begin{blochsphere}[radius=1.5 cm,tilt=15,rotation=-20]
244 % \drawBallGrid[style={opacity=0.3}]{30}{30}
245 %
246 % \drawGreatCircle[style={dashed}]{-60}{0}{0}
247 % \drawGreatCircle[style={dashed}]{60}{0}{0}
248 %
249 % \drawRotationLeft[scale=1.3,style={red}]{-60}{0}{0}{15}
250 % \drawRotationRight[scale=1.3,style={red}]{60}{0}{0}{15}
251 %
252 % \node at (-0.8,1.9) {\textcolor{red}{\tiny $J_{12}(t)$}};
253 % \node at (1.1,1.8) {\textcolor{red}{\tiny $J_{23}(t)$}};
254 %
255 % \labelLatLon{up}{90}{0};
256 % \labelLatLon{down}{-90}{90};
257 % \node[above] at (up) {{\tiny $\left|1\right>$ }};
258 % \node[below] at (down) {{\tiny $\left|0\right>$}};
259 %
260 % \labelLatLon[labelmark=false]{d}{15}{90};
261 % \node at (d) {\color{gray}\fontsize{0.15cm}{1em}\selectfont $60^\circ$};
262 %
263 % \labelLatLon[labelmark=falsa]{d2}{5}{78};
264 % \node at (d2) {\color{gray}\fontsize{0.15cm}{1em}\selectfont $60^\circ$};
265 % \end{blochsphere}
266 % \end{latexcode}
267 %
268 % \StopEventually{}
269 %
270 % \section{Implementation}
271 %
272 % \iffalse
273 %<*package>
274 % \fi
275 % \subsection{Preamble}
276 % \begin{macrocode}
277 \NeedsTeXFormat{LaTeX2e}[1994/06/01]
278 \ProvidesPackage{blochsphere}[2015/08/17]
279 % \end{macrocode}
280 % \iffalse
281 %% \usepackage{trace}
282 %%\tracingall
283 % \fi
284 %
285 % \begin{macrocode}
286 \usepackage{tikz,etoolbox,environ,ifthen,kvsetkeys,kvoptions}
287 \usetikzlibrary{decorations.pathreplacing, decorations.markings, calc, fadings}
288 % \end{macrocode}
289 %
290 % \subsection{Package/Environment/Macro options}
291 %
292 % \begin{key}{blochsphere}{radius}
293 % \begin{macrocode}
294 \define@key{blochsphere}{radius}{%
295 \def\blochsphere@radius{#1}%
296 }
297 \providecommand{\blochsphere@radius}{1cm}
298 % \end{macrocode}
299 % \end{key}
300 %
301 %
302 % \begin{key}{blochsphere}{tilt}
303 % \begin{macrocode}
304 \define@key{blochsphere}{tilt}{%
305 \def\blochsphere@tilt{#1}%
306 }
307 \providecommand{\blochsphere@tilt}{15}
308 % \end{macrocode}
309 % \end{key}
310 %
311 %
312 % \begin{key}{blochsphere}{rotation}
313 % \begin{macrocode}
314 \define@key{blochsphere}{rotation}{%
315 \def\blochsphere@rotation{#1}%
316 }
317 \providecommand{\blochsphere@rotation}{-20}
318 % \end{macrocode}
319 % \end{key}
320 %
321 %
322 % \begin{key}{blochsphere}{color}
323 % \begin{macrocode}
324 \define@key{blochsphere}{color}{%
325 \def\blochsphere@color{#1}%
326 }
327 \providecommand{\blochsphere@color}{white}
328 % \end{macrocode}
329 % \end{key}
330 %
331 %
332 % \begin{key}{blochsphere}{opacity}
333 % \begin{macrocode}
334 \define@key{blochsphere}{opacity}{%
335 \def\blochsphere@opacity{#1}%
336 }
337 \providecommand{\blochsphere@opacity}{0.7}
338 % \end{macrocode}
339 % \end{key}
340 %
341 %
342 % \begin{key}{blochsphere}{style}
343 % \begin{macrocode}
344 \define@key{blochsphere}{style}{%
345 \def\blochsphere@style{#1}%
346 }
347 \providecommand{\blochsphere@style}{}
348 % \end{macrocode}
349 % \end{key}
350 %
351 %
352 % \begin{key}{blochsphere}{scale}
353 % \begin{macrocode}
354 \define@key{blochsphere}{scale}{%
355 \def\blochsphere@scale{#1}%
356 }
357 \providecommand{\blochsphere@scale}{1}
358 % \end{macrocode}
359 % \end{key}
360 %
361 %
362 % \begin{key}{blochsphere}{plane}
363 % \begin{macrocode}
364 \define@key{blochsphere}{plane}{%
365 \def\blochsphere@plane{#1}%
366 }
367 \providecommand{\blochsphere@plane}{current plane}
368 % \end{macrocode}
369 % \end{key}
370 %
371 %
372 % \begin{key}{blochsphere}{shift}
373 % \begin{macrocode}
374 \define@key{blochsphere}{shift}{%
375 \def\blochsphere@shift{{#1}}%
376 }
377 \providecommand{\blochsphere@shift}{{0,0,0}}
378 % \end{macrocode}
379 % \end{key}
380 %
381 %
382 % \begin{key}{blochsphere}{ball}
383 % \begin{macrocode}
384 \define@key{blochsphere}{ball}{%
385 \def\blochsphere@ball{#1}%
386 }
387 \providecommand{\blochsphere@ball}{3d}
388 % \end{macrocode}
389 % \end{key}
390 %
391 %
392 % \begin{key}{blochsphere}{statecolor}
393 % \begin{macrocode}
394 \define@key{blochsphere}{statecolor}{%
395 \def\blochsphere@statecolor{#1}%
396 }
397 \providecommand{\blochsphere@statecolor}{black}
398 % \end{macrocode}
399 % \end{key}
400 %
401 %
402 % \begin{key}{blochsphere}{statewidth}
403 % \begin{macrocode}
404 \define@key{blochsphere}{statewidth}{%
405 \def\blochsphere@statewidth{#1}%
406 }
407 \providecommand{\blochsphere@statewidth}{0.4pt}
408 % \end{macrocode}
409 % \end{key}
410 %
411 %
412 % \begin{key}{blochsphere}{axisarrow}
413 % \begin{macrocode}
414 \define@key{blochsphere}{axisarrow}{%
415 \def\blochsphere@axisarrow{#1}%
416 }
417 \providecommand{\blochsphere@axisarrow}{}
418 % \end{macrocode}
419 % \end{key}
420 %
421 %
422 % \begin{key}{blochsphere}{labelmark}
423 % \begin{macrocode}
424 \define@key{blochsphere}{labelmark}{%
425 \setboolean{blochsphere@labelmark}{#1}%
426 }
427 \newboolean{blochsphere@labelmark}
428 \setboolean{blochsphere@labelmark}{false}
429 % \end{macrocode}
430 % \end{key}
431 %
432 %
433 % \begin{key}{blochsphere}{nested}
434 % \begin{macrocode}
435 \define@key{blochsphere}{nested}{%
436 \setboolean{blochsphere@nested}{#1}%
437 }
438 \newboolean{blochsphere@nested}
439 \setboolean{blochsphere@nested}{false}
440 % \end{macrocode}
441 % \end{key}
442 % \begin{macrocode}
443 \ProcessKeyvalOptions{blochsphere}\relax
444 % \end{macrocode}
445 %
446 % \subsection{{\bfseries\tt blochsphere} Environment}
447 % This is the central environment definition. Note that all macros defined
448 % below will only be defined within this environment.
449 %
450 % \begin{environment}{blochsphere}
451 % \begin{macrocode}
452 \newenvironment{blochsphere}[1][]{
453 \begingroup
454 \makeatletter
455 \ignorespaces\setkeys{blochsphere}{#1}\unskip%
456 \pgfdeclarelayer{back}%
457 \pgfdeclarelayer{front}%
458 \pgfsetlayers{back,main,front}%
459 \pgfkeys{%
460 /tikz/on layer/.code={%
461 \pgfonlayer{##1}\begingroup%
462 \aftergroup\endpgfonlayer%
463 \aftergroup\endgroup%
464 },%
465 /tikz/node on layer/.code={%
466 \gdef\node@@on@layer{%
467 \setbox\tikz@tempbox=\hbox\bgroup\pgfonlayer{##1}\unhbox\tikz@tempbox\endpgfonlayer\egroup}%
468 \aftergroup\node@on@layer%
469 },%
470 /tikz/end node on layer/.code={%
471 \endpgfonlayer\endgroup\endgroup%
472 }%
473 }%
474 \def\node@on@layer{\aftergroup\node@@on@layer}%
475 % \end{macrocode}
476 % \begin{macro}{\drawBall}
477 % \begin{macrocode}
478 \newcommand\drawBall[1][]{
479 \begingroup
480 \setkeys{blochsphere}{##1}
481 \expandafter\ifstrequal\expandafter{\blochsphere@ball}{3d}{
482 \fill[on layer=main,ball color=\blochsphere@color,opacity=\blochsphere@opacity] (0,0) circle (\blochsphere@radius); % 3D lighting effect
483 }{}
484 \expandafter\ifstrequal\expandafter{\blochsphere@ball}{circle}{
485 \draw[on layer=main,color=\blochsphere@color] (0,0) circle (\blochsphere@radius); % 3D lighting effect
486 }{}
487 \endgroup
488 }%
489 % \end{macrocode}
490 % \end{macro}
491 % \begin{macro}{\drawBallGrid}
492 % \begin{macrocode}
493 \newcommand\drawBallGrid[3][]{
494 \begingroup
495 \setkeys{blochsphere}{##1}
496 \foreach \phi in {0,##2,...,89} {
497 \drawLatitudeCircle{\phi}
498 \drawLatitudeCircle{-\phi}
499 }
500 \foreach \theta in {0,##3,...,179} {
501 \drawLongitudeCircle{\theta}
502 }
503 \endgroup
504 }%
505 % \end{macrocode}
506 % \end{macro}
507 % \begin{macro}{\setDrawingPlane}
508 % \begin{macrocode}
509 \newcommand\setDrawingPlane[3][]{%
510 \setkeys{blochsphere}{##1}
511 \pgfmathsetmacro\aphi{##2}
512 \pgfmathsetmacro\atheta{##3}
513 \pgfmathsetmacro\dot{cos(\aphi)}
514 \ifdim\dot pt<0.7 pt\relax%
515 \pgfmathsetmacro\norm{cos(asin(\dot))}
516 \pgfmathsetmacro\xx{-sin(\aphi)*sin(\blochsphere@rotation - \atheta)}
517 \pgfmathsetmacro\xy{sin(\aphi)*sin(\blochsphere@tilt)*cos(\blochsphere@rotation - \atheta)}
518 \pgfmathsetmacro\yx{-sin(2*\aphi - \blochsphere@rotation + \atheta)/4 - sin(2*\aphi + \blochsphere@rotation - \atheta)/4}
519 \pgfmathsetmacro\yy{(sin(\aphi)*cos(\blochsphere@tilt) - sin(\blochsphere@tilt)*sin(\blochsphere@rotation - \atheta)*cos(\aphi))*sin(\aphi)}
520 \else
521 \pgfmathsetmacro\norm{sqrt(abs(sin(\blochsphere@rotation - \atheta)*cos(\aphi))^2 + abs(sin(\blochsphere@tilt)*cos(\aphi)*cos(\blochsphere@rotation - \atheta))^2 + abs(cos(\aphi)*cos(\blochsphere@tilt)*cos(\blochsphere@rotation - \atheta))^2)}
522 \pgfmathsetmacro\xx{-sin(\blochsphere@rotation - \atheta)*cos(\aphi)}
523 \pgfmathsetmacro\xy{sin(\blochsphere@tilt)*cos(\aphi)*cos(\blochsphere@rotation - \atheta)}
524 \pgfmathsetmacro\yx{-cos(\aphi)^2*cos(\blochsphere@rotation - \atheta)}
525 \pgfmathsetmacro\yy{(sin(\aphi)*cos(\blochsphere@tilt) - sin(\blochsphere@tilt)*sin(\blochsphere@rotation - \atheta)*cos(\aphi))*cos(\aphi)}
526 \fi
527 \computeOffset{shift}
528 \begingroup\newcommand\tmp[1]{\endgroup\noexpand\relax%
529 \tikzset{\blochsphere@plane/.style={cm={\xx/\norm, \xy/\norm, \yx/\norm, \yy/\norm, \unexpanded\expandafter{####1} }}}%
530 } \tmp{\shift}
531 }%
532 % \end{macrocode}
533 % \end{macro}
534 % \begin{macro}{\setLatitudinalDrawingPlane}
535 % \begin{macrocode}
536 \newcommand\setLatitudinalDrawingPlane[2][]{%
537 \pgfmathsetmacro\yshift{sin(##2)*\the\blochsphere@radius}
538 \setDrawingPlane[##1,shift={{0,0,\yshift}}]{0}{0}
539 }%
540 % \end{macrocode}
541 % \end{macro}
542 % \begin{macro}{\setLatitudinalDrawingPlane}
543 % \begin{macrocode}
544 \newcommand\setLongitudinalDrawingPlane[2][]{%
545 \setDrawingPlane[##1]{90}{##2}
546 }%
547 % \end{macrocode}
548 % \end{macro}
549 % \begin{macro}{\drawCircle}
550 % \begin{macrocode}
551 \newcommand\drawCircle[3][]{
552 \begingroup
553 \setkeys{blochsphere}{##1}
554 \setDrawingPlane{##2}{##3}
555 \computeVisibility{##2}{##3}{agamma}{abeta}
556 \begingroup\edef\tmp{\endgroup%
557 \noexpand\draw[current plane,on layer=back,\unexpanded\expandafter{\blochsphere@style}] \unexpanded\expandafter{(\agamma-\abeta-0.2:\blochsphere@radius+\the\pgflinewidth*0.5) arc (\agamma-\abeta-0.2:\agamma+\abeta+0.2:\blochsphere@radius+\the\pgflinewidth*0.5)};
558 \noexpand\draw[current plane,on layer=front,\unexpanded\expandafter{\blochsphere@style}] \unexpanded\expandafter{(\agamma+\abeta:\blochsphere@radius+\the\pgflinewidth*0.5) arc (\agamma+\abeta:\agamma-\abeta+360:\blochsphere@radius+\the\pgflinewidth*0.5)};
559 } \tmp
560 \endgroup
561 }%
562 % \end{macrocode}
563 % \end{macro}
564 % \begin{macro}{\drawGreatCircle}
565 % \begin{macrocode}
566 \newcommand\drawGreatCircle[3][]{
567 \drawCircle[##1]{##2}{##3}
568 }
569 % \end{macrocode}
570 % \end{macro}
571 % \begin{macro}{\drawSmallCircle}
572 % \begin{macrocode}
573 \newcommand\drawSmallCircle[4][]{
574 \begingroup
575 \pgfmathsetmacro\xshift{sin(##2)*cos(##3)*\blochsphere@radius*sin(##4)}
576 \pgfmathsetmacro\yshift{sin(##2)*sin(##3)*\blochsphere@radius*sin(##4)}
577 \pgfmathsetmacro\zshift{cos(##2)*\blochsphere@radius*sin(##4)}
578 \pgfmathsetmacro\radius{\blochsphere@radius*cos(##4)}
579 \drawCircle[##1,shift={{\xshift,\yshift,\zshift}},radius=\radius]{##2}{##3}
580 \endgroup
581 }%
582 % \end{macrocode}
583 % \end{macro}
584 % \begin{macro}{\drawLatitudeCircle}
585 % \begin{macrocode}
586 \newcommand\drawLatitudeCircle[2][]{
587 \begingroup
588 \pgfmathsetmacro\yshift{sin(##2)*\blochsphere@radius}
589 \pgfmathsetmacro\radius{\blochsphere@radius*cos(##2)}
590 \drawCircle[##1,shift={{0,0,\yshift}},radius=\radius]{0}{0}
591 \endgroup
592 }%
593 % \end{macrocode}
594 % \end{macro}
595 % \begin{macro}{\drawLongitudeCircle}
596 % \begin{macrocode}
597 \newcommand\drawLongitudeCircle[2][]{
598 \begingroup
599 \drawCircle[##1]{90}{##2+90}
600 \endgroup
601 }%
602 % \end{macrocode}
603 % \end{macro}
604 % \begin{macro}{\drawRotationLeft}
605 % \begin{macrocode}
606 \newcommand\drawRotationLeft[5][]{
607 \begingroup
608 \setkeys{blochsphere}{##1}
609 \pgfmathsetmacro\xshift{sin(##2)*cos(##3)*##4}
610 \pgfmathsetmacro\yshift{sin(##2)*sin(##3)*##4}
611 \pgfmathsetmacro\zshift{cos(##2)*##4}
612 \setDrawingPlane[shift={\xshift,\yshift,\zshift}]{##2}{##3}
613 \computeVisibility{##2}{##3}{agamma}{abeta}
614 \begingroup\edef\tmp{\endgroup%
615 \noexpand\draw[current plane,on layer=back,\unexpanded\expandafter{\blochsphere@style}] \unexpanded\expandafter{(\agamma-\abeta-0.2:\blochsphere@radius*\blochsphere@scale+\the\pgflinewidth*0.5) arc (\agamma-\abeta-0.2:\agamma+\abeta+0.2:\blochsphere@radius*\blochsphere@scale+\the\pgflinewidth*0.5)};
616 \noexpand\draw[<-,current plane,on layer=front,\unexpanded\expandafter{\blochsphere@style}] \unexpanded\expandafter{(\agamma+\abeta+##5:\blochsphere@radius*\blochsphere@scale+\the\pgflinewidth*0.5) arc (\agamma+\abeta+##5:\agamma-\abeta+360:\blochsphere@radius*\blochsphere@scale+\the\pgflinewidth*0.5)};
617 } \tmp
618 \endgroup
619 }%
620 % \end{macrocode}
621 % \end{macro}
622 % \begin{macro}{\drawRotationRight}
623 % \begin{macrocode}
624 \newcommand\drawRotationRight[5][]{
625 \begingroup
626 \setkeys{blochsphere}{##1}
627 \pgfmathsetmacro\xshift{sin(##2)*cos(##3)*##4}
628 \pgfmathsetmacro\yshift{sin(##2)*sin(##3)*##4}
629 \pgfmathsetmacro\zshift{cos(##2)*##4}
630 \setDrawingPlane[shift={\xshift,\yshift,\zshift}]{##2}{##3}
631 \computeVisibility{##2}{##3}{agamma}{abeta}
632 \begingroup\edef\tmp{\endgroup%
633 \noexpand\draw[current plane,on layer=back,\unexpanded\expandafter{\blochsphere@style}] \unexpanded\expandafter{(\agamma-\abeta-0.2:\blochsphere@radius*\blochsphere@scale+\the\pgflinewidth*0.5) arc (\agamma-\abeta-0.2:\agamma+\abeta+0.2:\blochsphere@radius*\blochsphere@scale+\the\pgflinewidth*0.5)};
634 \noexpand\draw[->,current plane,on layer=front,\unexpanded\expandafter{\blochsphere@style}] \unexpanded\expandafter{(\agamma+\abeta:\blochsphere@radius*\blochsphere@scale+\the\pgflinewidth*0.5) arc (\agamma+\abeta:\agamma-\abeta+360-##5:\blochsphere@radius*\blochsphere@scale+\the\pgflinewidth*0.5)};
635 } \tmp
636 \endgroup
637 }%
638 % \end{macrocode}
639 % \end{macro}
640 % \begin{macro}{\drawAxis}
641 % \begin{macrocode}
642 \newcommand\drawAxis[3][] {
643 \begingroup
644 \setkeys{blochsphere}{##1}
645 \pgfmathsetmacro\behind{ifthenelse(-sin(##2)*sin(\blochsphere@rotation - ##3)*cos(\blochsphere@tilt) + sin(\blochsphere@tilt)*cos(##2)<0, 1, 0)}
646 \pgfmathsetmacro\newphi{180-##2}
647 \pgfmathsetmacro\newtheta{##3+180}
648 \ifnum\behind=0\relax
649 \labelPolar[scale=1]{blochspheretmpp}{##2}{##3}
650 \labelPolar{blochspheretmps}{##2}{##3}
651 \labelPolar{blochspheretmpe}{\newphi}{\newtheta}
652 \else
653 \labelPolar[scale=1]{blochspheretmpp}{\newphi}{\newtheta}
654 \labelPolar{blochspheretmpe}{##2}{##3}
655 \labelPolar{blochspheretmps}{\newphi}{\newtheta}
656 \fi
657 \begingroup\edef\tmp{\endgroup%
658 \ifnum\behind=0\relax
659 \noexpand\draw[on layer=front,\unexpanded\expandafter{\blochsphere@style},\blochsphere@axisarrow] (blochspheretmpp) -- (blochspheretmps);
660 \noexpand\draw[on layer=back,\unexpanded\expandafter{\blochsphere@style}] (blochspheretmpe) -- (blochspheretmpp);
661 \else
662 \noexpand\draw[on layer=front,\unexpanded\expandafter{\blochsphere@style},\blochsphere@axisarrow] (blochspheretmpp) -- (blochspheretmpe);
663 \noexpand\draw[on layer=back,\unexpanded\expandafter{\blochsphere@style}] (blochspheretmps) -- (blochspheretmpp);
664 \fi
665 } \tmp
666 \endgroup
667 }%
668 % \end{macrocode}
669 % \end{macro}
670 % \begin{macro}{\labelPolar}
671 % \begin{macrocode}
672 \newcommand\labelPolar[4][]{%
673 \begingroup
674 \setkeys{blochsphere}{##1}
675 \setLongitudinalDrawingPlane{##4+90}
676 \pgfmathsetmacro\behind{ifthenelse(-sin(##3)*sin(\blochsphere@rotation - ##4)*cos(\blochsphere@tilt) + sin(\blochsphere@tilt)*cos(##3)<0, 1, 0)}
677 \ifthenelse{\boolean{blochsphere@labelmark}}
678 {
679 \ifnum\behind=1\relax
680 \path[current plane,on layer=back] (90-##3:\blochsphere@radius*\blochsphere@scale) coordinate[mark coordinate] (##2); %
681 \else
682 \path[current plane,on layer=front] (90-##3:\blochsphere@radius*\blochsphere@scale) coordinate[mark coordinate] (##2); %
683 \fi
684 }
685 {
686 \ifnum\behind=1\relax
687 \path[current plane,on layer=back] (90-##3:\blochsphere@radius*\blochsphere@scale) coordinate (##2); %
688 \else
689 \path[current plane,on layer=front] (90-##3:\blochsphere@radius*\blochsphere@scale) coordinate (##2); %
690 \fi
691 }
692 \endgroup
693 }%
694 % \end{macrocode}
695 % \end{macro}
696 % \begin{macro}{\labelLatLon}
697 % \begin{macrocode}
698 \newcommand\labelLatLon[4][]{%
699 \begingroup
700 \pgfmathsetmacro\newphi{90-##3}
701 \labelPolar[##1]{##2}{\newphi}{##4}
702 \endgroup
703 }%
704 % \end{macrocode}
705 % \end{macro}
706 % \begin{macro}{\drawStateLatLon}
707 % \begin{macrocode}
708 \newcommand\drawStateLatLon[4][]{
709 \begingroup
710 \pgfmathsetmacro\newphi{90-##3}
711 \drawStatePolar[##1]{##2}{\newphi}{##4}
712 \endgroup
713 }%
714 % \end{macrocode}
715 % \end{macro}
716 % \begin{macro}{\drawStatePolar}
717 % \begin{macrocode}
718 \newcommand\drawStatePolar[4][]{
719 \begingroup
720 \setkeys{blochsphere}{##1}
721 \labelPolar{##2}{##3}{##4}
722 \pgfmathsetmacro\behind{ifthenelse(-sin(##3)*sin(\blochsphere@rotation - ##4)*cos(\blochsphere@tilt) + sin(\blochsphere@tilt)*cos(##3)<0, 1, 0)}
723 \ifnum\behind=1\relax
724 \tikzset{test/.style={
725 postaction={
726 decorate,
727 decoration={
728 markings,
729 mark=at position \pgfdecoratedpathlength-0.5pt with {\arrow[\blochsphere@statecolor,line width=\blochsphere@statewidth,opacity=0.2] {>}; },
730 mark=between positions 0 and \pgfdecoratedpathlength-5pt step 0.5pt with {
731 \pgfmathsetmacro\myval{multiply(divide(
732 \pgfkeysvalueof{/pgf/decoration/mark info/distance from start}, \pgfdecoratedpathlength),100)};
733 \pgfsetfillcolor{\blochsphere@statecolor};
734 \pgfsetfillopacity{0.4-\myval/100*0.2}
735 \pgfpathcircle{\pgfpointorigin}{\blochsphere@statewidth};
736 \pgfusepath{fill};}
737 }}}}
738 \else
739 \tikzset{test/.style={
740 postaction={
741 decorate,
742 decoration={
743 markings,
744 mark=at position \pgfdecoratedpathlength-0.5pt with {\arrow[\blochsphere@statecolor,line width=\blochsphere@statewidth] {>}; },
745 mark=between positions 0 and \pgfdecoratedpathlength-5pt step 0.5pt with {
746 \pgfmathsetmacro\myval{multiply(divide(
747 \pgfkeysvalueof{/pgf/decoration/mark info/distance from start}, \pgfdecoratedpathlength),100)};
748 \pgfsetfillcolor{\blochsphere@statecolor};
749 \pgfsetfillopacity{0.4+\myval/100*0.6}
750 \pgfpathcircle{\pgfpointorigin}{\blochsphere@statewidth};
751 \pgfusepath{fill};}
752 }}}}
753 \fi
754 \path [test,on layer=main] (0,0) -- (##2);
755 \endgroup
756 }%
757 % \end{macrocode}
758 % \end{macro}
759 % \begin{macro}{\computeOffset}
760 % \begin{macrocode}
761 \newcommand\computeOffset[2][]{%
762 \setkeys{blochsphere}{##1}%
763 \pgfmathsetmacro{\x}{\blochsphere@shift[0]*cos(\blochsphere@rotation) + \blochsphere@shift[1]*sin(\blochsphere@rotation)}%
764 \pgfmathsetmacro\y{\blochsphere@shift[0]*sin(\blochsphere@rotation)*sin(\blochsphere@tilt) - \blochsphere@shift[1]*sin(\blochsphere@tilt)*cos(\blochsphere@rotation) + \blochsphere@shift[2]*cos(\blochsphere@tilt)}
765 \expandafter\def\csname ##2\endcsname{(\x pt,\y pt)}%
766 }%
767 % \end{macrocode}
768 % \end{macro}
769 % \begin{macro}{\computeVisibility}
770 % \begin{macrocode}
771 \newcommand\computeVisibility[5][]{
772 \setkeys{blochsphere}{##1}
773 \pgfmathsetmacro\aphi{##2}
774 \pgfmathsetmacro\atheta{##3}
775 \pgfmathsetmacro\d{sqrt(\blochsphere@shift[0]^2+\blochsphere@shift[1]^2+\blochsphere@shift[2]^2)}%% distance from origin of shift
776 \pgfmathsetmacro\tatheta{\atheta+\blochsphere@rotation}
777 \pgfmathsetmacro\dot{cos(\aphi)}
778 \ifdim\dot pt<0.7 pt\relax%
779 \pgfmathsetmacro\domaintest{ifthenelse(and(\blochsphere@rotation - \atheta==0,or(\aphi==0,sin(\blochsphere@tilt==0))),0,1)}
780 \ifdim\domaintest pt=0 pt\relax
781 \pgfmathsetmacro\agamma{0}
782 \else
783 \pgfmathsetmacro\agamma{-90+atan(cos(\blochsphere@tilt)*cos(\blochsphere@rotation - \atheta)/(sin(\aphi)*sin(\blochsphere@tilt) + sin(\blochsphere@rotation - \atheta)*cos(\aphi)*cos(\blochsphere@tilt)))}
784 \fi
785 \else
786 \pgfmathsetmacro\domaintest{ifthenelse(and(Mod(\blochsphere@rotation-\atheta,180)==90,Mod(\blochsphere@tilt,180)==90),0,1)}
787 \pgfmathsetmacro\domaintesttwo{ifthenelse(Mod(\blochsphere@rotation-\atheta,180)==90,0,1)}
788 \pgfmathsetmacro\domaintestthree{ifthenelse(Mod(\blochsphere@tilt,180)==90,0,1)}
789 \ifdim\domaintest pt=0 pt\relax
790 \pgfmathsetmacro\agamma{360-90*\blochsphere@tilt/abs(\blochsphere@tilt)}
791 \else
792 \ifdim\domaintesttwo pt=0 pt\relax
793 \pgfmathsetmacro\agamma{360-90*(sin(\aphi)*tan(\blochsphere@tilt)+cos(\aphi)*sin(\blochsphere@rotation-\atheta))/abs(sin(\aphi)*tan(\blochsphere@tilt)+cos(\aphi)*sin(\blochsphere@rotation-\atheta))}
794 \else
795 \ifdim\domaintestthree pt=0 pt\relax
796 \pgfmathsetmacro\agamma{360-90*\blochsphere@tilt/abs(\blochsphere@tilt)*cos(\blochsphere@rotation-\atheta)/abs(cos(\blochsphere@rotation-\atheta))}
797 \else
798 \pgfmathsetmacro\agamma{360-atan( (sin(\aphi)*tan(\blochsphere@tilt)+cos(\aphi)*sin(\blochsphere@rotation-\atheta))/cos(\blochsphere@rotation-\atheta) )}
799 \fi
800 \fi
801 \fi
802 \fi
803 \pgfmathsetmacro\aalpha{acos(-sin(\aphi)*sin(\blochsphere@rotation)*cos(\atheta)*cos(\blochsphere@tilt) + sin(\aphi)*sin(\atheta)*cos(\blochsphere@rotation)*cos(\blochsphere@tilt) + sin(\blochsphere@tilt)*cos(\aphi))}
804 \pgfmathsetmacro\aalphatest{atan(\d/\blochsphere@radius)}
805 \ifdim\aalpha pt<\aalphatest pt\relax
806 \pgfmathsetmacro\abeta{0}
807 \else
808 \pgfmathsetmacro\abeta{acos(\d*cot(\aalpha)/\blochsphere@radius)}
809 \fi
810 \pgfmathsetmacro\abeta{ifthenelse(\blochsphere@shift[2]<0,\abeta+2*(90-\abeta),\abeta)}
811 \expandafter\pgfmathsetmacro\csname ##4\endcsname{\agamma}
812 \expandafter\pgfmathsetmacro\csname ##5\endcsname{\abeta}
813 }%
814 % \end{macrocode}
815 % \end{macro}
816 % \begin{macrocode}
817 \tikzset{%
818 >=latex, % option for nice arrows
819 inner sep=0pt,%
820 outer sep=2pt,%
821 mark coordinate/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,
822 fill=black,circle}%
823 }%
824 \pgfdeclareradialshading[tikz@ball]{ball}{\pgfqpoint{-10bp}{10bp}}{%
825 color(0bp)=(tikz@ball!30!white);
826 color(9bp)=(tikz@ball!75!white);
827 color(18bp)=(tikz@ball!90!black);
828 color(25bp)=(tikz@ball!70!black);
829 color(50bp)=(black)
830 }%
831 \ifthenelse{\boolean{blochsphere@nested}}{}{%
832 \begin{tikzpicture}
833 \drawBall
834 }%
835 }
836 % \end{macrocode}
837 % \end{environment}
838 % \begin{macrocode}
839 {
840 \ifthenelse{\boolean{blochsphere@nested}}{}{
841 \end{tikzpicture}%
842 }%
843 \endgroup%
844 }
845 \endinput
846 % \end{macrocode}
847
848 %
849 % \iffalse
850 %</package>
851 % \fi
852 %
853 % \Finale
854 \endinput