• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Qiskit / qiskit-addon-aqc-tensor / 11669945803

04 Nov 2024 06:01PM UTC coverage: 95.253% (-0.1%) from 95.39%
11669945803

Pull #15

github

web-flow
Merge 52fe04dad into ca9f7413d
Pull Request #15: Add `ansatz` parameter to the objective function

2 of 3 new or added lines in 1 file covered. (66.67%)

602 of 632 relevant lines covered (95.25%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/qiskit_addon_aqc_tensor/objective.py
1
# This code is a Qiskit project.
2
#
3
# (C) Copyright IBM 2024.
4
#
5
# This code is licensed under the Apache License, Version 2.0. You may
6
# obtain a copy of this license in the LICENSE.txt file in the root directory
7
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8
#
9
# Any modifications or derivative works of this code must retain this
10
# copyright notice, and modified files need to carry a notice indicating
11
# that they have been altered from the originals.
12
"""
13
Code for building and evaluating objective functions used for AQC parameter optimization.
14

15
Currently, this module provides the simplest possible objective function, :class:`.OneMinusFidelity`.
16

17
.. currentmodule:: qiskit_addon_aqc_tensor.objective
18

19
.. autosummary::
20
    :toctree: ../stubs/
21
    :nosignatures:
22

23
    OneMinusFidelity
24
"""
25

26
from __future__ import annotations
1✔
27

28
from typing import TYPE_CHECKING
1✔
29

30
import numpy as np
1✔
31
from qiskit.circuit import QuantumCircuit
1✔
32

33
if TYPE_CHECKING:  # pragma: no cover
34
    from .simulation.abstract import (
35
        TensorNetworkSimulationSettings,
36
        TensorNetworkState,
37
    )
38

39

40
class OneMinusFidelity:
1✔
41
    r"""Simplest possible objective function for use with AQC-Tensor.
42

43
    Its value is given by Eq. (7) in `arXiv:2301.08609v6 <https://arxiv.org/abs/2301.08609v6>`__:
44

45
    .. math::
46
       C = 1 - \left| \langle 0 | V^{\dagger}(\vec\theta) | \psi_\mathrm{target} \rangle \right|^2 .
47

48
    Minimizing this function is equivalent to maximizing the pure-state fidelity
49
    between the state prepared by the ansatz circuit at the current parameter
50
    point,(:math:`V(\vec\theta) |0\rangle`, and the target state,
51
    :math:`| \psi_\mathrm{target} \rangle`.
52

53
    When called with an :class:`~numpy.ndarray` of parameters, this object will return
54
    ``(objective_value, gradient)`` as a ``tuple[float, numpy.ndarray]``.
55
    """
56

57
    def __init__(
1✔
58
        self,
59
        target: TensorNetworkState,
60
        ansatz: QuantumCircuit,
61
        settings: TensorNetworkSimulationSettings,
62
    ):
63
        """
64
        Initialize the objective function.
65

66
        Args:
67
            ansatz: Parametrized ansatz circuit.
68
            target: Target state in tensor-network representation.
69
            settings: Tensor network simulation settings.
70
        """
71
        if ansatz is not None:
1✔
72
            from .ansatz_generation import AnsatzBlock
1✔
73

74
            ansatz = ansatz.decompose(AnsatzBlock)
1✔
75
        self._ansatz = ansatz
1✔
76
        self._simulation_settings = settings
1✔
77
        self._target_tensornetwork = target
1✔
78
        if settings is not None:
1✔
79
            from .simulation.abstract import _preprocess_for_gradient
1✔
80

81
            self._preprocessed = _preprocess_for_gradient(self, settings)
1✔
82

83
    def __call__(self, x: np.ndarray) -> tuple[float, np.ndarray]:
1✔
84
        """Evaluate ``(objective_value, gradient)`` of function at point ``x``."""
85
        from .simulation.abstract import _compute_objective_and_gradient
1✔
86

87
        return _compute_objective_and_gradient(
1✔
88
            self, self._simulation_settings, self._preprocessed, x
89
        )
90

91
    @property
1✔
92
    def target(self) -> TensorNetworkState:
1✔
93
        """Target tensor network."""
94
        return self._target_tensornetwork
1✔
95

96
    @property
1✔
97
    def ansatz(self) -> QuantumCircuit | None:
1✔
98
        """Parametrized ansatz circuit."""
NEW
99
        return self._ansatz
×
100

101

102
__all__ = [
1✔
103
    "OneMinusFidelity",
104
]
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc