
    z	i!                       S r SSKJr  SSKJr  SSKJrJr  SSKJr  SSK	r
SSKJr  SS	KJr  \ " S
 S5      5       r\ " S S5      5       rS rSS jr\ " S S5      5       r\
R(                  " / SQ/ SQ/ SQ/ SQ/ SQ/ SQ/ SQ/5      r\R-                  SSS5      r\
R0                  R3                  \5      r\
R(                  " \" \S5       V Vs/ s H  u  p\
R)                  X/\S9PM     snn 5      r\
R0                  R3                  \5      r\
R(                  " \" \S5       V VVs/ s H  u  pn\
R)                  XU/\S9PM     snnn 5      r\
R0                  R3                  \5      rgs  snn f s  snnn f )z
Defines bare dataclasses which house polytope information, as well as a specialized data structure
which describes those two-qubit programs accessible to a given sequence of XX-type interactions.
    )annotations)copy)	dataclassfield)combinationsN)QiskitError   )EPSILONc                  H    \ rS rSr% SrS\S'   \" \S9rS\S'   Sr	S\S	'   S
r
g)ConvexPolytopeData   a6  
The raw data underlying a ConvexPolytope.  Describes a single convex
polytope, specified by families of `inequalities` and `equalities`, each
entry of which respectively corresponds to

    inequalities[j][0] + sum_i inequalities[j][i] * xi >= 0

and

    equalities[j][0] + sum_i equalities[j][i] * xi == 0.
zlist[list[int]]inequalities)default_factory
equalities strname N)__name__
__module____qualname____firstlineno____doc____annotations__r   listr   r   __static_attributes__r       k/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/synthesis/two_qubit/xx_decompose/polytopes.pyr   r      s'    
 "!"'"=J=D#Nr   r   c                  $    \ rS rSr% SrS\S'   Srg)PolytopeData1   z.
The raw data of a union of convex polytopes.
zlist[ConvexPolytopeData]convex_subpolytopesr   N)r   r   r   r   r   r   r   r   r   r   r    r    1   s     21r   r    c                   ^ [        U4S jU R                   5       5      =(       a    [        U4S jU R                   5       5      $ )z+
Tests whether `polytope` contains `point.
c              3  ~   >#    U  H2  n[         * US    [        S [        TUSS 5       5       5      -   :*  v   M4     g7f)r   c              3  .   #    U  H  u  pX-  v   M     g 7fNr   .0xys      r   	<genexpr>1polytope_has_element.<locals>.<genexpr>.<genexpr>?   s     'U:T$!:T   r	   N)r
   sumzip)r(   
inequalitypoints     r   r+   'polytope_has_element.<locals>.<genexpr>>   sB      /J 
JqMC'U#eZPQPR^:T'U$UUU/s   :=c              3     >#    U  H:  n[        US    [        S [        TUSS 5       5       5      -   5      [        :*  v   M<     g7f)r   c              3  .   #    U  H  u  pX-  v   M     g 7fr&   r   r'   s      r   r+   r,   B   s     I0Hae0Hr-   r	   N)absr.   r/   r
   )r(   equalityr1   s     r   r+   r2   A   sC      +H 	HQK#IE8AB<0HIIIJgU+s   AA)allr   r   )polytoper1   s    `r   polytope_has_elementr9   :   sG      "//     ++ r   c           
     x   [         R                  R                  U5      n[        U [        5      (       a  [        U R                  5      nO0[        U [        5      (       a  U /nO[        [        U 5       S35      eUR                  U5        U H  nUR                  UR                  -   n[        U5      S:X  a  M-  [        US   5      S-
  nUR                  U5        [        XV5       H  n[         R                  " U Vs/ s H  oSS PM	     sn5      n	[         R                  " U Vs/ s H  oS   PM	     sn5      n
 [         R                   R#                  U	* 5      U
-  n[%        XK5      (       a  Us  s  $ M     M     [)        SU  35      es  snf s  snf ! [         R                   R&                   a     M  f = f)z1
Returns a single random vertex from `polytope`.
z is not polytope-like.r   r	   Nz$Polytope has no feasible solutions:
)nprandomdefault_rng
isinstancer    r   r"   r   	TypeErrortypeshuffler   r   lenr   arraylinalginvr9   LinAlgErrorr   )r8   seedrng
paragraphsconvex_subpolytope	sentences	dimensionr   r)   matrixbvertexs               r   manual_get_vertexrP   G   s}    ))


%C(L))(667
	H0	1	1Z
4>**@ABBKK
(&336H6S6SS	y>Q	!%)	I(>LXXl;lul;<F51A$56Aw/!3'(:CC!M D ? )  =hZH
II <5
 99(( s   ?F(F>4FF98F9c                  ~    \ rS rSr% SrSrS\S'   SrS\S'   SrS\S'   \	S 5       r
SSS	 jjr\S
 5       rS rS rSrg)
XXPolytopeh   z
Describes those two-qubit programs accessible to a given sequence of XX-type interactions.

NOTE: Strengths are normalized so that CX corresponds to pi / 4, which differs from Qiskit's
      conventions around RZX elsewhere.
        floattotal_strengthmax_strengthplace_strengthc                ^    Su  p#nU H  nX%-  nXS:  a  XSpCM  XT:  d  M  UnM     [        X#US9$ )z8
Constructs an XXPolytope from a sequence of strengths.
)r   r   r   rV   rW   rX   )rR   )cls	strengthsrV   rW   rX   strengths         r   from_strengthsXXPolytope.from_strengthsx   sK    
 8?4n!H&N'/7n+!) " )Uc
 	
r   c                    [        U R                  U-   [        U R                  U5      XR                  :  a  U R                  S9$ XR                  :  a  US9$ U R                  S9$ )z@
Returns a new XXPolytope with one new XX interaction appended.
rZ   )rR   rV   maxrW   rX   )selfnew_strengths     r   add_strengthXXPolytope.add_strength   sy     ..=T..=  "3"33 !!	
 	
 &24G4G%G\
 	
 NRM`M`
 	
r   c           
         [         R                  " SSS[         R                  S-  U R                  U R                  SU R                  -  -
  U R                  U R                  -
  U R
                  -
  /5      $ )u?   
Returns b with A*x + b ≥ 0 iff x belongs to the XXPolytope.
r      )r;   rC   pirV   rW   rX   )rb   s    r   _offsetsXXPolytope._offsets   so    
 xx	####a$*;*;&;;##d&7&77$:M:MM

 
	
r   c                   UR                  5       R                  SS5      nUSS2S4   [        R                  S-  [        -   :  n[        R                  S-  X#S4   -
  X#S4'   UR                  UR
                  5      n[        R                  " U R                  [        R                  " S[        U5      -   [        * :  SS9$ )	z2
Returns True when `point` is a member of `self`.
   Nr      rg   zij,...j->...iaxis)
r   reshaper;   rh   r
   shaper7   ri   einsumA)rb   r1   reflected_pointrowss       r   memberXXPolytope.member   s    
  **,..r15q!t$	G(;;#%5519Qw/G#Ga )11%++>vvMMBIIoq/JJwhV]_
 	
r   c                H   [        U[        R                  5      (       a*  [        UR                  5      S:X  a  UR                  5       nOG[        U[        5      (       a  [        R                  " U5      nO[        SU S[        U5       S35      eUS   [        R                  S-  [        -   :  nU(       a  [        R                  S-  US   -
  US'   U R                  U5      (       a%  U(       a  [        R                  S-  US   -
  US'   U$ U R                  R                  SS5      n[        R                  " S	[         U5      n[        R                  " S
["        XE-   5      U-
  n[        R                  " / [%        U R                  S5      Q5      n[        R                  " S	[&        U5      n[        R                  " S
[(        Xx-   5      U-
  n	[        R                  " / [%        U R                  S5      Q5      n
[        R                  " S
[*        U
5      n[        R,                  " XiU/5      * nXR                  U5         n[        R.                  " [        R0                  R3                  X-
  SS95      nU(       a   [        R                  S-  X   S   -
  X   S'   X   $ )zJ
Finds the nearest point (in Euclidean or infidelity distance) to `self`.
r	   zCan't handle type of point: z ()r   rn   rg      z	ijk,k->ijz
ijk,ik->ijrm   ro   )r>   r;   ndarrayrB   rr   r   r   rC   r?   r@   rh   r
   rw   ri   rq   rs   A1A1invr   A2A2invA3invconcatenateargminrD   norm)rb   r1   y0reflected_pb1A1y0nearest1b2A2y0nearest2b3nearest3nearestsmallest_indexs                 r   r   XXPolytope.nearest   s   " eRZZ((S-=-BBt$$%B:5'DK=PQRSSebeeai'11EEAI1%BqE ;;r??	BqE)1I ]]""1a(yyb"-99\5")<rA XX7T]]A678yyb"-99\5")<rA XX7T]]A67899\5"5 >>8x"@AA++g./299>>',Q>#GH)+W5LQ5O)OG#A&&&r   r   N)rT   )rc   rU   )r   r   r   r   r   rV   r   rW   rX   classmethodr^   rd   propertyri   rw   r   r   r   r   r   rR   rR   h   s\      NEL%NE
 
 
 
 
 
7'r   rR   )r	   rl   r   )r   r	   rl   )r   r   r	   )rl   rl   r   )rl   rl   rl   )r	   rl   rl   )r   r   rl   rl   rm   rg   )dtype)*   ) r   
__future__r   r   dataclassesr   r   	itertoolsr   numpyr;   qiskit.exceptionsr   	utilitiesr
   r   r    r9   rP   rR   rC   rt   rq   r}   rD   pinvr~   rU   r   r   A3r   )r)   r*   zs   000r   <module>r      sq  
 #  ( "  )    $ 2 2 2
JB D' D' D'N HH
 YYr1a
		rXX<1;MN;M!rxxex,;MNO
		rXXaQRASTASIQ1rxxq	x/ASTU
		r	 OTs   E,
(E2