File size: 13,899 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 |
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020, 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.
"""DictStateFn Class"""
import itertools
from typing import Dict, List, Optional, Set, Union, cast
import numpy as np
from scipy import sparse
from qiskit.circuit import ParameterExpression
from qiskit.opflow.exceptions import OpflowError
from qiskit.opflow.list_ops.list_op import ListOp
from qiskit.opflow.operator_base import OperatorBase
from qiskit.opflow.state_fns.state_fn import StateFn
from qiskit.opflow.state_fns.vector_state_fn import VectorStateFn
from qiskit.quantum_info import Statevector
from qiskit.result import Result
from qiskit.utils import algorithm_globals
from qiskit.utils.deprecation import deprecate_func
class DictStateFn(StateFn):
"""Deprecated: A class for state functions and measurements which are defined by a lookup table,
stored in a dict.
"""
primitive: Dict[str, complex]
# TODO allow normalization somehow?
@deprecate_func(
since="0.24.0",
additional_msg="For code migration guidelines, visit https://qisk.it/opflow_migration.",
)
def __init__(
self,
primitive: Union[str, dict, Result] = None,
coeff: Union[complex, ParameterExpression] = 1.0,
is_measurement: bool = False,
from_operator: bool = False,
) -> None:
"""
Args:
primitive: The dict, single bitstring (if defining a basis sate), or Qiskit
Result, which defines the behavior of the underlying function.
coeff: A coefficient by which to multiply the state function.
is_measurement: Whether the StateFn is a measurement operator.
from_operator: if True the StateFn is derived from OperatorStateFn. (Default: False)
Raises:
TypeError: invalid parameters.
"""
# If the initial density is a string, treat this as a density dict
# with only a single basis state.
if isinstance(primitive, str):
primitive = {primitive: 1}
# NOTE:
# 1) This is not the same as passing in the counts dict directly, as this will
# convert the shot numbers to
# probabilities, whereas passing in the counts dict will not.
# 2) This will extract counts for both shot and statevector simulations.
# To use the statevector,
# simply pass in the statevector.
# 3) This will only extract the first result.
if isinstance(primitive, Result):
counts = primitive.get_counts()
# NOTE: Need to square root to take correct Pauli measurements!
primitive = {
bstr: (shots / sum(counts.values())) ** 0.5 for (bstr, shots) in counts.items()
}
if not isinstance(primitive, dict):
raise TypeError(
"DictStateFn can only be instantiated with dict, "
"string, or Qiskit Result, not {}".format(type(primitive))
)
super().__init__(primitive, coeff=coeff, is_measurement=is_measurement)
self.from_operator = from_operator
def primitive_strings(self) -> Set[str]:
return {"Dict"}
@property
def num_qubits(self) -> int:
return len(next(iter(self.primitive)))
@property
def settings(self) -> Dict:
"""Return settings."""
data = super().settings
data["from_operator"] = self.from_operator
return data
def add(self, other: OperatorBase) -> OperatorBase:
if not self.num_qubits == other.num_qubits:
raise ValueError(
"Sum over statefns with different numbers of qubits, {} and {}, is not well "
"defined".format(self.num_qubits, other.num_qubits)
)
# Right now doesn't make sense to add a StateFn to a Measurement
if isinstance(other, DictStateFn) and self.is_measurement == other.is_measurement:
# TODO add compatibility with vector and Operator?
if self.primitive == other.primitive:
return DictStateFn(
self.primitive,
coeff=self.coeff + other.coeff,
is_measurement=self.is_measurement,
)
else:
new_dict = {
b: (v * self.coeff) + (other.primitive.get(b, 0) * other.coeff)
for (b, v) in self.primitive.items()
}
new_dict.update(
{
b: v * other.coeff
for (b, v) in other.primitive.items()
if b not in self.primitive
}
)
return DictStateFn(new_dict, is_measurement=self._is_measurement)
# pylint: disable=cyclic-import
from ..list_ops.summed_op import SummedOp
return SummedOp([self, other])
def adjoint(self) -> "DictStateFn":
return DictStateFn(
{b: np.conj(v) for (b, v) in self.primitive.items()},
coeff=self.coeff.conjugate(),
is_measurement=(not self.is_measurement),
)
def permute(self, permutation: List[int]) -> "DictStateFn":
new_num_qubits = max(permutation) + 1
if self.num_qubits != len(permutation):
raise OpflowError("New index must be defined for each qubit of the operator.")
# helper function to permute the key
def perm(key):
list_key = ["0"] * new_num_qubits
for i, k in enumerate(permutation):
list_key[k] = key[i]
return "".join(list_key)
new_dict = {perm(key): value for key, value in self.primitive.items()}
return DictStateFn(new_dict, coeff=self.coeff, is_measurement=self.is_measurement)
def _expand_dim(self, num_qubits: int) -> "DictStateFn":
pad = "0" * num_qubits
new_dict = {key + pad: value for key, value in self.primitive.items()}
return DictStateFn(new_dict, coeff=self.coeff, is_measurement=self.is_measurement)
def tensor(self, other: OperatorBase) -> OperatorBase:
# Both dicts
if isinstance(other, DictStateFn):
new_dict = {
k1 + k2: v1 * v2
for (
(
k1,
v1,
),
(k2, v2),
) in itertools.product(self.primitive.items(), other.primitive.items())
}
return StateFn(
new_dict, coeff=self.coeff * other.coeff, is_measurement=self.is_measurement
)
# pylint: disable=cyclic-import
from ..list_ops.tensored_op import TensoredOp
return TensoredOp([self, other])
def to_density_matrix(self, massive: bool = False) -> np.ndarray:
OperatorBase._check_massive("to_density_matrix", True, self.num_qubits, massive)
states = int(2**self.num_qubits)
return self.to_matrix(massive=massive) * np.eye(states) * self.coeff
def to_matrix(self, massive: bool = False) -> np.ndarray:
OperatorBase._check_massive("to_matrix", False, self.num_qubits, massive)
states = int(2**self.num_qubits)
probs = np.zeros(states) + 0.0j
for k, v in self.primitive.items():
probs[int(k, 2)] = v
vec = probs * self.coeff
# Reshape for measurements so np.dot still works for composition.
return vec if not self.is_measurement else vec.reshape(1, -1)
def to_spmatrix(self) -> sparse.spmatrix:
"""Same as to_matrix, but returns csr sparse matrix.
Returns:
CSR sparse matrix representation of the State function.
Raises:
ValueError: invalid parameters.
"""
indices = [int(v, 2) for v in self.primitive.keys()]
vals = np.array(list(self.primitive.values())) * self.coeff
spvec = sparse.csr_matrix(
(vals, (np.zeros(len(indices), dtype=int), indices)), shape=(1, 2**self.num_qubits)
)
return spvec if not self.is_measurement else spvec.transpose()
def to_spmatrix_op(self) -> OperatorBase:
"""Convert this state function to a ``SparseVectorStateFn``."""
from .sparse_vector_state_fn import SparseVectorStateFn
return SparseVectorStateFn(self.to_spmatrix(), self.coeff, self.is_measurement)
def to_circuit_op(self) -> OperatorBase:
"""Convert this state function to a ``CircuitStateFn``."""
from .circuit_state_fn import CircuitStateFn
csfn = CircuitStateFn.from_dict(self.primitive) * self.coeff
return csfn.adjoint() if self.is_measurement else csfn
def __str__(self) -> str:
prim_str = str(self.primitive)
if self.coeff == 1.0:
return "{}({})".format(
"DictStateFn" if not self.is_measurement else "DictMeasurement", prim_str
)
else:
return "{}({}) * {}".format(
"DictStateFn" if not self.is_measurement else "DictMeasurement",
prim_str,
self.coeff,
)
# pylint: disable=too-many-return-statements
def eval(
self,
front: Optional[
Union[str, Dict[str, complex], np.ndarray, OperatorBase, Statevector]
] = None,
) -> Union[OperatorBase, complex]:
if front is None:
sparse_vector_state_fn = self.to_spmatrix_op().eval()
return sparse_vector_state_fn
if not self.is_measurement and isinstance(front, OperatorBase):
raise ValueError(
"Cannot compute overlap with StateFn or Operator if not Measurement. Try taking "
"sf.adjoint() first to convert to measurement."
)
if isinstance(front, ListOp) and front.distributive:
return front.combo_fn(
[self.eval(front.coeff * front_elem) for front_elem in front.oplist]
)
# For now, always do this. If it's not performant, we can be more granular.
if not isinstance(front, OperatorBase):
front = StateFn(front)
# pylint: disable=cyclic-import
from ..operator_globals import EVAL_SIG_DIGITS
# If the primitive is a lookup of bitstrings,
# we define all missing strings to have a function value of
# zero.
if isinstance(front, DictStateFn):
# If self is come from operator, it should be expanded as
# <self|front> = <front| self | front>.
front_coeff = (
front.coeff * front.coeff.conjugate() if self.from_operator else front.coeff
)
return np.round(
cast(
float,
sum(v * front.primitive.get(b, 0) for (b, v) in self.primitive.items())
* self.coeff
* front_coeff,
),
decimals=EVAL_SIG_DIGITS,
)
# All remaining possibilities only apply when self.is_measurement is True
if isinstance(front, VectorStateFn):
# TODO does it need to be this way for measurement?
# return sum([v * front.primitive.data[int(b, 2)] *
# np.conj(front.primitive.data[int(b, 2)])
return np.round(
cast(
float,
sum(v * front.primitive.data[int(b, 2)] for (b, v) in self.primitive.items())
* self.coeff,
),
decimals=EVAL_SIG_DIGITS,
)
from .circuit_state_fn import CircuitStateFn
if isinstance(front, CircuitStateFn):
# Don't reimplement logic from CircuitStateFn
self_adjoint = cast(DictStateFn, self.adjoint())
return np.conj(front.adjoint().eval(self_adjoint.primitive)) * self.coeff
from .operator_state_fn import OperatorStateFn
if isinstance(front, OperatorStateFn):
return cast(Union[OperatorBase, complex], front.adjoint().eval(self.adjoint()))
# All other OperatorBases go here
self_adjoint = cast(DictStateFn, self.adjoint())
adjointed_eval = cast(OperatorBase, front.adjoint().eval(self_adjoint.primitive))
return adjointed_eval.adjoint() * self.coeff
def sample(
self, shots: int = 1024, massive: bool = False, reverse_endianness: bool = False
) -> Dict[str, float]:
probs = np.square(np.abs(np.array(list(self.primitive.values()))))
unique, counts = np.unique(
algorithm_globals.random.choice(
list(self.primitive.keys()), size=shots, p=(probs / sum(probs))
),
return_counts=True,
)
counts = dict(zip(unique, counts))
if reverse_endianness:
scaled_dict = {bstr[::-1]: (prob / shots) for (bstr, prob) in counts.items()}
else:
scaled_dict = {bstr: (prob / shots) for (bstr, prob) in counts.items()}
return dict(sorted(scaled_dict.items(), key=lambda x: x[1], reverse=True))
|