§
    º³ÙgÑ+  ã            	       óÈ   — d Z ddlmZ ddlZddlmZ ddlmZ ddl	m
Z
mZmZmZmZ 	 	 dded	ed
edefd„Zdedefd„Zdedefd„Zdedefd„Zdedefd„Zdefd„Zdefd„ZdS )zXModule containing multi-controlled circuits synthesis with and without ancillary qubits.é    )ÚceilN)ÚQuantumRegister)ÚQuantumCircuit)ÚHGateÚMCU1GateÚCU1GateÚRC3XGateÚC3SXGateFÚnum_ctrl_qubitsÚrelative_phaseÚaction_onlyÚreturnc                 ó†	  — d| z  dz
  }t          |d¬¦  «        }t          |d¬¦  «        }|d| …         }||          }|| dz   d…         }| dk    r|                     ||¦  «         |S | dk    r%|                     |d         |d         |¦  «         |S |s3| dk    r-t	          ¦   «         }	|                     |	g |¢|‘d	d
¬¦  «         |S | dz
  }
|g|d|
…         ddd…         z   }t          d¦  «        D �]Ì}t          | ¦  «        D �]Õ}|| dz
  k     �r¯||         |k    s|�rY|r´||         |k    r¨|dk    r¢|                     ||
|z
  dz
           ||         ¦  «         |                     ||         ¦  «         |                     || |z
  dz
           ||         ¦  «         |                     ||         ¦  «         | 	                    ||         ¦  «         ŒÒ| 	                    ||         ¦  «         |                     ||         ¦  «         |                     || |z
  dz
           ||         ¦  «         |                     ||         ¦  «         |                     ||
|z
  dz
           ||         ¦  «         �Œu|| |z
  dz
           ||
|z
  dz
           g}|                     |d         |d         ||         ¦  «         �Œ¼| 	                    ||         ¦  «         |                     ||         ¦  «         |                     || |z
  dz
           ||         ¦  «         |                     ||         ¦  «         |                     || |z
  dz
           ||         ¦  «         |                     ||         ¦  «         |                     || |z
  dz
           ||         ¦  «         |                     ||         ¦  «         | 	                    ||         ¦  «          t          |
dz
  ¦  «        D ]©}|                     ||         ||dz            ¦  «         |                     ||dz            ¦  «         |                     |d|z            ||dz            ¦  «         |                     ||dz            ¦  «         | 	                    ||dz            ¦  «         Œª|r%|                     |d         |d         |¦  «          n�ŒÎ|S )a‘  
    Synthesize a multi-controlled X gate with :math:`k` controls using :math:`k - 2`
    dirty ancillary qubits producing a circuit with :math:`2 * k - 1` qubits and at most
    :math:`8 * k - 6` CX gates, by Iten et. al. [1].

    Args:
        num_ctrl_qubits: The number of control qubits.

        relative_phase: when set to ``True``, the method applies the optimized multi-controlled X gate
            up to a relative phase, in a way that, by lemma 8 of [1], 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 of [1].

    Returns:
        The synthesized quantum circuit.

    References:
        1. Iten et. al., *Quantum Circuits for Isometries*, Phys. Rev. A 93, 032318 (2016),
           `arXiv:1501.06911 <http://arxiv.org/abs/1501.06911>`_
    é   é   Úq©ÚnameÚ
mcx_vchainNr   é   TF)ÚinplaceÚcopyéÿÿÿÿ)
r   r   ÚcxÚccxÚ	synth_c3xÚcomposeÚrangeÚtÚtdgÚh)r   r   r   Ú
num_qubitsr   ÚqcÚ
q_controlsÚq_targetÚ
q_ancillasÚcircuitÚnum_ancillasÚtargetsÚjÚiÚcontrolss                  úe/granary/edev/quantum/lib/python3.11/site-packages/qiskit/synthesis/multi_controlled/mcx_synthesis.pyÚsynth_mcx_n_dirty_i15r.      sþ  € ð6 �_Ñ$ qÑ(€JÝ˜
¨Ð-Ñ-Ô-€AÝ	˜ Ð	-Ñ	-Ô	-€BØÐ#�OÐ#Ô$€JØ�Ô!€HØ�? QÑ&Ð(Ð(Ô)€Jà˜!ÒÐØ
�Šˆj˜(Ñ#Ô#Ð#Øˆ	Ø	˜AÒ	Ð	Ø
�Šˆz˜!Œ}˜j¨œm¨XÑ6Ô6Ð6Øˆ	Øð  °1Ò 4Ð 4Ý‘+”+ˆØ
�
Š
�7Ð3˜jÐ3¨(Ð3¸TÈˆ
ÑNÔNÐNØˆ	à" QÑ&€LØˆj˜: m | mÔ4°T°T°r°TÔ:Ñ:€Gå�1‰XŒXð 4ñ 4ˆÝ�Ñ'Ô'ð '	ñ '	ˆAØ�? QÑ&Ò&Ñ&Ø˜1”: Ò)Ð)¨^Ñ)ð
 &ð L¨'°!¬*¸Ò*@Ð*@ÀQÈ!ÂVÀVØŸš˜j¨¸Ñ)9¸AÑ)=Ô>ÀÈÄ
ÑKÔKÐKØŸš˜W QœZÑ(Ô(Ð(ØŸš˜j¨¸1Ñ)<¸qÑ)@ÔAÀ7È1Ä:ÑNÔNÐNØŸš˜w qœzÑ*Ô*Ð*ØŸš˜W QœZÑ(Ô(Ð(Ð(àŸš˜W QœZÑ(Ô(Ð(ØŸš˜W QœZÑ(Ô(Ð(ØŸš˜j¨¸1Ñ)<¸qÑ)@ÔAÀ7È1Ä:ÑNÔNÐNØŸš˜w qœzÑ*Ô*Ð*ØŸš˜j¨¸Ñ)9¸AÑ)=Ô>ÀÈÄ
ÑKÔKÐKÑKð # ?°QÑ#6¸Ñ#:Ô;Ø" <°!Ñ#3°aÑ#7Ô8ð �Hð
 —F’F˜8 Aœ;¨°¬°W¸Q´ZÑ@Ô@Ð@Ñ@ð —’�W˜Q”ZÑ Ô Ð Ø—’�W˜Q”ZÑ Ô Ð Ø—’�j °1Ñ!4°qÑ!8Ô9¸7À1¼:ÑFÔFÐFØ—’�w˜q”zÑ"Ô"Ð"Ø—’�j °1Ñ!4°qÑ!8Ô9¸7À1¼:ÑFÔFÐFØ—’�W˜Q”ZÑ Ô Ð Ø—’�j °1Ñ!4°qÑ!8Ô9¸7À1¼:ÑFÔFÐFØ—’�w˜q”zÑ"Ô"Ð"Ø—’�W˜Q”ZÑ Ô Ð àå�| aÑ'Ñ(Ô(ð 	$ð 	$ˆAØ�EŠE�*˜Q”- ¨A°©EÔ!2Ñ3Ô3Ð3Ø�DŠD�˜A ™EÔ"Ñ#Ô#Ð#Ø�EŠE�*˜Q ™UÔ# Z°°A±Ô%6Ñ7Ô7Ð7Ø�FŠF�:˜a !™eÔ$Ñ%Ô%Ð%Ø�DŠD�˜A ™EÔ"Ñ#Ô#Ð#Ð#àð 	Ø�FŠF�:˜b”> :¨b¤>°8Ñ<Ô<Ð<àˆEñ	ð
 €Ió    c                 ó¼  — d| z  dz
  }t          |d¬¦  «        }t          |d¬¦  «        }|d| …         }||          }|| dz   d…         }|                     |d         |d         |d         ¦  «         d}t          d| dz
  ¦  «        D ]3}|                     ||         ||         ||dz            ¦  «         |dz  }Œ4|                     |d         ||         |¦  «         t          t          d| dz
  ¦  «        ¦  «        D ]3}|                     ||         ||dz
           ||         ¦  «         |dz  }Œ4|                     |d         |d         ||         ¦  «         |S )	aç  
    Synthesize a multi-controlled X gate with :math:`k` controls using :math:`k - 2`
    clean ancillary qubits with producing a circuit with :math:`2 * k - 1` qubits
    and at most :math:`6 * k - 6` CX gates, by Maslov [1].

    Args:
        num_ctrl_qubits: The number of control qubits.

    Returns:
        The synthesized quantum circuit.

    References:
        1. Maslov., Phys. Rev. A 93, 022311 (2016),
           `arXiv:1508.03273 <https://arxiv.org/pdf/1508.03273>`_
    r   r   r   r   r   Nr   r   )r   r   Úrccxr   r   Úreversed)	r   r"   r   r#   r$   r%   r&   r+   r*   s	            r-   Úsynth_mcx_n_clean_m15r3   †   sz  € ð" �_Ñ$ qÑ(€JÝ˜
¨Ð-Ñ-Ô-€AÝ	˜ Ð	-Ñ	-Ô	-€BØÐ#�OÐ#Ô$€JØ�Ô!€HØ�? QÑ&Ð(Ð(Ô)€Jà‡G‚GˆJ�qŒM˜: aœ=¨*°Q¬-Ñ8Ô8Ð8Ø	€AÝ�1�o¨Ñ)Ñ*Ô*ð ð ˆØ
�Š�
˜1”˜z¨!œ}¨j¸¸Q¹Ô.?Ñ@Ô@Ð@à	ˆQ‰ˆˆà‡F‚Fˆ:�bŒ>˜: aœ=¨(Ñ3Ô3Ð3å•e˜A˜°Ñ2Ñ3Ô3Ñ4Ô4ð ð ˆØ
�Š�
˜1”˜z¨!¨a©%Ô0°*¸Q´-Ñ@Ô@Ð@à	ˆQ‰ˆˆà‡G‚GˆJ�qŒM˜: aœ=¨*°Q¬-Ñ8Ô8Ð8à€Ir/   c           
      óv  — | dk    rt          ¦   «         S | dk    rt          ¦   «         S | dz   }t          |d¬¦  «        }t          |d¬¦  «        }t	          |¦  «        dz
  } |d         }|d	         }t          | dz  ¦  «        }g |d
|…         ¢}g ||| dz
  …         ¢|‘}t          t	          |¦  «        ¬¦  «        }	t          t	          |¦  «        ¬¦  «        }
|                     |	g |¢|‘|||t	          |¦  «        z   dz
  …         ¢g ¬¦  «         |                     |
g |¢|‘|d
t	          |¦  «        dz
  …         ¢g ¬¦  «         |                     |	g |¢|‘|||t	          |¦  «        z   dz
  …         ¢g ¬¦  «         |                     |
g |¢|‘|d
t	          |¦  «        dz
  …         ¢g ¬¦  «         |S )að  
    Synthesize a multi-controlled X gate with :math:`k` controls using a single
    clean ancillary qubit producing a circuit with :math:`k + 2` qubits and at most
    :math:`16 * k - 8` CX gates, by Barenco et al. [1].

    Args:
        num_ctrl_qubits: The number of control qubits.

    Returns:
        The synthesized quantum circuit.

    References:
        1. Barenco et. al., Phys.Rev. A52 3457 (1995),
           `arXiv:quant-ph/9503016 <https://arxiv.org/abs/quant-ph/9503016>`_
    r   é   r   r   r   Úmcx_recursiver   r   éþÿÿÿN©r   )ÚqargsÚcargs)r   Ú	synth_c4xr   r   Úlenr   r.   Úappend)r   r"   r   r#   Ú	q_ancillar%   ÚmiddleÚ
first_halfÚsecond_halfÚqc_first_halfÚqc_second_halfs              r-   Úsynth_mcx_1_clean_b95rD   ±   s)  € ð" ˜!ÒÐÝ‰{Œ{Ðà	˜AÒ	Ð	Ý‰{Œ{Ðà  1Ñ$€JÝ˜
¨Ð-Ñ-Ô-€AÝ	˜ Ð	0Ñ	0Ô	0€Bå˜!‘f”f˜q‘j€OØ�"”€IØ�Œu€HÝ�/ AÑ%Ñ&Ô&€FØ�1�W�f�W”:�€JØ?�A�f˜°Ñ2Ð2Ô3Ð?°YÐ?€Kå)½#¸j¹/¼/ÐJÑJÔJ€MÝ*½3¸{Ñ;KÔ;KÐLÑLÔL€Nà‡I‚IØØQ�
ÐQ˜IÐQ¨¨&°6½CÀ
¹O¼OÑ3KÈaÑ3OÐ*OÔ(PÐQØð ñ ô ð ð
 ‡I‚IØØB�ÐB˜XÐB¨Ð*@­C°Ñ,<Ô,<¸qÑ,@Ð*@Ô(AÐBØð ñ ô ð ð
 ‡I‚IØØQ�
ÐQ˜IÐQ¨¨&°6½CÀ
¹O¼OÑ3KÈaÑ3OÐ*OÔ(PÐQØð ñ ô ð ð
 ‡I‚IØØB�ÐB˜XÐB¨Ð*@­C°Ñ,<Ô,<¸qÑ,@Ð*@Ô(AÐBØð ñ ô ð ð €Ir/   c                 ól  — | dz   }t          |d¬¦  «        }t          |d¬¦  «        }|                     t          ¦   «         |d         gg ¦  «         |                     t	          t
          j        | ¬¦  «        |dd…         g ¦  «         |                     t          ¦   «         |d         gg ¦  «         |S )a  
    Synthesize a multi-controlled X gate with :math:`k` controls using the Gray code.

    Produces a quantum circuit with :math:`k + 1` qubits. This method
    produces exponentially many CX gates and should be used only for small
    values of :math:`k`.

    Args:
        num_ctrl_qubits: The number of control qubits.

    Returns:
        The synthesized quantum circuit.
    r   r   r   Úmcx_grayr   r8   N)r   r   Ú_appendr   r   ÚnpÚpi)r   r"   r   r#   s       r-   Úsynth_mcx_gray_coderJ   î   s¤   € ð ! 1Ñ$€JÝ˜
¨Ð-Ñ-Ô-€AÝ	˜ 
Ð	+Ñ	+Ô	+€BØ‡J‚J�u‰wŒw˜˜2œ˜ Ñ$Ô$Ð$Ø‡J‚J�x�œ¨Ð?Ñ?Ô?ÀÀ1À1À1ÄÀrÑJÔJÐJØ‡J‚J�u‰wŒw˜˜2œ˜ Ñ$Ô$Ð$Ø€Ir/   c                 ór  — | dk    rt          ¦   «         S | dk    rt          ¦   «         S | dz   }t          |d¬¦  «        }t          |¦  «        }t	          t          | ¦  «        ¦  «        }| }|                     |¦  «         |                     t          j	        ||¦  «         |                     |¦  «         |S )a²  
    Synthesize a multi-controlled X gate with :math:`k` controls based on
    the implementation for MCPhaseGate.

    In turn, the MCPhase gate uses the decomposition for multi-controlled
    special unitaries described in [1].

    Produces a quantum circuit with :math:`k + 1` qubits.
    The number of CX-gates is quadratic in :math:`k`.

    Args:
        num_ctrl_qubits: The number of control qubits.

    Returns:
        The synthesized quantum circuit.

    References:
        1. Vale et. al., *Circuit Decomposition of Multicontrolled Special Unitary
           Single-Qubit Gates*, IEEE TCAD 43(3) (2024),
           `arXiv:2302.06377 <https://arxiv.org/abs/2302.06377>`_
    r   r5   r   r   r   )
r   r;   r   r   Úlistr   r!   ÚmcprH   rI   )r   r"   r   r#   r$   r%   s         r-   Úsynth_mcx_noaux_v24rN     s¬   € ð, ˜!ÒÐÝ‰{Œ{Ðà˜!ÒÐÝ‰{Œ{Ðà  1Ñ$€JÝ˜
¨Ð-Ñ-Ô-€AÝ	˜Ñ	Ô	€BÝ•e˜OÑ,Ô,Ñ-Ô-€JØ€HØ‡D‚Dˆ�N„N€NØ‡F‚F�2Œ5�*˜hÑ'Ô'Ð'Ø‡D‚Dˆ�N„N€NØ€Ir/   c                  ó`  — t          dd¬¦  «        } t          | d¬¦  «        }|                     d¦  «         |                     t          j        dz  g d¢¦  «         |                     dd	¦  «         |                     t          j         dz  d	¦  «         |                     dd	¦  «         |                     d	d
¦  «         |                     t          j         dz  d
¦  «         |                     dd
¦  «         |                     t          j        dz  d
¦  «         |                     d	d
¦  «         |                     t          j         dz  d
¦  «         |                     dd
¦  «         |                     d
d¦  «         |                     t          j         dz  d¦  «         |                     d	d¦  «         |                     t          j        dz  d¦  «         |                     d
d¦  «         |                     t          j         dz  d¦  «         |                     dd¦  «         |                     t          j        dz  d¦  «         |                     d
d¦  «         |                     t          j         dz  d¦  «         |                     d	d¦  «         |                     t          j        dz  d¦  «         |                     d
d¦  «         |                     t          j         dz  d¦  «         |                     dd¦  «         |                     d¦  «         |S )z+Efficient synthesis of 3-controlled X-gate.r5   r   r   Úmcxr   é   )r   r   r   r   r   r   r   )r   r   r!   ÚprH   rI   r   )r   r#   s     r-   r   r   ,  sƒ  € õ 	˜ Ð$Ñ$Ô$€AÝ	˜ Ð	&Ñ	&Ô	&€BØ‡D‚Dˆ�G„G€GØ‡D‚D�Œ�‰�L�L�LÑ!Ô!Ð!Ø‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�"Œ%ˆ�!‰�QÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�"Œ%ˆ�!‰�QÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�Œ�‰�AÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�"Œ%ˆ�!‰�QÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�"Œ%ˆ�!‰�QÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�Œ�‰�AÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�"Œ%ˆ�!‰�QÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�Œ�‰�AÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�"Œ%ˆ�!‰�QÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�Œ�‰�AÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚D�"Œ%ˆ�!‰�QÑÔÐØ‡E‚Eˆ!ˆQ�K„K€KØ‡D‚Dˆ�G„G€GØ€Ir/   c                  ó$  — t          dd¬¦  «        } t          | d¬¦  «        }t          ¦   «         | d         gg ft          t          j        dz  ¦  «        | d         | d         gg ft          ¦   «         | d         gg ft          ¦   «         | d         | d	         | d         | d         gg ft          ¦   «         | d         gg ft          t          j         dz  ¦  «        | d         | d         gg ft          ¦   «         | d         gg ft          ¦   «                              ¦   «         | d         | d	         | d         | d         gg ft          ¦   «         | d         | d	         | d         | d         gg fg	}|D ]\  }}}| 	                    |||¦  «         Œ|S )
z+Efficient synthesis of 4-controlled X-gate.é   r   r   rP   r5   r   r   r   r   )
r   r   r   r   rH   rI   r	   Úinverser
   rG   )r   r#   ÚrulesÚinstrr9   r:   s         r-   r;   r;   P  s‚  € õ 	˜ Ð$Ñ$Ô$€AÝ	˜ Ð	&Ñ	&Ô	&€Bõ 
‰Œ�1�Q”4�&˜"ÐÝ	•”˜‘Ñ	Ô	˜a œd A a¤D˜\¨2Ð.Ý	‰Œ�1�Q”4�&˜"ÐÝ	‰Œ�a˜”d˜A˜aœD ! A¤$¨¨!¬Ð-¨rÐ2Ý	‰Œ�1�Q”4�&˜"ÐÝ	•"”%�˜!‘Ñ	Ô	˜q œt Q q¤T˜l¨BÐ/Ý	‰Œ�1�Q”4�&˜"ÐÝ	‰Œ×	Ò	Ñ	Ô	  !¤ a¨¤d¨A¨a¬D°!°A´$Ð7¸Ð<Ý	‰Œ�a˜”d˜A˜aœD ! A¤$¨¨!¬Ð-¨rÐ2ð
€Eð  %ð (ð (Ñˆˆu�eØ
�
Š
�5˜% Ñ'Ô'Ð'Ð'à€Ir/   )FF)Ú__doc__Úmathr   ÚnumpyrH   Úqiskit.circuit.quantumregisterr   Úqiskit.circuit.quantumcircuitr   Ú%qiskit.circuit.library.standard_gatesr   r   r   r	   r
   ÚintÚboolr.   r3   rD   rJ   rN   r   r;   © r/   r-   ú<module>ra      s±  ðð _Ð ^à Ð Ð Ð Ð Ð Ø Ð Ð Ð à :Ð :Ð :Ð :Ð :Ð :Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8ðð ð ð ð ð ð ð ð ð ð ð ð ð ð !Øðfð fØðfàðfð ðfð ð	fð fð fð fðR(¨3ð (°>ð (ð (ð (ð (ðV:¨3ð :°>ð :ð :ð :ð :ðz¨ð °ð ð ð ð ð.$¨ð $°ð $ð $ð $ð $ðN!�>ð !ð !ð !ð !ðH�>ð ð ð ð ð ð r/   