§
    º³Ùgî)  ã                  ó¸   — d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ erdd	lmZ 	 	 	 	 	 d#d$d„Z	 	 d%d&d„Z	 d'd(d„Zd)d „Zd*d"„Zd
S )+z+Module for common pulse programming macros.é    )Úannotations)ÚSequence)ÚTYPE_CHECKING)ÚchannelsÚ
exceptionsÚinstructionsÚutils)ÚInstructionScheduleMap)ÚSchedule)Ú	BackendV2)ÚTargetNÚmeasureÚqubitsúSequence[int]Úinst_mapúInstructionScheduleMap | NoneÚmeas_mapú-list[list[int]] | dict[int, list[int]] | NoneÚqubit_mem_slotsúdict[int, int] | NoneÚmeasure_nameÚstrÚreturnr   c                ó¶  — t          |t          ¦  «        rWt          | |j        |p|j        |p6t          t          | t          t          | ¦  «        ¦  «        ¦  «        ¦  «        |¬¦  «        S 	 t          | |p| 
                    ¦   «         j        |p|                     ¦   «         j        ||¬¦  «        S # t          $ r}t          j        d¦  «        |‚d}~ww xY w)ad  Return a schedule which measures the requested qubits according to the given
    instruction mapping and measure map, or by using the defaults provided by the backend.

    .. note::
        This function internally dispatches schedule generation logic depending on input backend model.
        For the :class:`.BackendV1`, it considers conventional :class:`.InstructionScheduleMap`
        and utilizes the backend calibration defined for a group of qubits in the `meas_map`.
        For the :class:`.BackendV2`, it assembles calibrations of single qubit measurement
        defined in the backend target to build a composite measurement schedule for `qubits`.

    By default, the measurement results for each qubit are trivially mapped to the qubit
    index. This behavior is overridden by qubit_mem_slots. For instance, to measure
    qubit 0 into MemorySlot(1), qubit_mem_slots can be provided as {0: 1}.

    Args:
        qubits: List of qubits to be measured.
        backend (Union[Backend, BaseBackend]): A backend instance, which contains
            hardware-specific data required for scheduling.
        inst_map: Mapping of circuit operations to pulse schedules. If None, defaults to the
                  ``instruction_schedule_map`` of ``backend``.
        meas_map: List of sets of qubits that must be measured together. If None, defaults to
                  the ``meas_map`` of ``backend``.
        qubit_mem_slots: Mapping of measured qubit index to classical bit index.
        measure_name: Name of the measurement schedule.

    Returns:
        A measurement schedule corresponding to the inputs provided.
    )r   Útargetr   r   r   )r   r   r   r   r   z?inst_map or meas_map, and backend cannot be None simultaneouslyN)Ú
isinstancer   Ú_measure_v2r   r   ÚdictÚzipÚrangeÚlenÚ_measure_v1ÚdefaultsÚinstruction_schedule_mapÚconfigurationÚAttributeErrorr   Ú
PulseError)r   Úbackendr   r   r   r   Úexs          úI/granary/edev/quantum/lib/python3.11/site-packages/qiskit/pulse/macros.pyr   r      sû   € õL �'�9Ñ%Ô%ð åØØ”>ØÐ1 Ô!1Ø+ÐT­tµC¸ÅÅcÈ&ÁkÄkÑ@RÔ@RÑ4SÔ4SÑ/TÔ/TØ%ð
ñ 
ô 
ð 	
ð	ÝØØ!ÐP W×%5Ò%5Ñ%7Ô%7Ô%PØ!ÐE W×%:Ò%:Ñ%<Ô%<Ô%EØ /Ø)ðñ ô ð øõ ð 	ð 	ð 	ÝÔ'ØQñô àðøøøøð	øøøs   Á.AB4 Â4
CÂ>CÃCr
   ú&list[list[int]] | dict[int, list[int]]c                ó²  — t          d| › �¬¦  «        }t          |t          ¦  «        rt          j        |¦  «        }t          ¦   «         }| D ]*}|                     t          ||         ¦  «        ¦  «         Œ+|D �][}|€t          ¦   «         n0t          |¦  «        t          |                     ¦   «         ¦  «        z
  }		 | 	                    ||¦  «        }
n7# t          j        $ r%}t          j        d|› d|j        › �¦  «        |‚d}~ww xY w|
j        D ]À\  }}|j        j        | vrŒ|r”t          |t          j        ¦  «        rz|j        j        |v r%t!          j        ||j        j                 ¦  «        }n&t!          j        |	                     ¦   «         ¦  «        }t          j        |j        |j        |¬¦  «        }|                     ||¦  «        }ŒÁ�Œ]|S )aÝ  Return a schedule which measures the requested qubits according to the given
    instruction mapping and measure map, or by using the defaults provided by the backendV1.

    Args:
        qubits: List of qubits to be measured.
        backend (Union[Backend, BaseBackend]): A backend instance, which contains
            hardware-specific data required for scheduling.
        inst_map: Mapping of circuit operations to pulse schedules. If None, defaults to the
                  ``instruction_schedule_map`` of ``backend``.
        meas_map: List of sets of qubits that must be measured together. If None, defaults to
                  the ``meas_map`` of ``backend``.
        qubit_mem_slots: Mapping of measured qubit index to classical bit index.
        measure_name: Name of the measurement schedule.
    Returns:
        A measurement schedule corresponding to the inputs provided.
    Raises:
        PulseError: If both ``inst_map`` or ``meas_map``, and ``backend`` is None.
    ú(Default measurement schedule for qubits ©ÚnameNú9We could not find a default measurement schedule called 'ú‚'. Please provide another name using the 'measure_name' keyword argument. For assistance, the instructions which are defined are: ©Úmem_slot)r   r   Úlistr	   Úformat_meas_mapÚsetÚaddÚtupleÚvaluesÚgetr   r'   r   ÚchannelÚindexÚAcquirer   Ú
MemorySlotÚpopÚdurationÚinsert)r   r   r   r   r   ÚscheduleÚmeasure_groupsÚqubitÚmeasure_group_qubitsÚunused_mem_slotsÚdefault_schedr)   ÚtimeÚinstr3   s                  r*   r"   r"   \   s  € õ4 ÐPÈÐPÐPÐQÑQÔQ€Hå�(�DÑ!Ô!ð 3ÝÔ(¨Ñ2Ô2ˆå‘U”U€NØð 3ð 3ˆØ×Ò�5 ¨%¤Ñ1Ô1Ñ2Ô2Ð2Ð2Ø .ð 3ñ 3Ðð Ð&õ ‰EŒEˆEåÐ)Ñ*Ô*­S°×1GÒ1GÑ1IÔ1IÑ-JÔ-JÑJð 	ð	Ø$ŸLšL¨Ð7KÑLÔLˆMˆMøÝÔ$ð 	ð 	ð 	ÝÔ'ð+ÈLð +ð +ð Ô(ð+ð +ñô ð
 ðøøøøð	øøøð (Ô4ð 
	3ð 
	3‰JˆD�$ØŒ|Ô!¨Ð/Ð/ØØð \¥:¨dµLÔ4HÑ#IÔ#Ið \Ø”<Ô%¨Ð8Ð8Ý'Ô2°?À4Ä<ÔCUÔ3VÑWÔW�H�Hå'Ô2Ð3C×3GÒ3GÑ3IÔ3IÑJÔJ�HÝ#Ô+¨D¬M¸4¼<ÐRZÐ[Ñ[Ô[�à—’ t¨TÑ2Ô2ˆHˆHñ
	3ð €Os   Â?CÃD
Ã% DÄD
r   r   údict[int, int]c                óò  — t          d| › �¬¦  «        }t          |t          ¦  «        rt          j        |¦  «        }t          ¦   «         }| D ]}|t          ||         ¦  «        z  }Œt          |¦  «        }t          t          t          |¦  «        dz   ¦  «        ¦  «        }t          t          |¦  «        | 	                    ¦   «         z
  ¦  «        }	t          |t          | 
                    ¦   «         ¦  «        z
  d¬¦  «        }
t          | 
                    ¦   «         ¦  «                             |¦  «        r|	D ]}|
                     ¦   «         ||<   Œ|D ]�}	 || v rd|                     ||f¦  «                             t          j        |¦  «        t          j        |¦  «        g¬¦  «        }|t%          ||¦  «        z  }Œl# t&          $ r%}t)          j        d|› d|j        › �¦  «        |‚d	}~ww xY w|S )
aG  Return a schedule which measures the requested qubits according to the given
    target and measure map, or by using the defaults provided by the backendV2.

    Args:
        qubits: List of qubits to be measured.
        target: The :class:`~.Target` representing the target backend.
        meas_map: List of sets of qubits that must be measured together.
        qubit_mem_slots: Mapping of measured qubit index to classical bit index.
        measure_name: Name of the measurement schedule.

    Returns:
        A measurement schedule corresponding to the inputs provided.
    r-   r.   é   T)Úreverse)r   r0   r1   N)r   r   r4   r	   r5   r6   Úsortedr    ÚmaxÚkeysr9   Úissubsetr?   Ú_get_calibrationÚfilterr   ÚMeasureChannelÚAcquireChannelÚ_schedule_remapping_memory_slotÚKeyErrorr   r'   r   )r   r   r   r   r   rB   Ú
meas_grouprD   Úmeas_group_setÚunassigned_qubit_indicesÚunassigned_reg_indicesÚmeasure_qubitrG   r)   s                 r*   r   r   ž   s,  € õ( ÐPÈÐPÐPÐQÑQÔQ€Hå�(�DÑ!Ô!ð 3ÝÔ(¨Ñ2Ô2ˆÝ‘”€JØð +ð +ˆØ•c˜( 5œ/Ñ*Ô*Ñ*ˆ
ˆ
Ý˜
Ñ#Ô#€Jå��s :™œ°Ñ2Ñ3Ô3Ñ4Ô4€NÝ%¥c¨*¡o¤o¸×8LÒ8LÑ8NÔ8NÑ&NÑOÔOÐÝ# NµS¸×9OÒ9OÑ9QÔ9QÑ5RÔ5RÑ$RÐ\`ÐaÑaÔaÐÝ
ˆ?×!Ò!Ñ#Ô#Ñ$Ô$×-Ò-¨nÑ=Ô=ð BØ-ð 	Bð 	BˆEØ%;×%?Ò%?Ñ%AÔ%AˆO˜EÑ"Ð"à#ð ð ˆð	Ø Ð&Ð&Ø &× 7Ò 7¸À}ÐFVÑ WÔ W× ^Ò ^å Ô/°Ñ>Ô>Ý Ô/°Ñ>Ô>ðð !_ñ !ô !�ð Õ;¸MÈ?Ñ[Ô[Ñ[�øøÝð 	ð 	ð 	ÝÔ'ð)ÈLð )ð )ð Ô&ð)ð )ñô ð
 ðøøøøð	øøøð €Os   ÅA(GÇ
G4Ç G/Ç/G4c                óø   — t          | t          ¦  «        r"t          t          | j        ¦  «        ¦  «        }n3t          t          |                      ¦   «         j        ¦  «        ¦  «        }t          || ¬¦  «        S )a+  
    Return a Schedule which measures all qubits of the given backend.

    Args:
        backend (Union[Backend, BaseBackend]): A backend instance, which contains
            hardware-specific data required for scheduling.

    Returns:
        A schedule corresponding to the inputs provided.
    )r   r(   )r   r   r4   r    Ú
num_qubitsr%   Ún_qubitsr   )r(   r   s     r*   Úmeasure_allr`   Ö   sg   € õ �'�9Ñ%Ô%ð ?Ý•e˜GÔ.Ñ/Ô/Ñ0Ô0ˆˆå•e˜G×1Ò1Ñ3Ô3Ô<Ñ=Ô=Ñ>Ô>ˆÝ˜&¨'Ð2Ñ2Ô2Ð2ó    rB   c                ó’  — t          ¦   «         }| j        D ]°\  }}t          |t          j        ¦  «        ry|j        j        }|                     ||¦  «        }|                     |t          j        |j        t          j
        |¦  «        t          j        |¦  «        ¬¦  «        d¬¦  «         Œ˜|                     ||d¬¦  «         Œ±|S )a*  
    A helper function to overwrite MemorySlot index of :class:`.Acquire` instruction.

    Args:
        schedule: A measurement schedule.
        qubit_mem_slots: Mapping of measured qubit index to classical bit index.

    Returns:
        A measurement schedule with new memory slot index.
    r2   T)Úinplace)r   r   r   r=   r;   r<   r:   rA   r@   r   rU   r>   )rB   r   Únew_scheduleÚt0rI   Úqubit_indexÚ	reg_indexs          r*   rV   rV   é   sÝ   € õ ‘:”:€LØÔ)ð 8ð 8‰ˆˆDÝ�d�LÔ0Ñ1Ô1ð 	8Øœ,Ô,ˆKØ'×+Ò+¨K¸ÑEÔEˆIØ×ÒØÝÔ$Ø”MÝÔ+¨KÑ8Ô8Ý%Ô0°Ñ;Ô;ðñ ô ð
 ð  ñ ô ð ð ð ×Ò  D°$ÐÑ7Ô7Ð7Ð7ØÐra   )NNNNr   )r   r   r   r   r   r   r   r   r   r   r   r   )Nr   )r   r   r   r
   r   r+   r   r   r   r   r   r   )r   )r   r   r   r   r   r+   r   rJ   r   r   r   r   )r   r   )rB   r   r   rJ   r   r   )Ú__doc__Ú
__future__r   Úcollections.abcr   Útypingr   Úqiskit.pulser   r   r   r	   Ú%qiskit.pulse.instruction_schedule_mapr
   Úqiskit.pulse.scheduler   Úqiskit.providers.backendr   Úqiskit.transpilerr   r   r"   r   r`   rV   © ra   r*   ú<module>rr      sW  ðð 2Ð 1Ø "Ð "Ð "Ð "Ð "Ð "à $Ð $Ð $Ð $Ð $Ð $Ø  Ð  Ð  Ð  Ð  Ð  à BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BØ HÐ HÐ HÐ HÐ HÐ HØ *Ð *Ð *Ð *Ð *Ð *Ø .Ð .Ð .Ð .Ð .Ð .ð ð )Ø(Ð(Ð(Ð(Ð(Ð(ð
 Ø.2Ø>BØ-1Ø!ð<ð <ð <ð <ð <ðF .2Ø!ð?ð ?ð ?ð ?ð ?ðN "ð5ð 5ð 5ð 5ð 5ðp3ð 3ð 3ð 3ð&ð ð ð ð ð ra   