§
    ¹³Ùg×9  ã                   ó|   — d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZmZ 	 	 	 	 	 	 dd
efd„Zdd„ZdS )z1Utility functions for generating random circuits.é    N)ÚClassicalRegisterÚQuantumCircuitÚCircuitInstruction)ÚReset)Ústandard_gates)ÚCircuitError)Ú	_BASIS_1QÚ	_BASIS_2Qé   FÚnum_operand_distributionc                 ó  ‡1— |€At           j                             dt          j        t           j        ¦  «        j        ¦  «        }t           j                             |¦  «        }|rÃt          |                     ¦   «         ¦  «        dk     s%t          |                     ¦   «         ¦  «        dk    rt          d¦  «        ‚| 
                    ¦   «         D ]'\  }	}
|	| k    r|
dk    rt          d|	› d| › d	�¦  «        ‚Œ(t          t          | 
                    ¦   «         ¦  «        ¦  «        }|s—|r•|dk     s|dk    rt          d
¦  «        ‚| |k    r|n| }|                     t          j        |¦  «        ¦  «        Š1ˆ1fd„t          |¦  «        D ¦   «         }t          t          | 
                    ¦   «         ¦  «        ¦  «        }t          j        t#          |                     ¦   «         ¦  «        d¦  «        st          d¦  «        ‚| dk    rt'          ¦   «         S t(          j        ddft(          j        ddft(          j        ddft(          j        ddft(          j        ddft(          j        ddft(          j        ddft(          j        ddft(          j        ddft(          j        ddft(          j        ddft(          j         ddft(          j!        ddft(          j"        ddft(          j#        ddft(          j$        ddft(          j%        ddft(          j&        ddft(          j'        ddft(          j(        ddfg}|r| )                    tT          ddf¦  «         t(          j+        ddft(          j,        ddft(          j-        ddft(          j.        ddft(          j/        ddft(          j0        ddft(          j1        ddft(          j2        ddft(          j3        ddft(          j4        ddft(          j5        ddft(          j6        ddft(          j7        ddft(          j8        ddft(          j9        ddft(          j:        ddft(          j;        ddft(          j<        ddft(          j=        ddft(          j>        ddft(          j?        ddft(          j@        ddft(          jA        ddft(          jB        ddfg}t(          jC        ddft(          jD        ddft(          jE        ddft(          jF        ddfg}t(          jG        ddft(          jH        ddfg}t          jI        |dt”          fdt           jK        fdt           jK        fg¬¦  «        }t          jI        ||jL        ¬¦  «        }t          jI        ||jL        ¬¦  «        }t          jI        ||jL        ¬¦  «        }||||g}g }g }| 
                    ¦   «         D ][\  }}||dz
           }| M                    |¦  «         | M                    |t�          |¦  «        z  gt�          |¦  «        z  ¦  «         Œ\t          jI        ||jL        ¬¦  «        }t'          | ¦  «        }|s|r%tŸ          | d¦  «        }| P                    |¦  «         t          jI        |jQ        t”          d¬¦  «        }t          jR        t�          |¦  «        dz   t           jK        ¬¦  «        }d}t          |¦  «        D �]3}| S                    |t�          |¦  «        |¬¦  «        }t          jT        |d         t           jK        ¬¦  «        }t          jU        || d¬¦  «        }|d|…         }| ||dz
           z
  }|d         } |t          jV        | t�          |¦  «        dz   ¬¦  «        z  }|t�          |¦  «        z  }|dk    rÈd}!t          | 
                    ¦   «         d¬¦  «        D ]™\  }	}"||	k    rŽ||	         |z  |"k     rt          jI        ||	dz
           | W                    dt�          ||	dz
           ¦  «        ¦  «                 ¦  «        }#t          jX        ||#f¦  «        }||	xx         dz  cc<   |dz  }||	z  }d}!Œš|!sn|dk    °Èt          jY        t�          |¦  «        dz   t           jK        ¬¦  «        }$t          jY        t�          |¦  «        dz   t           jK        ¬¦  «        }%dx|$d<   |%d<   t          jT        |d         |$dd…         ¬¦  «         t          jT        |d         |%dd…         ¬¦  «         | Z                    ddt           j[        z  |%d         ¬¦  «        }&| \                    |¦  «         |�r5|dk    �r.|                     t�          |¦  «        ¬¦  «        dk     }'| W                    ddt          | d ¦  «        z  t          j]        |'¦  «        ¬¦  «        }(d})t½          |d         |$dd…         |$dd…         |%dd…         |%dd…         |'¦  «        D ]�\  }*}+},}-}.}/ |*|&|-|.…         Ž }0|/rI| _                    |jQ        |¦  «         |0 `                    |tÃ          |(|)         ¦  «        ¦  «        }0|)dz  })| b                    tÇ          |0||+|,…         ¬!¦  «        ¦  «         ŒŽ�Œ¸t½          |d         |$dd…         |$dd…         |%dd…         |%dd…         ¦  «        D ]A\  }*}+},}-}. |*|&|-|.…         Ž }0| b                    tÇ          |0||+|,…         ¬!¦  «        ¦  «         ŒB�Œ5|r| _                    |jQ        |¦  «         |S )"a?  Generate random circuit of arbitrary size and form.

    This function will generate a random circuit by randomly selecting gates
    from the set of standard gates in :mod:`qiskit.circuit.library.standard_gates`. For example:

    .. plot::
       :alt: Circuit diagram output by the previous code.
       :include-source:

       from qiskit.circuit.random import random_circuit

       circ = random_circuit(2, 2, measure=True)
       circ.draw(output='mpl')

    Args:
        num_qubits (int): number of quantum wires
        depth (int): layers of operations (i.e. critical path length)
        max_operands (int): maximum qubit operands of each gate (between 1 and 4)
        measure (bool): if True, measure all qubits at the end
        conditional (bool): if True, insert middle measurements and conditionals
        reset (bool): if True, insert middle resets
        seed (int): sets random seed (optional)
        num_operand_distribution (dict): a distribution of gates that specifies the ratio
        of 1-qubit, 2-qubit, 3-qubit, ..., n-qubit gates in the random circuit. Expect a
        deviation from the specified ratios that depends on the size of the requested
        random circuit. (optional)

    Returns:
        QuantumCircuit: constructed circuit

    Raises:
        CircuitError: when invalid options given
    Nr   é   r   z9'num_operand_distribution' must have keys between 1 and 4g        z''num_operand_distribution' cannot have z-qubit gates for circuit with z qubitsz$max_operands must be between 1 and 4c                 ó(   •— i | ]}|d z   ‰|         “ŒS )r   © )Ú.0ÚiÚ	rand_dists     €úQ/granary/edev/quantum/lib/python3.11/site-packages/qiskit/circuit/random/utils.pyú
<dictcomp>z"random_circuit.<locals>.<dictcomp>Y   s#   ø€ Ð#UÐ#UÐ#U¸A A¨¡E¨9°Q¬<Ð#UÐ#UÐ#Uó    zAThe sum of all the values in 'num_operand_distribution' is not 1.é   é   ÚclassÚ
num_qubitsÚ
num_params)ÚdtypeÚcT)r   Úcopy)ÚsizeÚpÚright)Úside)Ú	minlengthF)Úreverse)Úoutéÿÿÿÿ)r   gš™™™™™¹?é?   )Ú	operationÚqubits)dÚnpÚrandomÚrandintÚiinfoÚint32ÚmaxÚdefault_rngÚminÚkeysr   ÚitemsÚdictÚsortedÚ	dirichletÚonesÚrangeÚiscloseÚsumÚvaluesr   r   ÚIGateÚSXGateÚXGateÚRZGateÚRGateÚHGateÚ	PhaseGateÚRXGateÚRYGateÚSGateÚSdgGateÚSXdgGateÚTGateÚTdgGateÚUGateÚU1GateÚU2GateÚU3GateÚYGateÚZGateÚappendr   ÚCXGateÚDCXGateÚCHGateÚ
CPhaseGateÚCRXGateÚCRYGateÚCRZGateÚCSXGateÚCUGateÚCU1GateÚCU3GateÚCYGateÚCZGateÚRXXGateÚRYYGateÚRZZGateÚRZXGateÚXXMinusYYGateÚXXPlusYYGateÚECRGateÚCSGateÚCSdgGateÚSwapGateÚ	iSwapGateÚCCXGateÚ	CSwapGateÚCCZGateÚRCCXGateÚC3SXGateÚRC3XGateÚarrayÚobjectÚint64r   ÚextendÚlenr   Úadd_registerr)   ÚzerosÚchoiceÚcumsumÚsearchsortedÚbincountÚintegersÚhstackÚemptyÚuniformÚpiÚshuffleÚcount_nonzeroÚzipÚmeasureÚc_ifÚintÚ_appendr   )2r   ÚdepthÚmax_operandsr‚   ÚconditionalÚresetÚseedr   ÚrngÚkeyÚprobÚgates_1qÚgates_2qÚgates_3qÚgates_4qÚall_gate_listsÚgates_to_considerÚdistributionÚn_qubitsÚratioÚ	gate_listÚgatesÚqcÚcrr)   ÚcounterÚtotal_gatesÚlayer_numberÚ
gate_specsÚcumulative_qubitsÚ	max_indexÚslackÚgate_qubitsÚgate_added_flagÚdistÚgate_to_addÚ	q_indicesÚ	p_indicesÚ
parametersÚis_conditionalÚcondition_valuesÚc_ptrÚgateÚq_startÚq_endÚp_startÚp_endÚis_condr(   r   s2                                                    @r   Úrandom_circuitr²      si  ø€ ðV €|ÝŒy× Ò  ¥B¤H­R¬XÑ$6Ô$6Ô$:Ñ;Ô;ˆÝ
Œ)×
Ò
 Ñ
%Ô
%€Càð 	RÝÐ'×,Ò,Ñ.Ô.Ñ/Ô/°!Ò3Ð3µsÐ;S×;XÒ;XÑ;ZÔ;ZÑ7[Ô7[Ð^_Ò7_Ð7_ÝÐZÑ[Ô[Ð[Ø1×7Ò7Ñ9Ô9ð 	ð 	‰IˆC�Ø�ZÒÐ D¨C¢K KÝ"ð=¸cð =ð =Ø)3ð=ð =ð =ñô ð øõ $(­Ð/G×/MÒ/MÑ/OÔ/OÑ(PÔ(PÑ#QÔ#QÐ à#ð R¨ð RØ˜!ÒÐ˜|¨aÒ/Ð/ÝÐEÑFÔFÐFØ'1°LÒ'@Ð'@�|�|ÀjˆØ—M’MÝŒG�LÑ!Ô!ñ
ô 
ˆ	ð $VÐ#UÐ#UÐ#UÅÀ|ÑATÔATÐ#UÑ#UÔ#UÐ Ý#'­Ð/G×/MÒ/MÑ/OÔ/OÑ(PÔ(PÑ#QÔ#QÐ õ Œ:•cÐ2×9Ò9Ñ;Ô;Ñ<Ô<¸aÑ@Ô@ð `ÝÐ^Ñ_Ô_Ð_à�Q‚€ÝÑÔÐõ 
Ô	˜q !Ð$Ý	Ô	  1Ð%Ý	Ô	˜q !Ð$Ý	Ô	  1Ð%Ý	Ô	˜q !Ð$Ý	Ô	˜q !Ð$Ý	Ô	! 1 aÐ(Ý	Ô	  1Ð%Ý	Ô	  1Ð%Ý	Ô	˜q !Ð$Ý	Ô	  AÐ&Ý	Ô	  ! QÐ'Ý	Ô	˜q !Ð$Ý	Ô	  AÐ&Ý	Ô	˜q !Ð$Ý	Ô	  1Ð%Ý	Ô	  1Ð%Ý	Ô	  1Ð%Ý	Ô	˜q !Ð$Ý	Ô	˜q !Ð$ð+€Hð. ð 'Ø�Š�  1˜Ñ&Ô&Ð&å	Ô	  1Ð%Ý	Ô	  AÐ&Ý	Ô	  1Ð%Ý	Ô	" A qÐ)Ý	Ô	  AÐ&Ý	Ô	  AÐ&Ý	Ô	  AÐ&Ý	Ô	  AÐ&Ý	Ô	  1Ð%Ý	Ô	  AÐ&Ý	Ô	  AÐ&Ý	Ô	  1Ð%Ý	Ô	  1Ð%Ý	Ô	  AÐ&Ý	Ô	  AÐ&Ý	Ô	  AÐ&Ý	Ô	  AÐ&Ý	Ô	% q¨!Ð,Ý	Ô	$ a¨Ð+Ý	Ô	  AÐ&Ý	Ô	  1Ð%Ý	Ô	  ! QÐ'Ý	Ô	  ! QÐ'Ý	Ô	! 1 aÐ(ð1€Hõ6 
Ô	  AÐ&Ý	Ô	! 1 aÐ(Ý	Ô	  AÐ&Ý	Ô	  ! QÐ'ð	€Hõ 
Ô	  ! QÐ'Ý	Ô	  ! QÐ'ð€Hõ
 ŒxØ˜'¥6Ð*¨\½2¼8Ð,DÀ|ÕUWÔU]ÐF^Ð_ðñ ô €Hõ Œx˜¨¬Ð7Ñ7Ô7€HÝŒx˜¨¬Ð7Ñ7Ô7€HÝŒx˜¨¬Ð7Ñ7Ô7€Hà ¨(°HÐ=€Nð
 ÐØ€LØ3×9Ò9Ñ;Ô;ð Gð G‰ˆ�%Ø" 8¨a¡<Ô0ˆ	Ø× Ò  Ñ+Ô+Ð+Ø×Ò˜U¥S¨¡^¤^Ñ3Ð4µs¸9±~´~ÑEÑFÔFÐFÐFåŒHÐ&¨h¬nÐ=Ñ=Ô=€Eå	˜
Ñ	#Ô	#€Bàð �+ð Ý˜z¨3Ñ/Ô/ˆØ
�Š˜ÑÔÐåŒX�b”i¥v°DÐ9Ñ9Ô9€Fõ Œh•s˜>Ñ*Ô*¨QÑ.µb´hÐ?Ñ?Ô?€GØ€Kõ ˜e™œð Ybñ Ybˆð —Z’Z ­C°©K¬K¸<�ZÑHÔHˆ
ÝœI j°Ô&>ÅbÄhÐOÑOÔOÐõ
 ”OÐ$5°zÈÐPÑPÔPˆ	Ø 
  
Ô+ˆ
àÐ.¨y¸1©}Ô=Ñ=ˆð ! Ô.ˆØ•2”;˜{µc¸.Ñ6IÔ6IÈAÑ6MÐNÑNÔNÑNˆà•s˜:‘”Ñ&ˆð �aŠiˆiØ#ˆOå#Ð$<×$BÒ$BÑ$DÔ$DÈdÐSÑSÔSð 	+ð 	+‘	��TØ˜C’<�< G¨C¤L°;Ñ$>ÀÒ$EÐ$EÝ"$¤(Ø& s¨Q¡wÔ/°·²¸QÅÀNÐSVÐYZÑSZÔD[Ñ@\Ô@\Ñ0]Ô0]Ô^ñ#ô #�Kõ "$¤¨J¸Ð+DÑ!EÔ!E�JØ˜C�L�L”L AÑ%�L�L‘LØ 1Ñ$�KØ˜S‘L�EØ&*�OøØ"ð Øð �aŠiˆiõ& ”H�S ™_œ_¨qÑ0½¼ÐAÑAÔAˆ	Ý”H�S ™_œ_¨qÑ0½¼ÐAÑAÔAˆ	Ø&'Ð'ˆ	�!‰�y ‘|Ý
Œ	�*˜\Ô*°	¸!¸"¸"´Ð>Ñ>Ô>Ð>Ý
Œ	�*˜\Ô*°	¸!¸"¸"´Ð>Ñ>Ô>Ð>Ø—[’[  A­¬¡I°I¸b´M�[ÑBÔBˆ
Ø�Š�FÑÔÐð
 ñ 	b˜<¨1Ò,Ñ,Ø ŸZšZ­S°©_¬_˜ZÑ=Ô=ÀÒCˆNØ"Ÿ|š|Ø�1�˜J¨Ñ+Ô+Ñ+µ"Ô2BÀ>Ñ2RÔ2Rð  ,ñ  ô  Ðð ˆEÝADØ˜7Ô#Ø˜#˜2˜#”Ø˜!˜"˜"”Ø˜#˜2˜#”Ø˜!˜"˜"”ØñBô Bð bð bÑ=��g˜u g¨u°gð !˜D *¨W°U¨]Ô";Ð<�	Øð Ø—J’J˜rœy¨"Ñ-Ô-Ð-à )§¢¨rµ3Ð7GÈÔ7NÑ3OÔ3OÑ PÔ P�IØ˜Q‘J�EØ—
’
Õ-¸	È&ÐQXÐY^ÐQ^ÔJ_Ð`Ñ`Ô`ÑaÔaÐaÐañbõ  9<Ø˜7Ô# Y¨s°¨s¤^°Y¸q¸r¸r´]ÀIÈcÈrÈcÄNÐT]Ð^_Ð^`Ð^`ÔTañ9ô 9ð bð bÑ4��g˜u g¨uð !˜D *¨W°U¨]Ô";Ð<�	Ø—
’
Õ-¸	È&ÐQXÐY^ÐQ^ÔJ_Ð`Ñ`Ô`ÑaÔaÐaÐañ	bð
 ð "Ø
�
Š
�2”9˜bÑ!Ô!Ð!à€Ir   Úallc                 ó   — t          t          t          j        ¦   «         ¦  «        h d£z
  ¦  «        }t          t	          j        ¦   «         ¦  «        }|dk    r| dk    r|}n||z   }i dt          j        ¦   «         df“dt          j        ¦   «         df“dt          j        ¦   «         df“dt          j	        ¦   «         df“dt          j
        ¦   «         df“d	t          j        ¦   «         df“d
t          j        ¦   «         df“dt          j        ¦   «         df“dt          j        ¦   «         df“dt          j        ¦   «         df“dt          j        ¦   «         df“dt          j        ¦   «         df“dt          j        ¦   «         df“dt          j        ¦   «         df“dt          j        ¦   «         df“dt          j        ¦   «         df“}t-          |t.          j        j        ¦  «        r|}nt.          j                             |¦  «        }|                     ||¦  «        }t9          | ¦  «        }	|D ]\}
||
         \  }}|                     t;          | ¦  «        |d¬¦  «                             ¦   «         }|	                     ||d¬¦  «         Œ]|	S )a±  Generate a pseudo-random Clifford circuit.

    This function will generate a Clifford circuit by randomly selecting the chosen amount of Clifford
    gates from the set of standard gates in :mod:`qiskit.circuit.library.standard_gates`. For example:

    .. plot::
       :alt: Circuit diagram output by the previous code.
       :include-source:

       from qiskit.circuit.random import random_clifford_circuit

       circ = random_clifford_circuit(num_qubits=2, num_gates=6)
       circ.draw(output='mpl')

    Args:
        num_qubits (int): number of quantum wires.
        num_gates (int): number of gates in the circuit.
        gates (list[str]): optional list of Clifford gate names to randomly sample from.
            If ``"all"`` (default), use all Clifford gates in the standard library.
        seed (int | np.random.Generator): sets random seed/generator (optional).

    Returns:
        QuantumCircuit: constructed circuit
    >   ÚvÚwÚidÚidenÚsinvr³   r   r   ÚxÚyÚzÚhÚsÚsdgÚsxÚsxdgÚcxr   ÚcyÚczÚswapÚiswapÚecrÚdcxF)Úreplace)r   ) ÚlistÚsetr	   r2   r
   r   r<   r>   rN   rO   rA   rE   rF   r=   rG   rQ   r\   r]   rg   rh   rd   rR   Ú
isinstancer*   r+   Ú	Generatorr0   rv   r   r8   ÚtolistrP   )r   Ú	num_gatesr˜   rŠ   rŽ   r�   Úinstructionsr‹   ÚsamplesÚcircÚnamer¬   ÚnqargsÚqargss                 r   Úrandom_clifford_circuitrÖ   $  sÅ  € õ4 •C�	œÑ(Ô(Ñ)Ô)Ð,LÐ,LÐ,LÑLÑMÔM€HÝ•I”NÑ$Ô$Ñ%Ô%€Hà�‚~€~Ø˜Š?ˆ?ØˆEˆEà˜xÑ'ˆEðØ�nÔ"Ñ$Ô$ aÐ(ðà�nÔ"Ñ$Ô$ aÐ(ðð 	�nÔ"Ñ$Ô$ aÐ(ðð 	�nÔ"Ñ$Ô$ aÐ(ð	ð
 	�nÔ"Ñ$Ô$ aÐ(ðð 	�nÔ"Ñ$Ô$ aÐ(ðð 	•Ô&Ñ(Ô(¨!Ð,ðð 	�~Ô$Ñ&Ô&¨Ð*ðð 	•Ô(Ñ*Ô*¨AÐ.ðð 	�~Ô$Ñ&Ô&¨Ð*ðð 	�~Ô$Ñ&Ô&¨Ð*ðð 	�~Ô$Ñ&Ô&¨Ð*ðð 	•Ô(Ñ*Ô*¨AÐ.ðð 	•.Ô*Ñ,Ô,¨aÐ0ðð 	•Ô&Ñ(Ô(¨!Ð,ðð  	•Ô&Ñ(Ô(¨!Ð,ð!€Lõ& �$�œ	Ô+Ñ,Ô,ð *ØˆˆåŒi×#Ò# DÑ)Ô)ˆà�jŠj˜ 	Ñ*Ô*€Gå˜*Ñ%Ô%€Dàð -ð -ˆØ# DÔ)‰ˆˆfØ—
’
�5 Ñ,Ô,¨f¸e�
ÑDÔD×KÒKÑMÔMˆØ�Š�D˜% eˆÑ,Ô,Ð,Ð,à€Kr   )r   FFFNN)r³   N)Ú__doc__Únumpyr*   Úqiskit.circuitr   r   r   r   Úqiskit.circuit.libraryr   Úqiskit.circuit.exceptionsr   Ú:qiskit.quantum_info.operators.symplectic.clifford_circuitsr	   r
   r4   r²   rÖ   r   r   r   ú<module>rÝ      sç   ðð 8Ð 7à Ð Ð Ð à PÐ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PÐ PØ  Ð  Ð  Ð  Ð  Ð  Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø [Ð [Ð [Ð [Ð [Ð [Ð [Ð [ð ØØØ
Ø	Ø%)ðIð Ið #ðIð Ið Ið IðXDð Dð Dð Dð Dð Dr   