| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | """Validate an initial point.""" |
| |
|
| | from __future__ import annotations |
| |
|
| | import numpy as np |
| |
|
| | from qiskit.circuit import QuantumCircuit |
| | from qiskit_algorithms.utils.algorithm_globals import algorithm_globals |
| |
|
| |
|
| | def validate_initial_point(point: np.ndarray | None | None, circuit: QuantumCircuit) -> np.ndarray: |
| | r""" |
| | Validate a choice of initial point against a choice of circuit. If no point is provided, a |
| | random point will be generated within certain parameter bounds. It will first look to the |
| | circuit for these bounds. If the circuit does not specify bounds, bounds of :math:`-2\pi`, |
| | :math:`2\pi` will be used. |
| | |
| | Args: |
| | point: An initial point. |
| | circuit: A parameterized quantum circuit. |
| | |
| | Returns: |
| | A validated initial point. |
| | |
| | Raises: |
| | ValueError: If the dimension of the initial point does not match the number of circuit |
| | parameters. |
| | """ |
| | expected_size = circuit.num_parameters |
| |
|
| | if point is None: |
| | |
| | bounds = getattr(circuit, "parameter_bounds", None) |
| | if bounds is None: |
| | bounds = [(-2 * np.pi, 2 * np.pi)] * expected_size |
| |
|
| | |
| | lower_bounds = [] |
| | upper_bounds = [] |
| | for lower, upper in bounds: |
| | lower_bounds.append(lower if lower is not None else -2 * np.pi) |
| | upper_bounds.append(upper if upper is not None else 2 * np.pi) |
| |
|
| | |
| | point = algorithm_globals.random.uniform(lower_bounds, upper_bounds) |
| |
|
| | elif len(point) != expected_size: |
| | raise ValueError( |
| | f"The dimension of the initial point ({len(point)}) does not match the " |
| | f"number of parameters in the circuit ({expected_size})." |
| | ) |
| |
|
| | return point |
| |
|