
    z	i7                        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
  SSKJr  SSKJr  S	S
KJrJrJr        S               SS jjr " S S\5      rg)zVA generalized QAOA quantum circuit with a support of custom initial states and mixers.    )annotationsN)ParameterVector)QuantumCircuit)QuantumRegister)SparsePauliOp)BaseOperator   )EvolvedOperatorAnsatz_is_pauli_identityevolved_operator_ansatzc                &   U R                   nUc%  [        U5      nUR                  [        U5      5        Uc5  [        R
                  " [        U5       Vs/ s H	  nSU/S4PM     snU5      nSS/n	UR                  [        X/UUU	UUS9SS9$ s  snf )a-  A generalized QAOA quantum circuit with a support of custom initial states and mixers.

Examples:

To define the QAOA ansatz we require a cost Hamiltonian, encoding the classical
optimization problem:

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

    from qiskit.quantum_info import SparsePauliOp
    from qiskit.circuit.library import qaoa_ansatz

    cost_operator = SparsePauliOp(["ZZII", "IIZZ", "ZIIZ"])
    ansatz = qaoa_ansatz(cost_operator, reps=3, insert_barriers=True)
    ansatz.draw("mpl")

Args:
    cost_operator: The operator representing the cost of the optimization problem, denoted as
        :math:`U(C, \gamma)` in [1].
    reps: The integer determining the depth of the circuit, called :math:`p` in [1].
    initial_state: An optional initial state to use, which defaults to a layer of
        Hadamard gates preparing the :math:`|+\rangle^{\otimes n}` state.
        If a custom mixer is chosen, this circuit should be set to prepare its ground state,
        to appropriately fulfill the annealing conditions.
    mixer_operator: An optional custom mixer, which defaults to global Pauli-:math:`X`
        rotations. This is denoted as :math:`U(B, \beta)` in [1]. If this is set,
        the ``initial_state`` might also require modification.
    insert_barriers: Whether to insert barriers in-between the cost and mixer operators.
    name: The name of the circuit.
    flatten: If ``True``, a flat circuit is returned instead of nesting it inside multiple
        layers of gate objects. Setting this to ``False`` is significantly less performant,
        especially for parameter binding, but can be desirable for a cleaner visualization.

References:

[1] Farhi et al., A Quantum Approximate Optimization Algorithm.
`arXiv:1411.4028 <https://arxiv.org/pdf/1411.4028>`_
Xr	      γ   β)repsinsert_barriersparameter_prefixnameflattenF)copy)
num_qubitsr   hranger   from_sparse_listcomposer   )
cost_operatorr   initial_statemixer_operatorr   r   r   r   ir   s
             d/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/circuit/library/n_local/qaoa_ansatz.pyqaoa_ansatzr!   !   s    b ))J&z2j)*&77$)*$56$5qcA3]$56

 d|  ++-	
  ! 
 
 7s   Bc                    ^  \ rS rSrSr      S       SU 4S jjjrSSU 4S jjjr\SS j5       r\R                  SS j5       r\SS j5       r
\S 5       r\R                  SS	 j5       r\SS
 j5       r\R                  SS j5       r\SS j5       r\R                  SS j5       r\S 5       r\R                  SS j5       r\SS j5       rU 4S jrSrU =r$ )
QAOAAnsatzl   zA generalized QAOA quantum circuit with a support of custom initial states and mixers.

References:

[1] Farhi et al., A Quantum Approximate Optimization Algorithm.
`arXiv:1411.4028 <https://arxiv.org/pdf/1411.4028>`_
c                n   > [         TU ]  X%US9  SU l        X l        X0l        X@l        SU l        Xl        g)a  
Args:
    cost_operator (BaseOperator or OperatorBase, optional): The operator
        representing the cost of the optimization problem, denoted as :math:`U(C, \gamma)`
        in the original paper. Must be set either in the constructor or via property setter.
    reps (int): The integer parameter p, which determines the depth of the circuit,
        as specified in the original paper, default is 1.
    initial_state (QuantumCircuit, optional): An optional initial state to use.
        If `None` is passed then a set of Hadamard gates is applied as an initial state
        to all qubits.
    mixer_operator (BaseOperator or OperatorBase or QuantumCircuit, optional): An optional
        custom mixer to use instead of the global X-rotations, denoted as :math:`U(B, \beta)`
        in the original paper. Can be an operator or an optionally parameterized quantum
        circuit.
    name (str): A name of the circuit, default 'qaoa'
    flatten: Set this to ``True`` to output a flat circuit instead of nesting it inside multiple
        layers of gate objects. By default currently the contents of
        the output circuit will be wrapped in nested objects for
        cleaner visualization. However, if you're using this circuit
        for anything besides visualization its **strongly** recommended
        to set this flag to ``True`` to avoid a large performance
        overhead for parameter binding.
)r   r   r   N)super__init___cost_operator_reps_initial_state_mixer_boundsr   )selfr   r   r   r   r   r   	__class__s          r    r'   QAOAAnsatz.__init__u   sC    @ 	dw?"
5B$ HL +    c                  > Sn[         TU ]  U5      (       d  gU R                  c  SnU(       a  [        S5      eU R                  b\  U R                  R
                  U R
                  :w  a8  SnU(       a/  [        SU R                  R
                   SU R
                   35      eU R                  b\  U R                  R
                  U R
                  :w  a8  SnU(       a/  [        SU R                  R
                   SU R
                   35      eU$ )z,Check if the current configuration is valid.TFzIThe operator representing the cost of the optimization problem is not setz*The number of qubits of the initial state z: does not match the number of qubits of the cost operator z"The number of qubits of the mixer )r&   _check_configurationr   
ValueErrorr   r   r   )r-   raise_on_failurevalidr.   s      r    r2   QAOAAnsatz._check_configuration   s   w+,<==%E _  )d.@.@.K.Kt.^E @ASASA^A^@_AAE@QS  *t/B/B/M/MQUQ`Q`/`E 89L9L9W9W8XAAE@QS  r0   c                Z   U R                   b  U R                   $ [        U R                  [        5      (       a  gSS[        R
                  -  4nSn/ n[        U R                  5      (       d  X0R                  U/-  -  n[        U R                  5      (       d  X0R                  U/-  -  nU$ )a  The parameter bounds for the unbound parameters in the circuit.

Returns:
    A list of pairs indicating the bounds, as (lower, upper). None indicates an unbounded
    parameter in the corresponding direction. If None is returned, problem is fully
    unbounded.
Nr      )NN)	r,   
isinstancer   r   nppir   r   r   )r-   beta_boundsgamma_boundsboundss       r    parameter_boundsQAOAAnsatz.parameter_bounds   s     <<#<< d))>:: !bee)n#:<!$"5"566ii;-//F!$"4"455ii<.00Fr0   c                    Xl         g)zGSet the parameter bounds.

Args:
    bounds: The new parameter bounds.
N)r,   )r-   r>   s     r    r?   r@      s	     r0   c                2    U R                   U R                  /$ )zThe operators that are evolved in this circuit.

Returns:
     List[Union[BaseOperator, OperatorBase, QuantumCircuit]]: The operators to be evolved
        (and circuits) in this ansatz.
)r   r   r-   s    r    	operatorsQAOAAnsatz.operators   s     ""D$7$788r0   c                    U R                   $ )zReturns an operator representing the cost of the optimization problem.

Returns:
    BaseOperator or OperatorBase: cost operator.
)r(   rC   s    r    r   QAOAAnsatz.cost_operator   s     """r0   c                d    Xl         [        U R                  SS9/U l        U R	                  5         g)zmSets cost operator.

Args:
    cost_operator (BaseOperator or OperatorBase, optional): cost operator to set.
q)r   N)r(   r   r   qregs_invalidate)r-   r   s     r    r   rG      s+     ,%dooC@A
r0   c                    U R                   $ )zHReturns the `reps` parameter, which determines the depth of the circuit.)r)   rC   s    r    r   QAOAAnsatz.reps  s     zzr0   c                0    Xl         U R                  5         g)zSets the `reps` parameter.N)r)   rK   )r-   r   s     r    r   rM     s     
r0   c                    U R                   b  U R                   $ U R                  S:  a;  [        U R                  5      nUR                  [	        U R                  5      5        U$ g)z.Returns an optional initial state as a circuitNr   )r*   r   r   r   r   r-   r   s     r    r   QAOAAnsatz.initial_state  sX     *&&& ??Q*4??;MOOE$//23   r0   c                0    Xl         U R                  5         g)zSets initial state.N)r*   rK   rP   s     r    r   rQ   "  s     ,r0   c                   U R                   b  U R                   $ U R                  b\  U R                  R                  n[        U5       Vs/ s H  nSU-  S-   SX-
  S-
  -  -   S4PM     nn[        R
                  " U5      nU$ gs  snf )zReturns an optional mixer operator expressed as an operator or a quantum circuit.

Returns:
    BaseOperator or OperatorBase or QuantumCircuit, optional: mixer operator or circuit.
NIr   r	   )r+   r   r   r   r   	from_list)r-   r   leftmixer_termsmixers        r    r   QAOAAnsatz.mixer_operator*  s     ;;";; )++66J
 SXXbRcRc$tc!C:+<q+@$AA1ERc   "++K8EL s   
Bc                0    Xl         U R                  5         g)zSets mixer operator.

Args:
    mixer_operator (BaseOperator or OperatorBase or QuantumCircuit, optional): mixer
        operator or circuit to set.
N)r+   rK   )r-   r   s     r    r   rY   D  s     %r0   c                J    U R                   c  gU R                   R                  $ )Nr   )r(   r   rC   s    r    r   QAOAAnsatz.num_qubitsO  s#    &""---r0   c                  > U R                   (       a  g[        TU ]	  5         [        U R                  5      (       a  SOSn[        U R                  [        5      (       a  U R                  R                  nO[        U R                  5      (       a  SOSn[        SU R                  U-  5      n[        SU R                  U-  5      n/ n[        U R                  5       H;  nUR                  XFU-  US-   U-   5        UR                  X6U-  US-   U-   5        M=     U R                  [        [        U R                   U5      5      SS9  g)z(If not already built, build the circuit.Nr   r	   r   r   T)inplace)	_is_builtr&   _buildr   r   r9   r   r   num_parametersr   r   r   extendassign_parametersdictzipordered_parameters)r-   num_cost	num_mixerbetasgammas	reorderedrepr.   s          r    r`   QAOAAnsatz._buildU  s   >> +4+=+=>>1Ad))>::++::I/0C0CDD!Idii)&;< tyy8';< 	#CV(NcAg5IJKU?cAg5JKL $ 	tC(?(?$KLVZ[r0   )r,   r(   r*   r+   r)   r   rJ   )Nr	   NNQAOAN)r   intr   QuantumCircuit | Noner   strr   zbool | None)T)r4   boolreturnrr   )rs   .list[tuple[float | None, float | None]] | None)r>   rt   rs   None)rs   list)rs   ru   )rs   ro   )r   ro   rs   ru   )rs   rp   )r   rp   rs   ru   )__name__
__module____qualname____firstlineno____doc__r'   r2   propertyr?   setterrD   r   r   r   r   r   r`   __static_attributes____classcell__)r.   s   @r    r#   r#   l   sr    /3#++ ++ -	++ ++ ++ ++Z   D  8   9 9 # #     
[[ 
      2   . .
\ \r0   r#   )r	   NNFrn   T)r   r   r   ro   r   rp   r   zBaseOperator | Noner   rr   r   rq   r   rr   rs   r   )r{   
__future__r   numpyr:   qiskit.circuit.parametervectorr   qiskit.circuit.quantumcircuitr   qiskit.circuitr   qiskit.quantum_infor   +qiskit.quantum_info.operators.base_operatorr   r   r
   r   r!   r#    r0   r    <module>r      s    ] #  : 8 * - D  +/*.!HH
H )H (	H
 H H H HVC\& C\r0   