ó
    Óz	iM  ã                   óº   • S r SSK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  SSKJ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Jr   " S S\5      rg)aj  
Given a template and a circuit: it applies template matching and substitutes
all compatible maximal matches that reduces the size of the circuit.

**Reference:**

[1] Iten, R., Moyard, R., Metger, T., Sutter, D. and Woerner, S., 2020.
Exact and practical pattern matching for quantum circuit optimization.
`arXiv:1909.05270 <https://arxiv.org/abs/1909.05270>`_
é    N)ÚQuantumCircuit)ÚDAGDependency)Úcircuit_to_dagdependency)Údagdependency_to_circuit)Údag_to_dagdependency)Údagdependency_to_dag)ÚTransformationPass)Útemplate_nct_2a_1Útemplate_nct_2a_2Útemplate_nct_2a_3)ÚOperator)ÚTranspilerError)ÚTemplateMatchingÚTemplateSubstitutionÚMaximalMatchesc                   ó>   ^ • \ rS rSrSr    SU 4S jjrS rSrU =r$ )ÚTemplateOptimizationé*   z+
Class for the template optimization pass.
c                 óª   >• [         TU ]  5         Uc  [        5       [        5       [	        5       /nXl        Ub  UO/ U l        Ub  UO/ U l        X@l        g)aë  
Args:
    template_list (list[QuantumCircuit()]): list of the different template circuit to apply.
    heuristics_backward_param (list[int]): [length, survivor] Those are the parameters for
        applying heuristics on the backward part of the algorithm. This part of the
        algorithm creates a tree of matching scenario. This tree grows exponentially. The
        heuristics evaluate which scenarios have the longest match and keep only those.
        The length is the interval in the tree for cutting it and survivor is the number
        of scenarios that are kept. We advise to use l=3 and s=1 to have serious time
        advantage. We remind that the heuristics implies losing a part of the maximal
        matches. Check reference for more details.
    heuristics_qubits_param (list[int]): [length] The heuristics for the qubit choice make
        guesses from the dag dependency of the circuit in order to limit the number of
        qubit configurations to explore. The length is the number of successors or not
        predecessors that will be explored in the dag dependency of the circuit, each
        qubits of the nodes are added to the set of authorized qubits. We advise to use
        length=1. Check reference for more details.
    user_cost_dict (Dict[str, int]): quantum cost dictionary passed to TemplateSubstitution
        to configure its behavior. This will override any default values if None
        is not given. The key is the name of the gate and the value its quantum cost.
N)	ÚsuperÚ__init__r
   r   r   Útemplate_listÚheuristics_qubits_paramÚheuristics_backward_paramÚuser_cost_dict)Úselfr   r   r   r   Ú	__class__s        €Úu/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/optimization/template_optimization.pyr   ÚTemplateOptimization.__init__/   sf   ø€ ô8 	‰ÑÔàÑ Ü.Ó0Ô2CÓ2EÔGXÓGZÐ[ˆMØ*Ôà'>Ñ'JÑ#ÐPRð 	Ô$ð *CÑ)NÑ%ÐTVð 	Ô&ð -Õó    c                 ó   • Un[        U5      nU R                   GHÔ  n[        U[        [        45      (       d  [        S5      e[        UR                  5      [        UR                  5      :”  a  MX  [        R                  " S[        UR                  5      -  [        S9n [        U[        5      (       a  [        [        U5      5      R                  nO[        U5      R                  n[        R                  " Xe5      nU(       d  [        S5      e [        U[        5      (       a  [!        U5      nOUn[#        UUU R$                  U R&                  5      n	U	R)                  5         U	R*                  n
U
(       ar  [-        U
5      nUR/                  5         UR0                  n[3        UU	R4                  U	R6                  U R8                  5      nUR;                  5         UR<                  nGMÔ  GM×     [?        U5      nU$ ! [         a     Nøf = f)zâ
Args:
    dag(DAGCircuit): DAG circuit.
Returns:
    DAGCircuit: optimized DAG circuit.
Raises:
    TranspilerError: If the template has not the right form or
     if the output circuit acts differently as the input circuit.
z1A template is a Quantumciruit or a DAGDependency.é   )Údtypez;A template is a Quantumciruit() that performs the identity.) r   r   Ú
isinstancer   r   r   ÚlenÚqubitsÚnpÚidentityÚcomplexr   r   ÚdataÚallcloseÚ	TypeErrorr   r   r   r   Úrun_template_matchingÚ
match_listr   Úrun_maximal_matchesÚmax_match_listr   Úcircuit_dag_depÚtemplate_dag_depr   Úrun_dag_optÚdag_dep_optimizedr   )r   ÚdagÚcircuit_dagr1   Útemplater(   r*   Ú
comparisonr2   Ú
template_mÚmatchesÚmaximalÚmax_matchesÚsubstitutions                 r   ÚrunÚTemplateOptimization.runY   sÈ  € ð ˆÜ.¨{Ó;ˆà×*Õ*ˆHÜ˜h¬¼Ð(G×HÑHÜ%Ð&YÓZÐZä8—?‘?Ó#¤c¨/×*@Ñ*@Ó&AÓAÙä—{’{ 1¬¨H¯O©OÓ(<Ñ#<ÄGÑLˆHðÜ˜h¬×6Ñ6Ü#Ô$<¸XÓ$FÓG×LÑL‘Dä# HÓ-×2Ñ2DäŸ[š[¨Ó8
æ!Ü)ØUóð ð "ô ˜(¤N×3Ñ3Ü#;¸HÓ#EÑ à#+Ð ä)ØØ Ø×,Ñ,Ø×.Ñ.ó	ˆJð ×,Ñ,Ô.à ×+Ñ+ˆGæÜ(¨Ó1Ø×+Ñ+Ô-Ø%×4Ñ4ä3ØØ×.Ñ.Ø×/Ñ/Ø×'Ñ'ó	 ð ×(Ñ(Ô*à".×"@Ñ"@“âñm +ôn +¨?Ó;ˆØÐøôI ó Ùðús   Â"A1H È 
HÈH)r   r   r   r   )NNNN)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r>   Ú__static_attributes__Ú__classcell__)r   s   @r   r   r   *   s(   ø† ñð Ø $Ø"&Ø÷(-÷TEð Er    r   )rD   Únumpyr'   Úqiskit.circuit.quantumcircuitr   Úqiskit.dagcircuitr   Ú*qiskit.converters.circuit_to_dagdependencyr   Ú*qiskit.converters.dagdependency_to_circuitr   Ú&qiskit.converters.dag_to_dagdependencyr   Ú&qiskit.converters.dagdependency_to_dagr   Úqiskit.transpiler.basepassesr	   Ú qiskit.circuit.library.templatesr
   r   r   Ú&qiskit.quantum_info.operators.operatorr   Úqiskit.transpiler.exceptionsr   Ú7qiskit.transpiler.passes.optimization.template_matchingr   r   r   r   © r    r   Ú<module>rT      sI   ðñ	ó å 8Ý +Ý OÝ OÝ GÝ GÝ ;ß dÑ dÝ ;Ý 8÷ñ ôtÐ-õ tr    