§
    º³Ùgó  ã                  óœ   — d Z ddlmZ ddlmZ ddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZmZmZmZ eddfdd„Zd„ Zdd„ZdS )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_stateFÚstabr   Úcz_synth_funcú&Callable[[np.ndarray], QuantumCircuit]Úcz_func_reverse_qubitsÚboolÚvalidateÚreturnr   c                óˆ  — t          | t          ¦  «        st          d¦  «        ‚| j        }|j        }|rt          |¦  «        }n|}t          ||¬¦  «        \  }}t          |||¬¦  «        \  }	}
}}t          t          |¦  «        ¦  «        }t          |¦  «        }|                     |	|¦  «         |                     ||¦  «         |                     |
|¦  «         |r|                     ¦   «         }|                     ||¦  «         ddlm}  ||¦  «        }t          ||¦  «        }|                     ||¦  «         |S )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.)r   )r   r   r   )ÚClifford)Ú
isinstancer   r   ÚcliffordÚ
num_qubitsr   r   r   ÚlistÚranger   ÚappendÚreverse_bitsÚ(qiskit.quantum_info.operators.symplecticr   Ú_calc_pauli_diff_stabilizer)r   r   r   r   Úcliffr   Úcliff0ÚH1_circÚcliff1ÚH2_circÚCZ1_circÚS1_circÚ_Ú
qubit_listÚlayeredCircuitr   Úclifford_targetÚ
pauli_circs                     úf/granary/edev/quantum/lib/python3.11/site-packages/qiskit/synthesis/stabilizer/stabilizer_decompose.pyÚsynth_stabilizer_layersr-   $   sp  € õ` �d�OÑ,Ô,ð AÝÐ?Ñ@Ô@Ð@àŒM€EØÔ!€Jàð Ý" 5Ñ)Ô)ˆˆàˆå)¨&¸8ÐDÑDÔD�O€GˆVå$:Ø˜°ð%ñ %ô %Ñ!€GˆX�w õ •e˜JÑ'Ô'Ñ(Ô(€JÝ# JÑ/Ô/€Nà×Ò˜' :Ñ.Ô.Ð.Ø×Ò˜' :Ñ.Ô.Ð.Ø×Ò˜( JÑ/Ô/Ð/àð )Ø×&Ò&Ñ(Ô(ˆØ×Ò˜' :Ñ.Ô.Ð.ð BÐAÐAÐAÐAÐAà�h˜~Ñ.Ô.€OÝ,¨U°OÑDÔD€JØ×Ò˜* jÑ1Ô1Ð1àÐó    c                ó<  ‡‡‡— ddl mŠ | j        }| j        |j        k    rt          d¦  «        ‚t	          | ¦  «        j                             ¦   «         d         Št	          |¦  «        Šdg|z  }ˆˆˆfd„t          |¦  «        D ¦   «         }g }|                     |¦  «         |                     |¦  «         t          j
        |t          ¬¦  «        }| j                             t          d¬¦  «        }t          |¦  «        }t          j        ||¦  «        d	z  }t#          |d
¬¦  «        }	t          |¦  «        D ]^}
||
         }||
|z            }|r|r|	                     |
¦  «         Œ/|r|	                     |
¦  «         ŒG|r|	                     |
¦  «         Œ_|	S )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.Ú
stabilizerFc                ó`   •— g | ]*}‰                       ‰‰|         ¦  «        ¦  «        d k    ‘Œ+S )éÿÿÿÿ)Úexpectation_value)Ú.0Úir0   Ústab_genÚtss     €€€r,   ú
<listcomp>z/_calc_pauli_diff_stabilizer.<locals>.<listcomp>Œ   s:   ø€ Ð\Ð\Ð\ÀQ�"×&Ò& u u¨X°a¬[Ñ'9Ô'9Ñ:Ô:¸bÒ@Ð\Ð\Ð\r.   )Údtype)Úcopyé   r0   )Úname)r   r0   r   r   r   r   Úto_dictr   ÚextendÚnpÚarrayÚintÚsymplectic_matrixÚastyper   r   Úmatmulr   ÚyÚxÚz)r    Úcliff_targetr   Úphase_destabÚ
phase_stabÚphaseÚAÚAinvÚCr+   ÚkÚdestabr   r0   r7   r8   s                @@@r,   r   r   {   s½  øøø€ ð ?Ð>Ð>Ð>Ð>Ð>àÔ!€JØÔ˜<Ô2Ò2Ð2ÝÐ`ÑaÔaÐaõ ˜uÑ%Ô%Ô.×6Ò6Ñ8Ô8¸ÔF€Hõ 
˜Ñ	&Ô	&€Bà�7˜ZÑ'€LØ\Ð\Ð\Ð\Ð\Ð\Í%ÐPZÑJ[ÔJ[Ð\Ñ\Ô\€Jà€EØ	‡L‚L�ÑÔÐØ	‡L‚L�ÑÔÐÝŒH�U¥#Ð&Ñ&Ô&€EàÔ×&Ò&¥t°%Ð&Ñ8Ô8€AÝ˜qÑ!Ô!€Dõ 	Œ	�$˜ÑÔ Ñ"€Aõ   
°Ð9Ñ9Ô9€JÝ�:ÑÔð ð ˆØ�1”ˆØ��Z‘Ô ˆØð 	�Fð 	Ø�LŠL˜‰OŒOˆOˆOØð 	Ø�LŠL˜‰OŒOˆOˆOØð 	Ø�LŠL˜‰OŒOˆOøàÐr.   c                ó4   — t          | t          d¬¦  «        }|S )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_lnnrT   ©   s(   € õ( #ØÝ,Ø#ðñ ô €Dð
 €Kr.   )
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   )Ú__doc__Ú
__future__r   Úcollections.abcr   Únumpyr@   Ú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   rT   © r.   r,   ú<module>r`      s?  ððð ð
 #Ð "Ð "Ð "Ð "Ð "à $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ø )Ð )Ð )Ð )Ð )Ð )Ø )Ð )Ð )Ð )Ð )Ð )Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6ðð ð ð ð ð ð AÐ @Ð @Ð @Ð @Ð @ðð ð ð ð ð ð ð ð ð ð ð ð =SØ#(Øð	Tð Tð Tð Tð Tðn+ð +ð +ð\ð ð ð ð ð r.   