§
    ¹³Ùg2  ã                   ó^   — d Z ddlmZmZ ddlZddlmZmZm	Z	 ddl
mZ  G d„ de¦  «        ZdS )	z)Compute the weighted sum of qubit states.é    )ÚListÚOptionalN)ÚQuantumRegisterÚAncillaRegisterÚQuantumCircuité   )ÚBlueprintCircuitc            	       óp  ‡ — e Zd ZdZ	 	 	 ddee         deee                  deddfˆ fd„Ze	defd	„¦   «         Z
e	dee         fd
„¦   «         Zej        dee         ddfd„¦   «         Ze	defd„¦   «         Zej        deddfd„¦   «         Zd„ Ze	defd„¦   «         Ze	defd„¦   «         Zdd„Zˆ fd„Zˆ xZS )ÚWeightedAdderu8  A circuit to compute the weighted sum of qubit registers.

    Given :math:`n` qubit basis states :math:`q_0, \ldots, q_{n-1} \in \{0, 1\}` and non-negative
    integer weights :math:`\lambda_0, \ldots, \lambda_{n-1}`, this circuit performs the operation

    .. math::

        |q_0 \ldots q_{n-1}\rangle |0\rangle_s
        \mapsto |q_0 \ldots q_{n-1}\rangle |\sum_{j=0}^{n-1} \lambda_j q_j\rangle_s

    where :math:`s` is the number of sum qubits required.
    This can be computed as

    .. math::

        s = 1 + \left\lfloor \log_2\left( \sum_{j=0}^{n-1} \lambda_j \right) \right\rfloor

    or :math:`s = 1` if the sum of the weights is 0 (then the expression in the logarithm is
    invalid).

    For qubits in a circuit diagram, the first weight applies to the upper-most qubit.
    For an example where the state of 4 qubits is added into a sum register, the circuit can
    be schematically drawn as

    .. code-block:: text

                   â”Œâ”€â”€â”€â”€â”€â”€â”€â”€â”�
          state_0: â”¤0       â”œ | state_0 * weights[0]
                   â”‚        â”‚ |
          state_1: â”¤1       â”œ | + state_1 * weights[1]
                   â”‚        â”‚ |
          state_2: â”¤2       â”œ | + state_2 * weights[2]
                   â”‚        â”‚ |
          state_3: â”¤3       â”œ | + state_3 * weights[3]
                   â”‚        â”‚
            sum_0: â”¤4       â”œ |
                   â”‚  Adder â”‚ |
            sum_1: â”¤5       â”œ | = sum_0 * 2^0 + sum_1 * 2^1 + sum_2 * 2^2
                   â”‚        â”‚ |
            sum_2: â”¤6       â”œ |
                   â”‚        â”‚
          carry_0: â”¤7       â”œ
                   â”‚        â”‚
          carry_1: â”¤8       â”œ
                   â”‚        â”‚
        control_0: â”¤9       â”œ
                   â””â”€â”€â”€â”€â”€â”€â”€â”€â”˜
    NÚadderÚnum_state_qubitsÚweightsÚnameÚreturnc                 ó„   •— t          ¦   «                              |¬¦  «         d| _        d| _        || _        || _        dS )a:  Computes the weighted sum controlled by state qubits.

        Args:
            num_state_qubits: The number of state qubits.
            weights: List of weights, one for each state qubit. If none are provided they
                default to 1 for every qubit.
            name: The name of the circuit.
        ©r   N)ÚsuperÚ__init__Ú_weightsÚ_num_state_qubitsr   r   )Úselfr   r   r   Ú	__class__s       €úf/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/library/arithmetic/weighted_adder.pyr   zWeightedAdder.__init__I   sD   ø€ õ 	‰Œ×Ò˜dÐÑ#Ô#Ð#àˆŒØ!%ˆÔàˆŒØ 0ˆÔÐÐó    c           	      óÆ   — t          | j        ¦  «        dk    rHt          t          j        t          j        t          | j        ¦  «        ¦  «        ¦  «        dz   ¦  «        S dS )z˜The number of sum qubits in the circuit.

        Returns:
            The number of qubits needed to represent the weighted sum of the qubits.
        r   é   )Úsumr   ÚintÚnpÚfloorÚlog2©r   s    r   Únum_sum_qubitszWeightedAdder.num_sum_qubits_   sO   € õ ˆtŒ|ÑÔ˜qÒ Ð Ý•r”x¥¤­¨D¬LÑ(9Ô(9Ñ :Ô :Ñ;Ô;¸aÑ?Ñ@Ô@Ð@Øˆqr   c                 óF   — | j         r| j         S | j        rdg| j        z  S dS )ziThe weights for the qubit states.

        Returns:
            The weight for the qubit states.
        r   N)r   r   r"   s    r   r   zWeightedAdder.weightsj   s6   € ð Œ=ð 	!Ø”=Ð ØÔ ð 	/Ø�3˜Ô.Ñ.Ð.Øˆtr   c                 ó,  — |rbt          |¦  «        D ]R\  }}t          j        |t          j        |¦  «        ¦  «        st	          d¦  «        ‚t          j        |¦  «        ||<   ŒS|                      ¦   «          || _        |                      ¦   «          dS )zÀSet the weights for summing the qubit states.

        Args:
            weights: The new weights.

        Raises:
            ValueError: If not all weights are close to an integer.
        z&Non-integer weights are not supported!N)Ú	enumerater   ÚiscloseÚroundÚ
ValueErrorÚ_invalidater   Ú_reset_registers)r   r   ÚiÚweights       r   r   zWeightedAdder.weightsw   s›   € ð ð 	.Ý& wÑ/Ô/ð .ð .‘	��6Ý”z &­"¬(°6Ñ*:Ô*:Ñ;Ô;ð OÝ$Ð%MÑNÔNÐNÝœX fÑ-Ô-�˜‘
�
à×ÒÑÔÐØˆŒØ×ÒÑÔÐÐÐr   c                 ó   — | j         S )zeThe number of qubits to be summed.

        Returns:
            The number of state qubits.
        )r   r"   s    r   r   zWeightedAdder.num_state_qubits‹   s   € ð Ô%Ð%r   c                 óŒ   — | j         �|| j         k    r1|                      ¦   «          || _         |                      ¦   «          dS dS )zuSet the number of state qubits.

        Args:
            num_state_qubits: The new number of state qubits.
        N)r   r*   r+   )r   r   s     r   r   zWeightedAdder.num_state_qubits”   sV   € ð Ô!Ð)Ð-=ÀÔAWÒ-WÐ-WØ×ÒÑÔÐØ%5ˆDÔ"Ø×!Ò!Ñ#Ô#Ð#Ð#Ð#ð .XÐ-Wr   c                 ól  — g | _         | j        r£t          | j        d¬¦  «        }t          | j        d¬¦  «        }||g| _         | j        dk    r+t          | j        d¬¦  «        }|                      |¦  «         | j        dk    r/t          | j        d¬¦  «        }|                      |¦  «         dS dS dS )zReset the registers.Ústater   r   r   ÚcarryÚcontrolN)Úqregsr   r   r#   Únum_carry_qubitsr   Úadd_registerÚnum_control_qubits)r   Úqr_stateÚqr_sumÚqr_carryÚ
qr_controls        r   r+   zWeightedAdder._reset_registers    sÐ   € àˆŒ
àÔ ð 	.Ý& tÔ'<À7ÐKÑKÔKˆHÝ$ TÔ%8¸uÐEÑEÔEˆFØ" FÐ+ˆDŒJàÔ$ qÒ(Ð(Ý*¨4Ô+@ÀwÐOÑOÔO�Ø×!Ò! (Ñ+Ô+Ð+àÔ&¨Ò*Ð*Ý,¨TÔ-DÈ9ÐUÑUÔU�
Ø×!Ò! *Ñ-Ô-Ð-Ð-Ð-ð	.ð 	.ð +Ð*r   c                 ó   — | j         dz
  S )a#  The number of carry qubits required to compute the sum.

        Note that this is not necessarily equal to the number of ancilla qubits, these can
        be queried using ``num_ancilla_qubits``.

        Returns:
            The number of carry qubits required to compute the sum.
        r   )r#   r"   s    r   r5   zWeightedAdder.num_carry_qubits±   s   € ð Ô" QÑ&Ð&r   c                 ó2   — t          | j        dk    ¦  «        S )a  The number of additional control qubits required.

        Note that the total number of ancilla qubits can be obtained by calling the
        method ``num_ancilla_qubits``.

        Returns:
            The number of additional control qubits required (0 or 1).
        r   )r   r#   r"   s    r   r7   z WeightedAdder.num_control_qubits½   s   € õ �4Ô&¨Ò*Ñ+Ô+Ð+r   Tc                 óž   — d}| j         €d}|rt          d¦  «        ‚| j         t          | j        ¦  «        k    rd}|rt	          d¦  «        ‚|S )z,Check if the current configuration is valid.TNFz,The number of state qubits has not been set.z/Mismatching number of state qubits and weights.)r   ÚAttributeErrorÚlenr   r)   )r   Úraise_on_failureÚvalids      r   Ú_check_configurationz"WeightedAdder._check_configurationÉ   sg   € àˆØÔ!Ð)ØˆEØð UÝ$Ð%SÑTÔTÐTàÔ!¥S¨¬Ñ%6Ô%6Ò6Ð6ØˆEØð TÝ Ð!RÑSÔSÐSàˆr   c           	      ó  •— | j         rdS t          ¦   «                              ¦   «          | j        | j        z   }t          | j        Ž }|j        d| j        …         }|j        | j        |…         }|j        ||| j        z   …         }|j        || j        z   d…         }t          | j
        ¦  «        D �]¨\  }}t          j        |d¦  «        rŒ||         }	t          |¦  «        d›                     | j        d¦  «        ddd…         }
t          |
¦  «        D �]q\  }}|dk    �rÂ| j        dk    r|                     |	||         ¦  «         Œ5|dk    r@|                     |	||         ||         ¦  «         |                     |	||         ¦  «         Œ{|| j        dz
  k    rC|                     |	||         ¦  «         |                     |	||dz
           ||         ¦  «         ŒÌ|                     ||         ¦  «         |                     ||dz
           ¦  «         |                     |	||         ||dz
           g||         |d¬	¦  «         |                     |	||         ¦  «         |                     ||         ¦  «         |                     ||dz
           ¦  «         |                     |	||         ¦  «         |                     |	||dz
           ||         ¦  «         �ŒÏ| j        dk    r�ŒÜ|dk    r�Œä|| j        dz
  k    r(|                     |	||dz
           ||         ¦  «         �Œ|                     |	||         ||dz
           g||         |d¬	¦  «         |                     |	||dz
           ||         ¦  «         �Œst'          t)          t+          |
¦  «        ¦  «        ¦  «        D �]¤}|
|         }|dk    �r| j        dk    rŒ|dk    rZ|                     ||         ¦  «         |                     |	||         ||         ¦  «         |                     ||         ¦  «         Œ~|| j        dz
  k    rŒ�|                     ||dz
           ¦  «         |                     |	||         ||dz
           g||         |d¬	¦  «         |                     |	||         ¦  «         |                     ||dz
           ¦  «         �Œ| j        dk    r�Œ%|dk    r�Œ-|| j        dz
  k    r�Œ=|                     ||         ¦  «         |                     |	||         ||dz
           g||         |d¬	¦  «         |                     ||         ¦  «         �Œ¦�Œª|                      |                     ¦   «         | j        ¦  «         dS )
z(If not already built, build the circuit.Nr   ÚbÚ0éÿÿÿÿÚ1r   zv-chain)Úmode)Ú	_is_builtr   Ú_buildr   r#   r   r4   Úqubitsr5   r&   r   r   r'   r   ÚrjustÚcxÚccxÚxÚmcxÚreversedÚranger@   ÚappendÚto_gate)r   Únum_result_qubitsÚcircuitr8   r9   r:   r;   r,   r-   Úq_stateÚweight_binaryÚjÚbitr   s                €r   rK   zWeightedAdder._buildØ   sÏ  ø€ àŒ>ð 	ØˆFå‰Œ�ŠÑÔÐà Ô1°DÔ4GÑGÐå  $¤*Ð-ˆØ”>Ð"9 DÔ$9Ð"9Ô:ˆØ” Ô 5Ð8IÐ IÔJˆØ”>Ð"3Ð6GÈ$ÔJ_Ñ6_Ð"_Ô`ˆØ”^Ð$5¸Ô8MÑ$MÐ$OÐ$OÔPˆ
õ # 4¤<Ñ0Ô0ð g	-ñ g	-‰IˆAˆvåŒz˜& !Ñ$Ô$ð Øð ˜q”kˆGõ  # 6™{œ{Ð.Ð.×4Ò4°TÔ5HÈ#ÑNÔNÈtÈtÐQSÈtÔTˆMõ $ MÑ2Ô2ð 2Iñ 2I‘��3Ø˜#’:‘:ØÔ*¨aÒ/Ð/ØŸ
š
 7¨F°1¬IÑ6Ô6Ð6Ð6Ø˜aš˜ð  Ÿš G¨V°A¬Y¸À¼ÑDÔDÐDØŸ
š
 7¨F°1¬IÑ6Ô6Ð6Ð6Ø˜dÔ1°AÑ5Ò5Ð5ð  Ÿ
š
 7¨F°1¬IÑ6Ô6Ð6ØŸš G¨X°a¸!±e¬_¸fÀQ¼iÑHÔHÐHÐHð  Ÿ	š	 &¨¤)Ñ,Ô,Ð,ØŸ	š	 (¨1¨q©5¤/Ñ2Ô2Ð2ØŸšØ$ f¨Q¤i°¸!¸a¹%´ÐAØ$ QœKØ&Ø!*ð	 $ñ ô ð ð  Ÿ
š
 7¨H°Q¬KÑ8Ô8Ð8ØŸ	š	 &¨¤)Ñ,Ô,Ð,ØŸ	š	 (¨1¨q©5¤/Ñ2Ô2Ð2ØŸ
š
 7¨F°1¬IÑ6Ô6Ð6ØŸš G¨X°a¸!±e¬_¸fÀQ¼iÑHÔHÐHÑHàÔ*¨aÒ/Ð/ÙØ˜aš˜ÙØ˜dÔ1°AÑ5Ò5Ð5ð  Ÿš G¨X°a¸!±e¬_¸fÀQ¼iÑHÔHÐHÑHð  ŸšØ$ f¨Q¤i°¸!¸a¹%´ÐAØ$ QœKØ&Ø!*ð	 $ñ ô ð ð  Ÿš G¨X°a¸!±e¬_¸fÀQ¼iÑHÔHÐHÑHõ �e¥C¨Ñ$6Ô$6Ñ7Ô7Ñ8Ô8ð &-ñ &-�Ø# AÔ&�Ø˜#’:‘:ØÔ*¨aÒ/Ð/ØØ˜aš˜ØŸ	š	 &¨¤)Ñ,Ô,Ð,ØŸš G¨V°A¬Y¸À¼ÑDÔDÐDØŸ	š	 &¨¤)Ñ,Ô,Ð,Ð,Ø˜dÔ1°AÑ5Ò5Ð5ØàŸ	š	 (¨1¨q©5¤/Ñ2Ô2Ð2ØŸšØ$ f¨Q¤i°¸!¸a¹%´ÐAØ$ QœKØ&Ø!*ð	 $ñ ô ð ð  Ÿ
š
 7¨H°Q¬KÑ8Ô8Ð8ØŸ	š	 (¨1¨q©5¤/Ñ2Ô2Ð2Ñ2àÔ*¨aÒ/Ð/ÙØ˜aš˜ÙØ˜dÔ1°AÑ5Ò5Ð5Ùð  Ÿ	š	 &¨¤)Ñ,Ô,Ð,ØŸšØ$ f¨Q¤i°¸!¸a¹%´ÐAØ$ QœKØ&Ø!*ð	 $ñ ô ð ð  Ÿ	š	 &¨¤)Ñ,Ô,Ð,Ñ,ñM&-ðP 	�Š�G—O’OÑ%Ô% t¤{Ñ3Ô3Ð3Ð3Ð3r   )NNr   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Ústrr   Úpropertyr#   r   Úsetterr   r+   r5   r7   rC   rK   Ú__classcell__)r   s   @r   r   r      sý  ø€ € € € € ð/ð /ðf +/Ø'+Øð	1ð 1à" 3œ-ð1ð ˜$˜sœ)Ô$ð1ð ð	1ð
 
ð1ð 1ð 1ð 1ð 1ð 1ð, ð ð ð ð ñ „Xðð ð
˜˜cœð 
ð 
ð 
ñ „Xð
ð „^ð ˜t Cœyð  ¨Tð  ð  ð  ñ „^ð ð& ð& #ð &ð &ð &ñ „Xð&ð Ôð	$°ð 	$¸ð 	$ð 	$ð 	$ñ Ôð	$ð.ð .ð .ð" ð	' #ð 	'ð 	'ð 	'ñ „Xð	'ð ð	, Cð 	,ð 	,ð 	,ñ „Xð	,ðð ð ð ðy4ð y4ð y4ð y4ð y4ð y4ð y4ð y4ð y4r   r   )r_   Útypingr   r   Únumpyr   Úqiskit.circuitr   r   r   Úblueprintcircuitr	   r   © r   r   ú<module>ri      s    ðð 0Ð /à !Ð !Ð !Ð !Ð !Ð !Ð !Ð !Ø Ð Ð Ð à KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ Kà /Ð /Ð /Ð /Ð /Ð /ðz4ð z4ð z4ð z4ð z4Ð$ñ z4ô z4ð z4ð z4ð z4r   