qpe_toolbox.estimation.hadamard_test ==================================== .. py:module:: qpe_toolbox.estimation.hadamard_test Functions --------- .. autoapisummary:: qpe_toolbox.estimation.hadamard_test.build_hadamard_test_circuit qpe_toolbox.estimation.hadamard_test.run_hadamard_test Module Contents --------------- .. py:function:: build_hadamard_test_circuit(init_mps, unitary, phase_gate_angle, *, cutoff=1e-10, max_bond=None) Construct the quantum circuit implementing the Hadamard test. The circuit prepares an ancilla qubit, applies a phase rotation and a controlled unitary :math:`U`, and measures the ancilla in the X basis. It is the single-phase-qubit Quantum Phase Estimation circuit, built with ``qpe_circuit``. The phase rotation is applied before the controlled unitary; both commute, so the output is identical to the textbook ordering where the rotation comes after. This circuit can be used to estimate the real or imaginary part of :math:`\bra{\psi} U \ket{\psi}` by choosing appropriate values of ``phase_gate_angle``. :param init_mps: Initial state :math:`\ket{\psi}` of the data register. A single ancilla qubit is prepended, giving a :quimb-api:`CircuitMPS`. :type init_mps: :quimb-api:`MatrixProductState` :param unitary: The unitary :math:`U`, acting on data-register-local qubit indices ``[0, n_data)`` without controls (same convention as in ``qpe_circuit``). Either a single gate (e.g. an exact :math:`U`) or its gate decomposition as an iterable of gates (e.g. a Trotterized :math:`U`). An iterable may be a one-shot generator, consumed exactly once. :type unitary: :quimb-api:`Gate` or iterable of :quimb-api:`Gate` :param phase_gate_angle: Phase angle :math:`\beta` applied to the ancilla qubit. Typical values: - ``0`` for estimating the real part - ``-π/2`` for estimating the imaginary part :type phase_gate_angle: float :param cutoff: Singular-value truncation threshold of the underlying :quimb-api:`CircuitMPS`. :type cutoff: float, default ``1e-10`` :param max_bond: Maximum bond dimension of the underlying :quimb-api:`CircuitMPS`. If ``None``, no explicit limit is imposed. :type max_bond: int or None, default ``None`` :returns: **circ** -- Circuit implementing the Hadamard test. :rtype: :quimb-api:`CircuitMPS` .. py:function:: run_hadamard_test(init_mps, unitary, phase_gate_angle, n_shots, *, cutoff=1e-10, max_bond=None, rng=None) Run the Hadamard test circuit and estimate the expectation value :math:`Z(\beta)`. The returned value is .. math:: Z(\beta) = P(0) - P(1) = \mathrm{Re} \left[e^{i\beta} \bra{\psi} U \ket{\psi} \right] where :math:`P(0)` and :math:`P(1)` are the normalized probabilities of measuring the ancilla qubit in states :math:`\ket{0}` and :math:`\ket{1}`. :param init_mps: Initial state :math:`\ket{\psi}` of the data register. :type init_mps: :quimb-api:`MatrixProductState` :param unitary: The unitary :math:`U` used in the Hadamard test, either a single gate (e.g. an exact :math:`U`) or its gate decomposition as an iterable of gates (e.g. a Trotterized :math:`U`). See ``build_hadamard_test_circuit`` for the convention. A one-shot generator is consumed by this call; build a fresh one per call. :type unitary: :quimb-api:`Gate` or iterable of :quimb-api:`Gate` :param phase_gate_angle: Phase angle :math:`\beta` applied to the ancilla qubit. :type phase_gate_angle: float :param n_shots: Number of measurement shots. If ``EXACT``, probabilities are computed exactly, else probabilities are estimated by sampling. :type n_shots: int or qpe_toolbox.EXACT :param cutoff: Singular-value truncation threshold of the underlying :quimb-api:`CircuitMPS`. :type cutoff: float, default ``1e-10`` :param max_bond: Maximum bond dimension of the underlying :quimb-api:`CircuitMPS`. If ``None``, no explicit limit is imposed. :type max_bond: int or None, default ``None`` :param rng: Random generator forwarded to the circuit sampler (``circ.sample``). Ignored when ``n_shots`` is ``EXACT``. :type rng: :numpy-random:`numpy.random.Generator `, optional :returns: **Z** -- Estimated value of :math:`Z(\beta) = P(0) - P(1)`. :rtype: float