
    z	i                        S r SSKJr  SSKrSSKJrJr  SSKJr  SSK	J
r
  SSKJr  \R                  (       a  SSKJr   " S	 S
\5      rg)zLTransform a circuit with virtual qubits into a circuit with physical qubits.    )annotationsN)apply_layoutupdate_layout)TransformationPass)TranspilerError)TranspileLayout)Targetc                  *    \ rS rSrSrS rS rS rSrg)ApplyLayout   ab  Apply or update the mapping of virtual qubits to physical qubits in the :class:`.DAGCircuit`.

A "layout" in Qiskit is a mapping of virtual qubits (as the user typically creates circuits in
terms of) to the physical qubits used to represent them on hardware.

This pass has two modes of operation, depending on the state of the ``layout`` and
``post_layout`` keys in the :class:`.PropertySet`:

1. Standard operation: ``post_layout`` is not set.  This takes in a :class:`.DAGCircuit` defined
   over virtual qubits, and rewrites it in terms of physical qubits.  In this case, the
   ``layout`` field must have been chosen by a layout pass (for example :class:`.SetLayout` or
   :class:`.VF2Layout`), and both it and the :class:`.DAGCircuit` must have been expanded with
   ancillas (see :class:`.EnlargeWithAncilla` and :class:`.FullAncillaAllocation`).

2. Improving a layout: ``post_layout`` is set (such as by :class:`.VF2PostLayout`).  In this
   case, the ``post_layout`` must already be the correct size.  It is interpreted as an
   _additional_ relabelling on top of the relabelling that is already applied to the input
   :class:`.DAGCircuit`.

   After the pass runs, the ``layout`` field will be updated to represent the composition of the
   two relabellings, as will the :class:`.DAGCircuit` and any final permutation.  The
   ``post_layout`` field will be removed.
c                    U R                   R                  SS5      =nb  U R                  X5      $ U R                  U5      $ )zRun 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.
post_layoutN)property_setpop_apply_post_layout_apply_layout)selfdagr   s      f/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/transpiler/passes/layout/apply_layout.pyrunApplyLayout.run5   sD      ,,00EEKR**3<<!!#&&    c           
     P   U R                   R                  SS 5      nUc  [        S5      e[        U5      S[	        UR                  5       SS9-   :w  a  [        S5      e[        U5      n[        R                  " XR                   5      nUc  S nU R                   R                  SS 5      nOUR                  5       nUR                  nUc  UR                  5       n[        UUUUR                   Vs/ s H  orU   PM	     snU5      nUR                  U R                   5        U$ s  snf )NlayoutzJNo 'layout' is found in property_set. Please run a Layout pass in advance.   )defaultz)The 'layout' must be full (with ancilla).num_input_qubits)r   r   r   lenmaxget_physical_bitsr   from_property_setgetrouting_permutation_input_qubit_count
num_qubitsr   qubitswrite_into_property_set)	r   r   r   num_physical_qubitsprev_layoutpermutationnum_virtual_qubitsqubittranspile_layouts	            r   r   ApplyLayout._apply_layoutE   s!    ""&&x6>!\  v;1s6#;#;#=rJJK!"MNN!&k%77=N=NOK!%!2!2!6!67I4!P%99;K!,!?!?%!$!1'(+

3
uE]
3
 	001B1BC
	 4s   0D#c           	         [         R                  " XR                  5      n[        XUR                   Vs/ s H  oBU   PM	     sn5      nUR                  U R                  5        U$ s  snf )N)r   r"   r   r   r'   r(   )r   r   r   r.   qs        r   r   ApplyLayout._apply_post_layoute   s_    *<<SBSBST(CJJ#GJqNJ#G
 	001B1BC
 $Hs   A' N)	__name__
__module____qualname____firstlineno____doc__r   r   r   __static_attributes__r3   r   r   r   r      s    0' @r   r   )r8   
__future__r   typingqiskit._accelerate.apply_layoutr   r   qiskit.transpiler.basepassesr   qiskit.transpiler.exceptionsr   qiskit.transpiler.layoutr   TYPE_CHECKINGqiskit.transpilerr	   r   r3   r   r   <module>rB      s8    S "  G ; 8 4	(O$ Or   