
    z	i0                    p    S r SSKJr  SSKrSSKJr  SSKrSSKJr  SSKJ	r	  SSK
rSSKJr   " S S	5      rg)
z
SpecialPolynomial class.
    )annotationsN)combinations)reduce)mul)QiskitErrorc                  j    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS r\S 5       rS rS rSrg)SpecialPolynomial   zdMultivariate polynomial with special form.

Maximum degree 3, n Z_2 variables, coefficients in Z_8.
c                   US:  a  [        S5      eXl        [        XS-
  -  S-  5      U l        [        XS-
  -  US-
  -  S-  5      U l        SU l        [        R                  " U[        R                  S9U l	        [        R                  " U R                  [        R                  S9U l
        [        R                  " U R                  [        R                  S9U l        g)z2Construct the zero polynomial on n_vars variables.   z*n_vars for SpecialPolynomial is too small.      r   )dtypeN)r   n_varsintnc2nc3weight_0npzerosint8weight_1weight_2weight_3)selfr   s     k/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/quantum_info/operators/dihedral/polynomial.py__init__SpecialPolynomial.__init__!   s     A:JKKv!,q01v!,
;a?@rww799    c                   [        U5      nUS:  a  [        S5      e[        R                  " U5      nUS:  R	                  5       (       a,  X0R
                  :  R	                  5       (       a  [        S5      eUS:  a7  [        R                  " U5      S:*  R	                  5       (       a  [        S5      e[        U R
                  5      nUS:X  a  [        R                  " U 5      nU$ / /n[        [        [        U R
                  5      SS95      n[        [        [        U R
                  5      SS95      n[        [        [        U R
                  5      S	S95      nXV-   U-   U-    Hf  n	U R                  U	5      n
[        [        U	5      R                  [        U5      5      5      nUR!                  XR                  U5      U
-   S
-  5        Mh     U$ )z?Multiply by a monomial given by indices.

Returns the product.
   z-There is no term with on more than 3 indices.r   Indices are out of bounds.r   zIndices are non-increasing!rr         )lenr   r   arrayanyr   diffr	   copydeepcopylistr   rangeget_termsetunionset_term)r   indiceslengthindices_arrresultterms0terms1terms2terms3termvaluenew_terms               r   mul_monomialSpecialPolynomial.mul_monomial2   sx   
 WQ;MNNhhw'!O  ""kk(A'F'F'H'H:;;A:277;/1499;;;<<"4;;/Q;]]4(F  TF,uT[['9Q?@F,uT[['9Q?@F,uT[['9Q?@F&069d+D	G =>??8+Du+LPQ*QR : r   c                <   [        U[        5      (       d  [        U5      n[        U R                  5      n[        U[        5      (       a^  U R                  U-  S-  Ul        U R
                  U-  S-  Ul        U R                  U-  S-  Ul        U R                  U-  S-  Ul        U$ U R                  UR                  :w  a  [        S5      e/ /n[        [        [        U R                  5      SS95      n[        [        [        U R                  5      SS95      n[        [        [        U R                  5      SS95      nX4-   U-   U-    HK  nUR                  U5      nUS:w  d  M  [        R                  " U 5      n	U	R                  U5      n	X-  n	X)-   nMM     U$ )zMultiply two polynomials.r&   z#Multiplication on different n_vars.r   r#   r   r%   r   )
isinstancer	   r   r   r   r   r   r   r   r-   r   r.   r/   r+   r,   r>   )
r   otherr6   r7   r8   r9   r:   r;   r<   temps
             r   __mul__SpecialPolynomial.__mul__M   s`   %!233JE"4;;/eS!!#}}u49FO#}}u49FO#}}u49FO#}}u49FO  {{ell*!"GHHTF,uT[['9Q?@F,uT[['9Q?@F,uT[['9Q?@F&069t,A:==.D,,T2D<D#]F : r   c                $    U R                  U5      $ )z6Right multiplication.

This operation is commutative.
)rD   )r   rB   s     r   __rmul__SpecialPolynomial.__rmul__g   s    
 ||E""r   c                   [        U[        5      (       d  [        S5      eU R                  UR                  :w  a  [        S5      e[        U R                  5      nU R                  UR                  -   S-  Ul        U R
                  UR
                  -   S-  Ul        U R                  UR                  -   S-  Ul        U R                  UR                  -   S-  Ul        U$ )zAdd two polynomials.z*Element to add is not a SpecialPolynomial.zAddition on different n_vars.r&   )rA   r	   r   r   r   r   r   r   )r   rB   r6   s      r   __add__SpecialPolynomial.__add__n   s    %!233JKK;;%,,&=>>"4;;/==5>>9Q>==5>>9Q>==5>>9Q>==5>>9Q>r   c           	        [        U5      U R                  :w  a  [        S5      eU Vs/ s H  n[        U[        5      PM     nnU Vs/ s H  n[        U[
        5      PM     nnSU;   a  SU;   a  [        S5      eSU;  nU(       d9  SU Vs/ s H  ofR                  U R                  :H  PM     sn;   a  [        S5      eOUS-  n/ /n[        [        [        U R                  5      SS95      n[        [        [        U R                  5      SS95      n	[        [        [        U R                  5      SS95      n
U(       a  S	nSnO1[        U R                  5      n[        U R                  5      nSUl	        Xx-   U	-   U
-    HG  nU R                  U5      nUS	:w  d  M  [        [        U Vs/ s H  oU   PM	     snU5      nXU-  -   nMI     [        U[        5      (       a  US
-  nU$ s  snf s  snf s  snf s  snf )zEvaluate the multinomial at xval.

if xval is a length n z2 vector, return element of Z8.
if xval is a length n vector of multinomials, return
a multinomial. The multinomials must all be on n vars.
z&Evaluate on wrong number of variables.FzEvaluate on a wrong type.z%Evaluate on incompatible polynomials.r   r   r#   r%   r   r&   )r'   r   r   rA   r   r	   r-   r   r.   r   r/   r   r   )r   xvalx	check_int
check_polyis_intir7   r8   r9   r:   r6   startr;   r<   jnewterms                    r   evaluateSpecialPolynomial.evaluate{   s    t9#FGG156AZ3'	6@DE1j$56
EI%:"59::i'$?$QT[[0$??!"IJJ @ !8Dl5#5;<l5#5;<l5#5;<FE&t{{3F%dkk2EENOf,v5DMM$'Ez &=1Aw&=uE'/1	 6
 fc""aZF? 7E
 @* '>s   G)	G."G3,G8c                   [         R                  " U5      nUS:  R                  5       (       d!  X R                  :  R                  5       (       a  [	        S5      e[        U5      n[        R                  " US5      n[        R                  " US5      nSU l        [         R                  " U R                  5      U l
        [         R                  " U R                  5      U l        [         R                  " U R                  5      U l        U H  nU R                  U/S5        M     U H  nU R                  [!        U5      S5        M      U H  nU R                  [!        U5      S5        M      g)	zoSet to special form polynomial on subset of variables.

p_J(x) := sum_{a subseteq J,|a| neq 0} (-2)^{|a|-1}x^a
r   r"   r   r%   r   r   r!   N)r   r(   r)   r   r   sorted	itertoolsr   r   r   r   r   r   r   r   r2   r-   )r   r3   r5   	subsets_2	subsets_3rT   s         r   set_pjSpecialPolynomial.set_pj   s   
 hhw'!O  ""{kk'A&F&F&H&H:;;/**7A6	**7A6	-**AMM1#q! AMM$q'1% AMM$q'1% r   c                   [        U5      nUS:  a  g[        R                  " U5      nUS:  R                  5       (       d!  X0R                  :  R                  5       (       a  [        S5      eUS:  a7  [        R                  " U5      S:*  R                  5       (       a  [        S5      eUS:X  a  U R                  $ US:X  a  U R                  US      $ US:X  aU  [        US   U R                  -  US   S-   US   -  S-  -
  5      n[        US   US   -
  S-
  5      nU R                  XE-      $ U R                  US   -
  n[        US-
  US-
  -  US-
  -  S-  5      nU R                  US   -
  n[        US-
  US-
  -  S-  5      nU R                  US   -
  n[        U R                  U R                  S-
  -  U R                  S-
  -  S-  U-
  U-
  U-
  5      n	U R                  U	   $ )	a  Get the value of a term given the list of variables.

Example: indices = [] returns the constant
         indices = [0] returns the coefficient of x_0
         indices = [0,3] returns the coefficient of x_0x_3
         indices = [0,1,3] returns the coefficient of x_0x_1x_3

If len(indices) > 3 the method fails.
If the indices are out of bounds the method fails.
If the indices are not increasing the method fails.
r!   r   r"   r   Indices are non-increasing.r   r%   r   r'   r   r(   r)   r   r   r*   r   r   r   r   r   )
r   r3   r4   r5   offset_1offset_2tmp_1tmp_2offset_3offsets
             r   r/   SpecialPolynomial.get_term   s    WQ;hhw'!O  ""{kk'A&F&F&H&H:;;A:277;/1499;;;<<Q;== Q;==,,Q;71:3
Q'RS*7TXY6YYZH71:
2Q67H==!455 gaj(	eai0EAI>BCgaj(	eai0145;;+KK4;;?+t{{Q?!ChNQYY\dd
 }}V$$r   c                   [        U5      nUS:  a  g[        R                  " U5      nUS:  R                  5       (       d!  X@R                  :  R                  5       (       a  [        S5      eUS:  a7  [        R                  " U5      S:*  R                  5       (       a  [        S5      eUS-  nUS:X  a  X l        gUS:X  a  X R                  US   '   gUS:X  aU  [        US   U R                  -  US   S-   US   -  S-  -
  5      n[        US   US   -
  S-
  5      nX R                  XV-   '   gU R                  US   -
  n[        US	-
  US-
  -  US-
  -  S
-  5      nU R                  US   -
  n[        US-
  US-
  -  S-  5      nU R                  US   -
  n	[        U R                  U R                  S-
  -  U R                  S-
  -  S
-  U-
  U-
  U	-
  5      n
X R                  U
'   g)a  Set the value of a term given the list of variables.

Example: indices = [] returns the constant
         indices = [0] returns the coefficient of x_0
         indices = [0,3] returns the coefficient of x_0x_3
         indices = [0,1,3] returns the coefficient of x_0x_1x_3

If len(indices) > 3 the method fails.
If the indices are out of bounds the method fails.
If the indices are not increasing the method fails.
The value is reduced modulo 8.
r!   Nr   r"   r   r`   r&   r   r%   r   ra   )r   r3   r<   r4   r5   rb   rc   rd   re   rf   rg   s              r   r2   SpecialPolynomial.set_term   s    WQ;hhw'!O  ""{kk'A&F&F&H&H:;;A:277;/1499;;;<<	Q;!Mq[(-MM'!*%q[71:3
Q'RS*7TXY6YYZH71:
2Q67H16MM(-.KK'!*,EEAI%!)4	BQFGHKK'!*,EEAI%!)4q89H{{WQZ/Ht{{Q/4;;?CaG F %*MM&!r   c                    U R                   [        U R                  5      [        U R                  5      [        U R                  5      4nU$ )zReturn a string representation.)r   tupler   r   r   )r   tups     r   keySpecialPolynomial.key  s9     }}eDMM2E$--4H%PTP]P]J^_
r   c                b    [        U[        5      =(       a    U R                  UR                  :H  $ )zTest equality.)rA   r	   rn   )r   rN   s     r   __eq__SpecialPolynomial.__eq__  s"    !./EDHH4EEr   c                   [        U R                  5      n[        U R                  5       HJ  nU R	                  U/5      nUS:w  d  M  US-  nUS:w  a  U[        U5      S-   -  nUS[        U5      -   -  nML     [        U R                  S-
  5       Hy  n[        US-   U R                  5       HY  nU R	                  X$/5      nUS:w  d  M  US-  nUS:w  a  U[        U5      S-   -  nUS[        U5      -   S-   [        U5      -   -  nM[     M{     [        U R                  S-
  5       H  n[        US-   U R                  S-
  5       H  n[        US-   U R                  5       Hi  nU R	                  X$U/5      nUS:w  d  M  US-  nUS:w  a  U[        U5      S-   -  nUS[        U5      -   S-   [        U5      -   S-   [        U5      -   -  nMk     M     M     U$ )z'Return formatted string representation.r   z + r   *x_z*x_r   )strr   r.   r   r/   )r   outrR   r<   rT   ks         r   __str__SpecialPolynomial.__str__  s   $-- t{{#AMM1#&EzuA:3u:++Ctc!f}$ $ t{{Q'A1q5$++.qf-A:5LCzs5zC//4#a&=503q699C / ( t{{Q'A1q5$++/2q1udkk2A MM1)4Ezu A:3u:#33Ctc!f}u4s1v=EANN 3 3 ( 
r   )r   r   r   r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r>   rD   rG   rJ   rV   r]   r/   r2   propertyrn   rq   ry   __static_attributes__ r   r   r	   r	      sV    
:"64#(T&,)%V,*\  
Fr   r	   )r   
__future__r   rZ   r   r+   	functoolsr   operatorr   numpyr   qiskit.exceptionsr   r	   r   r   r   <module>r      s0    #  "     )^ ^r   