
    z	i                    p    S r SSKJr  SSKJrJrJrJr  SSKJ	r	  SSK
Jr   " S S\5      r " S S	\5      rg
)zBoolean AND circuit and gate.    )annotations)QuantumRegisterQuantumCircuitAncillaRegisterGate)MCXGate)deprecate_funcc                  \   ^  \ rS rSrSr\" SSSS9  S	       S
U 4S jjj5       rSrU =r$ )AND   aU  A circuit implementing the logical AND operation on a number of qubits.

For the AND operation the state :math:`|1\rangle` is interpreted as ``True``. The result
qubit is flipped, if the state of all variable qubits is ``True``. In this format, the AND
operation equals a multi-controlled X gate, which is controlled on all variable qubits.
Using a list of flags however, qubits can be skipped or negated. Practically, the flags
allow to skip controls or to apply pre- and post-X gates to the negated qubits.

The AND gate without special flags equals the multi-controlled-X gate:

.. plot::
   :alt: Diagram illustrating the previously described circuit.

   from qiskit.circuit.library import AND
   from qiskit.visualization.library import _generate_circuit_library_visualization
   circuit = AND(5)
   _generate_circuit_library_visualization(circuit)

Using flags we can negate qubits or skip them. For instance, if we have 5 qubits and want to
return ``True`` if the first qubit is ``False`` and the last two are ``True`` we use the flags
``[-1, 0, 0, 1, 1]``.

.. plot::
   :alt: Diagram illustrating the previously described circuit.

   from qiskit.circuit.library import AND
   from qiskit.visualization.library import _generate_circuit_library_visualization
   circuit = AND(5, flags=[-1, 0, 0, 1, 1])
   _generate_circuit_library_visualization(circuit)

z2.1z+Use qiskit.circuit.library.AndGate instead.zin Qiskit 3.0)sinceadditional_msgremoval_timelinec                  > Xl         X l        [        USS9n[        SSS9n[        XESS9nU=(       d    S/U-  n[	        XB5       VVs/ s H  u  pxUS:w  d  M  UPM     n	nn[	        XB5       VVs/ s H  u  pxUS:  d  M  UPM     n
nn[
        R                  " [        U	5      US9nUS:  a  [        US5      nUR                  U5        O[        S5      n[        U
5      S:  a  UR                  U
5        UR                  XS	S	 US	S	 US9  [        U
5      S:  a  UR                  U
5        [        TU ]4  " UR                  S
S06  U R                  UR!                  5       U R"                  SS9  g	s  snnf s  snnf )a   
Args:
    num_variable_qubits: The qubits of which the AND is computed. The result will be written
        into an additional result qubit.
    flags: A list of +1/0/-1 marking negations or omissions of qubits.
    mcx_mode: The mode to be used to implement the multi-controlled X gate.
variablename   resultandr   )modeancillaNr   T)qubitsinplace)num_variable_qubitsflagsr   r   zipr   get_num_ancilla_qubitslenr   add_registerxmcxsuper__init__qregscomposeto_gater   )selfr   r   mcx_modeqr_variable	qr_resultcircuitqflagcontrol_qubitsflip_qubitsnum_ancillas
qr_ancilla	__class__s                j/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/library/boolean_logic/quantum_and.pyr$   AND.__init__8   sd   $ $7 
 &&9
K#AH5	 eD 2!22+.{+BP+Bdai!+BP ),K(?L(?WQ4!8q(?L 55c.6IPXY!(yAJ  ,(+J{aIIk"NaL*Q-hO{aIIk"'--4e4W__&t{{DI) Q Ms   	E1E10E7 E7)r   r   )N	noancilla)r   intr   list[int] | Noner)   strreturnNone)	__name__
__module____qualname____firstlineno____doc__r	   r$   __static_attributes____classcell__r3   s   @r4   r   r      s_    @ D( #'#	,J ,J  ,J 	,J
 
,J
,J    r   c                  Z   ^  \ rS rSrSr S     S	U 4S jjjrS rS
SS jjrS rSr	U =r
$ )AndGatel   a2  A gate representing the logical AND operation on a number of qubits.

For the AND operation the state :math:`|1\rangle` is interpreted as ``True``. The result
qubit is flipped, if the state of all variable qubits is ``True``. In this format, the AND
operation equals a multi-controlled X gate, which is controlled on all variable qubits.
Using a list of flags however, qubits can be skipped or negated. Practically, the flags
allow to skip controls or to apply pre- and post-X gates to the negated qubits.

The AndGate gate without special flags equals the multi-controlled-X gate:

.. plot::
   :alt: Diagram illustrating the previously described circuit.

   from qiskit.circuit import QuantumCircuit
   from qiskit.circuit.library import AndGate
   from qiskit.visualization.library import _generate_circuit_library_visualization
   circuit = QuantumCircuit(6)
   circuit.append(AndGate(5), [0, 1, 2, 3, 4, 5])
   _generate_circuit_library_visualization(circuit)

Using flags we can negate qubits or skip them. For instance, if we have 5 qubits and want to
return ``True`` if the first qubit is ``False`` and the last two are ``True`` we use the flags
``[-1, 0, 0, 1, 1]``.

.. plot::
   :alt: Diagram illustrating the previously described circuit.

   from qiskit.circuit import QuantumCircuit
   from qiskit.circuit.library import AndGate
   from qiskit.visualization.library import _generate_circuit_library_visualization
   circuit = QuantumCircuit(6)
   circuit.append(AndGate(5, flags=[-1, 0, 0, 1, 1]), [0, 1, 2, 3, 4, 5])
   _generate_circuit_library_visualization(circuit)

c                F   > [         TU ]  SUS-   / 5        Xl        X l        g)z
Args:
    num_variable_qubits: The qubits of which the AND is computed. The result will be written
        into an additional result qubit.
    flags: A list of +1/0/-1 marking negations or omissions of qubits.
r   r   N)r#   r$   r   r   )r(   r   r   r3   s      r4   r$   AndGate.__init__   s'     	 3a 7<#6 
rD   c                   [        U R                  SS9n[        SSS9nU R                  =(       d    S/U R                  -  n[        X5       VVs/ s H  u  pEUS:w  d  M  UPM     nnn[        X5       VVs/ s H  u  pEUS:  d  M  UPM     nnn[	        XSS9n[        U5      S:  a  UR                  U5        UR                  XbS S  5        [        U5      S:  a  UR                  U5        Xl        g s  snnf s  snnf )Nr   r   r   r   r   r   )	r   r   r   r   r   r   r!   r"   
definition)	r(   r*   r+   r   r-   r.   r/   r0   r,   s	            r4   _defineAndGate._define   s    %d&>&>ZP#AH5	 

<qcD$<$<<+.{+BP+Bdai!+BP ),K(?L(?WQ4!8q(?L !eD{aIIk"NaL1{aIIk"! Q Ms   C5 C57C;C;c                B    [        U R                  U R                  5      $ )an  Return inverted AND gate (itself).

Args:
    annotated: when set to ``True``, this is typically used to return an
        :class:`.AnnotatedOperation` with an inverse modifier set instead of a concrete
        :class:`.Gate`. However, for this class this argument is ignored as this gate
        is self-inverse.

Returns:
    AndGate: inverse gate (self-inverse).
)rF   r   r   )r(   	annotateds     r4   inverseAndGate.inverse   s     t//<<rD   c                    [        U[        5      =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ N)
isinstancerF   r   r   )r(   others     r4   __eq__AndGate.__eq__   s?    ug& *((E,E,EE*

ekk)	
rD   )rK   r   r   rS   )r   r7   r   r8   r:   r;   )F)rO   bool)r<   r=   r>   r?   r@   r$   rL   rP   rV   rA   rB   rC   s   @r4   rF   rF   l   sH    "N #'    
	 "0=
 
rD   rF   N)r@   
__future__r   qiskit.circuitr   r   r   r   %qiskit.circuit.library.standard_gatesr   qiskit.utils.deprecationr	   r   rF    rD   r4   <module>r^      s8    $ " Q Q 9 3RJ. RJj_
d _
rD   