
    z	i/                         S r SSKrSSKJrJrJrJr  SSKrSSK	J
r
  SSKJr  SS jr " S S5      r\" S	S
S9SS j5       rg)z^
Visualization function for animation of state transitions by applying gates to single qubit.
    N)sincosacossqrt)MissingOptionalLibraryError)deprecate_funcc                    ^ [        S U  5       5      n[        US-
  5      U:  a   [        U5      m[        U4S jU  5       5      n [        R
                  " U 5      $ )z<Makes sure magnitude of the vector is 1 with given tolerancec              3   *   #    U  H	  oU-  v   M     g 7fN ).0ns     g/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/visualization/transition_visualization.py	<genexpr>_normalize.<locals>.<genexpr>   s      a1uas   g      ?c              3   ,   >#    U  H	  oT-  v   M     g 7fr   r   )r   r   mags     r   r   r      s     %1ac'1s   )sumabsr   tuplenparray)v	tolerancemag2r   s      @r   
_normalizer      sL      a  D
4#:"4j%1%%88A;    c                   t    \ rS rSrSrS r\S 5       r\S 5       rS r	S r
S rS	 rS
 rS rS rS rS rSrg)_Quaternion"   z&For calculating vectors on unit spherec                     S U l         g r   _valselfs    r   __init___Quaternion.__init__%   s	    	r   c                 R    [        U5      n[        5       nUR                  X5        U$ )zCreate quaternion from axis)r   r   _axisangle_to_q)thetar   new_quaternions      r   from_axisangle_Quaternion.from_axisangle(   s(     qM$&&u0r   c                 &    [        5       nXl        U$ )zCreate quaternion from vector)r   r#   )valuer+   s     r   
from_value_Quaternion.from_value1   s     %#r   c                     US   nUS   nUS   n[        US-  5      nU[        US-  5      -  nU[        US-  5      -  nU[        US-  5      -  n[        R                  " XcXE/5      U l        g)z$Convert axis and angle to quaternionr                @N)r   r   r   r   r#   )r%   r*   r   xyzws          r   r)   _Quaternion._axisangle_to_q8   sx    aDaDaDECK  ECK  ECK  HHaA\*	r   c                    [        U[        5      (       a  U R                  U5      $ [        U[        [        [
        R                  45      (       a7  [        U5      S:w  a  [        S[        U5       35      eU R                  U5      $ [        $ )z6Multiplication of quaternion with quaternion or vector   z Input vector has invalid length )
isinstancer   _multiply_with_quaternionlistr   r   ndarraylen
ValueError_multiply_with_vectorNotImplemented)r%   bs     r   __mul___Quaternion.__mul__E   ss     a%%11!44D%4551v{ #CCF8!LMM--a00!!r   c                 0   U R                   u  p#pEUR                   u  pgpX&-  X7-  -
  XH-  -
  XY-  -
  n
X'-  X6-  -   XI-  -   XX-  -
  nX(-  XF-  -   XW-  -   X9-  -
  nX)-  XV-  -   X8-  -   XG-  -
  n[        R                  [        R                  " XX45      5      nU$ )z,Multiplication of quaternion with quaternionr#   r   r0   r   r   )r%   q_2w_1x_1y_1z_1w_2x_2y_2z_2r9   r6   r7   r8   results                  r   r>   %_Quaternion._multiply_with_quaternionQ   s    !YY# XX#I	!CI-	9I	!CI-	9I	!CI-	9I	!CI-	9''!(>?r   c                     [         R                  [        R                  " SU5      5      nX-  U R	                  5       -  R
                  SS $ )z(Multiplication of quaternion with vectorg        r3   N)r   r0   r   appendget_conjugater#   )r%   r   rJ   s      r   rC   !_Quaternion._multiply_with_vector]   s@    $$RYYa%89
T//1177;;r   c                     U R                   u  pp4[        R                  [        R                  " X* U* U* 45      5      nU$ )zConjugation of quaternionrI   )r%   r9   r6   r7   r8   rS   s         r   rW   _Quaternion.get_conjugateb   s9    YY
a''!R!aR(ABr   c           	      V    U R                  5       u  pSU SUS    SUS    SUS    S3	$ )Nz((z; r   z, r3   r4   z)))get_axisangle)r%   r*   r   s      r   __repr___Quaternion.__repr__h   s?    %%'E7"QqTF"QqTF"QqTF"55r   c                 r    U R                   S   U R                   SS p![        U5      S-  nU[        U5      4$ )z&Returns angle and vector of quaternionr   r3   Nr5   )r#   r   r   )r%   r9   r   r*   s       r   r\   _Quaternion.get_axisanglel   s7    yy|TYYqr]1Q#jm##r   c                 6    U R                   R                  5       $ )zConverts quaternion to a list)r#   tolistr$   s    r   rb   _Quaternion.tolists   s    yy!!r   c                 d    U R                  5       u  p[        R                  R                  U5      $ )zCalculates norm of quaternion)r\   r   linalgnorm)r%   _r   s      r   vector_norm_Quaternion.vector_normw   s%    !!#yy~~a  r   r"   N)__name__
__module____qualname____firstlineno____doc__r&   staticmethodr,   r0   r)   rF   r>   rC   rW   r]   r\   rb   rh   __static_attributes__r   r   r   r   r   "   s_    0    +
"
<
6$"!r   r   z1.2.0zin the 2.0 release)sinceremoval_timelinec           
        ^^^^^^  SSK Jn  Sn SSKnSSKJn  SSKJn	  SSKJn
  S	S
KJ	n  S	SK
Jn  SnSnS[        R                  ;   a  S[        R                  ;  a  SnU(       d  [        SSSS9eU(       d  USL a  [        SSSS9e[        U R                   5      S	:w  a  W" S5      eUmUS-  U-  n0 nS["        R%                  [&        R(                  T-  / SQ5      S4US'   S["        R%                  [&        R(                  T-  / SQ5      S4US'   S["        R%                  [&        R(                  T-  / SQ5      S4US'   S["        R%                  [&        R(                  S -  T-  / SQ5      S!4US'   S"["        R%                  [&        R(                  * S -  T-  / SQ5      S#4US"'   S$["        R%                  [&        R(                  T-  [+        / S%Q5      5      S&4US$'   S'["        R%                  [&        R(                  S(-  T-  / SQ5      S)4US''   S*["        R%                  [&        R(                  * S(-  T-  / SQ5      S+4US*'   / mU R,                   GH  u  n  nUR.                  S,:X  a  M  UR.                  U;   a   TR1                  UUR.                     5        MJ  UR.                  S-:X  aP  UR2                  S   n["        R%                  UT-  / SQ5      nTR1                  UR.                   S.US/ 3US045        M  UR.                  S1:X  aQ  UR2                  S   n["        R%                  UT-  / SQ5      nTR1                  UR.                   S.US/ 3US245        GM  UR.                  S3:X  aQ  UR2                  S   n["        R%                  UT-  / SQ5      nTR1                  UR.                   S.US/ 3US445        GMl  UR.                  S5:X  aQ  UR2                  S   n["        R%                  UT-  / SQ5      nTR1                  UR.                   S.US/ 3US645        GM  W" S7UR.                   S835      e   [        T5      S:X  a  W" S95      e[+        [&        R4                  " / SQ5      5      nWR7                  S:S;9n[9        S< WR:                  R=                  S=5       5       5      S>:  a  W
" USS?9mUR?                  T5        OW
" U5      mTRA                  S@SA5        TRC                  S@SA5        W" TSB9m " SC SD5      nU" 5       mUTl"        UUUUUU4SE jnUU4SF jnW	RG                  UU[I        T[        T5      -  S	-   5      USSUSG9nU(       a  URK                  USHSI9  U(       a6  SJURL                  SK'   URO                  U5        W" URQ                  5       5      $ URS                  5         URO                  U5        g! [         a    Sn GNf = f! [         a    Sn GNf = f)La  
Creates animation showing transitions between states of a single
qubit by applying quantum gates.

Args:
    circuit (QuantumCircuit): Qiskit single-qubit QuantumCircuit. Gates supported are
        h,x, y, z, rx, ry, rz, s, sdg, t, tdg and u1.
    trace (bool): Controls whether to display tracing vectors - history of 10 past vectors
        at each step of the animation.
    saveas (str): User can choose to save the animation as a video to their filesystem.
        This argument is a string of path with filename and extension (e.g. "movie.mp4" to
        save the video in current working directory).
    fpg (int): Frames per gate. Finer control over animation smoothness and computational
        needs to render the animation. Works well for tkinter GUI as it is, for jupyter GUI
        it might be preferable to choose fpg between 5-30.
    spg (int): Seconds per gate. How many seconds should animation of individual gate
        transitions take.

Returns:
    IPython.core.display.HTML:
        If arg jupyter is set to True. Otherwise opens tkinter GUI and returns
        after the GUI is closed.

Raises:
    MissingOptionalLibraryError: Must have Matplotlib (and/or IPython) installed.
    VisualizationError: Given gate(s) are not supported.

r   )HTMLTFN)pyplot)	animation)Axes3Dr3   )Bloch)VisualizationError	ipykernelspyder
Matplotlibvisualize_transitionzpip install matplotlib)libnamenamepip_installIPythonzpip install ipythonz%Only one qubit circuits are supportedi  r6   )r3   r   r   z#1abc9cr7   )r   r3   r   z#2ecc71r8   )r   r   r3   z#3498dbsr4   z#9b59b6sdgz#8e44adh)r3   r   r3   z#34495et   z#e74c3ctdgz#c0392bbarrierrxz: z.2fz#16a085ryz#27ae60rzz#2980b9u1z#f1c40fzGate z is not supportedzNothing to visualize.)   r   )figsizec              3   8   #    U  H  n[        U5      v   M     g 7fr   )int)r   r6   s     r   r   'visualize_transition.<locals>.<genexpr>  s     ?>SVV>s   .)r<   r   r   )auto_add_to_figurei
   )axesc                       \ rS rSrSrS rSrg)'visualize_transition.<locals>.Namespacei  z'Helper class serving as scope containerc                 <    / U l         SU l        / U l        / U l        g )N)new_vec	last_gatecolorspntsr$   s    r   r&   0visualize_transition.<locals>.Namespace.__init__  s    DLDNDKDIr   )r   r   r   r   N)rj   rk   rl   rm   rn   r&   rp   r   r   r   	Namespacer     s
    5	r   r   c                 0  > T	R                  5         U S-
  T-  nUTR                  :w  a?  TR                  R                  / / / /5        TR                  R                  TU   S   5        U S:X  a  T	R                  TR                  5        TR                  S   S   R                  TR                  S   5        TR                  S   S   R                  TR                  S   5        TR                  S   S   R                  TR                  S   5        STR                  S'   T	R                  5         T$ TU   S   TR                  -  Tl        TR                  US-      S   R                  TR                  S   5        TR                  US-      S   R                  TR                  S   5        TR                  US-      S   R                  TR                  S   5        T	R                  TR                  5        T
(       a0  TR                   H   nT	R                  US   US   US   /5        M"     TU   S   /T	l	        TR                  T	l
        ST	l        TU   S   n/ SQnT	R                  UUTU   S   SSS	9  T	R                  5         UTl        T$ )
Nr3   r4   r   ro)gffffff?gܿgffffff?   left)colorfontsizehorizontalalignment)clearr   r   rV   r   add_vectorsr   make_sphere
add_pointsvector_colorpoint_colorpoint_markeradd_annotation)igate_counter	point_setannotation_textannotationvector_axframes_per_gatelist_of_circuit_gates	namespacespheretraces        r   animate%visualize_transition.<locals>.animate(  s    A/19...NN!!2r2,/##$9,$G$JK 6y001NN1a ''	(9(9!(<=NN1a ''	(9(9!(<=NN1a ''	(9(9!(<="%IQ J1,?BYEVEVV	|a'(+2293D3DQ3GH|a'(+2293D3DQ3GH|a'(+2293D3DQ3GH9,,-&^^	!!9Q<1y|"LM ,  5\B1EF&--!/=a@.'5a8 & 	 	
 	*	
r   c                     > S/Tl         T $ )Nr   )r   )r   r   s   r   init"visualize_transition.<locals>.initZ  s    "e
r   )	init_funcblitrepeatintervalr   )fps2   zanimation.embed_limit)*IPython.displayrt   ImportError
matplotlibru   rv   mpl_toolkits.mplot3drw   blochrx   
exceptionsry   sysmodulesr   rA   qubitsr   r,   r   pir   _datar   rV   paramsr   figurer   __version__splitadd_axesset_xlimset_ylimr   FuncAnimationrangesavercParamsclose	to_jshtmlshow) circuitr   saveasfpgspgrt   has_ipythonr   pltrv   rw   rx   ry   has_matplotlibjupytertime_between_framessimple_gatesgaterg   r*   
quaternionstarting_posfigr   r   r   anir   r   r   r   r   s     `                         @@@@@r   r}   r}   }   s   B(
,(/ 2 Gs{{")D) '0
 	

 7d?)'-
 	

 7>>a !HIIO:, L""255?#:IFL 	""255?#:IFL 	""255?#:IFL 	""25519#>	JL 	""BEE6A:#?KL 	""255?#:Jy<QRL 	""25519#>	JL 	""BEE6A:#?KL mm
a99	!99$!((dii)@AYY$KKNE$33EO4KYWJ!((TYYKr%*EzS\)]^YY$KKNE$33EO4KYWJ!((TYYKr%*EzS\)]^YY$KKNE$33EO4KYWJ!((TYYKr%*EzS\)]^YY$KKNE$33EO4KYWJ!((TYYKr%*EzS\)]^$uTYYK7H%IJJ- $0  !Q& !899bhhy12L
**V*
$C?Z3399#>??9LSU3SSkLLbLLb_F  I$I0 0d 
!
!o$9 ::Q>?$ " C R 79
34		#CMMO$$HHJIIcN_    s"   W $W WWW*)W*)gh㈵>)FNd   r4   )rn   r   mathr   r   r   r   numpyr   qiskit.exceptionsr   qiskit.utils.deprecationr   r   r   r}   r   r   r   <module>r      sQ     % %  9 3X! X!v 
)p	pr   