
    z	i                        S 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
  SSKJr  SSKJr  SS	KJr  SS
KJrJrJrJr  \SS4         SS jjrS rSS jrg)z?
Circuit synthesis for a stabilizer state preparation circuit.
    )annotations)CallableN)QuantumCircuit)QiskitError)StabilizerState)calc_inverse_matrix)synth_cz_depth_line_mr)_default_cz_synth_func_reverse_clifford_create_graph_state_decompose_graph_stateFc                   [        U [        5      (       d  [        S5      eU R                  nUR                  nU(       a  [        U5      nOUn[        XcS9u  px[        XUS9u  pp[        [        U5      5      n[        U5      nUR                  X5        UR                  X5        UR                  X5        U(       a  UR                  5       nUR                  X}5        SSKJn  U" U5      n[        UU5      nUR                  UU5        U$ )u  Synthesis of a stabilizer state into layers.

It provides a similar decomposition to the synthesis described in Lemma 8 of reference [1],
without the initial Hadamard-free sub-circuit which do not affect the stabilizer state.

For example, a 5-qubit stabilizer state is decomposed into the following layers:

.. code-block:: text

         ┌─────┐┌─────┐┌─────┐┌─────┐┌────────┐
    q_0: ┤0    ├┤0    ├┤0    ├┤0    ├┤0       ├
         │     ││     ││     ││     ││        │
    q_1: ┤1    ├┤1    ├┤1    ├┤1    ├┤1       ├
         │     ││     ││     ││     ││        │
    q_2: ┤2 H2 ├┤2 S1 ├┤2 CZ ├┤2 H1 ├┤2 Pauli ├
         │     ││     ││     ││     ││        │
    q_3: ┤3    ├┤3    ├┤3    ├┤3    ├┤3       ├
         │     ││     ││     ││     ││        │
    q_4: ┤4    ├┤4    ├┤4    ├┤4    ├┤4       ├
         └─────┘└─────┘└─────┘└─────┘└────────┘

Args:
    stab: A stabilizer state.
    cz_synth_func: A function to decompose the CZ sub-circuit.
        It gets as input a boolean symmetric matrix, and outputs a :class:`.QuantumCircuit`.
    cz_func_reverse_qubits: ``True`` only if ``cz_synth_func`` is
        :func:`.synth_cz_depth_line_mr`,
        since this function returns a circuit that reverts the order of qubits.
    validate: If ``True``, validates the synthesis process.

Returns:
    A circuit implementation of the stabilizer state.

Raises:
    QiskitError: if the input is not a :class:`.StabilizerState`.

References:
    1. S. Bravyi, D. Maslov, *Hadamard-free circuits expose the
       structure of the Clifford group*,
       `arXiv:2003.09412 [quant-ph] <https://arxiv.org/abs/2003.09412>`_
z#The input is not a StabilizerState.)validate)r   cz_synth_funcr   )Clifford)
isinstancer   r   clifford
num_qubitsr   r   r   listranger   appendreverse_bits(qiskit.quantum_info.operators.symplecticr   _calc_pauli_diff_stabilizer)stabr   cz_func_reverse_qubitsr   cliffr   cliff0H1_circcliff1H2_circCZ1_circS1_circ_
qubit_listlayeredCircuitr   clifford_target
pauli_circs                     j/home/james-whalen/.local/lib/python3.13/site-packages/qiskit/synthesis/stabilizer/stabilizer_decompose.pysynth_stabilizer_layersr*   $   s    ` dO,,?@@MME!!J"5))&DOG$:%!Gw eJ'(J#J/N'.'.(/&&('. B~.O,UODJ*j1    c           	     J   SSK Jn  U R                  nU R                  UR                  :w  a  [        S5      e[	        U 5      R
                  R                  5       S   n[	        U5      nS/U-  n[        U5       Vs/ s H  ouR                  U" XG   5      5      S:H  PM      nn/ n	U	R                  U5        U	R                  U5        [        R                  " U	[        S9n	U R                  R                  [        SS9n
[!        U
5      n[        R"                  " X5      S	-  n[%        US
S9n[        U5       Hc  nX   nXU-      nU(       a  U(       a  UR'                  U5        M/  U(       a  UR)                  U5        MI  U(       d  MR  UR+                  U5        Me     U$ s  snf )zLGiven two Cliffords whose stabilizers differ by a Pauli, we find this Pauli.r   )PaulizDnum_qubits is not the same for the original clifford and the target.
stabilizerF)dtype)copy   r-   )name)r   r-   r   r   r   r   to_dictr   expectation_valueextendnparrayintsymplectic_matrixastypeboolr   matmulr   yxz)r   cliff_targetr-   r   stab_gentsphase_destabi
phase_stabphaseAAinvCr(   kdestabr   s                    r)   r   r   {   sn    ?!!J<222`aa u%..668FH 
	&B7Z'LJOPZJ[\J[Q&&uX['9:b@J[J\E	LL	LLHHU#&E&&t%&8Aq!D 			$"A  
9J:Z FLLOLLOVLLO  5 ]s   <%F c                $    [        U [        SS9nU$ )a  Synthesis of an n-qubit stabilizer state for linear-nearest neighbor connectivity,
in 2-qubit depth :math:`2n+2` and two distinct CX layers, using :class:`.CXGate`\ s and phase gates
(:class:`.SGate`, :class:`.SdgGate` or :class:`.ZGate`).

Args:
    stab: A stabilizer state.

Returns:
    A circuit implementation of the stabilizer state.

References:
    1. S. Bravyi, D. Maslov, *Hadamard-free circuits expose the
       structure of the Clifford group*,
       `arXiv:2003.09412 [quant-ph] <https://arxiv.org/abs/2003.09412>`_
    2. Dmitri Maslov, Martin Roetteler,
       *Shorter stabilizer circuits via Bruhat decomposition and quantum circuit transformations*,
       `arXiv:1705.09176 <https://arxiv.org/abs/1705.09176>`_.
T)r   r   )r*   r	   )r   circs     r)   synth_stabilizer_depth_lnnrO      s    ( #,#D
 Kr+   )
r   r   r   z&Callable[[np.ndarray], QuantumCircuit]r   r<   r   r<   returnr   )r   r   rP   r   )__doc__
__future__r   collections.abcr   numpyr7   qiskit.circuitr   qiskit.exceptionsr   qiskit.quantum_info.statesr   +qiskit.synthesis.linear.linear_matrix_utilsr   qiskit.synthesis.linear_phaser	   3qiskit.synthesis.clifford.clifford_decompose_layersr
   r   r   r   r*   r   rO    r+   r)   <module>r\      s   
 # $  ) ) 6 A  =S#(	T
T9T !T 	T
 Tn+\r+   