
    z	i                    |    S 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
Jr  SSKJr   " S S	\5      r " S
 S\5      rg)zInteger Comparator.    )annotations)QuantumRegisterAncillaRegisterGate)CircuitError)synth_integer_comparator_2ssynth_integer_comparator_greedy   )BlueprintCircuitc                     ^  \ rS rSrSr    S         SU 4S jjjr\SS j5       r\R                  SS j5       r\SS j5       r	\	R                  SS j5       r	\SS j5       r
\
R                  SS	 j5       r
SSS
 jjrSU 4S jjrSrU =r$ )IntegerComparator   a2  Integer Comparator.

Operator compares basis states :math:`|i\rangle_n` against a classically given integer
:math:`L` of fixed value and flips a target qubit if :math:`i \geq L`
(or :math:`<` depending on the parameter ``geq``):

.. math::

    |i\rangle_n |0\rangle \mapsto |i\rangle_n |i \geq L\rangle

This operation is based on two's complement implementation of binary subtraction but only
uses carry bits and no actual result bits. If the most significant carry bit
(the results bit) is 1, the :math:`\geq` condition is ``True`` otherwise it is ``False``.
c                n   > [         TU ]  US9  SU l        SU l        SU l        X l        X0l        Xl        g)a  
Args:
    num_state_qubits: Number of state qubits. If this is set it will determine the number
        of qubits required for the circuit.
    value: The fixed value to compare with.
    geq: If True, evaluate a ``>=`` condition, else ``<``.
    name: Name of the circuit.
nameN)super__init___value_geq_num_state_qubitsvaluegeqnum_state_qubits)selfr   r   r   r   	__class__s        n/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/library/arithmetic/integer_comparator.pyr   IntegerComparator.__init__+   s;     	d#	!%
 0    c                    U R                   $ )zThe value to compare the qubit register to.

Returns:
    The value against which the value of the qubit register is compared.
)r   r   s    r   r   IntegerComparator.valueD   s     {{r   c                P    XR                   :w  a  U R                  5         Xl         g g )N)r   _invalidate)r   r   s     r   r   r!   M   s"    KKK  r   c                    U R                   $ )zReturn whether the comparator compares greater or less equal.

Returns:
    True, if the comparator compares ``>=``, False if ``<``.
)r   r    s    r   r   IntegerComparator.geqS   s     yyr   c                P    XR                   :w  a  U R                  5         Xl         gg)zSet whether the comparator compares greater or less equal.

Args:
    geq: If True, the comparator compares ``>=``, if False ``<``.
N)r   r#   )r   r   s     r   r   r%   \   s$     ))I r   c                    U R                   $ )zfThe number of qubits encoding the state for the comparison.

Returns:
    The number of state qubits.
)r   r    s    r   r   "IntegerComparator.num_state_qubitsg   s     %%%r   c                    U R                   b  XR                   :w  a_  U R                  5         Xl         UbE  [        USS9n[        SSS9nX#/U l        US-
  nUS:  a  [	        U5      nU R                  U5        gggg)zSet the number of state qubits.

Note that this will change the quantum registers.

Args:
    num_state_qubits: The new number of state qubits.
Nstater      comparer   )r   r#   r   qregsr   add_register)r   r   qr_state	q_comparenum_ancillas
qr_ancillas         r   r   r(   p   s     !!)-=AWAW-W%5"+*+;'J+AI>	&2
  0!3!#!0!>J%%j1 $ ,	 .Xr   c                    SnU R                   c  SnU(       a  [        S5      eU R                  c  SnU(       a  [        S5      eSU R                  -  nU R                  U:w  a  SnU(       a  [        S5      eU$ )z,Check if the current configuration is valid.TFz"Number of state qubits is not set.zNo comparison value set.r
   z:Number of qubits does not match required number of qubits.)r   AttributeErrorr   r   
num_qubitsr   )r   raise_on_failurevalidrequired_num_qubitss       r   _check_configuration&IntegerComparator._check_configuration   s|    !!)E$%IJJ;;E$%?@@$"7"77??11E"#_``r   c                   > U R                   (       a  g[        TU ]	  5         [        U R                  U R
                  U R                  5      nU R                  UR                  5       U R                  5        g)z(If not already built, build the circuit.N)
	_is_builtr   _buildr   r   r   r   appendto_gatequbits)r   circuitr   s     r   r=   IntegerComparator._build   sO    >>-d.C.CTZZQUQYQYZGOO%t{{3r   )r   r   r   r   r   r-   r   )NNTcmp)
r   
int | Noner   rD   r   boolr   strreturnNone)rG   int)r   rI   rG   rH   )rG   rE   )r   rE   rG   rH   )r   rD   rG   rH   )T)r6   rE   rG   rE   )rG   rH   )__name__
__module____qualname____firstlineno____doc__r   propertyr   setterr   r   r9   r=   __static_attributes____classcell__r   s   @r   r   r      s    " (, 1$1 1 	1
 1 
1 12   \\   
   	ZZ  & & 2 22,4 4r   r   c                  J   ^  \ rS rSrSr S       SU 4S jjjrS rSrU =r$ )IntegerComparatorGate   ay  Perform a :math:`\geq` (or :math:`<`) on a qubit register against a classical integer.

This operator compares basis states :math:`|i\rangle_n` against a classically given integer
:math:`L` of fixed value and flips a target qubit if :math:`i \geq L`
(or :math:`<` depending on the parameter ``geq``):

.. math::

    |i\rangle_n |0\rangle \mapsto |i\rangle_n |i \geq L\rangle

c                D   > [         TU ]  SUS-   / US9  X l        X0l        g)z
Args:
    num_state_qubits: The number of qubits in the registers.
    value: The value :math:`L` to compre to.
    geq: If ``True`` compute :math:`i \geq L`, otherwise compute :math:`i < L`.
    label: An optional label for the gate.
IntCompr+   )labelN)r   r   r   r   )r   r   r   r   rY   r   s        r   r   IntegerComparatorGate.__init__   s+     	$4q$8"EJ
r   c                j    [        U R                  S-
  U R                  U R                  5      U l        g )Nr+   )r	   r5   r   r   
definitionr    s    r   _defineIntegerComparatorGate._define   s&    9$//A:Mtzz[_[c[cdr   )r\   r   r   )TN)r   rI   r   rI   r   rE   rY   z
str | None)	rJ   rK   rL   rM   rN   r   r]   rQ   rR   rS   s   @r   rU   rU      sE    
 X\ #,/6:JT e er   rU   N)rN   
__future__r   qiskit.circuitr   r   r   qiskit.circuit.exceptionsr   'qiskit.synthesis.arithmetic.comparatorsr   r	   blueprintcircuitr   r   rU    r   r   <module>re      s>     " A A 2 0M4( M4`eD er   