qpe_toolbox.estimation.qpe_circuit ================================== .. py:module:: qpe_toolbox.estimation.qpe_circuit .. autoapi-nested-parse:: Generic construction of Quantum Phase Estimation circuits from arbitrary unitaries. The functions in this module are agnostic to how the controlled unitaries are obtained. The ``unitaries`` input is a sequence of length ``n_phase_bits``, where ``unitaries[k]`` is an iterable of uncontrolled :quimb-api:`Gate` objects implementing the unitary controlled by phase qubit ``k`` (in textbook QPE, :math:`U^{2^k}`), acting on data-register-local qubit indices ``[0, n_data)``. Entries may be lists or one-shot generators, enabling lazy circuit construction. Each power can be supplied as an independent gate decomposition, so non-squaring implementations (e.g. modular exponentiation in Shor's algorithm) are supported. Functions --------- .. autoapisummary:: qpe_toolbox.estimation.qpe_circuit.qpe_gates qpe_toolbox.estimation.qpe_circuit.qpe_circuit Module Contents --------------- .. py:function:: qpe_gates(unitaries, *, global_phase=0) Generate the full textbook QPE gate sequence without simulating it. The sequence is the QPE first stage (Hadamard wall and controlled unitaries) followed by the inverse QFT on the phase register. Suitable for resource analysis and serialization. :param unitaries: ``unitaries[k]`` is the gate decomposition of the unitary controlled by phase qubit ``k`` (in textbook QPE, :math:`U^{2^k}`), acting on data-register-local qubit indices ``[0, n_data)`` without controls. Entries may be one-shot generators; each is consumed exactly once. :type unitaries: sequence of iterable of :quimb-api:`Gate` :param global_phase: Global phase :math:`\phi` of the unitary :math:`U`. The phase correction applied before the k-th controlled unitary is :math:`\phi \, 2^k` (the global phase of :math:`U^{2^k}`), and the whole correction is omitted when ``global_phase`` is zero. :type global_phase: float, default ``0`` :returns: Lazy gate sequence of the full QPE circuit. :rtype: generator of :quimb-api:`Gate` .. py:function:: qpe_circuit(initial_circ, unitaries, *, global_phase=0, with_iqft=True, verbosity=0) Apply the textbook QPE circuit to an initial circuit. The circuit is built from a Hadamard wall on the phase register and the controlled unitaries (in textbook QPE, :math:`U^{2^k}` controlled by phase qubit ``k``), optionally followed by the inverse QFT on the phase register. :param initial_circ: Circuit preparing the trial state. The phase register occupies qubits ``[0, len(unitaries))``, the data register the remaining qubits. :type initial_circ: :quimb-api:`Circuit` or :quimb-api:`CircuitMPS` :param unitaries: Same convention as in ``qpe_gates``. :type unitaries: sequence of iterable of :quimb-api:`Gate` :param global_phase: Same convention as in ``qpe_gates``. :type global_phase: float, default ``0`` :param with_iqft: If ``True``, apply the inverse QFT on the phase register after the controlled unitaries. If ``False``, stop after the controlled unitaries (the QPE first stage), leaving the phase register in the Fourier basis. :type with_iqft: bool, default ``True`` :param verbosity: Verbosity level. If >= 1, print progress and bond dimension information. :type verbosity: int, default ``0`` :returns: * **traces** (*dict*) -- Contains per-stage computation times and bond dimensions. * **circ** (:quimb-api:`Circuit` or :quimb-api:`CircuitMPS`) -- Copy of ``initial_circ`` with the QPE circuit applied. When ``with_iqft`` is ``True``, phase probabilities can be obtained with ``circ.compute_marginal(where=range(len(unitaries)))``. .. rubric:: Notes Whether the circuit is contracted lazily or eagerly is governed by the *class* of ``initial_circ``, not by any argument here: - With a :quimb-api:`Circuit`, gates accumulate lazily into an uncontracted tensor network (no truncation). The returned ``circ.psi`` is that network; the contraction strategy is deferred to post-processing, e.g. ``circ.compute_marginal(..., optimize=..., rehearse=...)`` or handing ``circ.psi`` to a contraction-path optimizer. - With a :quimb-api:`CircuitMPS`, each gate is applied eagerly to the MPS with SVD truncation (controlled by the ``CircuitMPS`` ``max_bond`` / ``cutoff``), i.e. the circuit is simulated as it is built. In both cases the per-stage ``bond_dims`` trace is read from ``circ.psi.max_bond()``, which is cheap and does not force a contraction.