qpe_toolbox.circuit.mpo_circuit_transpilation ============================================= .. py:module:: qpe_toolbox.circuit.mpo_circuit_transpilation Functions --------- .. autoapisummary:: qpe_toolbox.circuit.mpo_circuit_transpilation.init_cost_tn qpe_toolbox.circuit.mpo_circuit_transpilation.get_envs_tns qpe_toolbox.circuit.mpo_circuit_transpilation.find_transfer_structure qpe_toolbox.circuit.mpo_circuit_transpilation.build_first_sweep qpe_toolbox.circuit.mpo_circuit_transpilation.build_loc_cost_tn qpe_toolbox.circuit.mpo_circuit_transpilation.update_cost_tn qpe_toolbox.circuit.mpo_circuit_transpilation.update_contracted_envs qpe_toolbox.circuit.mpo_circuit_transpilation.optimize_one_gate qpe_toolbox.circuit.mpo_circuit_transpilation.sweep_direction qpe_toolbox.circuit.mpo_circuit_transpilation.optimize_single_gate_update qpe_toolbox.circuit.mpo_circuit_transpilation.transpile_mpo_to_circuit Module Contents --------------- .. py:function:: init_cost_tn(ref_mpo, depth, *, param_scaling=0.1, closed=False, rng=None) Initialize the tensor network used for optimization. It represents a cost function obtained from contracting: 1. A brickwall unitary circuit ansatz, 2. A reference target unitary (or state x zero) register represented as an MPO. The ansatz is generated as a layered brickwall circuit of two-qubit ``SU4SWAP`` gates initialized close to the identity: unlike quimb's ``SU4`` (which tends to SWAP), ``SU4SWAP`` tends to the identity as its parameters go to zero. The resulting unitary tensor network is then combined with the target MPO into a single tensor network suitable for overlap evaluation. :param ref_mpo: Target MPO to be reproduced by the Ansatz. :type ref_mpo: :quimb-api:`MatrixProductOperator` :param depth: Depth of the brickwall circuit ansatz (even and odd count as 1). :type depth: int :param param_scaling: Scale of the random initialization parameters for the gates. Smaller values initialize each ``SU4SWAP`` gate closer to the identity. Default is ``1e-1``. :type param_scaling: float, optional :param closed: If ``False`` (default), the bra indices of the MPO remain open. If ``True``, ket indices are contracted (trace contraction). :type closed: bool, optional :param rng: Random number generator to generate gate parameters. If ``None``, a default generator is created. :type rng: :numpy-random:`numpy.random.Generator `, optional :returns: Tensor network containing both the variational circuit ansatz and the target MPO. :rtype: :quimb-api:`TensorNetwork` .. py:function:: get_envs_tns(n_qubits, site_index, cost_tn) Extract the left and right environment TNs associated to a given site. This function removes the tensors tagged with ``I{site_index}`` from the full cost tensor network and partitions the remaining network into left and/or right environments relative to ``site_index``. :param n_qubits: Number of qubits (sites) in the tensor network. :type n_qubits: int :param site_index: Site index for which the environments are constructed. :type site_index: int :param cost_tn: Full cost tensor network. :type cost_tn: :quimb-api:`TensorNetwork` :returns: List containing the environment tensor networks. - If ``site_index == 0``, only the right environment is returned. - If ``site_index == n_qubits - 1``, only the left environment is returned. - Otherwise, the list is ordered as ``[left_env, right_env]``. :rtype: list of :quimb-api:`TensorNetwork` .. py:function:: find_transfer_structure(n_qubits, cost_tn) Determine the transfer structures connecting neighboring environments in a cost TN. This function analyzes the left and right environment tensor networks associated with each site and identifies the tensor tags involved in transferring contractions between neighboring environments. :param n_qubits: Number of qubits (sites) in the tensor network. :type n_qubits: int :param cost_tn: Full cost tensor network. :type cost_tn: TensorNetwork :returns: Nested dictionary containing the transfer structures. The returned dictionary has the form: .. code-block:: python { "L": { "L1_to_L2": [...], ... "L{n_qubits-1}_to_L{n_qubits}": [...] }, "R": { "R{n_qubits-2}_to_R{n_qubits-1}": [...], ... "R1_to_R0": [...] } } where each value is a list of tensor tags participating in the corresponding transfer operation. :rtype: dict .. py:function:: build_first_sweep(n_qubits, cost_tn, transfer_structure, *, drop_tags=True) Construct the initial set of contracted left and right environments for a sweeping TN optimization. This function iteratively builds partially contracted environments by propagating contractions from the edges of the tensor network toward the center using the transfer structures generated by :func:`find_transfer_structure`. :param n_qubits: Number of qubits (sites) in the tensor network. :type n_qubits: int :param cost_tn: Full cost tensor network. :type cost_tn: :quimb-api:`TensorNetwork` :param transfer_structure: Transfer structure dictionary generated by :func:`find_transfer_structure`. :type transfer_structure: dict :param drop_tags: Whether to drop tensor tags during contractions. Passed to :func:`tensor_contract`. Default is ``True``. :type drop_tags: bool, optional :returns: Nested dictionary containing contracted environments: .. code-block:: python { "L": { "L1": Tensor, ... }, "R": { "R{n_qubits-2}": Tensor, ... } } Each entry corresponds to an effective contracted environment tensor. :rtype: dict .. py:function:: build_loc_cost_tn(n_qubits, site_index, contracted_envs, cost_tn) Construct the local cost tensor network associated with a given site. This function combines the local gate tensors acting on site ``site_index`` with the corresponding contracted left and/or right environments to form an effective local tensor network suitable for optimization. It also returns the ordered list of gate tags corresponding to the variational tensors to optimize. :param n_qubits: Number of qubits (sites) in the tensor network. :type n_qubits: int :param site_index: Site index for which the local cost tensor network is constructed. :type site_index: int :param contracted_envs: Dictionary of contracted environments generated by :func:`build_first_sweep`. :type contracted_envs: dict :param cost_tn: Full cost tensor network. :type cost_tn: :quimb-api:`TensorNetwork` :returns: * **loc_cost_tn** (:quimb-api:`TensorNetwork`) -- Local effective tensor network containing the relevant environments and gate tensors for site ``site_index``. * **gate_to_opt_tags** (*list of str*) -- Ordered list of gate tensor tags to optimize. .. rubric:: Notes The local tensor network has the structure .. math:: \mathcal{S}_x = L_x \; \text{- gates -} \; R_x, where: - ``L_x`` is the contracted left environment, - ``R_x`` is the contracted right environment, - ``gates`` are the tensors tagged with ``I{site_index}``. Only tags beginning with ``"G"`` are considered optimization gate tags. Boundary sites are treated separately: - for ``site_index = 0``, only the right environment is included, - for ``site_index = n_qubits - 1``, only the left environment is included. .. py:function:: update_cost_tn(cost_tn, gate_tens) Update a cost tensor network with a newly optimized gate tensor. This function replaces the existing gate tensor in the cost tensor network with the updated optimized tensor, keeping the same gate tag. :param cost_tn: Full cost tensor network. :type cost_tn: :quimb-api:`TensorNetwork` :param gate_tens: Optimized gate tensor to insert into the network. :type gate_tens: :quimb-api:`Tensor` :returns: Updated cost tensor network containing the optimized gate tensor. :rtype: :quimb-api:`TensorNetwork` .. py:function:: update_contracted_envs(mode, gate_tens, cost_tn, transfer_structure, contracted_envs) Update contracted environments after a local gate optimization. This function updates the cached contracted left or right environments affected by a newly optimized gate tensor during a sweeping optimization procedure. Depending on the sweep direction, only the environments influenced by the updated gate are recomputed. :param mode: Sweep direction. - ``"LR"`` : left-to-right sweep, - ``"RL"`` : right-to-left sweep. :type mode: {"LR", "RL"} :param gate_tens: Optimized gate tensor. :type gate_tens: :quimb-api:`Tensor` :param cost_tn: Full updated cost tensor network. :type cost_tn: :quimb-api:`TensorNetwork` :param transfer_structure: Transfer structure dictionary generated by :func:`find_transfer_structure`. :type transfer_structure: dict :param contracted_envs: Updated dictionary of contracted environments initialized by :func:`build_first_sweep`. :type contracted_envs: dict :returns: Updated dictionary of contracted environments. :rtype: dict .. rubric:: Notes The optimized gate acts on neighboring sites ``I{n}`` and ``I{n+1}``. Consequently, only nearby environments need to be updated. For a left-to-right sweep (``"LR"``): .. math:: L_{n+2} \leftarrow L_{n+1} \cdot T_{n+1 \to n+2}, while for a right-to-left sweep (``"RL"``): .. math:: R_{n-1} \leftarrow R_n \cdot T_{n \to n-1}. This local update strategy avoids rebuilding all environments from scratch after each optimization step. .. py:function:: optimize_one_gate(tag, loc_cost_tn, cost_tn, mode, transfer_structure, contracted_envs, *, optimize='auto-hq') Update a single gate tensor via its SVD-optimal unitary approximation. :param tag: Tag identifying the gate tensor to update. :type tag: str :param loc_cost_tn: Local cost tensor network for the site the gate belongs to. :type loc_cost_tn: :quimb-api:`TensorNetwork` :param cost_tn: Full cost tensor network. :type cost_tn: :quimb-api:`TensorNetwork` :param mode: Sweep direction, forwarded to :func:`update_contracted_envs`. :type mode: {"LR", "RL"} :param transfer_structure: Transfer structure dictionary generated by :func:`find_transfer_structure`. :type transfer_structure: dict :param contracted_envs: Dictionary of contracted environments generated by :func:`build_first_sweep`. :type contracted_envs: dict :param optimize: Contraction optimization strategy passed to :meth:`TensorNetwork.contract`. Default is ``"auto-hq"``. :type optimize: str or :cotengra-api:`HyperOptimizer`, optional :returns: * **cost_tn** (:quimb-api:`TensorNetwork`) -- Updated full cost tensor network. * **loc_cost_tn** (:quimb-api:`TensorNetwork`) -- Updated local cost tensor network. * **contracted_envs** (*dict*) -- Updated dictionary of contracted environments. * **overlap** (*float*) -- Cost-function value reached after this gate update. .. py:function:: sweep_direction(mode, site_indices, cost_tn, transfer_structure, contracted_envs, *, optimize='auto-hq') Run one left-to-right or right-to-left sweep over all sites. :param mode: Sweep direction, forwarded to :func:`optimize_one_gate`. :type mode: {"LR", "RL"} :param site_indices: Site indices to sweep over, in order. Its length must be ``n_qubits - 2`` (true of both the "LR" and "RL" ranges built by :func:`optimize_single_gate_update`), since ``n_qubits`` is recovered from it as ``len(site_indices) + 2``. :type site_indices: sequence of int :param cost_tn: Full cost tensor network. :type cost_tn: :quimb-api:`TensorNetwork` :param transfer_structure: Transfer structure dictionary generated by :func:`find_transfer_structure`. :type transfer_structure: dict :param contracted_envs: Dictionary of contracted environments generated by :func:`build_first_sweep`. :type contracted_envs: dict :param optimize: Contraction optimization strategy forwarded to :func:`optimize_one_gate`. Default is ``"auto-hq"``. :type optimize: str or :cotengra-api:`HyperOptimizer`, optional :returns: * **cost_tn** (:quimb-api:`TensorNetwork`) -- Updated full cost tensor network. * **contracted_envs** (*dict*) -- Updated dictionary of contracted environments. * **overlap** (*float*) -- Cost-function value reached after the last gate update of the sweep. .. py:function:: optimize_single_gate_update(n_qubits, cost_tn, rtol, n_sweeps_max, transfer_structure, contracted_envs, *, optimize='auto-hq') Optimize a TN ansatz circuit using sequential single-gate updates. This function performs alternating left-to-right and right-to-left sweeps over the variational gates of the cost tensor network. Each gate is optimized individually by contracting its effective environment, performing a singular value decomposition (SVD), and projecting the result back onto the unitary manifold. The optimization iteratively updates both the tensor network and the cached contracted environments. :param n_qubits: Number of qubits (sites) in the tensor network. :type n_qubits: int :param cost_tn: Full cost tensor network containing the variational circuit and target MPO. :type cost_tn: :quimb-api:`TensorNetwork` :param rtol: Relative tolerance condition for stopping. :type rtol: float :param n_sweeps_max: Maximum number of optimization sweeps. :type n_sweeps_max: int :param transfer_structure: Transfer structure dictionary generated by :func:`find_transfer_structure`. :type transfer_structure: dict :param contracted_envs: Dictionary of contracted environments generated by :func:`build_first_sweep`. :type contracted_envs: dict :param optimize: Contraction optimization strategy passed to :meth:`TensorNetwork.contract` when contracting each gate's local environment. Default is ``"auto-hq"``; pass e.g. ``"greedy"`` for a cheaper, deterministic strategy, or a :cotengra-api:`ReusableHyperOptimizer` instance for finer control. :type optimize: str or :cotengra-api:`HyperOptimizer`, optional :returns: * **cost_tn** (:quimb-api:`TensorNetwork`) -- Optimized cost tensor network. * **contracted_envs** (*dict*) -- Updated dictionary of contracted environments. * **overlap** (*float*) -- Overlap with the reference MPO after the last sweep, normalized by ``2**n_qubits``. .. rubric:: Notes The optimization proceeds as follows: 1. Construct a local effective tensor network around a gate, 2. Remove the gate tensor and contract the surrounding environment, 3. Perform an SVD of the resulting effective tensor, 4. Reconstruct the optimal unitary gate from the isometric factors, 5. Update the tensor network and cached environments. .. py:function:: transpile_mpo_to_circuit(ref_mpo, depth, rtol, n_sweeps_max, *, param_scaling=0.1, closed=False, rng=None, optimize='auto-hq') Fit a brickwall circuit ansatz to a reference MPO by single-gate sweeps. This is a convenience wrapper chaining :func:`init_cost_tn`, :func:`find_transfer_structure`, :func:`build_first_sweep` and :func:`optimize_single_gate_update`. :param ref_mpo: Target MPO to be reproduced by the Ansatz. :type ref_mpo: :quimb-api:`MatrixProductOperator` :param depth: Depth of the brickwall circuit ansatz (even and odd count as 1). :type depth: int :param rtol: Relative tolerance condition for stopping. :type rtol: float :param n_sweeps_max: Maximum number of optimization sweeps. :type n_sweeps_max: int :param param_scaling: Scale of the random initialization parameters for the gates. Smaller values initialize each ``SU4SWAP`` closer to the identity. Default is ``1e-1``. :type param_scaling: float, optional :param closed: If ``False`` (default), the bra indices of the MPO remain open. If ``True``, ket indices are contracted (trace contraction). :type closed: bool, optional :param rng: Random number generator to generate gate parameters. If ``None``, a default generator is created. :type rng: :numpy-random:`numpy.random.Generator `, optional :param optimize: Contraction optimization strategy forwarded to :func:`optimize_single_gate_update`. Default is ``"auto-hq"``; pass e.g. ``"greedy"`` for a cheaper, deterministic strategy. :type optimize: str or :cotengra-api:`HyperOptimizer`, optional :returns: * **cost_tn** (:quimb-api:`TensorNetwork`) -- Optimized cost tensor network. * **contracted_envs** (*dict*) -- Updated dictionary of contracted environments. * **overlap** (*float*) -- Overlap with the reference MPO after the last sweep, normalized by ``2**n_qubits``.