§
    º³Ùg8  ã                   óf   — d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ  G d„ de¦  «        Zd	S )
zLTransform a circuit with virtual qubits into a circuit with physical qubits.é    )ÚQuantumRegister)Ú
DAGCircuitÚ	DAGOpNode)ÚTransformationPass)ÚTranspilerError)ÚLayoutc                   ó   — e Zd ZdZd„ ZdS )ÚApplyLayoutaø  Transform a circuit with virtual qubits into a circuit with physical qubits.

    Transforms a DAGCircuit with virtual qubits into a DAGCircuit with physical qubits
    by applying the Layout given in `property_set`.
    Requires either of passes to set/select Layout, e.g. `SetLayout`, `TrivialLayout`.
    Assumes the Layout has full physical qubits.

    If a post layout pass is run and sets the ``post_layout`` property set field with
    a new layout to use after ``ApplyLayout`` has already run once this pass will
    compact the layouts so that we apply
    ``original_virtual`` -> ``existing_layout`` -> ``new_layout`` -> ``new_physical``
    so that the output circuit and layout combination become:
    ``original_virtual`` -> ``new_physical``
    c                 óÌ  ‡‡‡‡‡‡— | j         d         }|st          d¦  «        ‚t          |¦  «        dt          |                     ¦   «         ¦  «        z   k    rt          d¦  «        ‚| j         d         }t          t          |¦  «        d¦  «        Št          ¦   «         Š‰j        ‰_        ‰                     ‰¦  «         ‰ 	                    ¦   «         D ]}‰ 
                    |¦  «         Œ‰                     ¦   «         D ]}‰                     |¦  «         Œ‰                     ¦   «         D ]}‰                     |¦  «         Œ‰j        ‰_        ‰                     ‰j        ¦  «         ‰j                             ¦   «         D ]}‰                     |¦  «         Œ|€òd„ t+          ‰j        ¦  «        D ¦   «         | j         d	<   ‰j                             ¦   «         D ]"}| j         d                              |¦  «         Œ#|                     ¦   «         Š‰                     ¦   «         D ]e}ˆˆfd
„|j        D ¦   «         }‰                     t;          j        |                     ¦   «                               |¬¦  «        ¦  «        d¬¦  «         Œf�nÇtC          ¦   «         }	|                     ¦   «         }
|                     ¦   «         ŠtE          tG          t          ‰j        ¦  «        ¦  «        ¦  «        Š‰ $                    ¦   «         D ]B\  }}‰ %                    |¦  «        j&        }|
|         }|‰|<   |	 '                    ||¦  «         ŒC| (                    ¦   «         D ]}|	                     |¦  «         Œ‰                     ¦   «         D ]e}ˆˆfd„|j        D ¦   «         }‰                     t;          j        |                     ¦   «                               |¬¦  «        ¦  «        d¬¦  «         Œf|	| j         d<   | j         d         x}�Mˆˆˆfd„|                     ¦   «          $                    ¦   «         D ¦   «         }tC          |¦  «        }|| j         d<   ‰j)        ‰_)        ‰S )a)  Run the ApplyLayout pass on ``dag``.

        Args:
            dag (DAGCircuit): DAG to map.

        Returns:
            DAGCircuit: A mapped DAG (with physical qubits).

        Raises:
            TranspilerError: if no layout is found in ``property_set`` or no full physical qubits.
        ÚlayoutzJNo 'layout' is found in property_set. Please run a Layout pass in advance.é   z)The 'layout' must be full (with ancilla).Úpost_layoutÚqNc                 ó   — i | ]\  }}||“Œ	S © r   )Ú.0ÚindexÚbits      úb/granary/edev/quantum/lib/python3.11/site-packages/qiskit/transpiler/passes/layout/apply_layout.pyú
<dictcomp>z#ApplyLayout.run.<locals>.<dictcomp>K   s+   € ð ;ð ;ð ;Ù)˜u c��Uð;ð ;ð ;ó    Úoriginal_qubit_indicesc                 ó,   •— g | ]}‰‰|                  ‘ŒS r   r   )r   Úqargr   Úvirtual_physical_maps     €€r   ú
<listcomp>z#ApplyLayout.run.<locals>.<listcomp>R   s$   ø€ ÐNÐNÐN¸4˜Ð/°Ô5Ô6ÐNÐNÐNr   )ÚqubitsF)Úcheckc                 ó,   •— g | ]}‰‰|                  ‘ŒS r   r   )r   r   Únew_virtual_to_physicalr   s     €€r   r   z#ApplyLayout.run.<locals>.<listcomp>k   s$   ø€ ÐQÐQÐQ¸d˜Ð2°4Ô8Ô9ÐQÐQÐQr   Úfinal_layoutc                 óz   •— i | ]7\  }}‰j         ‰‰                     |¦  «        j                          ‰|         “Œ8S r   )r   Úfind_bitr   )r   Úold_virtÚold_physÚdagÚnew_dagÚphys_maps      €€€r   r   z#ApplyLayout.run.<locals>.<dictcomp>t   sP   ø€ ð (ð (ð (á*˜ (ð ”N 8¨C¯LªL¸Ñ,BÔ,BÔ,HÔ#IÔJÈHÐU]ÔL^ð(ð (ð (r   )*Úproperty_setr   ÚlenÚmaxÚget_physical_bitsr   r   ÚnameÚadd_qregÚiter_input_varsÚadd_input_varÚiter_captured_varsÚadd_captured_varÚiter_declared_varsÚadd_declared_varÚmetadataÚ
add_clbitsÚclbitsÚcregsÚvaluesÚadd_cregÚ	enumerater   ÚqregsÚadd_registerÚget_virtual_bitsÚtopological_op_nodesÚqargsÚ_apply_op_node_backr   Úfrom_instructionÚ_to_circuit_instructionÚreplacer   ÚlistÚrangeÚitemsr#   r   ÚaddÚget_registersÚglobal_phase)Úselfr&   r   r   ÚvarÚcregÚqregÚnoder@   Úfull_layoutÚold_phys_to_virtualÚnew_virtÚnew_physr%   r$   Úregr!   Úfinal_layout_mappingÚ
out_layoutr'   r    r(   r   r   s    `                 @@@@@r   ÚrunzApplyLayout.run&   sÆ  øøøøøø€ ð Ô" 8Ô,ˆØð 	Ý!Ø\ñô ð õ ˆv‰;Œ;˜1�s 6×#;Ò#;Ñ#=Ô#=Ñ>Ô>Ñ>Ò?Ð?Ý!Ð"MÑNÔNÐNàÔ'¨Ô6ˆÝ�C ™KœK¨Ñ-Ô-ˆå‘,”,ˆØ”xˆŒØ×Ò˜ÑÔÐØ×&Ò&Ñ(Ô(ð 	'ð 	'ˆCØ×!Ò! #Ñ&Ô&Ð&Ð&Ø×)Ò)Ñ+Ô+ð 	*ð 	*ˆCØ×$Ò$ SÑ)Ô)Ð)Ð)Ø×)Ò)Ñ+Ô+ð 	*ð 	*ˆCØ×$Ò$ SÑ)Ô)Ð)Ð)Øœ<ˆÔØ×Ò˜3œ:Ñ&Ô&Ð&Ø”I×$Ò$Ñ&Ô&ð 	#ð 	#ˆDØ×Ò˜TÑ"Ô"Ð"Ð"ØÐð;ð ;Ý-6°s´zÑ-BÔ-Bð;ñ ;ô ;ˆDÔÐ6Ñ7ð œ	×(Ò(Ñ*Ô*ð ?ð ?�ØÔ! (Ô+×8Ò8¸Ñ>Ô>Ð>Ð>Ø#)×#:Ò#:Ñ#<Ô#<Ð Ø×0Ò0Ñ2Ô2ð ð �ØNÐNÐNÐNÐNÀ4Ä:ÐNÑNÔN�Ø×+Ò+ÝÔ.Ø×4Ò4Ñ6Ô6×>Ò>ÀeÐ>ÑLÔLñô ð  ð	 ,ñ ô ð ð ñõ !™(œ(ˆKØ"(×":Ò":Ñ"<Ô"<ÐØ&1×&BÒ&BÑ&DÔ&DÐ#Ý�E¥# g¤nÑ"5Ô"5Ñ6Ô6Ñ7Ô7ˆHØ&=×&CÒ&CÑ&EÔ&Eð 4ð 4Ñ"�˜(ØŸ<š<¨Ñ1Ô1Ô7�Ø.¨xÔ8�Ø%-�˜Ñ"Ø—’ ¨(Ñ3Ô3Ð3Ð3Ø×+Ò+Ñ-Ô-ð .ð .�Ø×(Ò(¨Ñ-Ô-Ð-Ð-à×0Ò0Ñ2Ô2ð ð �ØQÐQÐQÐQÐQÀdÄjÐQÑQÔQ�Ø×+Ò+ÝÔ.Ø×4Ò4Ñ6Ô6×>Ò>ÀeÐ>ÑLÔLñô ð  ð	 ,ñ ô ð ð ð +6ˆDÔ˜hÑ'Ø $Ô 1°.Ô AÐA�ÐNð(ð (ð (ð (ð (ð (à.:×.KÒ.KÑ.MÔ.M×.SÒ.SÑ.UÔ.Uð(ñ (ô (Ð$õ $Ð$8Ñ9Ô9�
Ø4>�Ô! .Ñ1Ø"Ô/ˆÔàˆr   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rW   r   r   r   r
   r
      s2   € € € € € ðð ðVð Vð Vð Vð Vr   r
   N)r[   Úqiskit.circuitr   Úqiskit.dagcircuitr   r   Úqiskit.transpiler.basepassesr   Úqiskit.transpiler.exceptionsr   Úqiskit.transpiler.layoutr   r
   r   r   r   ú<module>ra      s­   ðð SÐ Rà *Ð *Ð *Ð *Ð *Ð *Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø +Ð +Ð +Ð +Ð +Ð +ðfð fð fð fð fÐ$ñ fô fð fð fð fr   