
    z	i                    :   S r SSKJr  SSKrSSKrSSKrSSKrSSKJ	r	  SSK
rSSKJrJrJrJr  SSKJrJr  SSKJr  SSKJrJrJrJr  SS	KJrJr  SS
KJrJ r J!r!J"r"  SSK#J$r$  \	 " S S5      5       r%\	 " S S5      5       r&S r'S r(S/S jr)S r*S r+S r, " S S\RZ                  S   5      r.        S0S jr/S1S jr0S2S jr1S r2S r3S r4S r5S  r6S! r7        S3S" jr8S4S# jr9S5S$ jr:S% r;S& r<S' r=SSSS(.S) jr>SSSS(.S* jr?S+SSS+S,.S- jr@S+SSS+S,.S. jrAg)6zEBinary IO for any value objects, such as numbers, string, parameters.    )annotationsN)	dataclass)CASE_DEFAULTClbitClassicalRegisterDuration)exprtypes)	Parameter)ParameterExpressionParameterValueTypeOpCodeop_code_to_method)ParameterVectorParameterVectorElement)commonformats
exceptions	type_keys)parse_sympy_reprc                  8    \ rS rSr% S\S'   S\S'   SrS\S'   Srg)	_INSTRUCTION'   r   opzParameterValueType | NonelhsNrhs )__name__
__module____qualname____firstlineno____annotations__r   __static_attributes__r       T/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/qpy/binary_io/value.pyr   r   '   s    J	""%)C	")r$   r   c                  B    \ rS rSr% S\S'   \R                  rS\S'   Srg)_SUBS.   dictbindsr   r   r   N)	r   r   r    r!   r"   r   
SUBSTITUTEr   r#   r   r$   r%   r'   r'   .   s    K""B"r$   r'   c                   UR                   R                  [        R                  5      nU R	                  [
        R                  " [        R                  [        U5      UR                  R                  5      5        U R	                  U5        g N)nameencoder   ENCODEwritestructpackr   PARAMETER_PACKlenuuidbytesfile_objobj
name_bytess      r%   _write_parameterr<   4   sO    /JNN6;;w55s:WXNN:r$   c           	     n   UR                   R                  R                  [        R                  5      nU R                  [        R                  " [        R                  [        U5      [        UR                   5      UR                  R                  UR                  5      5        U R                  U5        g r-   )vector_namer/   r   r0   r1   r2   r3   r   PARAMETER_VECTOR_ELEMENT_PACKr5   r6   r7   indexr8   s      r%   _write_parameter_vecrB   :   ss    !!((7JNN11
O

OHHNNII	
 NN:r$   Fc           	        S nS nU c  SnSnXE4$ [        U [        5      (       a  SnU R                  R                  nXE4$ [        U [        5      (       a1  Sn[
        R                  " SU R                  U R                  5      nXE4$ [        U [        5      (       a  Sn[
        R                  " SSU 5      nXE4$ [        U [        5      (       a  S	n[
        R                  " S
SU 5      nXE4$ [        U [        5      (       Gaa  U(       dH  [
        R                  " [        R                  SSR                  S5      SSR                  S5      S5      nOG[
        R                  " [        R                  SSR                  S5      SSR                  S5      S5      nUR                  U5        [!        XU5        U(       dH  [
        R                  " [        R                  SSR                  S5      SSR                  S5      S5      nOG[
        R                  " [        R                  SSR                  S5      SSR                  S5      S5      nUR                  U5        SnSnXE4$ ["        R$                  " S5      e)Nn    pc!ddf!Qdr   i!Qq   sutf8ez!Invalid parameter expression type)
isinstancer   r6   r7   complexr2   r3   realimagfloatintr   r   PARAM_EXPR_ELEM_V13_PACKr/   r1   _write_parameter_expression_v13r   QpyError)instr9   versionr_side	inst_type	inst_dataentrys          r%   _encode_replay_entryr`   H   s@   II|		r q 
D)	$	$	IIOO	l k 
D'	"	"	KKtyy$))<	f e 
D%	 	 	KKq$/	` _ 
D#			KKq$/	Z Y 
D-	.	.KK00

6"

6"E KK00

6"

6"E 	u'@KK00

6"

6"E KK00

6"

6"E 	u		  !!"EFFr$   c                   [         R                  " 5        nUS:  a:  U R                  R                  5        VVs0 s H  u  pEUR                  U_M     nnnOCU R                  R                  5        VVs0 s H  u  pEUR
                  R                  U_M     nnn[        R                  " X6[        US9  UR                  5       nS S S 5        [        R                  " [        R                  U R                  SR!                  S5      [        R                  " S[#        W5      S5      SR!                  S5      S5      nUR%                  U5        UR%                  U5        U R                  $ s  snnf s  snnf ! , (       d  f       N= f)	N   )mapping
serializerr[   urO   !QQr   rD   rE   )ioBytesIOr*   itemsr.   r6   r7   r   write_mappingdumps_valuegetvaluer2   r3   r   rW   r   r/   r5   r1   )	subsr9   r[   mapping_bufkv	subs_dictdatar_   s	            r%   _encode_replay_subsrs      s"   	R</3zz/?/?/AB/Atq/AIBI59ZZ5E5E5GH5GTQq5GIH{G	
 ##% 
 KK((

6E3t9a(

6E NN5NN4::# CH	 
s(   $E6E*!E64#E0-E6*E66
Fc           
        0 nUR                    H  n[        U[        5      (       a  UR                  [	        X@U5      5        M5  [        UR                  X5      u  pV[        UR                  XS5      u  px[        R                  " [        R                  [        UR                  5      UR                  S5      UUR                  S5      U5      n	U R                  U	5        M     U$ )NTrO   )_qpy_replayrQ   r'   updaters   r`   r   r   r2   r3   r   rW   rV   r   r/   r1   )
r9   r:   r[   
symbol_maprZ   lhs_typer   rhs_typer   r_   s
             r%   rX   rX      s     JdE""1$'JK,TXXxI,TXXx$O,,LOOF#OOF#
 	u   r$   c          	     `   S n[         R                  " 5        n[        XQU5      nUR                  5       nS S S 5        UR                  n[        U5      nU(       a  US[        U5      -  -  n[        R                  " [        R                  U[        W5      5      n	U R                  U	5        U R                  U5        U H  n
[        R                  R                  U
5      nU[        R                  R                  :X  a  [        R                   " U
["        5      nO[        R                   " U
[$        5      nUn['        5       n[        R                  " [        R(                  UU[        U5      5      nU R                  U5        U R                  U5        U R                  U5        M     U(       a  U H  n
[        R                  R                  U
5      nU[        R                  R                  :X  a  [        R                   " U
["        5      nO[        R                   " U
[$        5      n[+        XUS9u  p[        R                  " [        R(                  UU[        U5      5      nU R                  U5        U R                  U5        U R                  U5        M     g g ! , (       d  f       GNp= f)N   )r[   use_symengine)rg   rh   rX   rl   
parametersr5   r2   r3   r   PARAMETER_EXPR_PACKr1   r   ValueassignPARAMETER_VECTORr   data_to_binaryrB   r<   r7   PARAM_EXPR_MAP_ELEM_V3_PACKrk   )r9   r:   r|   r[   extra_symbolsbuf
expr_bytesr}   symbol_table_lenparam_expr_header_rawsymbol
symbol_keysymbol_data	value_key
value_dataelem_headers                   r%   _write_parameter_expressionr      s   M	7'J\\^
 
 J:AM 222"KK##%5s: NN()NN:__++F3
 999 //8LMK //8HIK 	W
kk//
O	
 	{#{#z"+ , #F"//7JY__===$33F<PQ$33F<LM$/}%!I !++33J	K NN;'NN;'NN:&) $ G 
s   J
J-c                  T    \ 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 rSrg)_ExprWriter   r9   clbit_indicesstandalone_var_indicesr[   c                4    Xl         X l        X0l        X@l        g r-   r   )selfr9   r   r   r[   s        r%   __init___ExprWriter.__init__   s     *&<#r$   c               D    [        U R                  XR                  5        g r-   )_write_expr_typer9   r[   )r   type_s     r%   r   _ExprWriter._write_expr_type   s    ||<r$   c               6    [         R                  " SU S35      e)Nzunhandled Expr object '')r   rY   r   nodes     r%   visit_generic_ExprWriter.visit_generic   s    !!$;D6"CDDr$   c          
        U R                   R                  [        R                  R                  5        U R                  UR                  5        UR                  (       a  U R                   R                  [        R                  R                  5        U R                   R                  [        R                  " [        R                  /[        R                  " U R                  U   5      Q76 5        g [!        UR"                  [$        5      (       a  U R                   R                  [        R                  R&                  5        U R                   R                  [        R                  " [        R(                  /[        R*                  " U R,                  UR"                     5      Q76 5        g [!        UR"                  [.        5      (       a  U R                   R                  [        R                  R0                  5        U R                   R                  [        R                  " [        R2                  /[        R4                  " [7        UR"                  R8                  5      5      Q76 5        U R                   R                  UR"                  R8                  R;                  [<        R>                  5      5        g [@        RB                  " SUR"                   S35      e)Nzunhandled Var object 'r   )"r9   r1   r   
ExpressionVARr   type
standaloneExprVarUUIDr2   r3   r   EXPR_VAR_UUID_PACKEXPR_VAR_UUIDr   rQ   varr   CLBITEXPR_VAR_CLBIT_PACKEXPR_VAR_CLBITr   r   REGISTEREXPR_VAR_REGISTER_PACKEXPR_VAR_REGISTERr5   r.   r/   r   r0   r   rY   r   s     r%   	visit_var_ExprWriter.visit_var  s   I00445dii(??MM	 1 1 6 67MM..**4+F+Ft+LM %((MM	 1 1 7 78MM//++D,>,>txx,HI "344MM	 1 1 : :;MM225<5N5NsSWS[S[S`S`Oa5b
 MM 4 4V]] CD%%(>txxj&JKKr$   c          	     ^   U R                   R                  [        R                  R                  5        U R                  UR                  5        U R                   R                  [        R                  " [        R                  /[        R                  " U R                  U   5      Q76 5        g r-   )r9   r1   r   r   STRETCHr   r   r2   r3   r   EXPRESSION_STRETCH_PACKEXPRESSION_STRETCHr   r   s     r%   visit_stretch_ExprWriter.visit_stretch   sw    I00889dii(KK//++D,G,G,MN	
r$   c               B   U R                   R                  [        R                  R                  5        U R                  UR                  5        UR                  SL d  UR                  SL a  U R                   R                  [        R                  R                  5        U R                   R                  [        R                  " [        R                  /[        R                  " UR                  5      Q76 5        g [        UR                  [         5      (       a  U R                   R                  [        R                  R"                  5        UR                  S:X  a  SnSnO;UR                  R%                  5       S-  S-   nUR                  R'                  USSS9nU R                   R                  [        R                  " [        R(                  /[        R*                  " U5      Q76 5        U R                   R                  U5        g [        UR                  [,        5      (       a  U R                   R                  [        R                  R.                  5        U R                   R                  [        R                  " [        R0                  /[        R2                  " UR                  5      Q76 5        g [        UR                  [4        5      (       a  U R6                  S	:  a>  UR                  R9                  5       S
:X  a   [:        R<                  " SS	U R6                  S9eU R                   R                  [        R                  R>                  5        [A        U R                   UR                  5        g [:        RB                  " SUR                   S35      e)NTFr   r$         bigsigned   pszDuration variant 'Duration.ps'requiredtargetzunhandled Value object 'r   )"r9   r1   r   r   VALUEr   r   value	ExprValueBOOLr2   r3   r   EXPR_VALUE_BOOL_PACKEXPR_VALUE_BOOLrQ   rV   INT
bit_lengthto_bytesEXPR_VALUE_INT_PACKEXPR_VALUE_INTrU   FLOATEXPR_VALUE_FLOAT_PACKEXPR_VALUE_FLOATr   r[   unitr   UnsupportedFeatureForVersionDURATION_write_durationrY   )r   r   	num_bytesbuffers       r%   visit_value_ExprWriter.visit_value*  st   I00667dii(::u!4MM	 3 3 8 89MMG88_7;R;RSWS]S];^_ 

C((MM	 3 3 7 78zzQ	
 "ZZ2249Q>	,,Yd,KMMG77\':P:PQZ:[\ MM'

E**MM	 3 3 9 9:MMG99aG<T<TUYU_U_<`a 

H--||b TZZ__%6$%> ==4r$,,  MM	 3 3 < <=DMM4::6%%(@A&NOOr$   c                  U R                   R                  [        R                  R                  5        U R                  UR                  5        U R                   R                  [        R                  " [        R                  /[        R                  " UR                  5      Q76 5        UR                  R                  U 5        g r-   )r9   r1   r   r   CASTr   r   r2   r3   r   EXPRESSION_CAST_PACKEXPRESSION_CASTimplicitoperandacceptr   s     r%   
visit_cast_ExprWriter.visit_castP  s~    I00556dii(KK44^w7N7Nt}}7]^	
 	D!r$   c                  U R                   R                  [        R                  R                  5        U R                  UR                  5        U R                   R                  [        R                  " [        R                  /[        R                  " UR                  R                  5      Q76 5        UR                  R                  U 5        g r-   )r9   r1   r   r   UNARYr   r   r2   r3   r   EXPRESSION_UNARY_PACKEXPRESSION_UNARYr   r   r   r   r   s     r%   visit_unary_ExprWriter.visit_unaryX  s    I00667dii(KK55`8P8PQUQXQXQ^Q^8_`	
 	D!r$   c                  U R                   R                  [        R                  R                  5        U R                  UR                  5        U R                   R                  [        R                  " [        R                  /[        R                  " UR                  R                  5      Q76 5        UR                  R                  U 5        UR                   R                  U 5        g r-   )r9   r1   r   r   BINARYr   r   r2   r3   r   EXPRESSION_BINARY_PACKEXPRESSION_BINARYr   r   leftr   rightr   s     r%   visit_binary_ExprWriter.visit_binary`  s    I00778dii(KK66b9R9RSWSZSZS`S`9ab	
 			

$r$   c               l   U R                   S:  a   [        R                  " SSU R                   S9eU R                  R	                  [
        R                  R                  5        U R                  UR                  5        UR                  R                  U 5        UR                  R                  U 5        g )N   zthe 'Index' expressionr   )r[   r   r   r9   r1   r   r   INDEXr   r   r   r   rA   r   s     r%   visit_index_ExprWriter.visit_indexi  s    <<"99(2dll  	I00667dii(4 

$r$   )r   r9   r   r[   N)r   r   r    r!   	__slots__r   r   r   r   r   r   r   r   r   r   r#   r   r$   r%   r   r      s=    RI=EL<
$PL""  r$   r   c                :    UR                  [        XX45      5        g r-   )r   r   )r9   r   r   r   r[   s        r%   _write_exprr   t  s     	KKH5KUVr$   c                   UR                   [        R                  L a*  U R                  [        R
                  R                  5        g UR                   [        R                  L a|  U R                  [        R
                  R                  5        U R                  [        R                  " [        R                  /[        R                  " UR                  5      Q76 5        g UR                   [        R                  L aF  US:  a  [         R"                  " SSUS9eU R                  [        R
                  R$                  5        g UR                   [        R&                  L aF  US:  a  [         R"                  " SSUS9eU R                  [        R
                  R(                  5        g [         R*                  " SU S35      e)N   zfloat-typed expressionsr   zduration-typed expressionszunhandled Type object ';)kindr
   Boolr1   r   ExprTyper   UintUINTr2   r3   r   EXPR_TYPE_UINT_PACKEXPR_TYPE_UINTwidthFloatr   r   r   r   r   rY   )r9   r   r[   s      r%   r   r   ~  s7   zzUZZy))../	uzz	!y))../KK33Zg6L6LU[[6YZ	
 
u{{	"R<99)Bw  	y))//0	u~~	%R<99,r'  	y))223!!$;E7!"DEEr$   c           	        UR                  5       nUS:X  a  U R                  [        R                  R                  5        U R                  [
        R                  " [        R                  /[        R                  " UR                  5       5      Q76 5        g US:X  a  U R                  [        R                  R                  5        U R                  [
        R                  " [        R                  /[        R                  " UR                  5       5      Q76 5        g US:X  a  U R                  [        R                  R                  5        U R                  [
        R                  " [        R                  /[        R                   " UR                  5       5      Q76 5        g US:X  a  U R                  [        R                  R"                  5        U R                  [
        R                  " [        R$                  /[        R&                  " UR                  5       5      Q76 5        g US:X  a  U R                  [        R                  R(                  5        U R                  [
        R                  " [        R*                  /[        R,                  " UR                  5       5      Q76 5        g US:X  a  U R                  [        R                  R.                  5        U R                  [
        R                  " [        R0                  /[        R2                  " UR                  5       5      Q76 5        g [4        R6                  " SU S35      e)	Ndtr   nsusmsrN   zunhandled Duration object 'r   )r   r1   r   CircuitDurationDTr2   r3   r   DURATION_DT_PACKDURATION_DTr   PSDURATION_PS_PACKDURATION_PSNSDURATION_NS_PACKDURATION_NSUSDURATION_US_PACKDURATION_USMSDURATION_MS_PACKDURATION_MSSDURATION_S_PACK
DURATION_Sr   rY   )r9   durationr   s      r%   r   r     s*   ==?Dt|y00334KK00Y73F3Fx~~GW3XY	
 
y00334KK00Y73F3Fx~~GW3XY	
 
y00334KK00Y73F3Fx~~GW3XY	
 
y00334KK00Y73F3Fx~~GW3XY	
 
y00334KK00Y73F3Fx~~GW3XY	
 
y00223v{{7#:#:bW=O=OPXP^P^P`=abc!!$?z"KLLr$   c                f   [         R                  " [        R                  " [         R                  U R                  [         R                  5      5      6 n[        R                  " UR                  S9nU R                  UR                  5      R                  [        R                  5      n[        X2S9$ )Nr7   r6   )r   	PARAMETERr2   unpackr4   readPARAMETER_SIZEr6   r   	name_sizedecoder   r0   r   )r9   rr   
param_uuidr.   s       r%   _read_parameterr+    su    	w--x}}W=S=S/T	UD +J==(//>DT++r$   c                *   [         R                  " [        R                  " [         R                  U R                  [         R                  5      5      6 n[        R                  " UR                  S9R                  UR                  -
  n[        R                  " US9nU R                  UR                  5      R                  [        R                  5      nXA;  a!  [        XRR                   5      [#        5       4X'   X   S   nXbR                     R                  U:w  ai  X   S   R%                  UR                  5        ['        XbR                  [        R                  " X2R                  -   S9S9UR(                  UR                  '   XbR                     $ )Nr"  )rV   r   r   r#  )r   PARAMETER_VECTOR_ELEMENTr2   r%  r@   r&  PARAMETER_VECTOR_ELEMENT_SIZEr6   r   rV   rA   vector_name_sizer)  r   r0   r   vector_sizesetaddr   _params)r9   vectorsrr   root_uuid_int	root_uuidr.   r>   s          r%   _read_parameter_vecr7    s(   ++	11MM'??@

D IIDII.22TZZ?M		m,I==../66v}}ED-d4D4DEsuM"Fjj)+1!!$**-%;JJTYY=::3M%N&
tzz" **r$   c           	     :   [         R                  " [        R                  " [         R                  U R                  [         R                  5      5      6 nU R                  UR                  5      R                  [        R                  5      n[        U5      n0 n[        UR                  5       GH  n[         R                  " [        R                  " [         R                  U R                  [         R                   5      5      6 n[#        U 5      n[$        R&                  " UR(                  5      nU R                  UR*                  5      n	U[$        R&                  R,                  :X  a  [        R                  " SU	5      n
OU[$        R&                  R.                  :X  a  [        R                  " SU	5      n
OU[$        R&                  R0                  :X  a,  [3        [        R                  " [         R4                  U	5      6 n
OtU[$        R&                  R6                  :X  a  Un
OSU[$        R&                  R8                  :X  a  [        R:                  " U	[<        5      n
O[>        R@                  " SU 35      eXURB                  '   GM     [E        U[G        U5      5      $ )N!q!d'Invalid parameter expression map type: )$r   PARAMETER_EXPRr2   r%  r~   r&  PARAMETER_EXPR_SIZE	expr_sizer)  r   r0   r   rangemap_elementsPARAM_EXPR_MAP_ELEMPARAM_EXPR_MAP_ELEM_PACKPARAM_EXPR_MAP_ELEM_SIZEr+  r   r   r   sizeINTEGERr   COMPLEXrR   COMPLEX_PACKr$  PARAMETER_EXPRESSIONdata_from_binary_read_parameter_expressionr   rY   r.   r   str)r9   rr   	sympy_strexpr_name_map_	elem_datar   elem_keybinary_datar   s              r%   rJ  rJ    s   !!	w22HMM'B]B]4^	_D dnn-44V]]CIY'EH4$$%//]]00g>>?
	 !*??9>>2mmINN3y...MM$4E...MM$4E000V]]7+?+?MNE222E===++K9STE%%(OPXz&Z[[ %/ &2 xU44r$   c           	        [         R                  " [        R                  " [         R                  U R                  [         R                  5      5      6 nU R                  UR                  5      nU(       a  [        R                  " U5      nO*UR                  [        R                  5      n[        U5      n0 n[        UR                  5       GHr  n[         R                  " [        R                  " [         R                   U R                  [         R"                  5      5      6 n	[$        R&                  " U	R(                  5      n
U
[$        R&                  R*                  :X  a  [-        U 5      nOCU
[$        R&                  R.                  :X  a  [1        X5      nO[2        R4                  " SU
 35      e[$        R&                  " U	R6                  5      nU R                  U	R8                  5      nU[$        R&                  R:                  :X  a  [        R                  " SU5      nGOU[$        R&                  R<                  :X  a  [        R                  " SU5      nOU[$        R&                  R>                  :X  a,  [A        [        R                  " [         RB                  U5      6 nOU[$        R&                  R*                  [$        R&                  R.                  4;   a  UnOSU[$        R&                  RD                  :X  a  [        RF                  " U[H        UUS9nO[2        R4                  " SU 35      eXURJ                  '   GMu     [M        U[O        U5      5      $ )Nr;  r9  r:  r4  r|   )(r   r<  r2   r%  r~   r&  r=  r>  r   load_symengine_payloadr)  r0   r   r?  r@  PARAM_EXPR_MAP_ELEM_V3r   PARAM_EXPR_MAP_ELEM_V3_SIZEr   r   symbol_typer$  r+  r   r7  r   rY   r   rD  rE  r   rF  rR   rG  rH  rI  _read_parameter_expression_v3r.   r   rK  )r9   r4  r|   rr   payloadrM  rL  rN  rO  rP  r   r   rQ  rR  r   s                  r%   rY  rY    sW   !!	w22HMM'B]B]4^	_D mmDNN+G--g6NN6==1	 +H4$$%22]]33gAAB
	 __Y%:%:;
222$X.F9??;;;(;F%%(OPZ|&\]]??9>>2mmINN3y...MM$4E...MM$4E000V]]7+?+?MNE)//33Y__5U5UVVE===++-+	E %%(OPXz&Z[[ %G &J xU44r$   c           	        [         R                  " [        R                  " [         R                  U R                  [         R                  5      5      6 nU R                  UR                  5      n0 n[        UR                  5       GH  n[         R                  " [        R                  " [         R                  U R                  [         R                  5      5      6 n[        R                  " UR                  5      nU[        R                  R                   :X  a  [#        U 5      n	OnU[        R                  R$                  :X  a  ['        X5      n	ODU[        R                  R(                  :X  a  [+        XU5      n	O[,        R.                  " SU 35      e[        R                  " UR0                  5      n
U R                  UR2                  5      nU
[        R                  R4                  :X  a  [        R                  " SU5      nGOU
[        R                  R6                  :X  a  [        R                  " SU5      nOU
[        R                  R8                  :X  a,  [;        [        R                  " [         R<                  U5      6 nOU
[        R                  R                   [        R                  R$                  4;   a  U	nOSU
[        R                  R(                  :X  a  [>        R@                  " U[*        UUS9nO[,        R.                  " SU
 35      eXU	'   GM     [B        RD                  " U5       n[G        XX!5      sS S S 5        $ ! , (       d  f       g = f)Nr;  r9  r:  r4  r[   )$r   r<  r2   r%  r~   r&  r=  r>  r?  r@  rV  r   rW  r   r   rX  r$  r+  r   r7  rH  _read_parameter_expression_v13r   rY   r   rD  rE  r   rF  rR   rG  r   rI  rg   rh   _read_parameter_expr_v13)r9   r4  r[   rr   rZ  rw   rO  rP  r   r   rQ  rR  r   r   s                 r%   r]  r]  3  sU   !!	w22HMM'B]B]4^	_D mmDNN+GJ4$$%22]]33gAAB
	 __Y%:%:;
222$X.F9??;;;(;F9?????3HwOF%%(OPZ|&\]]??9>>2mmINN3y...MM$4E...MM$4E000V]]7+?+?MNE)//33Y__5U5UVVE===++.	E %%(OPXz&Z[["6M &N 
G	'J 
		s   4M


Mc                6   U Vs0 s H'  n[        U[        5      (       d  M  UR                  U_M)     nnUR                  5        VVs0 s H  u  pg[	        U5      U_M     nnnU R                  [        R                  5      n	/ n
U	(       Ga  [        R                  R                  [        R                  " [        R                  U	5      5      nUR                  S:X  a2  U
R                  U[        R                  " UR                   S9   5        GOSUR                  S:X  a5  U
R                  [        R                  " SUR                   5      S   5        GOUR                  S:X  a  GOUR                  S:X  a8  U
R                  [#        [        R                  " SUR                   5      6 5        GOUR                  S	:X  a5  U
R                  [        R                  " S
UR                   5      S   5        GOoUR                  S:X  a"  U R                  [        R                  5      n	GM  UR                  S:X  a"  U R                  [        R                  5      n	GM  UR                  S:X  a  [        R$                  " SUR                   5      S   nU R                  U5      n[&        R(                  " U5       n[*        R,                  " U[.        UUS9nS S S 5        US:  a8  U
R                  WR                  5        VVs0 s H
  u  pgX   U_M     snn5        ONU
R                  WR                  5        VVs0 s H
  u  pgXV   U_M     snn5        O[0        R2                  " S5      eUR4                  S:X  a2  U
R                  U[        R                  " UR6                  S9   5        GOiUR4                  S:X  a5  U
R                  [        R                  " SUR6                  5      S   5        GO$UR4                  S:X  a  GOUR4                  S:X  a7  U
R                  [#        [        R                  " SUR6                  5      6 5        OUR4                  S	:X  a4  U
R                  [        R                  " S
UR6                  5      S   5        OUR4                  S:X  a"  U R                  [        R                  5      n	GM5  UR4                  S:X  a"  U R                  [        R                  5      n	GMg  [0        R2                  " SUR4                   35      eUR8                  S:X  a  GM  [;        UR8                  5      nUR8                  S;   a  U
R=                  5       nU
R=                  5       n[        U[>        5      (       dl  [        U[>        5      (       aW  UR8                  S;   aG  UR8                  S:X  a  SnOUR8                  S:X  a  SnU
R                  [A        UU5      " U5      5        ORU
R                  [A        UU5      " U5      5        O0U
R=                  5       nU
R                  [A        UU5      " 5       5        U R                  [        R                  5      n	U	(       a  GM  U
R=                  5       $ s  snf s  snnf ! , (       d  f       GN= fs  snnf s  snnf )N   pr"     frJ   r      n   crH      irL      s   e   urf   r   )deserializerr[   r4  rb   zEUnknown ParameterExpression operation type {expression_data.LHS_TYPE}z+Unknown ParameterExpression operation type rM   >
   r   r   r{            rb            >   r   r{   __radd__r{   __rmul__)!rQ   r   r6   ri   rK  r&  r   PARAM_EXPR_ELEM_V13_SIZEPARAM_EXPR_ELEM_V13_maker2   r%  rW   LHS_TYPEappendr   LHSrR   unpack_fromrg   rh   r   read_mappingloads_valuer   rY   RHS_TYPERHSOP_CODEr   popr   getattr)r   rw   r[   r4  r   param_uuid_mapro   rp   rN  rr   stackexpression_datarD  subs_map_datarn   rc   
method_strr   r   s                      r%   r^  r^  f  s   8Bd
fjQWYbFc)fkk6)
Nd&0&6&6&89&8daA	&8H988G445DE
!55;;MM'::DA
 ##t+LL		8K8K(LMN%%-LLuo.A.AB1EF%%-%%-LL&--7J7J"KLM%%-LLuo.A.AB1EF%%-88G<<=D%%-88G<<=D%%-%%e_-@-@A!DDHHTNMM*k --!,##	 + |Ihk1nIJw}}Otqn/2OP%%W  ##t+LL		8K8K(LMN%%-LLuo.A.AB1EF%%-%%-LL&--7J7J"KLM%%-LLuo.A.AB1EF%%-88G<<=D%%-88G<<=D%%=o>V>V=WX  ""c)&'>'>?
""&II))+C))+C s$788s$788#++v5"**a/!+J$,,1!+JWS*5c:;WS*5c:;))+CLLj134xx889m $p 99;y e96 +* JOs(   Y2Y2Y73Y=;Z
3Z
=
Zc           
     f   U R                  [        R                  5      n[        U 5      nU[        R
                  R                  :X  Ga.  U R                  [        R                  5      nU[        R                  R                  :X  am  [        R                  R                  [        R                  " [        R                  U R                  [        R                  5      5      5      nX7R                      $ U[        R                  R"                  :X  a  [        R$                  R                  [        R                  " [        R&                  U R                  [        R(                  5      5      5      n[*        R,                  " XR.                     U5      $ U[        R                  R0                  :X  a  [        R2                  R                  [        R                  " [        R4                  U R                  [        R6                  5      5      5      nU R                  UR8                  5      R;                  [<        R>                  5      n[*        R,                  " X(   U5      $ [@        RB                  " S5      eU[        R
                  RD                  :X  am  [        RF                  R                  [        R                  " [        RH                  U R                  [        RJ                  5      5      5      nX7R                      $ U[        R
                  RL                  :X  Gap  U R                  [        RN                  5      n	U	[        RP                  RR                  :X  a  [        RT                  R                  [        R                  " [        RV                  U R                  [        RX                  5      5      5      n[*        RZ                  " UR\                  U5      $ U	[        RP                  R^                  :X  a  [        R`                  R                  [        R                  " [        Rb                  U R                  [        Rd                  5      5      5      n[*        RZ                  " [f        Ri                  U R                  URj                  5      SSS9U5      $ U	[        RP                  Rl                  :X  a  [        Rn                  R                  [        R                  " [        Rp                  U R                  [        Rr                  5      5      5      n[*        RZ                  " UR\                  U5      $ U	[        RP                  Rt                  :X  a!  [w        U 5      n
[*        RZ                  " X5      $ [@        RB                  " S5      eU[        R
                  Rx                  :X  a  [        Rz                  R                  [        R                  " [        R|                  U R                  [        R~                  5      5      5      n[*        R                  " [        XX#5      XWR                  S9$ U[        R
                  R                  :X  a  [        R                  R                  [        R                  " [        R                  U R                  [        R                  5      5      5      n[*        R                  " [*        R                  R                  UR                  5      [        XX#5      U5      $ U[        R
                  R                  :X  a  [        R                  R                  [        R                  " [        R                  U R                  [        R                  5      5      5      n[*        R                  " [*        R                  R                  UR                  5      [        XX#5      [        XX#5      U5      $ U[        R
                  R                  :X  a,  [*        R                  " [        XX#5      [        XX#5      U5      $ [@        RB                  " SU S35      e)	Nz5Invalid classical-expression Var key '{var_type_key}'r   Tr   z9Invalid classical-expression Value key '{value_type_key}')r   z'Invalid classical-expression Expr key 'r   )Qr&  r   EXPRESSION_DISCRIMINATOR_SIZE_read_expr_typer   r   r   EXPR_VAR_DISCRIMINATOR_SIZEr   r   r   rs  r2   r%  r   EXPR_VAR_UUID_SIZE	var_indexr   r   r   EXPR_VAR_CLBIT_SIZEr	   VarrA   r   r   r   EXPR_VAR_REGISTER_SIZEreg_name_sizer)  r   r0   r   rY   r   r   r   EXPRESSION_STRETCH_SIZEr   EXPR_VALUE_DISCRIMINATOR_SIZEr   r   r   r   EXPR_VALUE_BOOL_SIZEr   r   r   r   r   EXPR_VALUE_INT_SIZErV   
from_bytesr   r   r   r   EXPR_VALUE_FLOAT_SIZEr   _read_durationr   r   r   EXPRESSION_CAST_SIZECast
_read_exprr   r   r   r   EXPRESSION_UNARY_SIZEUnaryOpopcoder   r   r   EXPRESSION_BINARY_SIZEBinaryr   Index)r9   clbitscregsstandalone_varstype_keyr   var_type_keyrZ  r.   value_type_keyr   s              r%   r  r    s    }}WBBCHH%E9''+++}}W%H%HI9,,111++11g88(--HbHb:cdG ##4#4559,,222,,22//w?Z?Z1[G
 88F==15999,,555//5522HMM'B`B`4aG
 ==!6!67>>v}}MD88EK//!!"YZZ9''///,,22MM//w?^?^1_

 00119''---!w'L'LMY00555--3300(--@\@\2]G
 ::gmmU33Y00444,,22//w?Z?Z1[G
 ::x}}W->->?tTV[  Y00666..44118==A^A^3_G
 ::gmmU33Y00999"8,E::e++!!"]^^9'',,,))//MM'66gFbFb8cd
 yyx@%RbRb
 	
 9''---**00MM--x}}W=Z=Z/[

 zzJJMM'..)x@
 	

 9''...++11MM..g>\>\0]

 {{KKNN7>>*x@x@	
 	
 9''---zzx@x@
 	

 

 GzQRS
TTr$   c                   U R                  [        R                  5      nU[        R                  R
                  :X  a  [        R                  " 5       $ U[        R                  R                  :X  a  [        R                  R                  [        R                  " [        R                  U R                  [        R                  5      5      5      n[        R                  " UR                   5      $ U[        R                  R"                  :X  a  [        R$                  " 5       $ U[        R                  R&                  :X  a  [        R(                  " 5       $ [*        R,                  " SU S35      e)Nz'Invalid classical-expression Type key 'r   )r&  r   EXPR_TYPE_DISCRIMINATOR_SIZEr   r  r   r
   r   r  r  rs  r2   r%  r  EXPR_TYPE_UINT_SIZEr  r  r   r  r   r   r   rY   r9   r  elems      r%   r  r  1  s    }}WAABH9%%***zz|9%%***%%++MM'55x}}WE`E`7ab
 zz$**%%9%%+++{{}9%%...~~


 GzQRS
TTr$   c                   U R                  [        R                  5      nU[        R                  R
                  :X  a  [        R                  R                  [        R                  " [        R                  U R                  [        R                  5      5      5      n[        R                  " UR                  5      $ U[        R                  R                  :X  a  [        R                   R                  [        R                  " [        R"                  U R                  [        R$                  5      5      5      n[        R&                  " UR                  5      $ U[        R                  R(                  :X  a  [        R*                  R                  [        R                  " [        R,                  U R                  [        R.                  5      5      5      n[        R0                  " UR                  5      $ U[        R                  R2                  :X  a  [        R4                  R                  [        R                  " [        R6                  U R                  [        R8                  5      5      5      n[        R:                  " UR                  5      $ U[        R                  R<                  :X  a  [        R>                  R                  [        R                  " [        R@                  U R                  [        RB                  5      5      5      n[        RD                  " UR                  5      $ U[        R                  RF                  :X  a  [        RH                  R                  [        R                  " [        RJ                  U R                  [        RL                  5      5      5      n[        RN                  " UR                  5      $ [P        RR                  " SU S35      e)NzInvalid duration Type key 'r   )*r&  r   DURATION_DISCRIMINATOR_SIZEr   r  r  r  rs  r2   r%  r  DURATION_DT_SIZEr   r	  r   r  r  r  DURATION_PS_SIZEr   r  r  r  DURATION_NS_SIZEr
  r  r  r  DURATION_US_SIZEr  r  r  r  DURATION_MS_SIZEr  r  r  r  DURATION_S_SIZErN   r   rY   r  s      r%   r  r  A  s}   }}W@@AH9,,///""((MM'22HMM'BZBZ4[\
 {{4::&&9,,///""((MM'22HMM'BZBZ4[\
 {{4::&&9,,///""((MM'22HMM'BZBZ4[\
 {{4::&&9,,///""((MM'22HMM'BZBZ4[\
 {{4::&&9,,///""((MM'22HMM'BZBZ4[\
 {{4::&&9,,...!!''MM'118==AXAX3YZ
 zz$**%%


 ;H:QG
HHr$   c           
     *   [         R                  R                  / [         R                  R                  / [         R                  R                  / [         R                  R
                  / [         R                  R                  / 0n/ n[        U5       GH{  n[        R                  R                  [        R                  " [        R                  U R                  [        R                  5      5      5      n[!        U 5      nU R                  UR"                  5      R%                  [&        R(                  5      nUR*                  [         R                  R
                  [         R                  R                  1;   a4  [,        R.                  " [0        R2                  " UR4                  S9U5      nO1[,        R6                  " [0        R2                  " UR4                  S9XgS9nX%R*                     R9                  U5        UR9                  U5        GM~     X#4$ )a  Read the ``num_vars`` standalone variable declarations from the file.

Args:
    file_obj (File): a file-like object to read from.
    num_vars (int): the number of variables to read.

Returns:
    tuple[dict, list]: the first item is a mapping of the ``ExprVarDeclaration`` type keys to
    the variables defined by that type key, and the second is the total order of variable
    declarations.
r"  )r.   )r   ExprVarDeclarationINPUTCAPTURELOCALSTRETCH_CAPTURESTRETCH_LOCALr?  r   EXPR_VAR_DECLARATIONrs  r2   r%  EXPR_VAR_DECLARATION_PACKr&  EXPR_VAR_DECLARATION_SIZEr  r(  r)  r   r0   usager	   Stretchr6   r   
uuid_bytesr  ru  )	r9   num_vars	read_vars	var_orderrO  rr   r   r.   r   s	            r%   read_standalone_varsr  d  sx    	$$**B$$,,b$$**B$$44b$$22BI I8_++11MM11g??@
  )}}T^^,33FMMB::((88((66
 
 ,,tyyt?FC((4994??;UNC**$$S)# $ r$   c                r   UR                   R                  [        R                  5      nU R	                  [
        R                  " [        R                  /[        R                  " UR                  R                  U[        U5      5      Q76 5        [        XR                  U5        U R	                  U5        g r-   )r.   r/   r   r0   r1   r2   r3   r   r  r  r   r7   r5   r   r   )r9   r   r  r[   r.   s        r%   _write_standalone_varr    sx    88??6==)DNN--	
))#''--3t9M	
 Xxx1NN4r$   c                   Sn0 nUR                  5        H1  n[        X[        R                  R                  U5        X4U'   US-  nM3     UR                  5        H1  n[        X[        R                  R                  U5        X4U'   US-  nM3     UR                  5        H1  n[        X[        R                  R                  U5        X4U'   US-  nM3     US:  a'  UR                  (       a  [        R                  " SSUS9eUR                  5        H1  n[        X[        R                  R                  U5        X4U'   US-  nM3     UR                  5        H1  n[        X[        R                  R                  U5        X4U'   US-  nM3     U$ )ag  Write the standalone variables out from a circuit.

Args:
    file_obj (File): the file-like object to write to.
    circuit (QuantumCircuit): the circuit to take the variables from.
    version (int): the QPY target version.

Returns:
    dict[expr.Var | expr.Stretch, int]: a mapping of the variables written to the
        index that they were written at.
r   r   r   z%circuits containing stretch variablesr   )iter_input_varsr  r   r  r  iter_captured_varsr  iter_declared_varsr  num_stretchesr   r   iter_captured_stretchesr  iter_declared_stretchesr  )r9   circuitr[   rA   outr   s         r%   write_standalone_varsr    st    E
C&&(hY-I-I-O-OQXYC
 ) ))+hY-I-I-Q-QSZ[C
 , ))+hY-I-I-O-OQXYC
 , |--553b
 	
 ..0hY-I-I-Y-Y[bcC
 1 ..0hY-I-I-W-WY`aC
 1 Jr$   )	index_mapr|   r   c                  [         R                  R                  U 5      nU[         R                  R                  :X  a  [        R
                  " SU 5      nXV4$ U[         R                  R                  :X  a  [        R
                  " SU 5      nXV4$ U[         R                  R                  :X  a=  [        R
                  " [        R                  U R                  U R                  5      nXV4$ U[         R                  R                  :X  a(  [        R                  " U [        R                   5      nXV4$ U[         R                  R"                  :X  a"  U R%                  [        R&                  5      nXV4$ U[         R                  R(                  [         R                  R*                  4;   a  SnXV4$ U[         R                  R,                  :X  a  [        R                  " U [.        5      nXV4$ U[         R                  R0                  :X  a  [        R                  " U [2        5      nXV4$ U[         R                  R4                  :X  a  [        R                  " U [6        X1S9nXV4$ U[         R                  R8                  :X  a0  Uc  0 OUS   nUc  0 OUn[        R                  " U [:        UUUS9nXV4$ [<        R>                  " SU S35      e)	a  Serialize input value object.

Args:
    obj (any): Arbitrary value object to serialize.
    version (int): the target QPY version for the dump.
    index_map (dict): Dictionary with two keys, "q" and "c".  Each key has a value that is a
        dictionary mapping :class:`.Qubit` or :class:`.Clbit` instances (respectively) to their
        integer indices.
    use_symengine (bool): If True, symbolic objects will be serialized using symengine's
        native mechanism. This is a faster serialization alternative, but not supported in all
        platforms. Please check that your target platform is supported by the symengine library
        before setting this option, as it will be required by qpy to deserialize the payload.
    standalone_var_indices (dict): Dictionary that maps standalone :class:`.expr.Var` entries to
        the index that should be used to refer to them.

Returns:
    tuple: TypeKey and binary data.

Raises:
    QpyError: Serializer for given format is not ready.
r9  r:  r$   )r|   r[   rG   )r   r   r[   Serialization for ! is not implemented in value I/O.) r   r   r   rE  r2   r3   r   rF  r   rG  rS   rT   	NUMPY_OBJr   r   npsaveSTRINGr/   r0   NULLr   r   rB   r$  r<   rH  r   
EXPRESSIONr   r   rY   )r:   r[   r  r|   r   r  rR  r   s           r%   rk   rk     sb   : %%c*H9??***kk$,@   ? 
Y__**	*kk$,<   ; 
Y__,,	,kk'"6"6#((K8   7 
Y__..	.++C94   3 
Y__++	+jj/0   / 
ioo**IOO,H,HI	I,   + 
Y__55	5++C1EF(   ' 
Y__..	.++C1AB$   # 
Y__99	9++,M
     
Y__//	/'/Ys^'='EKa++'#9
    !!$6xj@a"bccr$   c               P    [        UUUUUS9u  pg[        R                  " XU5        g)a  Write a value to the file like object.

Args:
    file_obj (File): A file like object to write data.
    obj (any): Value to write.
    version (int): the target QPY version for the dump.
    index_map (dict): Dictionary with two keys, "q" and "c".  Each key has a value that is a
        dictionary mapping :class:`.Qubit` or :class:`.Clbit` instances (respectively) to their
        integer indices.
    use_symengine (bool): If True, symbolic objects will be serialized using symengine's
        native mechanism. This is a faster serialization alternative, but not supported in all
        platforms. Please check that your target platform is supported by the symengine library
        before setting this option, as it will be required by qpy to deserialize the payload.
    standalone_var_indices (dict): Dictionary that maps standalone :class:`.expr.Var` entries to
        the index that should be used to refer to them.
)r[   r  r|   r   N)rk   r   write_generic_typed_data)r9   r:   r[   r  r|   r   r  rr   s           r%   write_valuer    s1    & !#5NH ##H=r$   r   r  r  r|   r  c                  [        U [        5      (       a  [        R                  " U 5      n U [        R                  R                  :X  a  [
        R                  " SU5      S   $ U [        R                  R                  :X  a  [
        R                  " SU5      S   $ U [        R                  R                  :X  a+  [        [
        R                  " [        R                  U5      6 $ U [        R                  R                  :X  a%  [        R                  " U[        R                   5      $ U [        R                  R"                  :X  a  UR%                  [        R&                  5      $ U [        R                  R(                  :X  a  gU [        R                  R*                  :X  a  [*        $ U [        R                  R,                  :X  a  [        R                  " U[.        US9$ U [        R                  R0                  :X  a  [        R                  " U[2        5      $ U [        R                  R4                  :X  a\  US:  a  [        R                  " U[6        5      $ US:  a  [        R                  " U[8        UUS9$ [        R                  " U[:        X2S	9$ U [        R                  R<                  :X  a%  [        R                  " U[>        UU=(       d    0 US
9$ [@        RB                  " SU  S35      e)a  Deserialize input binary data to value object.

Args:
    type_key (ValueTypeKey): Type enum information.
    binary_data (bytes): Data to deserialize.
    version (int): QPY version.
    vectors (dict): ParameterVector in current scope.
    clbits (Sequence[Clbit]): Clbits in the current scope.
    cregs (Mapping[str, ClassicalRegister]): Classical registers in the current scope.
    use_symengine (bool): If True, symbolic objects will be de-serialized using symengine's
        native mechanism. This is a faster serialization alternative, but not supported in all
        platforms. Please check that your target platform is supported by the symengine library
        before setting this option, as it will be required by qpy to deserialize the payload.
    standalone_vars (Sequence[Var]): standalone :class:`.expr.Var` nodes in the order that they
        were declared by the circuit header.
Returns:
    any: Deserialized value object.

Raises:
    QpyError: Serializer for given format is not ready.
r9  r   r:  N)r4  ri  rk  rT  r\  )r  r  r  r  r  )"rQ   r7   r   r   rE  r2   r%  r   rF  rR   r   rG  r  r   rI  r  loadr  r)  r0   r  r   r   r7  r$  r+  rH  rJ  rY  r]  r  r  r   rY   )r  rR  r[   r4  r  r  r|   r  s           r%   ry  ry  "  s8   D (E""??8,9??***}}T;/229??(((}}T;/229??***g&:&:KHII9??,,,&&{BGG<<9??)))!!&--009??'''9??///9??333&&
 	

 9??,,,&&{ODD9??777Q;**;8RSSr\**-+	  **;W  9??---&&+2+
 	
 

 28*<]^
__r$   c               R    [         R                  " U 5      u  px[        UUUUUUUUS9$ )ao  Read a value from the file like object.

Args:
    file_obj (File): A file like object to write data.
    version (int): QPY version.
    vectors (dict): ParameterVector in current scope.
    clbits (Sequence[Clbit]): Clbits in the current scope.
    cregs (Mapping[str, ClassicalRegister]): Classical registers in the current scope.
    use_symengine (bool): If True, symbolic objects will be de-serialized using symengine's
        native mechanism. This is a faster serialization alternative, but not supported in all
        platforms. Please check that your target platform is supported by the symengine library
        before setting this option, as it will be required by qpy to deserialize the payload.
    standalone_vars (Sequence[expr.Var]): standalone variables in the order they were defined in
        the QPY payload.

Returns:
    any: Deserialized value object.
r  )r   read_generic_typed_datary  )	r9   r[   r4  r  r  r|   r  r  rr   s	            r%   
read_valuer  w  s;    8 33H=NH#'	 	r$   )F)r   	expr.Exprr   z#collections.abc.Mapping[Clbit, int]r   z&collections.abc.Mapping[expr.Var, int]r[   rV   )r   
types.Typer[   rV   )r   r   )r  zcollections.abc.Sequence[Clbit]r  z/collections.abc.Mapping[str, ClassicalRegister]r  z"collections.abc.Sequence[expr.Var]returnr  )r  r  )r  r   )B__doc__
__future__r   collections.abccollectionsrg   r2   r6   dataclassesr   numpyr  qiskit.circuitr   r   r   r   qiskit.circuit.classicalr	   r
   qiskit.circuit.parameterr   "qiskit.circuit.parameterexpressionr   r   r   r   qiskit.circuit.parametervectorr   r   
qiskit.qpyr   r   r   r   %qiskit.qpy.binary_io.parse_sympy_reprr   r   r'   r<   rB   r`   rs   rX   r   ExprVisitorr   r   r   r   r+  r7  rJ  rY  r]  r^  r  r  r  r  r  r  rk   r  ry  r  r   r$   r%   <module>r     s   L "  	   !  K K 0 .  S = = B * * * # # #
> B.,:'z~ $""4( ~ BW
W 7W C	W
 WF0MD,2!5H25j0Kf]@hU+hU ;hU 8	hU
 hUVU  IF& R	&Z @!H *.U[_>F 
R`t 
'r$   