
    z	i              	          S 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JrJrJrJrJrJrJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  \" 5       \" 5       \	" 5       \
" 5       S.r\\\\\\\\S.r " S S\5      rg)zAReplace each block of consecutive gates by a single Unitary node.    )annotations)TwoQubitBasisDecomposerTwoQubitControlledUDecomposer)CXGateCZGate	iSwapGateECRGateRXXGateRYYGateRZZGateRZXGateCRXGateCRYGateCRZGate
CPhaseGate)TransformationPass)PassManager)consolidate_blocks   )Collect1qRuns)Collect2qBlocks)cxcziswapecr)rxxrzzryyrzxcphasecrxcrycrzc                  J   ^  \ rS rSrSrSr     SU 4S jjrS rS rSr	U =r
$ )	ConsolidateBlocks:   aS  Replace each block of consecutive gates by a single Unitary node.

Pass to consolidate sequences of uninterrupted gates acting on
the same qubits into a Unitary node, to be resynthesized later,
to a potentially more optimal subcircuit.

This pass reads the :class:`.PropertySet` key ``ConsolidateBlocks_qubit_map`` which it uses to
communicate with recursive worker instances of itself for control-flow operations.  The key
should never be observable in a user-facing :class:`.PassManager` pipeline (it is only set in
internal :class:`.PassManager` instances), but the pass may return incorrect results or error if
another pass sets this key.

Notes:
    This pass assumes that the 'blocks_list' property that it reads is
    given such that blocks are in topological order. The blocks are
    collected by a previous pass, such as `Collect2qBlocks`.
ConsolidateBlocks_qubit_mapc                  > [         TU ]  5         SU l        SU l        Ub  [	        UR
                  5      S:  a  UOSU l        Ub  [        U5      U l        X l        Ub"  [        U5      U l
        UR                  U l        gUb  [        R                  5       U=(       d    / -  n[        R                  5       U=(       d    / -  nU(       a7  [        [        [!        U5      S      5      U l
        [!        U5      S   U l        gU(       a?  [        [        [!        U5      S      U=(       d    SS9U l
        [!        U5      S   U l        gSU l
        g[        [#        5       5      U l
        SU l        g)a~  ConsolidateBlocks initializer.

If ``kak_basis_gate`` is not ``None`` it will be used as the basis gate for KAK decomposition.
Otherwise, if ``basis_gates`` is not ``None`` a basis gate will be chosen from this list.
Otherwise, the basis gate will be :class:`.CXGate`.

Args:
    kak_basis_gate (Gate): Basis gate for KAK decomposition.
    force_consolidate (bool): Force block consolidation.
    basis_gates (List(str)): Basis gates from which to choose a KAK gate.
    approximation_degree (float): a float between :math:`[0.0, 1.0]`. Lower approximates more.
    target (Target): The target object for the compilation target backend.
Nr         ?)basis_fidelityr   )super__init__basis_gatesbasis_gate_namelenoperation_namestargetsetforce_consolidater   
decomposernameKAK_GATE_NAMESkeysKAK_GATE_PARAM_NAMESr   listr   )	selfkak_basis_gater3   r-   approximation_degreer1   	kak_gateskak_param_gates	__class__s	           r/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/optimization/consolidate_blocks.pyr,   ConsolidateBlocks.__init__O   sA   * 	# !' 2s6;Q;Q7RUV7Vf\`"";/D!2%5nEDO#1#6#6D $&++-1BCI2779[=NBOO"?(o)>q)AB# (,O'<Q'?$"9"4	?1#56G[Gb_b# (,Iq'9$"&5fh?DO#'D     c                   U R                   c  U$ U R                  S   nUb-  U VVs/ s H  o3 Vs/ s H  oDR                  PM     snPM      nnnU R                  S   nUb-  U VVs/ s H  of Vs/ s H  oDR                  PM     snPM      nnnU R                  R                  U R                  S5      nUc"  [        [        UR                  5       5      5      n[        UU R                   R                  U R                  U R                  U R                  U R                  UUUS9	  U R                  X5      nSU R                  ;   a  U R                  S	 SU R                  ;   a  U R                  S	 U$ s  snf s  snnf s  snf s  snnf )zRun the ConsolidateBlocks pass on `dag`.

Iterate over each block and replace it with an equivalent Unitary
on the same wires.
N
block_listrun_list)r1   r-   blocksruns	qubit_map)r4   property_set_node_idget_QUBIT_MAP_KEYr9   range
num_qubitsr   _inner_decomposerr.   r3   r1   r-   _handle_control_flow_ops)r:   dagrF   blocknoderG   runrH   s           r@   rT   ConsolidateBlocks.run   sg    ??"J""<0EKLVE7}}7VFL  ,?CDts3st]]s3tDD%%))$*=*=tD	U3>>#345IOO--  "";;((
	
 ++C; ***!!*-4,,,!!,/
7 8L 4Ds.   	E8E3E8&	F/E>F3E8>Fc                  ^ ^^ [        5       mST R                  ;   a2  TR                  [        5       5        TR                  [	        5       5        TR                  T 5        UR                  5        H  nUR                   Vs/ s H   oBUR                  U5      R                     PM"     snmUR                  R                  UUU 4S jUR                  R                   5       5      nUR                  X55        M     U$ s  snf )z
This is similar to transpiler/passes/utils/control_flow.py except that the
collect blocks is redone for the control flow blocks.
rE   c              3  \   >#    U  H!  nTR                  UTR                  T0S 9v   M#     g7f))rI   N)rT   rL   ).0rR   inner_qubit_mappass_managerr:   s     r@   	<genexpr>=ConsolidateBlocks._handle_control_flow_ops.<locals>.<genexpr>   s3      ,+E   d6I6I?5[ \+s   ),)r   rI   appendr   r   control_flow_op_nodesqargsfind_bitindexopreplace_blocksrF   substitute_node)r:   rQ   rH   rS   qnew_oprY   rZ   s   `     @@r@   rP   *ConsolidateBlocks._handle_control_flow_ops   s     #}***0 12D!--/DIMTAa)>)>?TOWW++ ,!WW^^, F - 0 
 Us   'C>)r.   r-   r4   r3   r1   )NFNr)   N)__name__
__module____qualname____firstlineno____doc__rL   r,   rT   rP   __static_attributes____classcell__)r?   s   @r@   r%   r%   :   s6    $ 3N  2(h&P rB   r%   N) rl   
__future__r   qiskit.synthesis.two_qubitr   r   %qiskit.circuit.library.standard_gatesr   r   r   r	   r
   r   r   r   r   r   r   r   qiskit.transpiler.basepassesr   qiskit.transpiler.passmanagerr   %qiskit._accelerate.consolidate_blocksr   collect_1q_runsr   collect_2q_blocksr   r6   r8   r%    rB   r@   <module>rx      s    H " ]    < 5 D * . (
([9	 	 D* DrB   