/
simulation_state.py
349 lines (292 loc) · 12.3 KB
/
simulation_state.py
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
# Copyright 2021 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Objects and methods for acting efficiently on a state tensor."""
import abc
import copy
from typing import (
Any,
cast,
Dict,
Generic,
Iterator,
List,
Optional,
Sequence,
Set,
TypeVar,
TYPE_CHECKING,
Tuple,
)
from typing_extensions import Self
import numpy as np
from cirq import ops, protocols, value
from cirq.sim.simulation_state_base import SimulationStateBase
TState = TypeVar('TState', bound='cirq.QuantumStateRepresentation')
if TYPE_CHECKING:
import cirq
class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMeta):
"""State and context for an operation acting on a state tensor."""
def __init__(
self,
*,
state: TState,
prng: Optional[np.random.RandomState] = None,
qubits: Optional[Sequence['cirq.Qid']] = None,
classical_data: Optional['cirq.ClassicalDataStore'] = None,
):
"""Inits SimulationState.
Args:
prng: The pseudo random number generator to use for probabilistic
effects.
qubits: Determines the canonical ordering of the qubits. This
is often used in specifying the initial state, i.e. the
ordering of the computational basis states.
classical_data: The shared classical data container for this
simulation.
state: The underlying quantum state of the simulation.
"""
if qubits is None:
qubits = ()
classical_data = classical_data or value.ClassicalDataDictionaryStore()
super().__init__(qubits=qubits, classical_data=classical_data)
if prng is None:
prng = cast(np.random.RandomState, np.random)
self._prng = prng
self._state = state
@property
def prng(self) -> np.random.RandomState:
return self._prng
def measure(
self,
qubits: Sequence['cirq.Qid'],
key: str,
invert_mask: Sequence[bool],
confusion_map: Dict[Tuple[int, ...], np.ndarray],
):
"""Measures the qubits and records to `log_of_measurement_results`.
Any bitmasks will be applied to the measurement record.
Args:
qubits: The qubits to measure.
key: The key the measurement result should be logged under. Note
that operations should only store results under keys they have
declared in a `_measurement_key_names_` method.
invert_mask: The invert mask for the measurement.
confusion_map: The confusion matrices for the measurement.
Raises:
ValueError: If a measurement key has already been logged to a key.
"""
bits = self._perform_measurement(qubits)
confused = self._confuse_result(bits, qubits, confusion_map)
corrected = [bit ^ (bit < 2 and mask) for bit, mask in zip(confused, invert_mask)]
self._classical_data.record_measurement(
value.MeasurementKey.parse_serialized(key), corrected, qubits
)
def get_axes(self, qubits: Sequence['cirq.Qid']) -> List[int]:
return [self.qubit_map[q] for q in qubits]
def _perform_measurement(self, qubits: Sequence['cirq.Qid']) -> List[int]:
"""Delegates the call to measure the density matrix."""
if self._state is not None:
return self._state.measure(self.get_axes(qubits), self.prng)
raise NotImplementedError()
def _confuse_result(
self,
bits: List[int],
qubits: Sequence['cirq.Qid'],
confusion_map: Dict[Tuple[int, ...], np.ndarray],
):
"""Applies confusion matrices to measured results.
Compare with _confuse_results in cirq-core/cirq/sim/simulator.py.
"""
confused = list(bits)
dims = [q.dimension for q in qubits]
for indices, confuser in confusion_map.items():
mat_dims = [dims[k] for k in indices]
row = value.big_endian_digits_to_int((bits[k] for k in indices), base=mat_dims)
new_val = self.prng.choice(len(confuser), p=confuser[row])
new_bits = value.big_endian_int_to_digits(new_val, base=mat_dims)
for i, k in enumerate(indices):
confused[k] = new_bits[i]
return confused
def sample(
self,
qubits: Sequence['cirq.Qid'],
repetitions: int = 1,
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
) -> np.ndarray:
if self._state is not None:
return self._state.sample(self.get_axes(qubits), repetitions, seed)
raise NotImplementedError()
def copy(self, deep_copy_buffers: bool = True) -> Self:
"""Creates a copy of the object.
Args:
deep_copy_buffers: If True, buffers will also be deep-copied.
Otherwise the copy will share a reference to the original object's
buffers.
Returns:
A copied instance.
"""
args = copy.copy(self)
args._classical_data = self._classical_data.copy()
args._state = self._state.copy(deep_copy_buffers=deep_copy_buffers)
return args
def create_merged_state(self) -> Self:
"""Creates a final merged state."""
return self
def add_qubits(self: Self, qubits: Sequence['cirq.Qid']) -> Self:
"""Add `qubits` in the `|0>` state to a new state space and take the kron product.
Args:
qubits: Sequence of qubits to be added.
Returns:
NotImplemented: If the subclass does not implement this method.
Self: A `cirq.SimulationState` with qubits added or `self` if there are no qubits to
add.
"""
if not qubits:
return self
return NotImplemented
def remove_qubits(self: Self, qubits: Sequence['cirq.Qid']) -> Self:
"""Remove `qubits` from the state space.
The qubits to be removed should be untangled from rest of the system and in the |0> state.
Args:
qubits: Sequence of qubits to be removed.
Returns:
NotImplemented: If the subclass does not implement this method.
Self: A `cirq.SimulationState` with qubits removed or `self` if there are no qubits to
remove.
"""
if not qubits:
return self
return NotImplemented
def kronecker_product(self, other: Self, *, inplace=False) -> Self:
"""Joins two state spaces together."""
args = self if inplace else copy.copy(self)
args._state = self._state.kron(other._state)
args._set_qubits(self.qubits + other.qubits)
return args
def factor(
self, qubits: Sequence['cirq.Qid'], *, validate=True, atol=1e-07, inplace=False
) -> Tuple[Self, Self]:
"""Splits two state spaces after a measurement or reset."""
extracted = copy.copy(self)
remainder = self if inplace else copy.copy(self)
e, r = self._state.factor(self.get_axes(qubits), validate=validate, atol=atol)
extracted._state = e
remainder._state = r
extracted._set_qubits(qubits)
remainder._set_qubits([q for q in self.qubits if q not in qubits])
return extracted, remainder
@property
def allows_factoring(self):
"""Subclasses that allow factorization should override this."""
return self._state.supports_factor if self._state is not None else False
def transpose_to_qubit_order(self, qubits: Sequence['cirq.Qid'], *, inplace=False) -> Self:
"""Physically reindexes the state by the new basis.
Args:
qubits: The desired qubit order.
inplace: True to perform this operation inplace.
Returns:
The state with qubit order transposed and underlying representation
updated.
Raises:
ValueError: If the provided qubits do not match the existing ones.
"""
if len(self.qubits) != len(qubits) or set(qubits) != set(self.qubits):
raise ValueError(f'Qubits do not match. Existing: {self.qubits}, provided: {qubits}')
args = self if inplace else copy.copy(self)
args._state = self._state.reindex(self.get_axes(qubits))
args._set_qubits(qubits)
return args
@property
def qubits(self) -> Tuple['cirq.Qid', ...]:
return self._qubits
def swap(self, q1: 'cirq.Qid', q2: 'cirq.Qid', *, inplace=False):
"""Swaps two qubits.
This only affects the index, and does not modify the underlying
state.
Args:
q1: The first qubit to swap.
q2: The second qubit to swap.
inplace: True to swap the qubits in the current object, False to
create a copy with the qubits swapped.
Returns:
The original object with the qubits swapped if inplace is
requested, or a copy of the original object with the qubits swapped
otherwise.
Raises:
ValueError: If the qubits are of different dimensionality.
"""
if q1.dimension != q2.dimension:
raise ValueError(f'Cannot swap different dimensions: q1={q1}, q2={q2}')
args = self if inplace else copy.copy(self)
i1 = self.qubits.index(q1)
i2 = self.qubits.index(q2)
qubits = list(args.qubits)
qubits[i1], qubits[i2] = qubits[i2], qubits[i1]
args._set_qubits(qubits)
return args
def rename(self, q1: 'cirq.Qid', q2: 'cirq.Qid', *, inplace=False):
"""Renames `q1` to `q2`.
Args:
q1: The qubit to rename.
q2: The new name.
inplace: True to rename the qubit in the current object, False to
create a copy with the qubit renamed.
Returns:
The original object with the qubits renamed if inplace is
requested, or a copy of the original object with the qubits renamed
otherwise.
Raises:
ValueError: If the qubits are of different dimensionality.
"""
if q1.dimension != q2.dimension:
raise ValueError(f'Cannot rename to different dimensions: q1={q1}, q2={q2}')
args = self if inplace else copy.copy(self)
i1 = self.qubits.index(q1)
qubits = list(args.qubits)
qubits[i1] = q2
args._set_qubits(qubits)
return args
def __getitem__(self, item: Optional['cirq.Qid']) -> Self:
if item not in self.qubit_map:
raise IndexError(f'{item} not in {self.qubits}')
return self
def __len__(self) -> int:
return len(self.qubits)
def __iter__(self) -> Iterator[Optional['cirq.Qid']]:
return iter(self.qubits)
@property
def can_represent_mixed_states(self) -> bool:
return self._state.can_represent_mixed_states if self._state is not None else False
def strat_act_on_from_apply_decompose(
val: Any, args: 'cirq.SimulationState', qubits: Sequence['cirq.Qid']
) -> bool:
if isinstance(val, ops.Gate):
decomposed = protocols.decompose_once_with_qubits(val, qubits, flatten=False, default=None)
else:
decomposed = protocols.decompose_once(val, flatten=False, default=None)
if decomposed is None:
return NotImplemented
all_ancilla: Set['cirq.Qid'] = set()
for operation in ops.flatten_to_ops(decomposed):
curr_ancilla = tuple(q for q in operation.qubits if q not in args.qubits)
args = args.add_qubits(curr_ancilla)
if args is NotImplemented:
return NotImplemented
all_ancilla.update(curr_ancilla)
protocols.act_on(operation, args)
args = args.remove_qubits(tuple(all_ancilla))
if args is NotImplemented:
raise TypeError(f"{type(args)} implements add_qubits but not remove_qubits.")
return True
TSimulationState = TypeVar('TSimulationState', bound=SimulationState)