§
    ¹³Ùg#  ã                  ó"  — d Z ddlm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 ddlmZmZ ddlmZ  ej        g d	¢g d
¢g d¢g d¢g¦  «        Z ee¦  «         G d„ de¦  «        ¦   «         Z eed¬¦  «         G d„ de	¦  «        ¦   «         ZdS )z
Swap gate.é    )Úannotations)ÚOptionalÚUnionN)ÚSingletonGateÚSingletonControlledGateÚstdlib_singleton_key)ÚQuantumRegister)Úwith_gate_arrayÚwith_controlled_gate_array)ÚStandardGate)é   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   c                  ó|   ‡ — e Zd ZdZej        Zddddœdˆ fd„Z e¦   «         Z	d„ Z
	 	 	 	 ddˆ fd„Zddd„Zd„ Zˆ xZS )ÚSwapGateu°  The SWAP gate.

    This is a symmetric and Clifford gate.

    Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
    with the :meth:`~qiskit.circuit.QuantumCircuit.swap` method.

    **Circuit symbol:**

    .. code-block:: text

        q_0: â”€Xâ”€
              â”‚
        q_1: â”€Xâ”€

    **Matrix Representation:**

    .. math::

        SWAP =
            \begin{pmatrix}
                1 & 0 & 0 & 0 \\
                0 & 0 & 1 & 0 \\
                0 & 1 & 0 & 0 \\
                0 & 0 & 0 & 1
            \end{pmatrix}

    The gate is equivalent to a state swap and is a classical logic gate.

    .. math::

        |a, b\rangle \rightarrow |b, a\rangle
    NÚdt)ÚdurationÚunitÚlabelúOptional[str]c               óV   •— t          ¦   «                              ddg |||¬¦  «         dS )zCreate new SWAP gate.Úswapé   )r   r   r   N)ÚsuperÚ__init__)Úselfr   r   r   Ú	__class__s       €ú`/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/library/standard_gates/swap.pyr   zSwapGate.__init__B   s.   ø€ å‰Œ×Ò˜  B¨e¸hÈTÐÑRÔRÐRÐRÐRó    c                óP  — ddl m} ddlm} t	          dd¦  «        } ||| j        ¬¦  «        } |¦   «         |d         |d         gg f |¦   «         |d         |d         gg f |¦   «         |d         |d         gg fg}|D ]\  }}}|                     |||¦  «         Œ|| _        dS )	z;
        gate swap a,b { cx a,b; cx b,a; cx a,b; }
        r   ©ÚQuantumCircuitr   )ÚCXGater   Úq©ÚnameN)Úqiskit.circuit.quantumcircuitr    Úxr!   r	   r$   Ú_appendÚ
definition)	r   r    r!   r"   ÚqcÚrulesÚinstrÚqargsÚcargss	            r   Ú_definezSwapGate._defineH   så   € ð
 	AÐ@Ð@Ð@Ð@Ð@ØÐÐÐÐÐå˜A˜sÑ#Ô#ˆØˆ^˜A D¤IÐ.Ñ.Ô.ˆàˆV‰XŒX˜˜!œ˜a œd�| RÐ(ØˆV‰XŒX˜˜!œ˜a œd�| RÐ(ØˆV‰XŒX˜˜!œ˜a œd�| RÐ(ð
ˆð
 $)ð 	,ð 	,ÑˆE�5˜%Ø�JŠJ�u˜e UÑ+Ô+Ð+Ð+àˆŒˆˆr   r   Únum_ctrl_qubitsÚintú
str | NoneÚ
ctrl_stateústr | int | NoneÚ	annotatedúbool | Nonec                ó’   •— |s|dk    rt          ||| j        ¬¦  «        }n%t          ¦   «                              ||||¬¦  «        }|S )am  Return a (multi-)controlled-SWAP gate.

        One control returns a CSWAP (Fredkin) gate.

        Args:
            num_ctrl_qubits: number of control qubits.
            label: An optional label for the gate [Default: ``None``]
            ctrl_state: control state expressed as integer,
                string (e.g.``'110'``), or ``None``. If ``None``, use all 1s.
            annotated: indicates whether the controlled gate should be implemented
                as an annotated gate. If ``None``, this is handled as ``False``.

        Returns:
            ControlledGate: controlled version of this gate.
        r   )r   r2   Ú_base_label)r/   r   r2   r4   )Ú	CSwapGater   r   Úcontrol)r   r/   r   r2   r4   Úgater   s         €r   r9   zSwapGate.control\   s^   ø€ ð, ð 	˜_°Ò1Ð1Ý 5°ZÈTÌZÐXÑXÔXˆDˆDå‘7”7—?’?Ø /ØØ%Ø#ð	 #ñ ô ˆDð ˆr   FÚboolc                ó   — t          ¦   «         S )a¯  Return inverse Swap 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:
            SwapGate: inverse gate (self-inverse).
        )r   ©r   r4   s     r   ÚinversezSwapGate.inverse}   s   € õ ‰zŒzÐr   c                ó,   — t          |t          ¦  «        S ©N)Ú
isinstancer   ©r   Úothers     r   Ú__eq__zSwapGate.__eq__‹   s   € Ý˜%¥Ñ*Ô*Ð*r   r@   )r   r   )r   NNN)r/   r0   r   r1   r2   r3   r4   r5   ©F©r4   r;   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú_standard_gater   r   Ú_singleton_lookup_keyr.   r9   r>   rD   Ú__classcell__©r   s   @r   r   r      sè   ø€ € € € € ð ð  ðD "Ô*€NðSÀÈ4ð Sð Sð Sð Sð Sð Sð Sð Sð 1Ð0Ñ2Ô2Ððð ð ð,  !Ø Ø'+Ø!%ðð ð ð ð ð ð ðBð ð ð ð ð+ð +ð +ð +ð +ð +ð +r   r   r   ©r/   c                  óp   ‡ — e Zd ZdZej        Z	 	 dddddœdˆ fd	„Z ed
¬¦  «        Z	d„ Z
ddd„Zd„ Zˆ xZS )r8   uˆ	  Controlled-SWAP gate, also known as the Fredkin gate.

    Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
    with the :meth:`~qiskit.circuit.QuantumCircuit.cswap` and
    :meth:`~qiskit.circuit.QuantumCircuit.fredkin` methods.

    **Circuit symbol:**

    .. code-block:: text

        q_0: â”€â– â”€
              â”‚
        q_1: â”€Xâ”€
              â”‚
        q_2: â”€Xâ”€


    **Matrix representation:**

    .. math::

        CSWAP\ q_0, q_1, q_2 =
            I \otimes I \otimes |0 \rangle \langle 0| +
            SWAP \otimes |1 \rangle \langle 1| =
            \begin{pmatrix}
                1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
                0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\
                0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\
                0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\
                0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
                0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\
                0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\
                0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
            \end{pmatrix}

    .. note::

        In Qiskit's convention, higher qubit indices are more significant
        (little endian convention). In many textbooks, controlled gates are
        presented with the assumption of more significant qubits as control,
        which in our case would be q_2. Thus a textbook matrix for this
        gate will be:

        .. code-block:: text

            q_0: â”€Xâ”€
                  â”‚
            q_1: â”€Xâ”€
                  â”‚
            q_2: â”€â– â”€

        .. math::

            CSWAP\ q_2, q_1, q_0 =
                |0 \rangle \langle 0| \otimes I \otimes I +
                |1 \rangle \langle 1| \otimes SWAP =
                \begin{pmatrix}
                    1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
                    0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\
                    0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\
                    0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\
                    0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
                    0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\
                    0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\
                    0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
                \end{pmatrix}

    In the computational basis, this gate swaps the states of
    the two target qubits if the control qubit is in the
    :math:`|1\rangle` state.

    .. math::
        |0, b, c\rangle \rightarrow |0, b, c\rangle
        |1, b, c\rangle \rightarrow |1, c, b\rangle
    Nr   )r   r   r7   r   r   r2   úOptional[Union[str, int]]c               ó€   •— |€d}t          ¦   «                              ddg d||t          |¬¦  «        ||¬¦	  «	         dS )zCreate new CSWAP gate.Nr   Úcswapé   r   )r   )r/   r   r2   Ú	base_gater   r   )r   r   r   )r   r   r2   r   r   r7   r   s         €r   r   zCSwapGate.__init__ß   s`   ø€ ð ˆ<ØˆDÝ‰Œ×ÒØØØØØØ!Ý [Ð1Ñ1Ô1ØØð 	ñ 
	
ô 
	
ð 
	
ð 
	
ð 
	
r   r   rO   c                ób  — ddl m} ddlm}m} t          dd¦  «        } ||| j        ¬¦  «        } |¦   «         |d         |d         gg f |¦   «         |d         |d         |d         gg f |¦   «         |d         |d         gg fg}|D ]\  }}}	|                     |||	¦  «         Œ|| _        d	S )
ze
        gate cswap a,b,c
        { cx c,b;
          ccx a,b,c;
          cx c,b;
        }
        r   r   r   )r!   ÚCCXGaterT   r"   r#   r   N)	r%   r    r&   r!   rW   r	   r$   r'   r(   )
r   r    r!   rW   r"   r)   r*   r+   r,   r-   s
             r   r.   zCSwapGate._defineù   sò   € ð 	AÐ@Ð@Ð@Ð@Ð@Ø&Ð&Ð&Ð&Ð&Ð&Ð&Ð&å˜A˜sÑ#Ô#ˆØˆ^˜A D¤IÐ.Ñ.Ô.ˆàˆV‰XŒX˜˜!œ˜a œd�| RÐ(ØˆW‰YŒY˜˜1œ˜q œt Q q¤TÐ*¨BÐ/ØˆV‰XŒX˜˜!œ˜a œd�| RÐ(ð
ˆð
 $)ð 	,ð 	,ÑˆE�5˜%Ø�JŠJ�u˜e UÑ+Ô+Ð+Ð+àˆŒˆˆr   Fr4   r;   c                ó,   — t          | j        ¬¦  «        S )a±  Return inverse CSwap 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:
            CSwapGate: inverse gate (self-inverse).
        )r2   )r8   r2   r=   s     r   r>   zCSwapGate.inverse  s   € õ  D¤OÐ4Ñ4Ô4Ð4r   c                óL   — t          |t          ¦  «        o| j        |j        k    S r@   )rA   r8   r2   rB   s     r   rD   zCSwapGate.__eq__  s!   € Ý˜%¥Ñ+Ô+ÐS°´À5ÔCSÒ0SÐSr   )NN)r   r   r2   rQ   rE   rF   )rG   rH   rI   rJ   r   r8   rK   r   r   rL   r.   r>   rD   rM   rN   s   @r   r8   r8   �   sÊ   ø€ € € € € ðJð JðX "Ô+€Nð  $Ø04ð
ð
 ØØð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð0 1Ð0ÀÐCÑCÔCÐðð ð ð05ð 5ð 5ð 5ð 5ðTð Tð Tð Tð Tð Tð Tr   r8   )rJ   Ú
__future__r   Útypingr   r   ÚnumpyÚqiskit.circuit.singletonr   r   r   Úqiskit.circuit.quantumregisterr	   Úqiskit.circuit._utilsr
   r   Úqiskit._accelerate.circuitr   ÚarrayÚ_SWAP_ARRAYr   r8   © r   r   ú<module>rd      sv  ðð Ð à "Ð "Ð "Ð "Ð "Ð "à "Ð "Ð "Ð "Ð "Ð "Ð "Ð "Ø €€€Ø aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aØ :Ð :Ð :Ð :Ð :Ð :Ø MÐ MÐ MÐ MÐ MÐ MÐ MÐ MØ 3Ð 3Ð 3Ð 3Ð 3Ð 3ð ˆeŒk˜<˜<˜<¨¨¨°|°|°|À\À\À\ÐRÑSÔS€ð €�ÑÔðo+ð o+ð o+ð o+ð o+ˆ}ñ o+ô o+ñ Ôðo+ðd Ð˜K¸Ð;Ñ;Ô;ðPTð PTð PTð PTð PTÐ'ñ PTô PTñ <Ô;ðPTð PTð PTr   