
    z	i                     L    S r SSKrSSKJr  SSKJrJr   " S S5      rS rS r	g)	z@Circuit synthesizers and related classes for boolean expressions    N)QuantumCircuit)ZGateXGatec                   0    \ rS rSrSrS rS rS rS rSr	g)	EsopGenerator   zkGenerates an ESOP (Exlusive-sum-of-products) representation
for a boolean function given by its truth tablec                 F    Xl         0 U l        U R                  SS9U l        g )N )
assignment)truth_tablecofactor_tablegenerate_esopesop)selfr   s     k/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/synthesis/boolean/boolean_expression_synth.py__init__EsopGenerator.__init__   s%    & &&"&5	    c                 n    [        [        U5      5       Vs/ s H  o1U   X#   :w  d  M  UPM     sn$ s  snf )z1The indices of variables where the clauses differ)rangelen)r   clause1clause2is       r   clause_diffEsopGenerator.clause_diff   s0     W.K.a!*
2J.KKKs   22c                 ^   SnX-   nU(       a  Sn[         R                  " US5       Hy  u  pVU R                  XV5      n[        U5      S:X  d  M'  USUS    S-   XWS   S-   S -   nX;  d  MD  UR	                  U5        UR	                  U5        UR                  U5        Sn  O   U(       a  M  U$ )zKCombining clauses of distance 1 until no more combinations can be performedTF      Nr   -)	itertoolscombinationsr   r   removeappend)	r   	clauses_1	clauses_2something_changedcombined_clausesclause_1clause_2diff
new_clauses	            r   combine_and_optimize"EsopGenerator.combine_and_optimize    s     $0 %&/&<&<=Mq&Q"'';t9>!))DG!4s!:X1gPQkm=T!TJ!9(//9(//9(//
;,0) 'R    r   c                    [        U5      U R                  R                  :X  a0  U R                  U   (       a  SU R                  R                  -  /$ / $ [        U5      nU R                  US-   5      nU R                  US-   5      nU Vs/ s H  oUSU S-   XRS-   S -   PM     nnU Vs/ s H  oUSU S-   XRS-   S -   PM     nnU R	                  Xg5      U R
                  U'   U R
                  U   $ s  snf s  snf )zRecursively generates an ESOP for a partially determined boolean expression
'assignment' is a partial assignment to the expression's variables
r    )F)TN0r   1)r   r   num_bitsr   r-   r   )r   r   r   negative_esoppositive_esopclausenegative_esop_bool_expandedpositive_esop_bool_expandeds           r   r   EsopGenerator.generate_esop2   s#    z?d..777
+d..77788	
O**:+@A**:+?@ >K'
=J62AJv!eg.] 	$ '
 >K'
=J62AJv!eg.] 	$ '
 +/*C*C'+
J' "":..'
'
s   C2*C7)r   r   r   N)
__name__
__module____qualname____firstlineno____doc__r   r   r-   r   __static_attributes__r
   r   r   r   r      s    76
L $/r   r   c                    [        U5      nU  VVs/ s H/  n[        [        U5       Vs/ s H  oDS   S:w  d  M  UPM     sn6 PM1     nnnU H  u  pgSR                  U5      n[	        U5      S:X  aL  US:X  a  UR                  US   5        UR                  US   5        US:X  a  UR                  US   5        Mo  Mq  [        5       R                  [	        U5      S-
  USS SSS2   S9n	US   S:X  a  UR                  US   5        UR                  X5        US   S:X  d  M  UR                  US   5        M     U$ s  snf s  snnf )	z
Generates a phase oracle for the boolean function f given in ESOP (Exlusive sum of products) form
esop is of the form ('01-1', '11-0', ...) etc
where 1 is the variable, 0 is negated variable and - is don't care
r   r     r0   r   N
ctrl_state)
r   zip	enumeratejoinr   xzr   controlr$   )
r   
num_qubitsqcr5   
qubit_dataclause_dataqubit_indicescontrol_datacontrol_stategates
             r   synth_phase_oracle_from_esoprR   M   sV    

	#B F 
If,=V,=jARUAUz,=V	W   (3#-}"#]1%&DDq!"#]1%& $ 7??3}#5#9mTWUWFXY][]Y]F^?_DR C']2&'IId*R C']2&' (3  I' Ws   ED=D=E=Ec                 h   US-
  n[        U5      nU  VVs/ s H/  n[        [        U5       Vs/ s H  oUS   S:w  d  M  UPM     sn6 PM1     nnnU HR  u  pxSR                  U5      n	[	        5       R                  [        U5      U	SSS2   S9n
UR                  XU4-   5        MT     U$ s  snf s  snnf )z
Generates a bit-flip oracle for the boolean function f given in ESOP (Exlusive sum of products) form
esop is of the form ('01-1', '11-0', ...) etc
where 1 is the variable, 0 is negated variable and - is don't care
r   r    r@   NrA   rB   )r   rD   rE   rF   r   rI   r   r$   )r   rJ   output_indexrK   r5   rL   rM   rN   rO   rP   rQ   s              r   synth_bit_oracle_from_esoprU   k   s     >L	
	#B F 
If,=V,=jARUAUz,=V	W   (3#-ws=1mDbD>QR
		$78	 (3
 I Ws   B.B)B)B.)B.)
r=   r!   qiskit.circuitr   qiskit.circuit.libraryr   r   r   rR   rU   r
   r   r   <module>rX      s)    G  ) /7/ 7/t<r   