
    z	i@,                        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	r
SSKJr  SSKJrJrJr  SSKJr  S	S
KJr  S	SKJrJr  S	SKJrJrJrJrJr  S rS rS r g)ak  
Tools for building optimal circuits out of XX interactions.

Inputs:
 + A set of native XX operations, described as strengths.
 + A right-angled path, computed using the methods in `paths.py`.

Output:
 + A circuit which implements the target operation (expressed exactly as the exponential of
 `a XX + b YY + c ZZ`) using the native operations and local gates.
    )annotationsN)reduce)
itemgetter)QuantumCircuit)RXXGateRYYGateRZGate)QiskitError   )decomposition_hop)EPSILONsafe_arccos)apply_reflectionapply_shiftcanonical_rotation_circuitreflection_optionsshift_optionsc                t   [         R                  " X#-   5      [         R                  " X#-
  5      pe[         R                  " X#-   5      [         R                  " X#-
  5      p[        R                  " U5      [        R                  " U5      pS[	        US-  U	S-  -  US-  U
S-  -  -   [        R                  " X-
  5      S-  -
  SU-  U	-  U-  U
-  5      -  nS[	        US-  U	S-  -  US-  U
S-  -  -   [        R                  " X-   5      S-  -
  SU-  U	-  U-  U
-  5      -  nX-   S-  X-
  S-  p[        [        R                  [        SU-  5      R                  5       [        SU-  5      R                  5       -  [        R                  " [        SU-  5      R                  5       [        SU-  5      R                  5       5      [        SU-  5      R                  5       /5      n[        R                  " / SQ/ SQ/ SQ/ SQ/5      n[        R                  " US   5      [        R                  " US   5      [        R                  " US	   5      [        R                  S-  -   [        R                  " US
   5      [        R                  S-  -   /n[        R                  " UU5      u  nnnn[        [        R                  [        R                  " [        SU-  5      R                  5       [        SU-  5      R                  5       5      U[        R                  " [        SU-  5      R                  5       [        SU-  5      R                  5       5      /5      n[!        [        R                  " US   5      [        R                  S-  -
  5      S:  a  X:  d?  [!        [        R                  " US   5      [        R                  S-  -   5      S:  a]  X:  aX  U[        R                  S-  -  nU[        R                  S-  -  nU[        R                  S-  -  nU[        R                  S-  -  nUUXUU4$ )a)  
Consumes a target canonical interaction CAN(a_target, b_target) and source interactions
CAN(a1, b1), CAN(a2), then manufactures a circuit identity of the form

CAN(a_target, b_target) = (Zr, Zs) CAN(a_source, b_source) (Zu, Zv) CAN(interaction) (Zx, Zy).

Returns the 6-tuple (r, s, u, v, x, y).
g      ?   )      ?r   r   r   )r         пr   r   )r   r   r   r   )r   r   r   r   r   r   r   r   r   r   )r      )r   r   g{Gz?   )mathcossinnpr   r   dotr   	to_matrixr   kronr	   arraycmathphasepiabs)a_targetb_targeta_sourceb_sourceinteractioncpluscminussplussminuscasauplusvuminusvuvmiddle_matrixphase_solverinner_phasesrsxygenerated_matrixs                          j/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/synthesis/two_qubit/xx_decompose/circuits.pydecompose_xxyy_into_xxyy_xxrA   1   s    HHX01488H<O3P6HHX01488H<O3P6VVK "&&"5	
AIA	BE 11BFF8;N4OST4TTJOf$r)
	
 	
1Hr1uuax"a%//"&&9L2MQR2RRINU"R'
	
  !F$4#9q 
AL!++-H0E0O0O0QQGGF1q5M++-va!e}/F/F/HIAO$..0	
M 88'	
L: 	M$'(M$'(M$'(255194M$'(255194	L l3JAq!Q 
GGF1q5M++-va!e}/F/F/HIGGF1q5M++-va!e}/F/F/HI	
 	EKK(./"%%!);<tCH[EKK(./"%%!);<tCH[	RUUQY	RUUQY	RUUQY	RUUQYaq!    c                  ^^ Su  pE[          GH  n[        X`5      u  pxn	[         GHn  n
[        X5      u  pnSu  mmS H  u  p[	        [
        R                  " [	        X   X/   -
  5      [
        R                  5      5      [        :  dY  [	        [
        R                  " [	        X   X/   -
  5      [
        R                  5      [
        R                  -
  5      [        :  d  M  Xsmm  O   Tc  M  U4S jS 5       u  nnU4S jS 5       u  nn[        [        UU   5      [        UU   5      [        UU   5      [        UU   5      [        U5      5      u  nnnnnn[        S UUUUUU4 5       5      (       a  GMW  [        UU5      n[        UU5      n  O   Uc  GM    O   Uc  [        SU  SU S	U S
35      e[        S5      [        S5      nnUR                  UR!                  5       SS9  UR#                  SW-  S/5        UR#                  SW-  S/5        UR                  USS9  UR#                  SW-  S/5        UR#                  SW-  S/5        UR                  USS9  UR                  WSS9  U=R$                  [&        R(                  " W	5      -  sl        U=R$                  [&        R(                  " W5      -  sl        UR                  UR!                  5       SS9  UR                  WSS9  UR                  UR!                  5       SS9  UR#                  SW-  S/5        UR#                  SW-  S/5        UR                  USS9  UUS.$ )ac  
Builds a single step in an XX-based circuit.

`source` and `target` are positive canonical coordinates; `strength` is the interaction strength
at this step in the circuit as a canonical coordinate (so that CX = RZX(pi/2) corresponds to
pi/4); and `embodiment` is a Qiskit circuit which enacts the canonical gate of the prescribed
interaction `strength`.
)NN)	r   )r   r   )r   r   )r   r   r   r   )r   r   )r   r   )r   r   c              3  6   >#    U  H  oT:w  d  M
  Uv   M     g 7fN ).0r=   source_shareds     r@   	<genexpr>"xx_circuit_step.<locals>.<genexpr>        *ViCU11i   		)r   r   r   c              3  6   >#    U  H  oT:w  d  M
  Uv   M     g 7frE   rF   )rG   r=   target_shareds     r@   rI   rJ      rK   rL   c              3  N   #    U  H  n[         R                  " U5      v   M     g 7frE   )r   isnan)rG   vals     r@   rI   rJ      s     A.@s4::c??.@s   #%zSError during RZX decomposition: Could not find a suitable Weyl reflection to match z to z along .r   Tinplacer   r   )prefix_circuitaffix_circuit)r   r   r   r   r(   r    modr'   r   rA   floatanyr   r
   r   composeinverserzglobal_phaser%   r&   )sourcestrengthtarget
embodimentpermute_source_for_overlappermute_target_for_overlapsource_reflection_namereflected_source_coordsource_reflectionreflection_phase_shiftsource_shift_nameshifted_source_coordsource_shiftshift_phase_shiftijsource_firstsource_secondtarget_firsttarget_secondr;   r<   r6   r7   r=   r>   rU   rV   rH   rN   s                               @@r@   xx_circuit_steprr      s    >H: #5"4L\"M
I3I "/DO!EA 0A
 ,6(M=` s#7#:VY#FGOPSZZ266#&:&=	&I"JBEERUWUZUZZ[ 450M= a $ +Wi*V'L-*Vi*V'L-  ;f\*+f]+,*<89*=9:h Aq!Q1 Aq!Q1a.@AAA *DLR_)`&)CLR_)`&M "/P &1[ #5^ ")##)($vhghZqJ
 	

 %31$5~a7HMN 5==?Na!eaS!a!eaS!:t4a!eaS!a!eaS! 5tD,d;5;;/E#FF5;;/@#AA +335tD,54<<>MQUQC QUQC 4dC,}MMrB   c           	     j   [        U5      S:X  a  [        S5      $ [        S5      [        S5      pC[        U5      S:  ak  [        X5      nUS   n[        SS5      " [	        XVS-  XU   5      5      u  pxUR                  USS9  UR                  USSS	9  XQS
S p[        U5      S:  a  Mk  Un	U S   [        R                  S-  ::  a  U	R                  X!S      SS9  O[        S/ SQ5      u  pn[        S/ SQ5      u  pnU	R                  USS9  U	R                  X!S      SS9  U	R                  UR                  5       SS9  U	R                  USS9  U	=R                  [        R                  " U5      -  sl        U	=R                  [        R                  " U5      -  sl        U	R                  USS9  U	$ )a#  
Assembles a Qiskit circuit from a specified `strength_sequence` of XX-type interactions which
emulates the canonical gate at canonical coordinate `target`.  The circuits supplied by
`basis_embodiments` are used to instantiate the individual XX actions.

NOTE: The elements of `strength_sequence` are expected to be normalized so that np.pi/2
    corresponds to RZX(np.pi/2) = CX; `target` is taken to be a positive canonical coordinate;
    and `basis_embodiments` maps `strength_sequence` elements to circuits which instantiate
    these gates.
r   r   r   rU   rV   TrS   )rT   frontNr   zreflect XX, YY)r   r   r   zX shift)lenr   r   r   rr   rZ   r    r'   r   r   r[   r]   r%   r&   )r`   strength_sequencebasis_embodimentsrU   rV   r^   r_   preceding_prefix_circuitpreceding_affix_circuitcircuit_rf   rg   rj   rk   s                  r@   canonical_xx_circuitr}      s    "a   %31$5~a7HM

 1
$"6=$R(<Fo=

&Q,(8S
T=V9  	7F5t4P$*cr,B! 
 1
$ G ayBEEAI)A*>?N7GHXZc7d44-8I-N**)48)A*>?N)113TBd3,= >>,B CCOOM4O0NrB   )!__doc__
__future__r   r%   	functoolsr   r   operatorr   numpyr    qiskit.circuit.quantumcircuitr   %qiskit.circuit.library.standard_gatesr   r   r	   qiskit.exceptionsr
   pathsr   	utilitiesr   r   weylr   r   r   r   r   rA   rr   r}   rF   rB   r@   <module>r      sN   
 #      8 J J ) $ + `FeNP0rB   