
    z	i0                        S r SSKJr  SSKrSSKJ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  S	S
KJr   " S S\5      r " S S\5      r " S S\	5      rg)z'Multiple-Control, Multiple-Target Gate.    )annotationsN)Callable)circuit)ControlledGateGateQuantumCircuit)_ctrl_state_to_int)deprecate_func   )get_standard_gate_name_mappingc                     ^  \ rS rSrSr\" SSS9        SU 4S jj5       rS r\S 5       r	SU 4S	 jjr
SSS
 jjrSrU =r$ )MCMT   u;  The multi-controlled multi-target gate, for an arbitrary singly controlled target gate.

For example, the H gate controlled on 3 qubits and acting on 2 target qubit is represented as:

.. code-block:: text

    ───■────
       │
    ───■────
       │
    ───■────
    ┌──┴───┐
    ┤0     ├
    │  2-H │
    ┤1     ├
    └──────┘

This default implementations requires no ancilla qubits, by broadcasting the target gate
to the number of target qubits and using Qiskit's generic control routine to control the
broadcasted target on the control qubits. If ancilla qubits are available, a more efficient
variant using the so-called V-chain decomposition can be used. This is implemented in
:class:`~qiskit.circuit.library.MCMTVChain`.
1.4zUse MCMTGate instead.sinceadditional_msgc                  > US:X  d  US:X  a  [        S5      e[        U5      (       a'  [        R                  " S[        SS9  UR
                  nO?[        U[        5      (       a*  [        R                  " S[        SS9  UR                  5       n[        R                  U5      U l        X l        X0l        X#-   U R                  -   n[        TU ]A  USS9  U R#                  5         g	)
a  Create a new multi-control multi-target gate.

Args:
    gate: The gate to be applied controlled on the control qubits and applied to the target
        qubits. Can be either a Gate or a circuit method.
        If it is a callable, it will be casted to a Gate.
    num_ctrl_qubits: The number of control qubits.
    num_target_qubits: The number of target qubits.

Raises:
    AttributeError: If the gate cannot be casted to a controlled gate.
    AttributeError: If the number of controls or targets is 0.
r   z/Need at least one control and one target qubit.zPassing a callable to MCMT is pending deprecation since Qiskit 1.3. Pass a gate instance or the gate name instead, e.g. pass 'h' instead of QuantumCircuit.h.r   category
stacklevelzPassing a QuantumCircuit is pending deprecation since Qiskit 1.3. Pass a gate or turn the circuit into a gate using the ``to_gate`` method, instead.mcmt)nameN)AttributeErrorcallablewarningswarnDeprecationWarning__name__
isinstancer   to_gateMCMTGate_identify_base_gategatenum_ctrl_qubitsnum_target_qubitsnum_ancilla_qubitssuper__init___build)selfr$   r%   r&   
num_qubits	__class__s        g/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/library/generalized_gates/mcmt.pyr)   MCMT.__init__4   s    ( a#4#9 !RSSD>>MMe+	 ==Dn--MMY+	 <<>D006	.!2 %84;R;RR
&1    c                    [        U R                  U R                  U R                  5      nU R	                  XR
                  5        g N)r"   r$   r%   r&   appendqubits)r+   r$   s     r.   r*   MCMT._builde   s0    		4#7#79O9OPD++&r0   c                    g)zReturn the number of ancillas.r    r+   s    r.   r'   MCMT.num_ancilla_qubitsi   s     r0   c                   > U(       d3  Uc0  [        U R                  U R                  U-   U R                  5      nU$ [        TU ]  XX4S9nU$ )2Return the controlled version of the MCMT circuit.	annotated)r   r$   r%   r&   r(   control)r+   r%   label
ctrl_stater=   r$   r-   s         r.   r>   MCMT.controln   sN    Z/		4#7#7/#I4KaKabD  7??:?[Dr0   c                X    [        U R                  U R                  U R                  5      $ )z1Return the inverse MCMT circuit, which is itself.)r   r$   r%   r&   r+   r=   s     r.   inverseMCMT.inversev   s!    DIIt33T5K5KLLr0   )r$   r%   r&   r$   zTGate | Callable[[QuantumCircuit, circuit.Qubit, circuit.Qubit], circuit.Instruction]r%   intr&   rG   returnNone   NNFFr=   bool)r   
__module____qualname____firstlineno____doc__r
   r)   r*   propertyr'   r>   rD   __static_attributes____classcell__r-   s   @r.   r   r      sv    0 %0GH.b. . 	.
 
. I.`'  M Mr0   r   c                  x   ^  \ rS rSrSr\" SSS9        SU 4S jj5       rS r\S 5       r	SSS	 jjr
S
rU =r$ )
MCMTVChain{   u  The MCMT implementation using the CCX V-chain.

This implementation requires ancillas but is decomposed into a much shallower circuit
than the default implementation in :class:`~qiskit.circuit.library.MCMT`.

Expanded circuit:

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

   from qiskit.circuit.library import MCMTVChain, ZGate
   from qiskit.visualization.library import _generate_circuit_library_visualization
   circuit = MCMTVChain(ZGate(), 2, 2)
   _generate_circuit_library_visualization(circuit.decompose())

Examples:

    >>> from qiskit.circuit.library import HGate
    >>> MCMTVChain(HGate(), 3, 2).draw()

    q_0: ──■────────────────────────■──
           │                        │
    q_1: ──■────────────────────────■──
           │                        │
    q_2: ──┼────■──────────────■────┼──
           │    │  ┌───┐       │    │
    q_3: ──┼────┼──┤ H ├───────┼────┼──
           │    │  └─┬─┘┌───┐  │    │
    q_4: ──┼────┼────┼──┤ H ├──┼────┼──
         ┌─┴─┐  │    │  └─┬─┘  │  ┌─┴─┐
    q_5: ┤ X ├──■────┼────┼────■──┤ X ├
         └───┘┌─┴─┐  │    │  ┌─┴─┐└───┘
    q_6: ─────┤ X ├──■────■──┤ X ├─────
              └───┘          └───┘
r   z7Use MCMTGate with the V-chain synthesis plugin instead.r   c                &   > [         TU ]  XU5        g r2   )r(   r)   )r+   r$   r%   r&   r-   s       r.   r)   MCMTVChain.__init__   s     	0ABr0   c                    SSK Jn  U" U R                  U R                  U R                  5      nU R                  USSS9  g )Nr   )synth_mcmt_vchainTF)inplacecopy)!qiskit.synthesis.multi_controlledr]   r$   r%   r&   compose)r+   r]   synthesizeds      r.   r*   MCMTVChain._build   s5    G'		43G3GI_I_`[$U;r0   c                4    [        SU R                  S-
  5      $ )z-Return the number of ancilla qubits required.r   rK   )maxr%   r8   s    r.   r'   MCMTVChain.num_ancilla_qubits   s     1d**Q.//r0   c                X    [        U R                  U R                  U R                  5      $ r2   )rX   r$   r%   r&   rC   s     r.   rD   MCMTVChain.inverse   s!    $))T%9%94;Q;QRRr0   r7   rF   rL   rM   )r   rO   rP   rQ   rR   r
   r)   r*   rS   r'   rD   rT   rU   rV   s   @r.   rX   rX   {   sy    "H PCbC C 	C
 
C	C< 0 0S Sr0   rX   c                     ^  \ rS rSrSr  S	           S
U 4S jjjrS r\S 5       rSU 4S jjr	SSS jjr
SrU =r$ )r"      u  The multi-controlled multi-target gate, for an arbitrary singly controlled target gate.

For example, the H gate controlled on 3 qubits and acting on 2 target qubit is represented as:

.. parsed-literal::

    ───■────
       │
    ───■────
       │
    ───■────
    ┌──┴───┐
    ┤0     ├
    │  2-H │
    ┤1     ├
    └──────┘

Depending on the number of available auxiliary qubits, this operation can be synthesized
using different methods. For example, if :math:`n - 1` clean auxiliary qubits are available
(where :math:`n` is the number of control qubits), a V-chain decomposition can be used whose
depth is linear in :math:`n`. See also :func:`.synth_mcmt_chain`.
c           
        > US:  a  [        S5      eUS:  a  [        S5      eX0l        U R                  U5      nX#-   nUc  U SUR                  R	                  5        3n[
        TU ]  SUUUR                  UUUS9  g)a  
Args:
    gate: The base gate to apply on multiple target qubits, controlled by other qubits.
        This must be a single-qubit gate or a controlled single-qubit gate.
    num_ctrl_qubits: The number of control qubits.
    num_target_qubits: The number of target qubits.
    ctrl_state: The control state of the control qubits. Defaults to all closed controls.
    label: The gate label.
rK   zNeed at least one target qubit.z Need at least one control qubit.N-r   )	base_gater,   paramsr%   r@   r?   )
ValueErrorr&   r#   r   
capitalizer(   r)   rn   )	r+   r$   r%   r&   r@   r?   rm   r,   r-   s	           r.   r)   MCMTGate.__init__   s    " q >??Q?@@!2,,T2	$8
=()499+?+?+A*BCE!;;+! 	 	
r0   c                F    SSK Jn  U" 5       R                  U 5      U l        g)zSDefault definition relying on gate.control. Control state is handled by superclass.r   )MCMTSynthesisDefaultN).qiskit.transpiler.passes.synthesis.hls_pluginsrs   run
definition)r+   rs   s     r.   _defineMCMTGate._define   s     	X.044T:r0   c           	        [        U [        5      (       aA  [        5       nX;   a  X   n O-[        SU  S[	        [        R
                  " 5       5       35      e[        U [        5      (       a'  [        R                  " S[        SS9  U R                  nO0[        U [        5      (       a  U nO[        S[        U 5       S35      eUR                  S:w  a  [        S	UR                   S35      eU$ )
zAGet the control base gate. Note this must be a single qubit gate.zUnknown gate z. Available: zPassing a controlled gate to MCMT is pending deprecation since Qiskit 1.3. Pass a single-qubit gate instance or the gate name instead, e.g. pass 'h' instead of 'ch'.r   r   zInvalid gate type .rK   z;MCMTGate requires a base gate with a single qubit, but got )r    strr   r   listkeysr   r   r   r   rm   r   	TypeErrortyper,   ro   )r$   standard_gatesrm   s      r.   r#   MCMTGate._identify_base_gate  s    
 dC  ;=N%%+$#D6t<Z<_<_<a7b6cd 
 dN++MMf+	 Id##I0dA>??1$MiNbNbMccde  r0   c                   > U(       dL  [        X15      nU R                  U-  U-  n[        U R                  U R                  U-   U R
                  US9nU$ [        TU ]  XX4S9nU$ )r;   )r@   r<   )r	   r@   r"   rm   r%   r&   r(   r>   )r+   r%   r?   r@   r=   new_ctrl_stater$   r-   s          r.   r>   MCMTGate.control'  sn    +JHJ"oo@JNN$$6&&)	D  7??:?[Dr0   c                    [        U R                  R                  5       U R                  U R                  U R
                  5      $ )z Return the inverse MCMT circuit.)r"   rm   rD   r%   r&   r@   rC   s     r.   rD   MCMTGate.inverse8  s8    NN""$d&:&:D<R<RTXTcTc
 	
r0   )rv   r&   )NN)r$   r   r%   rG   r&   rG   r@   zint | str | Noner?   z
str | NonerH   rI   rJ   rL   rM   )r   rO   rP   rQ   rR   r)   rw   staticmethodr#   r>   rD   rT   rU   rV   s   @r.   r"   r"      s~    8 (, '
'
 '
 	'

 %'
 '
 
'
 '
R;    D"
 
r0   r"   )rR   
__future__r   r   collections.abcr   qiskitr   qiskit.circuitr   r   r   qiskit.circuit._utilsr	   qiskit.utils.deprecationr
   r   r   r   rX   r"   r7   r0   r.   <module>r      sS    . "  $  ? ? 4 3 ;]M> ]M@>S >SB@
~ @
r0   