
    z	iy                        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	r
SSKJr  SS	KJrJr  \R                   (       a  SSKr " S
 S\5      r " S S5      rg)z9A generic implementation of Approximate Quantum Compiler.    )annotationsN)partial)Callable)Protocol)Operator   )ApproximateCircuitApproximatingObjectivec                  <    \ rS rSrSr  S         SS jjrSrg)	Minimizer   aT  Callable Protocol for minimizer.

This interface is based on `SciPy's optimize module
<https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html>`__.

 This protocol defines a callable taking the following parameters:

     fun
         The objective function to minimize.
     x0
         The initial point for the optimization.
     jac
         The gradient of the objective function.
     bounds
         Parameters bounds for the optimization. Note that these might not be supported
         by all optimizers.

 and which returns a SciPy minimization result object.
Nc                    g)a  Minimize the objective function.

This interface is based on `SciPy's optimize module <https://docs.scipy.org/doc
/scipy/reference/generated/scipy.optimize.minimize.html>`__.

Args:
    fun: The objective function to minimize.
    x0: The initial point for the optimization.
    jac: The gradient of the objective function.
    bounds: Parameters bounds for the optimization. Note that these might not be supported
        by all optimizers.

Returns:
     The SciPy minimization result object.
N )selffunx0jacboundss        Z/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/synthesis/unitary/aqc/aqc.py__call__Minimizer.__call__4   s    , 	    r   NN)
r   zCallable[[np.ndarray], float]r   
np.ndarrayr   z)Callable[[np.ndarray], np.ndarray] | Noner   z list[tuple[float, float]] | Nonereturnzscipy.optimize.OptimizeResult)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r   r   r   r   r      sI    0 :>37*  7	
 1 
' r   r   c                  `   ^  \ rS rSrSr  S   SU 4S jjjr S         S	S jjrSrU =r$ )
AQCM   aI  
A generic implementation of the Approximate Quantum Compiler. This implementation is agnostic of
the underlying implementation of the approximate circuit, objective, and optimizer. Users may
pass corresponding implementations of the abstract classes:

* The *optimizer* is an implementation of the :class:`~.Minimizer` protocol, a callable used to run
  the optimization process. The choice of optimizer may affect overall convergence, required time
  for the optimization process and achieved objective value.

* The *approximate circuit* represents a template which parameters we want to optimize.  Currently,
  there's only one implementation based on 4-rotations CNOT unit blocks:
  :class:`.CNOTUnitCircuit`. See the paper for more details.

* The *approximate objective* is tightly coupled with the approximate circuit implementation and
  provides two methods for computing objective function and gradient with respect to approximate
  circuit parameters. This objective is passed to the optimizer. Currently, there are two
  implementations based on 4-rotations CNOT unit blocks: :class:`.DefaultCNOTUnitObjective` and
  its accelerated version :class:`.FastCNOTUnitObjective`. Both implementations share the same
  idea of maximization the Hilbert-Schmidt product between the target matrix and its
  approximation. The former implementation approach should be considered as a baseline one. It
  may suffer from performance issues, and is mostly suitable for a small number of qubits
  (up to 5 or 6), whereas the latter, accelerated one, can be applied to larger problems.

* One should take into consideration the exponential growth of matrix size with the number of
  qubits because the implementation not only creates a potentially large target matrix, but
  also allocates a number of temporary memory buffers comparable in size to the target matrix.
c                   > SSK n[        TU ]	  5         U=(       d"    [        UR                  R
                  SSSS0S9U l        X l        g)a2  
Args:
    optimizer: an optimizer to be used in the optimization procedure of the search for
        the best approximate circuit. By default, the scipy minimizer with the
        ``L-BFGS-B`` method is used with max iterations set to 1000.
    seed: a seed value to be used by a random number generator.
r   Nr   zL-BFGS-Bmaxiteri  )argsmethodoptions)scipy.optimizesuper__init__r   optimizeminimize
_optimizer_seed)r   	optimizerseedscipy	__class__s       r   r,   AQC.__init__j   sF     	# 
wNN##"Z)UYIZ(
 
r   c                6   UR                   S   n[        R                  R                  U5      n[        R                  " US5      (       d#  U[        R
                  " USU-  [        S9-  nSnOUnSnXsl        Uce  [        R                  R                  U R                  5        [        R                  R                  SS[        R                  -  UR                  5      nU R                  UR                  UUR                   S9n	UR#                  U	R$                  5        ['        U5      R(                  n
U(       a^  [        R*                  " [        R,                  " [        R.                  " U
R1                  5       R2                  U5      5      5      nXl        gg)	a8  
Approximately compiles a circuit represented as a unitary matrix by solving an optimization
problem defined by ``approximating_objective`` and using ``approximate_circuit`` as a
template for the approximate circuit.

Args:
    target_matrix: a unitary matrix to approximate.
    approximate_circuit: a template circuit that will be filled with the parameter values
        obtained in the optimization procedure.
    approximating_objective: a definition of the optimization problem.
    initial_point: initial values of angles/parameters to start optimization from.
r   r   )dtypeTFN   )r   r   r   )shapenplinalgdetisclosepowercomplextarget_matrixrandomr2   r0   uniformpi
num_thetasr/   	objectivegradientbuildxr   dataangletracedotconjTglobal_phase)r   r@   approximate_circuitapproximating_objectiveinitial_point
matrix_dim
target_det	su_matrixglobal_phase_required
opt_resultapprox_matrixalphas               r   compile_unitaryAQC.compile_unitary   s9   & #((+
YY]]=1
zz*a((%a*nU\(]]I$(!%I$)! 1:- IINN4::&II--aRUU<S<^<^_M__'11'00 % 

 	!!*,,/ !45:: HHRXXbff]-?-?-A-C-C]&STUE/4, !r   )r/   r0   r   )r1   zMinimizer | Noner2   z
int | None)N)
r@   r   rP   r	   rQ   r
   rR   znp.ndarray | Noner   None)	r   r   r   r   r    r,   rZ   r!   __classcell__)r4   s   @r   r#   r#   M   sk    < '+#  4 ,005!05 005 "8	05
 )05 
05 05r   r#   )r    
__future__r   typing	functoolsr   collections.abcr   r   numpyr:   qiskit.quantum_infor   approximater	   r
   TYPE_CHECKINGr*   r3   r   r#   r   r   r   <module>rf      sH    @ "   $   ( C	+ +\b5 b5r   