15
15
"""GroverOptimizer module"""
16
16
17
17
import logging
18
- from typing import Optional , Dict , Union , List
19
18
import math
19
+ from copy import deepcopy
20
+ from typing import Optional , Dict , Union , List
20
21
21
22
import numpy as np
23
+
22
24
from qiskit import QuantumCircuit , QuantumRegister
23
- from qiskit .circuit .library import QuadraticForm
24
- from qiskit .providers import BaseBackend
25
25
from qiskit .aqua import QuantumInstance , aqua_globals
26
26
from qiskit .aqua .algorithms .amplitude_amplifiers .grover import Grover
27
27
from qiskit .aqua .components .initial_states import Custom
28
28
from qiskit .aqua .components .oracles import CustomCircuitOracle
29
+ from qiskit .circuit .library import QuadraticForm
30
+ from qiskit .providers import BaseBackend
29
31
from .optimization_algorithm import OptimizationAlgorithm , OptimizationResult
32
+ from ..converters .quadratic_program_to_qubo import QuadraticProgramToQubo
30
33
from ..problems import Variable
31
34
from ..problems .quadratic_program import QuadraticProgram
32
- from ..converters .quadratic_program_to_qubo import QuadraticProgramToQubo
33
-
34
35
35
36
logger = logging .getLogger (__name__ )
36
37
@@ -152,6 +153,7 @@ def solve(self, problem: QuadraticProgram) -> OptimizationResult:
152
153
153
154
# convert problem to QUBO
154
155
problem_ = self ._qubo_converter .convert (problem )
156
+ problem_init = deepcopy (problem_ )
155
157
156
158
# convert to minimization problem
157
159
sense = problem_ .objective .sense
@@ -260,15 +262,16 @@ def solve(self, problem: QuadraticProgram) -> OptimizationResult:
260
262
opt_x = np .array ([1 if s == '1' else 0 for s in ('{0:%sb}' % n_key ).format (optimum_key )])
261
263
262
264
# Compute function value
263
- fval = problem_ .objective .evaluate (opt_x )
265
+ fval = problem_init .objective .evaluate (opt_x )
264
266
result = OptimizationResult (x = opt_x , fval = fval , variables = problem_ .variables )
265
267
266
268
# cast binaries back to integers
267
269
result = self ._qubo_converter .interpret (result )
268
270
269
271
return GroverOptimizationResult (x = result .x , fval = result .fval , variables = result .variables ,
270
272
operation_counts = operation_count , n_input_qubits = n_key ,
271
- n_output_qubits = n_value )
273
+ n_output_qubits = n_value , intermediate_fval = fval ,
274
+ threshold = threshold )
272
275
273
276
def _measure (self , circuit : QuantumCircuit ) -> str :
274
277
"""Get probabilities from the given backend, and picks a random outcome."""
@@ -332,7 +335,7 @@ class GroverOptimizationResult(OptimizationResult):
332
335
333
336
def __init__ (self , x : Union [List [float ], np .ndarray ], fval : float , variables : List [Variable ],
334
337
operation_counts : Dict [int , Dict [str , int ]], n_input_qubits : int ,
335
- n_output_qubits : int ) -> None :
338
+ n_output_qubits : int , intermediate_fval : float , threshold : float ) -> None :
336
339
"""
337
340
Constructs a result object with the specific Grover properties.
338
341
@@ -343,11 +346,16 @@ def __init__(self, x: Union[List[float], np.ndarray], fval: float, variables: Li
343
346
operation_counts: The counts of each operation performed per iteration.
344
347
n_input_qubits: The number of qubits used to represent the input.
345
348
n_output_qubits: The number of qubits used to represent the output.
349
+ intermediate_fval: The intermediate value of the objective function of the solution,
350
+ that is expected to be identical with ``fval``.
351
+ threshold: The threshold of Grover algorithm.
346
352
"""
347
353
super ().__init__ (x , fval , variables , None )
348
354
self ._operation_counts = operation_counts
349
355
self ._n_input_qubits = n_input_qubits
350
356
self ._n_output_qubits = n_output_qubits
357
+ self ._intermediate_fval = intermediate_fval
358
+ self ._threshold = threshold
351
359
352
360
@property
353
361
def operation_counts (self ) -> Dict [int , Dict [str , int ]]:
@@ -375,3 +383,21 @@ def n_output_qubits(self) -> int:
375
383
The number of qubits used to represent the output.
376
384
"""
377
385
return self ._n_output_qubits
386
+
387
+ @property
388
+ def intermediate_fval (self ) -> float :
389
+ """Getter of the intermediate fval
390
+
391
+ Returns:
392
+ The intermediate value of fval before interpret.
393
+ """
394
+ return self ._intermediate_fval
395
+
396
+ @property
397
+ def threshold (self ) -> float :
398
+ """Getter of the threshold of Grover algorithm.
399
+
400
+ Returns:
401
+ The threshold of Grover algorithm.
402
+ """
403
+ return self ._threshold
0 commit comments