File size: 22,509 Bytes
b7d9967 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2018, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""The Maximum Likelihood Amplitude Estimation algorithm."""
from __future__ import annotations
from collections.abc import Sequence
from typing import Callable, List, Tuple, cast
import warnings
import numpy as np
from scipy.optimize import brute
from scipy.stats import norm, chi2
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit
from qiskit.primitives import BaseSampler, Sampler
from .amplitude_estimator import AmplitudeEstimator, AmplitudeEstimatorResult
from .estimation_problem import EstimationProblem
from ..exceptions import AlgorithmError
MINIMIZER = Callable[[Callable[[float], float], List[Tuple[float, float]]], float]
class MaximumLikelihoodAmplitudeEstimation(AmplitudeEstimator):
"""The Maximum Likelihood Amplitude Estimation algorithm.
This class implements the quantum amplitude estimation (QAE) algorithm without phase
estimation, as introduced in [1]. In comparison to the original QAE algorithm [2],
this implementation relies solely on different powers of the Grover operator and does not
require additional evaluation qubits.
Finally, the estimate is determined via a maximum likelihood estimation, which is why this
class in named ``MaximumLikelihoodAmplitudeEstimation``.
References:
[1]: Suzuki, Y., Uno, S., Raymond, R., Tanaka, T., Onodera, T., & Yamamoto, N. (2019).
Amplitude Estimation without Phase Estimation.
`arXiv:1904.10246 <https://arxiv.org/abs/1904.10246>`_.
[2]: Brassard, G., Hoyer, P., Mosca, M., & Tapp, A. (2000).
Quantum Amplitude Amplification and Estimation.
`arXiv:quant-ph/0005055 <http://arxiv.org/abs/quant-ph/0005055>`_.
"""
def __init__(
self,
evaluation_schedule: list[int] | int,
minimizer: MINIMIZER | None = None,
sampler: BaseSampler | None = None,
) -> None:
r"""
Args:
evaluation_schedule: If a list, the powers applied to the Grover operator. The list
element must be non-negative. If a non-negative integer, an exponential schedule is
used where the highest power is 2 to the integer minus 1:
`[id, Q^2^0, ..., Q^2^(evaluation_schedule-1)]`.
minimizer: A minimizer used to find the minimum of the likelihood function.
Defaults to a brute search where the number of evaluation points is determined
according to ``evaluation_schedule``. The minimizer takes a function as first
argument and a list of (float, float) tuples (as bounds) as second argument and
returns a single float which is the found minimum.
sampler: A sampler primitive to evaluate the circuits.
Raises:
ValueError: If the number of oracle circuits is smaller than 1.
"""
super().__init__()
# get parameters
if isinstance(evaluation_schedule, int):
if evaluation_schedule < 0:
raise ValueError("The evaluation schedule cannot be < 0.")
self._evaluation_schedule = [0] + [2**j for j in range(evaluation_schedule)]
else:
if any(value < 0 for value in evaluation_schedule):
raise ValueError("The elements of the evaluation schedule cannot be < 0.")
self._evaluation_schedule = evaluation_schedule
if minimizer is None:
# default number of evaluations is max(10^4, pi/2 * 10^3 * 2^(m))
nevals = max(10000, int(np.pi / 2 * 1000 * 2 * self._evaluation_schedule[-1]))
def default_minimizer(objective_fn, bounds):
return brute(objective_fn, bounds, Ns=nevals)[0]
self._minimizer = default_minimizer
else:
self._minimizer = minimizer
self._sampler = sampler
@property
def sampler(self) -> BaseSampler | None:
"""Get the sampler primitive.
Returns:
The sampler primitive to evaluate the circuits.
"""
return self._sampler
@sampler.setter
def sampler(self, sampler: BaseSampler) -> None:
"""Set sampler primitive.
Args:
sampler: A sampler primitive to evaluate the circuits.
"""
self._sampler = sampler
def construct_circuits(
self, estimation_problem: EstimationProblem, measurement: bool = False
) -> list[QuantumCircuit]:
"""Construct the Amplitude Estimation w/o QPE quantum circuits.
Args:
estimation_problem: The estimation problem for which to construct the QAE circuit.
measurement: Boolean flag to indicate if measurement should be included in the circuits.
Returns:
A list with the QuantumCircuit objects for the algorithm.
"""
# keep track of the Q-oracle queries
circuits = []
num_qubits = max(
estimation_problem.state_preparation.num_qubits,
estimation_problem.grover_operator.num_qubits,
)
q = QuantumRegister(num_qubits, "q")
qc_0 = QuantumCircuit(q, name="qc_a") # 0 applications of Q, only a single A operator
# add classical register if needed
if measurement:
c = ClassicalRegister(len(estimation_problem.objective_qubits))
qc_0.add_register(c)
qc_0.compose(estimation_problem.state_preparation, inplace=True)
for k in self._evaluation_schedule:
qc_k = qc_0.copy(name=f"qc_a_q_{k}")
if k != 0:
qc_k.compose(estimation_problem.grover_operator.power(k), inplace=True)
if measurement:
# real hardware can currently not handle operations after measurements,
# which might happen if the circuit gets transpiled, hence we're adding
# a safeguard-barrier
qc_k.barrier()
qc_k.measure(estimation_problem.objective_qubits, c[:])
circuits += [qc_k]
return circuits
@staticmethod
def compute_confidence_interval(
result: "MaximumLikelihoodAmplitudeEstimationResult",
alpha: float,
kind: str = "fisher",
apply_post_processing: bool = False,
exact: bool = False,
) -> tuple[float, float]:
"""Compute the `alpha` confidence interval using the method `kind`.
The confidence level is (1 - `alpha`) and supported kinds are 'fisher',
'likelihood_ratio' and 'observed_fisher' with shorthand
notations 'fi', 'lr' and 'oi', respectively.
Args:
result: A maximum likelihood amplitude estimation result.
alpha: The confidence level.
kind: The method to compute the confidence interval. Defaults to 'fisher', which
computes the theoretical Fisher information.
apply_post_processing: If True, apply post-processing to the confidence interval.
exact: Whether the result comes from a statevector simulation or not
Returns:
The specified confidence interval.
Raises:
AlgorithmError: If `run()` hasn't been called yet.
NotImplementedError: If the method `kind` is not supported.
"""
interval: tuple[float, float] | None = None
# if statevector simulator the estimate is exact
if exact:
interval = (result.estimation, result.estimation)
elif kind in ["likelihood_ratio", "lr"]:
interval = _likelihood_ratio_confint(result, alpha)
elif kind in ["fisher", "fi"]:
interval = _fisher_confint(result, alpha, observed=False)
elif kind in ["observed_fisher", "observed_information", "oi"]:
interval = _fisher_confint(result, alpha, observed=True)
if interval is None:
raise NotImplementedError(f"CI `{kind}` is not implemented.")
if apply_post_processing:
return result.post_processing(interval[0]), result.post_processing(interval[1])
return interval
# TODO: add deprecation warning for num_state_qubits
def compute_mle(
self,
circuit_results: list[dict[str, int]],
estimation_problem: EstimationProblem,
num_state_qubits: int | None = None, # pylint: disable=unused-argument
return_counts: bool = False,
) -> float | tuple[float, list[int]]:
"""Compute the MLE via a grid-search.
This is a stable approach if sufficient grid-points are used.
Args:
circuit_results: A list of circuit outcomes. Can be counts or statevectors.
estimation_problem: The estimation problem containing the evaluation schedule and the
number of likelihood function evaluations used to find the minimum.
return_counts: If True, returns the good counts.
num_state_qubits: [Deprecated]. Number of qubits to process statevector results.
Returns:
The MLE for the provided result object.
"""
good_counts, all_counts = _get_counts(circuit_results, estimation_problem)
# search range
eps = 1e-15 # to avoid invalid value in log
search_range = [0 + eps, np.pi / 2 - eps]
def loglikelihood(theta):
# loglik contains the first `it` terms of the full loglikelihood
loglik = 0
for i, k in enumerate(self._evaluation_schedule):
angle = (2 * k + 1) * theta
loglik += np.log(np.sin(angle) ** 2) * good_counts[i]
loglik += np.log(np.cos(angle) ** 2) * (all_counts[i] - good_counts[i])
return -loglik
est_theta: float = self._minimizer(loglikelihood, [search_range])
if return_counts:
return est_theta, good_counts
return est_theta
def estimate(
self, estimation_problem: EstimationProblem
) -> "MaximumLikelihoodAmplitudeEstimationResult":
"""Run the amplitude estimation algorithm on provided estimation problem.
Args:
estimation_problem: The estimation problem.
Returns:
An amplitude estimation results object.
Raises:
AlgorithmError: If `state_preparation` is not set in
`estimation_problem`.
AlgorithmError: Sampler job run error
"""
if self._sampler is None:
warnings.warn("No sampler provided, defaulting to Sampler from qiskit.primitives")
self._sampler = Sampler()
if estimation_problem.state_preparation is None:
raise AlgorithmError(
"The state_preparation property of the estimation problem must be set."
)
result = MaximumLikelihoodAmplitudeEstimationResult()
result.evaluation_schedule = self._evaluation_schedule
result.minimizer = self._minimizer
result.post_processing = cast(Callable[[float], float], estimation_problem.post_processing)
circuits = self.construct_circuits(estimation_problem, measurement=True)
try:
job = self._sampler.run(circuits)
ret = job.result()
except Exception as exc:
raise AlgorithmError("The job was not completed successfully. ") from exc
circuit_results = []
shots = ret.metadata[0].get("shots")
exact = True
if shots is None:
for quasi_dist in ret.quasi_dists:
circuit_result = quasi_dist.binary_probabilities()
circuit_results.append(circuit_result)
shots = 1
else:
# get counts and construct MLE input
for quasi_dist in ret.quasi_dists:
counts = {k: round(v * shots) for k, v in quasi_dist.binary_probabilities().items()}
circuit_results.append(counts)
exact = False
result.shots = shots
result.circuit_results = circuit_results
# run maximum likelihood estimation
num_state_qubits = circuits[0].num_qubits - circuits[0].num_ancillas
theta, good_counts = cast(
Tuple[float, List[float]],
self.compute_mle(result.circuit_results, estimation_problem, num_state_qubits, True),
)
# store results
result.theta = theta
result.good_counts = good_counts
result.estimation = np.sin(result.theta) ** 2
# not sure why pylint complains, this is a callable and the tests pass
# pylint: disable=not-callable
result.estimation_processed = result.post_processing(result.estimation)
result.fisher_information = _compute_fisher_information(result)
result.num_oracle_queries = result.shots * sum(k for k in result.evaluation_schedule)
# compute and store confidence interval
confidence_interval = self.compute_confidence_interval(
result, alpha=0.05, kind="fisher", exact=exact
)
result.confidence_interval = confidence_interval
result.confidence_interval_processed = tuple( # type: ignore[assignment]
estimation_problem.post_processing(value) # type: ignore[arg-type]
for value in confidence_interval
)
return result
class MaximumLikelihoodAmplitudeEstimationResult(AmplitudeEstimatorResult):
"""The ``MaximumLikelihoodAmplitudeEstimation`` result object."""
def __init__(self) -> None:
super().__init__()
self._theta: float | None = None
self._minimizer: Callable | None = None
self._good_counts: list[float] | None = None
self._evaluation_schedule: list[int] | None = None
self._fisher_information: float | None = None
@property
def theta(self) -> float:
r"""Return the estimate for the angle :math:`\theta`."""
return self._theta
@theta.setter
def theta(self, value: float) -> None:
r"""Set the estimate for the angle :math:`\theta`."""
self._theta = value
@property
def minimizer(self) -> Callable:
"""Return the minimizer used for the search of the likelihood function."""
return self._minimizer
@minimizer.setter
def minimizer(self, value: Callable) -> None:
"""Set the number minimizer used for the search of the likelihood function."""
self._minimizer = value
@property
def good_counts(self) -> list[float]:
"""Return the percentage of good counts per circuit power."""
return self._good_counts
@good_counts.setter
def good_counts(self, counts: list[float]) -> None:
"""Set the percentage of good counts per circuit power."""
self._good_counts = counts
@property
def evaluation_schedule(self) -> list[int]:
"""Return the evaluation schedule for the powers of the Grover operator."""
return self._evaluation_schedule
@evaluation_schedule.setter
def evaluation_schedule(self, evaluation_schedule: list[int]) -> None:
"""Set the evaluation schedule for the powers of the Grover operator."""
self._evaluation_schedule = evaluation_schedule
@property
def fisher_information(self) -> float:
"""Return the Fisher information for the estimated amplitude."""
return self._fisher_information
@fisher_information.setter
def fisher_information(self, value: float) -> None:
"""Set the Fisher information for the estimated amplitude."""
self._fisher_information = value
def _safe_min(array: list[float], default: float = 0.0) -> float:
if len(array) == 0:
return default
return np.min(array)
def _safe_max(
array: list[float], default: float = (np.pi / 2) # pylint: disable=superfluous-parens
) -> float:
if len(array) == 0:
return default
return np.max(array)
def _compute_fisher_information(
result: "MaximumLikelihoodAmplitudeEstimationResult",
num_sum_terms: int | None = None,
observed: bool = False,
) -> float:
"""Compute the Fisher information.
Args:
result: A maximum likelihood amplitude estimation result.
num_sum_terms: The number of sum terms to be included in the calculation of the
Fisher information. By default all values are included.
observed: If True, compute the observed Fisher information, otherwise the theoretical
one.
Returns:
The computed Fisher information, or np.inf if statevector simulation was used.
Raises:
KeyError: Call run() first!
"""
a = result.estimation
# Corresponding angle to the value a (only use real part of 'a')
theta_a = np.arcsin(np.sqrt(np.real(a)))
# Get the number of hits (shots_k) and one-hits (h_k)
one_hits = result.good_counts
all_hits = [result.shots] * len(one_hits)
# Include all sum terms or just up to a certain term?
evaluation_schedule = result.evaluation_schedule
if num_sum_terms is not None:
evaluation_schedule = evaluation_schedule[:num_sum_terms]
# not necessary since zip goes as far as shortest list:
# all_hits = all_hits[:num_sum_terms]
# one_hits = one_hits[:num_sum_terms]
# Compute the Fisher information
fisher_information = None
if observed:
# Note, that the observed Fisher information is very unreliable in this algorithm!
d_loglik = 0
for shots_k, h_k, m_k in zip(all_hits, one_hits, evaluation_schedule):
tan = np.tan((2 * m_k + 1) * theta_a)
d_loglik += (2 * m_k + 1) * (h_k / tan + (shots_k - h_k) * tan)
d_loglik /= np.sqrt(a * (1 - a))
fisher_information = d_loglik**2 / len(all_hits)
else:
fisher_information = sum(
shots_k * (2 * m_k + 1) ** 2 for shots_k, m_k in zip(all_hits, evaluation_schedule)
)
fisher_information /= a * (1 - a)
return fisher_information
def _fisher_confint(
result: MaximumLikelihoodAmplitudeEstimationResult, alpha: float = 0.05, observed: bool = False
) -> tuple[float, float]:
"""Compute the `alpha` confidence interval based on the Fisher information.
Args:
result: A maximum likelihood amplitude estimation results object.
alpha: The level of the confidence interval (must be <= 0.5), default to 0.05.
observed: If True, use observed Fisher information.
Returns:
float: The alpha confidence interval based on the Fisher information
Raises:
AssertionError: Call run() first!
"""
# Get the (observed) Fisher information
fisher_information = None
try:
fisher_information = result.fisher_information
except KeyError as ex:
raise AssertionError("Call run() first!") from ex
if observed:
fisher_information = _compute_fisher_information(result, observed=True)
normal_quantile = norm.ppf(1 - alpha / 2)
confint = np.real(result.estimation) + normal_quantile / np.sqrt(fisher_information) * np.array(
[-1, 1]
)
return result.post_processing(confint[0]), result.post_processing(confint[1])
def _likelihood_ratio_confint(
result: MaximumLikelihoodAmplitudeEstimationResult,
alpha: float = 0.05,
nevals: int | None = None,
) -> tuple[float, float]:
"""Compute the likelihood-ratio confidence interval.
Args:
result: A maximum likelihood amplitude estimation results object.
alpha: The level of the confidence interval (< 0.5), defaults to 0.05.
nevals: The number of evaluations to find the intersection with the loglikelihood
function. Defaults to an adaptive value based on the maximal power of Q.
Returns:
The alpha-likelihood-ratio confidence interval.
"""
if nevals is None:
nevals = max(10000, int(np.pi / 2 * 1000 * 2 * result.evaluation_schedule[-1]))
def loglikelihood(theta, one_counts, all_counts):
loglik = 0
for i, k in enumerate(result.evaluation_schedule):
loglik += np.log(np.sin((2 * k + 1) * theta) ** 2) * one_counts[i]
loglik += np.log(np.cos((2 * k + 1) * theta) ** 2) * (all_counts[i] - one_counts[i])
return loglik
one_counts = result.good_counts
all_counts = [result.shots] * len(one_counts)
eps = 1e-15 # to avoid invalid value in log
thetas = np.linspace(0 + eps, np.pi / 2 - eps, nevals)
values = np.zeros(len(thetas))
for i, theta in enumerate(thetas):
values[i] = loglikelihood(theta, one_counts, all_counts)
loglik_mle = loglikelihood(result.theta, one_counts, all_counts)
chi2_quantile = chi2.ppf(1 - alpha, df=1)
thres = loglik_mle - chi2_quantile / 2
# the (outer) LR confidence interval
above_thres = thetas[values >= thres]
# it might happen that the `above_thres` array is empty,
# to still provide a valid result use safe_min/max which
# then yield [0, pi/2]
confint = [_safe_min(above_thres, default=0), _safe_max(above_thres, default=np.pi / 2)]
mapped_confint = cast(
Tuple[float, float], tuple(result.post_processing(np.sin(bound) ** 2) for bound in confint)
)
return mapped_confint
def _get_counts(
circuit_results: Sequence[dict[str, int]], estimation_problem: EstimationProblem
) -> tuple[list[int], list[int]]:
"""Get the good and total counts.
Returns:
A pair of two lists, ([1-counts per experiment], [shots per experiment]).
Raises:
AlgorithmError: If self.run() has not been called yet.
"""
one_hits = [] # h_k: how often 1 has been measured, for a power Q^(m_k)
all_hits = []
for counts in circuit_results:
all_hits.append(sum(counts.values()))
one_hits.append(
sum(
count
for bitstr, count in counts.items()
if estimation_problem.is_good_state(bitstr)
)
)
return one_hits, all_hits
|