qpe_toolbox.estimation.quantum_phase_estimation =============================================== .. py:module:: qpe_toolbox.estimation.quantum_phase_estimation .. autoapi-nested-parse:: Hamiltonian-driven Quantum Phase Estimation circuit construction. Build the controlled unitaries :math:`U^{2^k} = e^{-i H t 2^k}` from a Hamiltonian and feed them to the generic QPE circuit builder in ``qpe_circuit``. The assembled circuit places two contiguous registers, phase first: - **phase register** -- the ``n_phase_bits`` estimation qubits, at indices ``[0, n_phase_bits)`` (``phase_reg``). Hadamard wall, controls, inverse QFT. - **data register** -- the qubits the controlled unitary acts on, at indices ``[n_phase_bits, initial_circ.N)``. For textbook QPE (and RPE) this *is* the physical register; the generic ``qpe_circuit`` layer works in data-register local indices ``[0, n_data)`` and shifts them past the phase register. The Hamiltonian methods ``get_exact_unitary`` / ``get_trotter_step`` build gates on the **physical register** ``phys_reg`` -- the ``n_qubits`` qubits the Hamiltonian acts on. Here ``phys_reg`` coincides with the data register, so the default ``phys_reg = range(n_qubits)`` is used. The distinction only matters for block-encoded variants (e.g. LCU), where the data register additionally contains ancilla qubits and ``data_reg = ancilla_reg + phys_reg``. Functions --------- .. autoapisummary:: qpe_toolbox.estimation.quantum_phase_estimation.qpe_energy qpe_toolbox.estimation.quantum_phase_estimation.qpe_sample qpe_toolbox.estimation.quantum_phase_estimation.qpe_gate_list qpe_toolbox.estimation.quantum_phase_estimation.qpe_first_stage qpe_toolbox.estimation.quantum_phase_estimation.exact_evolution_powers qpe_toolbox.estimation.quantum_phase_estimation.evolution_powers qpe_toolbox.estimation.quantum_phase_estimation.set_search_window Module Contents --------------- .. py:function:: qpe_energy(hamiltonian, initial_circ, n_trotter_steps, E_target, size_interval, *, trotter_order=1, optimize='auto-hq', verbosity=0) Perform quantum phase estimation (QPE) to estimate the energy of a Hamiltonian. The algorithm encodes the phase corresponding to the Hamiltonian evolution into a phase register and samples it to extract the energy eigenvalue. :param hamiltonian: Hamiltonian object from the QPE-Toolbox ``Hamiltonian`` class. :type hamiltonian: Hamiltonian :param initial_circ: Initial circuit preparing the trial state in the data register. :type initial_circ: :quimb-api:`Circuit` or :quimb-api:`CircuitMPS` :param n_trotter_steps: Number of Trotter steps for the ``U(t)`` evolution, or ``EXACT`` for exact evolution. :type n_trotter_steps: int or qpe_toolbox.EXACT :param E_target: Central target energy for the search window, in physical energy units (i.e. including ``hamiltonian.e_const`` if present). :type E_target: float :param size_interval: Width of the energy search interval. :type size_interval: float :param trotter_order: Order of the Trotter decomposition. :type trotter_order: int, default ``1`` :param optimize: Optimization strategy when computing marginals with tensor networks. :type optimize: str, default ``"auto-hq"`` :param verbosity: Verbosity level. If >= 1, print timing and progress information and the result summary. :type verbosity: int, default ``0`` :returns: * **traces** (*dict*) -- Dictionary with computation information, including timing, bond dimensions, gate counts, and the phase values sorted by decreasing probability. * **energy** (*float*) -- Estimated energy eigenvalue in physical units (Pauli eigenvalue plus ``hamiltonian.e_const`` if present). .. rubric:: Notes - The phase register is automatically determined as ``initial_circ.N - hamiltonian.n_qubits``. - The estimated energy is computed as .. math:: E = E_\mathrm{max} - \frac{2 \pi \theta}{t_\mathrm{evol}} where :math:`\theta` is the phase of the most probable state and :math:`E_\mathrm{max} = E_\mathrm{target} + \Delta/2` (see :func:`set_search_window`). - Supports both Trotterized and exact evolution. .. py:function:: qpe_sample(hamiltonian, initial_circ, evolution_time, n_trotter_steps, global_phase, *, trotter_order=1, rehearse=False, optimize='auto-hq', verbosity=0) Apply quantum phase estimation to a given initial circuit and sample the output. :param hamiltonian: Hamiltonian object from the QPE-Toolbox ``Hamiltonian`` class. :type hamiltonian: Hamiltonian :param initial_circ: Circuit preparing the trial state. :type initial_circ: :quimb-api:`Circuit` or :quimb-api:`CircuitMPS` :param evolution_time: Total evolution time for the controlled-U operations. :type evolution_time: float :param n_trotter_steps: Number of Trotter steps for the ``U(t)`` evolution; if ``EXACT``, evolution is exact. :type n_trotter_steps: int or qpe_toolbox.EXACT :param global_phase: Global phase added to the controlled-U operations. :type global_phase: float :param trotter_order: Order of Trotter decomposition for time evolution. :type trotter_order: int, default ``1`` :param rehearse: If ``True``, the final ``compute_marginal`` is not run: it returns the contraction rehearsal (tensor network, path, and cost estimates) instead of the phase probabilities. :type rehearse: bool, default ``False`` :param optimize: Optimization strategy for tensor network marginal computation. :type optimize: str, default ``"auto-hq"`` :param verbosity: If ``> 0``, prints timing and progress information. :type verbosity: int, default ``0`` :returns: * **traces** (*dict*) -- Dictionary containing bond dimensions, full QPE circuit, timing and gate counts. * **probs** (*array or dict*) -- Probability tensor of the phase qubits, or the contraction rehearsal when ``rehearse=True``. .. rubric:: Notes - To obtain the gate list without simulating the circuit (resource analysis), use ``qpe_gate_list``. - The QPE quantum circuit can be recovered as ``traces["circuit"]``. - ``traces["gates_count"]`` covers the whole circuit, including any gates carried by ``initial_circ``; circuits built by ``make_circ`` carry none. - Whether the circuit is contracted lazily or eagerly is set by the *class* of ``initial_circ``: a :quimb-api:`Circuit` accumulates an uncontracted tensor network that the final ``compute_marginal`` contracts exactly (steered by ``optimize`` / ``rehearse``), while a :quimb-api:`CircuitMPS` is simulated eagerly with SVD truncation. Either way the marginal over the phase register is returned, unless ``rehearse`` is ``True``. - With ``rehearse=True`` no contraction is performed: ``compute_marginal`` returns the contraction rehearsal instead of the probabilities. Combined with a :quimb-api:`Circuit`, evaluation is then completely lazy. .. py:function:: qpe_gate_list(hamiltonian, n_phase_bits, evolution_time, n_trotter_steps, global_phase, *, trotter_order=1, savefile=None) Build the QPE gate list for a Hamiltonian evolution without simulating it. This produces the same QPE gate sequence as ``qpe_sample``, excluding any gates already present in its ``initial_circ``, and skips the tensor network simulation; use it for resource analysis and circuit serialization. :param hamiltonian: Hamiltonian object from the QPE-Toolbox ``Hamiltonian`` class. :type hamiltonian: Hamiltonian :param n_phase_bits: Number of phase estimation qubits. :type n_phase_bits: int :param evolution_time: Total evolution time for the controlled-U operations. :type evolution_time: float :param n_trotter_steps: Number of Trotter steps for the ``U(t)`` evolution; if ``EXACT``, evolution is exact. :type n_trotter_steps: int or qpe_toolbox.EXACT :param global_phase: Global phase added to the controlled-U operations. :type global_phase: float :param trotter_order: Order of Trotter decomposition for time evolution. :type trotter_order: int, default ``1`` :param savefile: If not ``None``, path to which the serialized gates are written as JSON. Not supported for exact time evolution (``n_trotter_steps=EXACT``). :type savefile: str or os.PathLike or None, default ``None`` :returns: * **gates_count** (*dict*) -- Dictionary mapping gate labels to their counts. * **gates_list** (list of :quimb-api:`Gate`) -- Gate sequence of the full QPE circuit. :raises ValueError: If ``savefile`` is given together with exact time evolution (``n_trotter_steps=EXACT``). .. py:function:: qpe_first_stage(hamiltonian, initial_circ, evolution_time, n_trotter_steps, global_phase, *, trotter_order=1, verbosity=0) Perform the first stage of the quantum phase estimation algorithm. This includes: * Applying a Hadamard wall on the phase register. * Controlled-U operations with the Hamiltonian evolution. * Optional Trotterization for approximate time evolution. :param hamiltonian: Hamiltonian object from the QPE-Toolbox ``Hamiltonian`` class. :type hamiltonian: Hamiltonian :param initial_circ: Initial state of the system. :type initial_circ: :quimb-api:`Circuit` or :quimb-api:`CircuitMPS` :param evolution_time: Total evolution time. :type evolution_time: float :param n_trotter_steps: Number of Trotter steps for the ``U(t)`` evolution; ``EXACT`` for exact evolution. :type n_trotter_steps: int or qpe_toolbox.EXACT :param global_phase: Global phase applied to controlled-U operations. :type global_phase: float :param trotter_order: Trotter order for time evolution. :type trotter_order: int, default ``1`` :param verbosity: Verbosity level. If >= 1, print progress and bond dimension information. :type verbosity: int, default ``0`` :returns: * **traces** (*dict*) -- Contains bond dimensions, computation times, and other metadata. * **circ** (:quimb-api:`Circuit` or :quimb-api:`CircuitMPS`) -- Updated circuit with the first stage applied. .. py:function:: exact_evolution_powers(hamiltonian, evolution_time, n_powers) Build the exact evolution unitaries :math:`U(t \, 2^k)` for the QPE sequence. :param hamiltonian: Hamiltonian object from the QPE-Toolbox ``Hamiltonian`` class. :type hamiltonian: Hamiltonian :param evolution_time: Total evolution time ``t``. :type evolution_time: float :param n_powers: Number of powers to build, indexed by ``k = 0, ..., n_powers - 1``. :type n_powers: int :returns: **unitaries** -- ``unitaries[k]`` holds the single dense gate implementing :math:`U(t \, 2^k) = e^{-i H t 2^k}` on data-register-local qubits, without controls, as expected by ``qpe_circuit`` and ``qpe_gates``. :rtype: list of list of :quimb-api:`Gate` .. py:function:: evolution_powers(hamiltonian, evolution_time, n_trotter_steps, n_powers, *, trotter_order=1) Build the evolution unitaries :math:`U(t \, 2^k)` for the QPE sequence. Dispatch to ``exact_evolution_powers`` or ``trotter_evolution_powers`` depending on ``n_trotter_steps``. :param hamiltonian: Hamiltonian object from the QPE-Toolbox ``Hamiltonian`` class. :type hamiltonian: Hamiltonian :param evolution_time: Total evolution time ``t``. :type evolution_time: float :param n_trotter_steps: Number of Trotter steps for the ``U(t)`` evolution; multiplied by ``2**k`` for power ``k`` to keep the Trotter step size constant. Use ``EXACT`` for exact time evolution. :type n_trotter_steps: int or qpe_toolbox.EXACT :param n_powers: Number of powers to build, indexed by ``k = 0, ..., n_powers - 1``. The number of phase bits in textbook QPE, of repetitions in RPE. :type n_powers: int :param trotter_order: Order of the Trotter decomposition. Ignored when ``n_trotter_steps is EXACT``. :type trotter_order: int, default ``1`` :returns: **unitaries** -- ``unitaries[k]`` holds the gates of :math:`U(t \, 2^k)` on data-register-local qubits, without controls, as expected by ``qpe_circuit`` and ``qpe_gates``. Trotterized entries are one-shot generators. :rtype: list of iterable of :quimb-api:`Gate` .. py:function:: set_search_window(hamiltonian, E_target, size_interval) Set up the energy search window for phase estimation. :param hamiltonian: Hamiltonian object from the QPE-Toolbox ``Hamiltonian`` class. :type hamiltonian: Hamiltonian :param E_target: Central target energy around which to search, in physical energy units (i.e. including ``hamiltonian.e_const`` if present). :type E_target: float :param size_interval: Width of the energy interval (must be > 0). :type size_interval: float :returns: * **E_max** (*float*) -- Upper bound of the search window in physical energy units: ``E_target + size_interval / 2``. * **evolution_time** (*float*) -- Total evolution time corresponding to the search interval. * **global_phase** (*float*) -- Global phase applied to the circuit: ``(E_max - e_const) * evolution_time``. .. rubric:: Notes - Evolution time is chosen as ``2 * pi / size_interval`` to map the interval to [0, 2π].