
    z	ia                       S r SSKJr  SSKrSSKJrJrJr  SSKrSSK	J
r
  SSKJrJrJr  SSKJrJrJr  SSKJr  SS	KJr  SS
/S
S//rSS/SS//r\" \5       " S S\5      5       r\" \S
S9 " S S\5      5       r\" \SSS9 " S S\5      5       r\" / SQ/ SQ/ SQ/ SQ/ SQ/ SQ/ SQ/ SQ/5       " S S \5      5       r\" \S!S"S9 " S# S$\5      5       r\" \S!S"S9 " S% S&\5      5       r\" / S'Q/ S(Q/ S)Q/ S*Q/ S+Q/ S,Q/ S-Q/ S.Q/ S/Q/ S0Q/ S1Q/ S2Q/ S3Q/ S4Q/ S5Q/ S6Q/5       " S7 S8\5      5       r\" \S9S:S9 " S; S<\5      5       r  " S= S>\
5      r! " S? S@\!5      r" " SA SB\!5      r# " SC SD\!5      r$g)Ez(X, CX, CCX and multi-controlled X gates.    )annotationsN)OptionalUnionType)ControlledGate)SingletonGateSingletonControlledGatestdlib_singleton_key)_ctrl_state_to_intwith_gate_arraywith_controlled_gate_array)StandardGate)deprecate_func   y      ?      ?y      ?      c                     ^  \ rS rSrSr\R                  rS	S
U 4S jjjr\	" 5       r
S r    S       SS jjrSSS jjrS rSrU =r$ )XGate   ug  The single-qubit Pauli-X gate (:math:`\sigma_x`).

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

Matrix representation:

.. math::

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

Circuit symbol:

.. code-block:: text

         ┌───┐
    q_0: ┤ X ├
         └───┘

Equivalent to a :math:`\pi` radian rotation about the X axis.

.. note::

    A global phase difference exists between the definitions of
    :math:`RX(\pi)` and :math:`X`.

    .. math::

        RX(\pi) = \begin{pmatrix}
                    0 & -i \\
                    -i & 0
                  \end{pmatrix}
                = -i X

The gate is equivalent to a classical bit flip.

.. math::

    |0\rangle \rightarrow |1\rangle \\
    |1\rangle \rightarrow |0\rangle
c                &   > [         TU ]  SS/ US9  g)z2
Args:
    label: An optional label for the gate.
xr   labelNsuper__init__selfr   	__class__s     a/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/library/standard_gates/x.pyr   XGate.__init__M   s    
 	a51    c                    SSK Jn  UR                  [        R                  R                  U R                  5      SU R                  S9U l        gDefault definitionr   )QuantumCircuitT)legacy_qubitsnameN)	qiskit.circuitr$   _from_circuit_datar   X_get_definitionparamsr&   
definitionr   r$   s     r   _defineXGate._defineV   sA     	2 );;NN**4;;7tRVR[R[ < 
r    c                2    [        UUUU R                  S9nU$ )a  Return a (multi-)controlled-X gate.

One control returns a CX gate. Two controls returns a CCX 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: when set to ``True``, this is typically used to return an
        :class:`.AnnotatedOperation` with a control modifier set instead of a concrete
        :class:`.Gate`. However, for this class this argument is ignored as a controlled
        X gate is a multi-controlled X-gate.

Returns:
    ControlledGate: controlled version of this gate.
num_ctrl_qubitsr   
ctrl_state_base_label)MCXGater   )r   r2   r   r3   	annotatedgates         r   controlXGate.controlc   s%    0 +!

	
 r    c                    [        5       $ )aj  Return inverted X 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:
    XGate: inverse gate (self-inverse).
)r   r   r6   s     r   inverseXGate.inverse   s     wr    c                "    [        U[        5      $ N)
isinstancer   r   others     r   __eq__XGate.__eq__   s    %''r    r,   r?   r   Optional[str]r   NNFr2   intr   rG   r3   Optional[Union[str, int]]r6   boolFr6   rL   )__name__
__module____qualname____firstlineno____doc__r   r)   _standard_gater   r
   _singleton_lookup_keyr.   r8   r<   rC   __static_attributes____classcell__r   s   @r   r   r      sw    +Z "^^N2 2 12
  !#04  .	
 @( (r    r   r2   c                     ^  \ rS rSrSr\R                  r  SSS.   SU 4S jjjjr\	" SS9r
    S       SS jjrSSS	 jjrS
 rSrU =r$ )CXGate   u8  Controlled-X gate.

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

Circuit symbol:

.. code-block:: text

    q_0: ──■──
         ┌─┴─┐
    q_1: ┤ X ├
         └───┘

Matrix representation:

.. math::

    CX\ q_0, q_1 =
        I \otimes |0\rangle\langle0| + X \otimes |1\rangle\langle1| =
        \begin{pmatrix}
            1 & 0 & 0 & 0 \\
            0 & 0 & 0 & 1 \\
            0 & 0 & 1 & 0 \\
            0 & 1 & 0 & 0
        \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_1. Thus a textbook matrix for this
    gate will be:

    .. code-block:: text

             ┌───┐
        q_0: ┤ X ├
             └─┬─┘
        q_1: ──■──

    .. math::

        CX\ q_1, q_0 =
            |0 \rangle\langle 0| \otimes I + |1 \rangle\langle 1| \otimes X =
            \begin{pmatrix}
                1 & 0 & 0 & 0 \\
                0 & 1 & 0 & 0 \\
                0 & 0 & 0 & 1 \\
                0 & 0 & 1 & 0
            \end{pmatrix}


In the computational basis, this gate flips the target qubit
if the control qubit is in the :math:`|1\rangle` state.
In this sense it is similar to a classical XOR gate.

.. math::
    `|a, b\rangle \rightarrow |a, a \oplus b\rangle`
Nr4   c               <   > [         TU ]  SS/ SUU[        US9US9  g)zCreate new CX gate.cx   r   r   )r2   r   r3   	base_gater4   Nr   r   r   r   r   r3   r4   r   s       r   r   CXGate.__init__   s4     	!+.# 	 		
r    r   rY   c                r    [        X15      nU R                  U-  U-  n[        US-   UUU R                  S9nU$ )a  Return a controlled-X gate with more control lines.

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: when set to ``True``, this is typically used to return an
        :class:`.AnnotatedOperation` with a control modifier set instead of a concrete
        :class:`.Gate`. However, for this class this argument is ignored as a controlled
        CX gate is a multi-controlled X-gate.

Returns:
    ControlledGate: controlled version of this gate.
r   r1   r   r3   r5   r   r   r2   r   r3   r6   new_ctrl_stater7   s          r   r8   CXGate.control   G    , (
D
//_<
J+a/%

	
 r    c                (    [        U R                  S9$ )al  Return inverted CX 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:
    CXGate: inverse gate (self-inverse).
r3   )r[   r3   r;   s     r   r<   CXGate.inverse  s     11r    c                b    [        U[        5      =(       a    U R                  UR                  :H  $ r?   )r@   r[   r3   rA   s     r   rC   CXGate.__eq__  s#    %(PT__@P@P-PPr     NNr   rG   r3   rK   rH   rI   rM   rN   )rO   rP   rQ   rR   rS   r   CXrT   r   r
   rU   r8   r<   rC   rV   rW   rX   s   @r   r[   r[      s    =~ "__N  $04

 

 .
 
& 1C  !#04  .	
 @2Q Qr    r[   r`   )   )r2   cached_statesc                     ^  \ rS rSrSr\R                  r  SSS.   SU 4S jjjjr\	" SS9r
S r    S       SS	 jjrSSS
 jjrS rSrU =r$ )CCXGatei  u  CCX gate, also known as Toffoli gate.

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

Circuit symbol:

.. code-block:: text

    q_0: ──■──
           │
    q_1: ──■──
         ┌─┴─┐
    q_2: ┤ X ├
         └───┘

Matrix representation:

.. math::

    CCX q_0, q_1, q_2 =
        I \otimes I \otimes |0 \rangle \langle 0| + CX \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 & 0 & 0 & 1\\
            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 & 1 & 0 & 0 & 0 & 0
        \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 and q_1. Thus a textbook matrix for this
    gate will be:

    .. code-block:: text

             ┌───┐
        q_0: ┤ X ├
             └─┬─┘
        q_1: ──■──
               │
        q_2: ──■──

    .. math::

        CCX\ q_2, q_1, q_0 =
            |0 \rangle \langle 0| \otimes I \otimes I + |1 \rangle \langle 1| \otimes CX =
            \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 & 1 & 0 & 0\\
                0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\
                0 & 0 & 0 & 0 & 0 & 0 & 1 & 0
            \end{pmatrix}

Nr]   c               :   > [         TU ]  SS/ SUU[        US9S9  g)zCreate new CCX gate.ccxrt   r`   r   r2   r   r3   ra   Nrb   rc   s       r   r   CCXGate.__init__f  1     	!+. 	 	
r    r`   rY   c                    SSK Jn  UR                  [        R                  R                  U R                  5      SU R                  S9U l        gr"   )	r'   r$   r(   r   CCXr*   r+   r&   r,   r-   s     r   r.   CCXGate._definez  sC     	2 );;,,T[[9TXT]T] < 
r    c                r    [        X15      nU R                  U-  U-  n[        US-   UUU R                  S9nU$ )aw  Controlled version of this 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: when set to ``True``, this is typically used to return an
        :class:`.AnnotatedOperation` with a control modifier set instead of a concrete
        :class:`.Gate`. However, for this class this argument is ignored as a controlled
        CCX gate is a multi-controlled X-gate.

Returns:
    ControlledGate: controlled version of this gate.
r`   r1   rf   rg   s          r   r8   CCXGate.control  rj   r    c                (    [        U R                  S9$ )au  Return an inverted CCX gate (also a CCX).

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:
    CCXGate: inverse gate (self-inverse).
rl   )rw   r3   r;   s     r   r<   CCXGate.inverse       $//22r    c                b    [        U[        5      =(       a    U R                  UR                  :H  $ r?   )r@   rw   r3   rA   s     r   rC   CCXGate.__eq__  #    %)QdooAQAQ.QQr    rE   rq   rr   rH   rI   rM   rN   )rO   rP   rQ   rR   rS   r   r~   rT   r   r
   rU   r.   r8   r<   rC   rV   rW   rX   s   @r   rw   rw     s    AF "%%N  $04

 

 .
 
$ 1C
&  !#04  .	
 @3R Rr    rw   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r                )r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )r   r   r                 ?r   r   r   r   c                  f   ^  \ rS rSrSr\R                  rSSU 4S jjjr\	" 5       r
S rS rSrU =r$ )	RCCXGatei  a|  The simplified Toffoli gate, also referred to as Margolus gate.

The simplified Toffoli gate implements the Toffoli gate up to relative phases.
This implementation requires three CX gates which is the minimal amount possible,
as shown in https://arxiv.org/abs/quant-ph/0312225.
Note, that the simplified Toffoli is not equivalent to the Toffoli. But can be used in places
where the Toffoli gate is uncomputed again.

This concrete implementation is from https://arxiv.org/abs/1508.03273, the dashed box
of Fig. 3.

Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
with the :meth:`~qiskit.circuit.QuantumCircuit.rccx` method.
c                &   > [         TU ]  SS/ US9  g)z!Create a new simplified CCX gate.rccxrt   r   Nr   r   s     r   r   RCCXGate.__init__  s    Be4r    c                    SSK Jn  UR                  [        R                  R                  U R                  5      SU R                  S9U l        gr"   )	r'   r$   r(   r   RCCXr*   r+   r&   r,   r-   s     r   r.   RCCXGate._define  sC     	2 );;--dkk:$UYU^U^ < 
r    c                "    [        U[        5      $ r?   )r@   r   rA   s     r   rC   RCCXGate.__eq__      %**r    rE   r?   rF   )rO   rP   rQ   rR   rS   r   r   rT   r   r
   rU   r.   rC   rV   rW   rX   s   @r   r   r     s9     "&&N5 5 12
"+ +r    r   rt   )   c                  v   ^  \ rS rSrSr\R                  r  SSS.   SU 4S jjjjr\	" SS9r
S rS	 rS
rU =r$ )C3SXGatei  zThe 3-qubit controlled sqrt-X gate.

This implementation is based on Page 17 of [1].

References:
    [1] Barenco et al., 1995. https://arxiv.org/pdf/quant-ph/9503016.pdf
Nr]   c               @   > SSK Jn  [        TU ]  SS/ SUUU" US9S9  g)	zCreate a new 3-qubit controlled sqrt-X gate.

Args:
    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.
r   )SXGatec3sx   rt   r   rz   N)sxr   r   r   )r   r   r3   r4   r   r   s        r   r   C3SXGate.__init__  s4     	!;/ 	 	
r    rt   rY   c                    SSK Jn  UR                  [        R                  R                  U R                  5      SU R                  S9U l        gr"   )	r'   r$   r(   r   C3SXr*   r+   r&   r,   r-   s     r   r.   C3SXGate._define  A     	2(;;--dkk:$UYU^U^ < 
r    c                b    [        U[        5      =(       a    U R                  UR                  :H  $ r?   )r@   r   r3   rA   s     r   rC   C3SXGate.__eq__'  s#    %*Rt%BRBR/RRr    rE   rq   rr   )rO   rP   rQ   rR   rS   r   r   rT   r   r
   rU   r.   rC   rV   rW   rX   s   @r   r   r     sa     "&&N  $04

 

 .
 
4 1C
S Sr    r   c                     ^  \ rS rSrSr\R                  r  SSS.   SU 4S jjjjr\	" SS9r
S r    S       SS	 jjrSSS
 jjrS rSrU =r$ )C3XGatei+  zaThe X gate controlled on 3 qubits.

This implementation uses :math:`\sqrt{T}` and 14 CNOT gates.
Nr]   c               :   > [         TU ]  SS/ SUU[        US9S9  g)z'Create a new 3-qubit controlled X gate.mcxr   rt   r   rz   Nrb   rc   s       r   r   C3XGate.__init__4  r|   r    rt   rY   c                    SSK Jn  UR                  [        R                  R                  U R                  5      SU R                  S9U l        gr"   )	r'   r$   r(   r   C3Xr*   r+   r&   r,   r-   s     r   r.   C3XGate._defineI  sA     	2(;;,,T[[9TXT]T] < 
r    c                r    [        X15      nU R                  U-  U-  n[        US-   UUU R                  S9nU$ )aw  Controlled version of this 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: when set to ``True``, this is typically used to return an
        :class:`.AnnotatedOperation` with a control modifier set instead of a concrete
        :class:`.Gate`. However, for this class this argument is ignored as a controlled
        C3X gate is a multi-controlled X-gate.

Returns:
    ControlledGate: controlled version of this gate.
rt   r1   rf   rg   s          r   r8   C3XGate.controlR  rj   r    c                (    [        U R                  S9$ )ay  Invert this gate. The C3X is its own inverse.

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:
    C3XGate: inverse gate (self-inverse).
rl   )r   r3   r;   s     r   r<   C3XGate.inverser  r   r    c                b    [        U[        5      =(       a    U R                  UR                  :H  $ r?   )r@   r   r3   rA   s     r   rC   C3XGate.__eq__  r   r    rE   rq   rr   rH   rI   rM   rN   )rO   rP   rQ   rR   rS   r   r   rT   r   r
   rU   r.   r8   r<   rC   rV   rW   rX   s   @r   r   r   +  s    
 "%%N  $04

 

 .
 
$ 1C
  !#04  .	
 @3R Rr    r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   c                  f   ^  \ rS rSrSr\R                  rSSU 4S jjjr\	" 5       r
S rS rSrU =r$ )	RC3XGatei  a  The simplified 3-controlled Toffoli gate.

The simplified Toffoli gate implements the Toffoli gate up to relative phases.
Note, that the simplified Toffoli is not equivalent to the Toffoli. But can be used in places
where the Toffoli gate is uncomputed again.

This concrete implementation is from https://arxiv.org/abs/1508.03273, the complete circuit
of Fig. 4.

Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
with the :meth:`~qiskit.circuit.QuantumCircuit.rcccx` method.
c                &   > [         TU ]  SS/ US9  g)zCreate a new RC3X gate.rcccxr   r   Nr   r   s     r   r   RC3XGate.__init__  s    !Ru5r    c                    SSK Jn  UR                  [        R                  R                  U R                  5      SU R                  S9U l        gr"   )	r'   r$   r(   r   RC3Xr*   r+   r&   r,   r-   s     r   r.   RC3XGate._define  r   r    c                "    [        U[        5      $ r?   )r@   r   rA   s     r   rC   RC3XGate.__eq__  r   r    rE   r?   rF   )rO   rP   rQ   rR   rS   r   r   rT   r   r
   rU   r.   rC   rV   rW   rX   s   @r   r   r     s9    * "&&N6 6 12
+ +r    r   r   )   c                     ^  \ rS rSrSr  SSS.   SU 4S jjjjr\" SS9rS r    S       SS	 jjr	SSS
 jjr
S rSrU =r$ )C4XGatei  a(  The 4-qubit controlled X gate.

This implementation is based on Page 21, Lemma 7.5, of [1], with the use
of the relative phase version of c3x, the rc3x [2].

References:
    1. Barenco et al., 1995. https://arxiv.org/pdf/quant-ph/9503016.pdf
    2. Maslov, 2015. https://arxiv.org/abs/1508.03273
Nr]   c               :   > [         TU ]  SS/ SUU[        US9S9  g)z'Create a new 4-qubit controlled X gate.r      r   r   rz   Nrb   rc   s       r   r   C4XGate.__init__  r|   r    r   rY   c                `   SSK JnJn  SSKJn  SSKJn  U" SSS9nU" XPR                  S9nU" 5       US	   // 4U" [        R                  S
-  5      US   US	   // 4U" 5       US	   // 4[        5       US   US   US
   US   // 4U" 5       US	   // 4U" [        R                  * S
-  5      US   US	   // 4U" 5       US	   // 4[        5       R                  5       US   US   US
   US   // 4[        5       US   US   US
   US	   // 4/	nU H  u  pn
UR                  XU
5        M     X`l        g)r#   r   )r$   QuantumRegisterr   )HGate)
CPhaseGater   qr&   r   r`   rt   N)r'   r$   r   hr   pr   r&   numpypir   r<   r   _appendr,   )r   r$   r   r   r   r   qcrulesinstrqargscargss              r   r.   C4XGate._define  s_    	C! AC(AII.Wqtfb!1%!ad|R8Wqtfb!Z!A$!adAaD126Wqtfb!	A&1qtb9Wqtfb!Z!AaD!A$!ad#;R@Z!A$!adAaD126

 $)E%JJuU+ $) r    c                r    [        X15      nU R                  U-  U-  n[        US-   UUU R                  S9nU$ )aw  Controlled version of this 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: when set to ``True``, this is typically used to return an
        :class:`.AnnotatedOperation` with a control modifier set instead of a concrete
        :class:`.Gate`. However, for this class this argument is ignored as a controlled
        C4X gate is a multi-controlled X-gate.

Returns:
    ControlledGate: controlled version of this gate.
r   r1   rf   rg   s          r   r8   C4XGate.control  rj   r    c                (    [        U R                  S9$ )ay  Invert this gate. The C4X is its own inverse.

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:
    C4XGate: inverse gate (self-inverse).
rl   )r   r3   r;   s     r   r<   C4XGate.inverse  r   r    c                b    [        U[        5      =(       a    U R                  UR                  :H  $ r?   )r@   r   r3   rA   s     r   rC   C4XGate.__eq__$  r   r    rE   rq   rr   rH   rI   rM   rN   )rO   rP   rQ   rR   rS   r   r
   rU   r.   r8   r<   rC   rV   rW   rX   s   @r   r   r     s      $04

 

 .
 
$ 1C8  !#04  .	
 @3R Rr    r   c                    ^  \ rS rSrSr   SSS.     SU 4S jjjjr  SSSS.     SU 4S jjjjrSSS	 jjr\\	" S
SSS9SSS jj5       5       r
S r\S 5       r    S       SS jjrSU 4S jjrSU 4S jjrSrU =r$ ) r5   i(  zThe general, multi-controlled X gate.

Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`
with the :meth:`~qiskit.circuit.QuantumCircuit.mcx` method.
Nr]   c                  > [         [        S.nUR                  US5      nUb#  UR                  XbX4S9nUR	                  UUUS9  U$ [
        TU ]  U 5      $ )zCreate a new MCX instance.

Depending on the number of controls and which mode of the MCX, this creates an
explicit CX, CCX, C3X or C4X instance or a generic MCX gate.
)r   r`   Nr   r3   r4   )r[   rw   get__new__r   r   	clsr2   r   r3   r4   explicit
gate_classr7   r   s	           r   r   MCXGate.__new__/  sq     9?74K\\/48
!%%J & D MM%'  
 Kws##r    r   )_namer4   c                 > U R                   [        [        [        [        4;   aU  [
        R                  " 5          [
        R                  " S[        SSS9  U R                   R                  U5      nSSS5        OU R                   R                  U5      n[        TU ]-  UUS-   W-   / UUU[        US9S9  g! , (       d  f       N/= f)	zCreate new MCX gate.ignorezP.+qiskit\.circuit\.library\.standard_gates\.x\.MCXGate\.get_num_ancilla_qubits.+qiskit)categorymessagemoduleNr   r   rz   )r   r5   MCXGrayCodeMCXRecursive	MCXVChainwarningscatch_warningsfilterwarningsDeprecationWarningget_num_ancilla_qubitsr   r   r   )r   r2   r   r3   r   r4   num_ancilla_qubitsr   s          r   r   MCXGate.__init__M  s     >>g{L)LL ((*''/0# &*^^%J%J?%[" +* "&!F!F!W 	a"44+!+. 	 	
 +*s   7B77
Cc                >    [        U R                  U R                  S9$ )ay  Invert this gate. The MCX is its own inverse.

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:
    MCXGate: inverse gate (self-inverse).
r2   r3   )r5   r2   r3   r;   s     r   r<   MCXGate.inverset  s     t';';XXr    a  For an MCXGate it is no longer possible to know the number of ancilla qubits that would be eventually used by the transpiler when the gate is created. Instead, it is recommended to use MCXGate and let HighLevelSynthesis choose the best synthesis method depending on the number of ancilla qubits available. However, if a specific synthesis method using a specific number of ancilla qubits is require, one can create a custom gate by calling the corresponding synthesis function directly.2.1in Qiskit 3.0)additional_msgsinceremoval_timelinec                    US:X  a  gUS;   a  [        U S:  5      $ USS S:X  d	  USS S	:X  a  [        SU S
-
  5      $ [        SU S35      e)zGet the number of required ancilla qubits without instantiating the class.

This staticmethod might be necessary to check the number of ancillas before
creating the gate, or to use the number of ancillas in the initialization.
	noancillar   )	recursionadvancedr   Nr   v-chainr   basicr`   zUnsupported mode (z) specified!)rJ   maxAttributeErrorr2   modes     r   r   MCXGate.get_num_ancilla_qubits  sj    ( ;,,*++8y D!H$7q/A-..1$|DEEr    c                @    SSK Jn  U" U R                  5      nX l        g)z7This definition is based on MCPhaseGate implementation.r   )synth_mcx_noaux_v24N)!qiskit.synthesis.multi_controlledr	  r2   r,   )r   r	  r   s      r   r.   MCXGate._define       	J !5!56r    c                8    U R                  U R                  5      $ )zThe number of ancilla qubits.)r   r2   )r   s    r   r   MCXGate.num_ancilla_qubits  s     **4+?+?@@r    c                   [        X15      nU R                  U-  U-  nU R                  [        [        [
        4;   aX  [        R                  " 5          [        R                  " S[        SS9  U R                  U R                  U-   UUS9nSSS5        U$ [        U R                  U-   UUU R                  S9nU$ ! , (       d  f       W$ = f)a  Return a multi-controlled-X gate with more control lines.

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: when set to ``True``, this is typically used to return an
        :class:`.AnnotatedOperation` with a control modifier set instead of a concrete
        :class:`.Gate`. However, for this class this argument is ignored as a controlled
        multi-controlled X gate is another multi-controlled X-gate.

Returns:
    ControlledGate: controlled version of this gate.
r   r   r   r   r   r3   Nr   )r   r3   r   r   r   r   r   r   r   r   r2   r5   r   rg   s          r   r8   MCXGate.control  s    , (
D
//_<
J >>k<CC ((*''/#
 ~~((?:- &  +$  $$6) JJ	D % +*$ s   9B::
C	c                
  > U R                   [        [        [        4;   aF  [        R
                  " 5          [        R                  " S[        SS9  [        TU ]%  US9sSSS5        $ [        TU ]%  US9$ ! , (       d  f       N= f)z
Copy of the instruction.

Args:
    name (str): name to be given to the copied circuit, if ``None`` then the name stays the same.

Returns:
    qiskit.circuit.Instruction: a copy of the current instruction, with the name updated if it
    was provided
r   z/.*qiskit\.circuit\.library\.standard_gates\.x.*r   r   r   N)
r   r   r   r   r   r   r   r   r   copy)r   r&   r   s     r   r  MCXGate.copy  so     >>k<CC((*''/N
 w||. +* w||&& +*   'A44
Bc                
  > U R                   [        [        [        4;   aF  [        R
                  " 5          [        R                  " S[        SS9  [        TU ]%  US9sS S S 5        $ [        TU ]%  US9$ ! , (       d  f       N= f)Nr   z.+MCXVChain.+r  )memo)
r   r   r   r   r   r   r   r   r   __deepcopy__)r   r  r   s     r   r  MCXGate.__deepcopy__  sp    >>k<CC((*'''9? w++6	 +*
 w##.. +*r  rE   NNNr2   Optional[int]r   rG   r3   rK   rq   r2   rJ   r   rG   r3   rK   rM   rN   )r   )r2   rJ   r  strreturnrJ   rH   rI   r?   )rO   rP   rQ   rR   rS   r   r   r<   staticmethodr   r   r.   propertyr   r8   r  r  rV   rW   rX   s   @r   r5   r5   (  s    *.#04	$ $&$ $ .	$ $B  $04	%
 %
%
 %
 .	%
 %
NY + (F F A A  !#0422 2 .	2
 2h'*/ /r    r5   c                     ^  \ rS rSrSr\" SSSS9   SSS.     SU 4S	 jjjj5       r  S     SU 4S
 jjjrSSS jjrS r	Sr
U =r$ )r   i  zImplement the multi-controlled X gate using the Gray code.

This delegates the implementation to the MCU1 gate, since :math:`X = H \cdot U1(\pi) \cdot H`.
r   ah  It is recommended to use MCXGate and let HighLevelSynthesis choose the best synthesis method depending on the number of ancilla qubits available. If this specific synthesis method is required, one can specify it using the high-level-synthesis plugin 'gray_code' for MCX gates, or, alternatively, one can use synth_mcx_gray_code' to construct the gate directly.r   r   r   r   Nr]   c                  > [         [        [        [        S.nUR	                  US5      nUb$  UR                  UUUUS9nUR                  UUS9  U$ [        TU ]  U 5      $ )z!Create a new MCXGrayCode instance)r   r`   rt   r   Nr   r  )r[   rw   r   r   r   r   r   r   r   s	           r   r   MCXGrayCode.__new__  sy    * 'g'B\\/48
!%%%'	 & D MM%   Kws##r    c                $   > [         TU ]  XUSS9  g )Nmcx_grayr   r3   r   r   )r   r2   r   r3   r   s       r   r   MCXGrayCode.__init__*  s     	*T^_r    c                    [         R                  " 5          [         R                  " S[        SS9  [	        U R
                  U R                  S9nSSS5        U$ ! , (       d  f       W$ = f)a}  Invert this gate. The MCX is its own inverse.

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:
    MCXGrayCode: inverse gate (self-inverse).
r   r   r  r   N)r   r   r   r   r   r2   r3   r   r6   r<   s      r   r<   MCXGrayCode.inverse2  sY     $$&##H7IRZ[!$2F2FSWSbSbcG '  '&    9A
A(c                @    SSK Jn  U" U R                  5      nX l        g)z(Define the MCX gate using the Gray code.r   )synth_mcx_gray_codeN)r
  r1  r2   r,   )r   r1  r   s      r   r.   MCXGrayCode._defineC  r  r    rE   r  r  rq   r  rM   rN   )rO   rP   rQ   rR   rS   r   r   r   r<   r.   rV   rW   rX   s   @r   r   r     s    
 O )
 *.#04	$ $&$ $ .	$ $
$<  $04	`` ` .	` `" r    r   c                     ^  \ rS rSrSr\" SSSS9   SSS.     SU 4S	 jjjj5       r  SSS.     SU 4S
 jjjjr\SSS jj5       r	SSS jjr
S rSrU =r$ )r   iL  a  Implement the multi-controlled X gate using recursion.

Using a single clean ancilla qubit, the multi-controlled X gate is split into
four sub-registers, each one of them uses the V-chain method.

The method is based on Lemma 9 of [2], first shown in Lemma 7.3 of [1].

References:
    1. Barenco et al., 1995. https://arxiv.org/pdf/quant-ph/9503016.pdf
    2. Iten et al., 2015. https://arxiv.org/abs/1501.06911
r   ao  It is recommended to use MCXGate and let HighLevelSynthesis choose the best synthesis method depending on the number of ancilla qubits available. If this specific synthesis method is required, one can specify it using the high-level-synthesis plugin ``'gray_code'`` for MCX gates, or, alternatively, one can use ``'synth_mcx_1_clean'`` to construct the gate directly.r   r%  Nr]   c               "   > [         TU ]  XX#US9$ )Nr]   r   r   )r   r2   r   r3   r4   r   s        r   r   MCXRecursive.__new__Y  s    & wsUT_``r    c               (   > [         TU ]  UUUSS S9  g )Nmcx_recursiver   r3   r   r4   r   )r   r2   r   r3   r4   r   s        r   r   MCXRecursive.__init__n  s&     	!! 	 	
r    c                ,    [         R                  X5      $ z*Get the number of required ancilla qubits.r5   r   r  s     r   r   #MCXRecursive.get_num_ancilla_qubits~       --oDDr    c                    [         R                  " 5          [         R                  " S[        SS9  [	        U R
                  U R                  S9nSSS5        U$ ! , (       d  f       W$ = f)a~  Invert this gate. The MCX is its own inverse.

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:
    MCXRecursive: inverse gate (self-inverse).
r   r   r  r   N)r   r   r   r   r   r2   r3   r-  s      r   r<   MCXRecursive.inverse  sY     $$&##H7IRZ["43G3GTXTcTcdG '  '& r/  c                @    SSK Jn  U" U R                  5      nX l        g)z$Define the MCX gate using recursion.r   )synth_mcx_1_clean_b95N)r
  rC  r2   r,   )r   rC  r   s      r   r.   MCXRecursive._define  s     	L"4#7#78r    rE   r  r  rq   r  )r   r2   rJ   r  r   rM   rN   )rO   rP   rQ   rR   rS   r   r   r   r"  r   r<   r.   rV   rW   rX   s   @r   r   r   L  s    
 R )
 *.#04	a a&a a .	a a
a  $04	
 

 
 .	
 
  E E" r    r   c                     ^  \ rS rSrSr\" SSSS9    SSSSS	.           SU 4S
 jjjj5       r   SSSSS	.           SU 4S jjjjrSSS jjr\	SSS jj5       r
S rSrU =r$ )r   i  zBImplement the multi-controlled X gate using a V-chain of CX gates.r   a  It is recommended to use MCXGate and let HighLevelSynthesis choose the best synthesis method depending on the number of ancilla qubits available. If this specific synthesis method is required, one can specify it using the high-level-synthesis plugins `n_clean_m15` (using clean ancillas) or `n_dirty_i15` (using dirty ancillas) for MCX gates. Alternatively, one can use synth_mcx_n_dirty_i15 and synth_mcx_n_clean_m15 to construct the gate directly.r   r%  NF)r4   relative_phaseaction_onlyc               &   > [         TU ]  U UUUUS9$ )zmCreate a new MCX instance.

This must be defined anew to include the additional argument ``dirty_ancillas``.
r   r5  )	r   r2   dirty_ancillasr   r3   r4   rG  rH  r   s	           r   r   MCXVChain.__new__  s*    6 w!#  
 	
r    c               j   > [         TU ]  UUUSUS9  X l        X`l        Xpl        [         TU ]  XUSS9  g)a
  
Args:
    dirty_ancillas: when set to ``True``, the method applies an optimized multicontrolled-X gate
        up to a relative phase using dirty ancillary qubits with the properties of lemmas 7 and 8
        from arXiv:1501.06911, with at most 8*k - 6 CNOT gates.
        For k within the range {1, ..., ceil(n/2)}. And for n representing the total number of
        qubits.
    relative_phase: when set to ``True``, the method applies the optimized multicontrolled-X gate
        up to a relative phase, in a way that, by lemma 7 of arXiv:1501.06911, the relative
        phases of the ``action part`` cancel out with the phases of the ``reset part``.

    action_only: when set to ``True``, the method applies only the action part of lemma 8
        from arXiv:1501.06911.


mcx_vchainr9  r*  N)r   r   _dirty_ancillas_relative_phase_action_only)	r   r2   rJ  r   r3   r4   rG  rH  r   s	           r   r   MCXVChain.__init__  sO    4 	!# 	 	
  .-'*T`ar    c           	        [         R                  " 5          [         R                  " S[        SS9  [	        U R
                  U R                  U R                  U R                  U R                  S9nSSS5        U$ ! , (       d  f       W$ = f)a{  Invert this gate. The MCX is its own inverse.

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:
    MCXVChain: inverse gate (self-inverse).
r   z<.+qiskit\.circuit\.library\.standard_gates\.x\.MCXVChain\..+r  )r2   rJ  r3   rG  rH  N)
r   r   r   r   r   r2   rN  r3   rO  rP  r-  s      r   r<   MCXVChain.inverse  sz     $$&##+W
   $ 4 4#33??#33 --G '  '& s   AA::
B	c                ,    [         R                  X5      $ r<  r=  r  s     r   r    MCXVChain.get_num_ancilla_qubits  r?  r    c                    U R                   (       a/  SSKJn  U" U R                  U R                  U R
                  5      nOSSKJn  U" U R                  5      nX l        g)z0Define the MCX gate using a V-chain of CX gates.r   )synth_mcx_n_dirty_i15)synth_mcx_n_clean_m15N)rN  r
  rW  r2   rO  rP  rX  r,   )r   rW  r   rX  s       r   r.   MCXVChain._define
  sP     O&$$$$!!B P&t';';<Br    )rP  rN  rO  r,   )NFNN)r2   r  rJ  rL   r   rG   r3   rK   rG  rL   rH  rL   )FNN)r2   rJ   rJ  rL   r   rG   r3   rK   rG  rL   rH  rL   rM   rN   )r  rE  )rO   rP   rQ   rR   rS   r   r   r   r<   r"  r   r.   rV   rW   rX   s   @r   r   r     s   Lb ) *.$#04
 $!
&
 
 	

 .
 
 
 

4  %#04$b $!$b$b $b 	$b
 .$b $b $b $bL6 E E r    r   )%rS   
__future__r   r   typingr   r   r   r   qiskit.circuit.controlledgater   qiskit.circuit.singletonr   r	   r
   qiskit.circuit._utilsr   r   r   qiskit._accelerate.circuitr   qiskit.utils.deprecationr   _X_ARRAY	_SX_ARRAYr   r[   rw   r   r   r   r   r   r5   r   r   r   rp   r    r   <module>rc     s"   / "  ( (  8 a a a a 3 3FQF*%
J'?@	 u(M u( u(p Ha8FQ$ FQ 9FQR HatLZR% ZR MZRz    " ! !	*+} *+*+Z IqM1S& 1S N1Sh HatLUR% UR MURp 88898888888:8889!( +}  +)( +F HauMiR% iR NiRXS/n S/lK' K\O7 Od r    