cirq-core 1.6.0.dev20250425004112__py3-none-any.whl → 1.6.0.dev20250429004516__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cirq-core might be problematic. Click here for more details.
- cirq/_version.py +1 -1
- cirq/_version_test.py +1 -1
- cirq/protocols/decompose_protocol.py +3 -2
- cirq/protocols/measurement_key_protocol.py +8 -7
- cirq/protocols/pow_protocol.py +7 -7
- cirq/protocols/qasm.py +5 -3
- cirq/protocols/resolve_parameters.py +5 -3
- cirq/qis/clifford_tableau.py +11 -9
- cirq/qis/measures.py +7 -7
- cirq/qis/quantum_state_representation.py +4 -5
- cirq/qis/states.py +19 -17
- cirq/sim/classical_simulator.py +15 -14
- cirq/sim/clifford/clifford_simulator.py +19 -17
- cirq/sim/clifford/clifford_tableau_simulation_state.py +7 -4
- cirq/sim/clifford/stabilizer_ch_form_simulation_state.py +5 -3
- cirq/sim/clifford/stabilizer_sampler.py +6 -4
- cirq/sim/clifford/stabilizer_simulation_state.py +9 -9
- cirq/sim/clifford/stabilizer_state_ch_form.py +6 -4
- cirq/sim/density_matrix_simulation_state.py +17 -18
- cirq/sim/density_matrix_simulator.py +21 -19
- cirq/sim/density_matrix_utils.py +4 -2
- cirq/sim/mux.py +25 -23
- cirq/sim/simulation_product_state.py +12 -12
- cirq/sim/simulation_product_state_test.py +3 -3
- cirq/sim/simulation_state.py +23 -19
- cirq/sim/simulation_state_base.py +16 -12
- cirq/sim/simulation_state_test.py +1 -1
- cirq/sim/simulator.py +71 -72
- cirq/sim/simulator_base.py +22 -23
- cirq/sim/simulator_base_test.py +12 -9
- cirq/sim/simulator_test.py +12 -8
- cirq/sim/sparse_simulator.py +13 -11
- cirq/sim/state_vector.py +9 -6
- cirq/sim/state_vector_simulation_state.py +20 -20
- cirq/sim/state_vector_simulator.py +13 -10
- cirq/study/flatten_expressions.py +8 -5
- cirq/study/resolver.py +12 -9
- cirq/study/result.py +6 -3
- cirq/study/sweeps.py +17 -14
- cirq/testing/consistent_act_on_test.py +2 -2
- cirq/testing/consistent_controlled_gate_op_test.py +10 -6
- cirq/testing/lin_alg_utils.py +8 -7
- cirq/testing/random_circuit.py +17 -16
- cirq/testing/routing_devices.py +4 -1
- cirq/testing/sample_circuits.py +4 -1
- cirq/transformers/align.py +6 -4
- cirq/transformers/analytical_decompositions/clifford_decomposition.py +9 -7
- cirq/transformers/analytical_decompositions/controlled_gate_decomposition.py +15 -13
- cirq/transformers/analytical_decompositions/cphase_to_fsim.py +7 -5
- cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +10 -8
- cirq/transformers/analytical_decompositions/single_to_two_qubit_isometry.py +6 -3
- cirq/transformers/analytical_decompositions/two_qubit_state_preparation.py +8 -10
- {cirq_core-1.6.0.dev20250425004112.dist-info → cirq_core-1.6.0.dev20250429004516.dist-info}/METADATA +1 -1
- {cirq_core-1.6.0.dev20250425004112.dist-info → cirq_core-1.6.0.dev20250429004516.dist-info}/RECORD +57 -57
- {cirq_core-1.6.0.dev20250425004112.dist-info → cirq_core-1.6.0.dev20250429004516.dist-info}/WHEEL +1 -1
- {cirq_core-1.6.0.dev20250425004112.dist-info → cirq_core-1.6.0.dev20250429004516.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.6.0.dev20250425004112.dist-info → cirq_core-1.6.0.dev20250429004516.dist-info}/top_level.txt +0 -0
cirq/sim/mux.py
CHANGED
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
Filename is a reference to multiplexing.
|
|
18
18
|
"""
|
|
19
19
|
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
20
22
|
from typing import List, Optional, Sequence, Type, TYPE_CHECKING, Union
|
|
21
23
|
|
|
22
24
|
import numpy as np
|
|
@@ -39,7 +41,7 @@ document(
|
|
|
39
41
|
)
|
|
40
42
|
|
|
41
43
|
|
|
42
|
-
def _is_clifford_circuit(program:
|
|
44
|
+
def _is_clifford_circuit(program: cirq.Circuit) -> bool:
|
|
43
45
|
return all(
|
|
44
46
|
clifford_simulator.CliffordSimulator.is_supported_operation(op)
|
|
45
47
|
for op in program.all_operations()
|
|
@@ -47,14 +49,14 @@ def _is_clifford_circuit(program: 'cirq.Circuit') -> bool:
|
|
|
47
49
|
|
|
48
50
|
|
|
49
51
|
def sample(
|
|
50
|
-
program:
|
|
52
|
+
program: cirq.Circuit,
|
|
51
53
|
*,
|
|
52
|
-
noise:
|
|
53
|
-
param_resolver: Optional[
|
|
54
|
+
noise: cirq.NOISE_MODEL_LIKE = None,
|
|
55
|
+
param_resolver: Optional[cirq.ParamResolver] = None,
|
|
54
56
|
repetitions: int = 1,
|
|
55
57
|
dtype: Type[np.complexfloating] = np.complex64,
|
|
56
|
-
seed:
|
|
57
|
-
) ->
|
|
58
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
59
|
+
) -> cirq.Result:
|
|
58
60
|
"""Simulates sampling from the given circuit.
|
|
59
61
|
|
|
60
62
|
Args:
|
|
@@ -90,7 +92,7 @@ def sample(
|
|
|
90
92
|
).run(program=program, param_resolver=param_resolver, repetitions=repetitions)
|
|
91
93
|
|
|
92
94
|
|
|
93
|
-
def _to_circuit(program:
|
|
95
|
+
def _to_circuit(program: cirq.CIRCUIT_LIKE) -> cirq.Circuit:
|
|
94
96
|
if isinstance(program, circuits.Circuit):
|
|
95
97
|
# No change needed.
|
|
96
98
|
result = program
|
|
@@ -103,14 +105,14 @@ def _to_circuit(program: 'cirq.CIRCUIT_LIKE') -> 'cirq.Circuit':
|
|
|
103
105
|
|
|
104
106
|
|
|
105
107
|
def final_state_vector(
|
|
106
|
-
program:
|
|
108
|
+
program: cirq.CIRCUIT_LIKE,
|
|
107
109
|
*,
|
|
108
|
-
initial_state:
|
|
109
|
-
param_resolver:
|
|
110
|
-
qubit_order:
|
|
110
|
+
initial_state: cirq.STATE_VECTOR_LIKE = 0,
|
|
111
|
+
param_resolver: cirq.ParamResolverOrSimilarType = None,
|
|
112
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
111
113
|
ignore_terminal_measurements: bool = False,
|
|
112
114
|
dtype: Type[np.complexfloating] = np.complex64,
|
|
113
|
-
seed:
|
|
115
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
114
116
|
) -> np.ndarray:
|
|
115
117
|
"""Returns the state vector resulting from acting operations on a state.
|
|
116
118
|
|
|
@@ -174,14 +176,14 @@ def final_state_vector(
|
|
|
174
176
|
|
|
175
177
|
|
|
176
178
|
def sample_sweep(
|
|
177
|
-
program:
|
|
178
|
-
params:
|
|
179
|
+
program: cirq.Circuit,
|
|
180
|
+
params: cirq.Sweepable,
|
|
179
181
|
*,
|
|
180
|
-
noise:
|
|
182
|
+
noise: cirq.NOISE_MODEL_LIKE = None,
|
|
181
183
|
repetitions: int = 1,
|
|
182
184
|
dtype: Type[np.complexfloating] = np.complex64,
|
|
183
|
-
seed:
|
|
184
|
-
) -> Sequence[
|
|
185
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
186
|
+
) -> Sequence[cirq.Result]:
|
|
185
187
|
"""Runs the supplied Circuit, mimicking quantum hardware.
|
|
186
188
|
|
|
187
189
|
In contrast to run, this allows for sweeping over different parameter
|
|
@@ -219,16 +221,16 @@ def sample_sweep(
|
|
|
219
221
|
|
|
220
222
|
|
|
221
223
|
def final_density_matrix(
|
|
222
|
-
program:
|
|
224
|
+
program: cirq.CIRCUIT_LIKE,
|
|
223
225
|
*,
|
|
224
|
-
noise:
|
|
225
|
-
initial_state:
|
|
226
|
-
param_resolver:
|
|
227
|
-
qubit_order:
|
|
226
|
+
noise: cirq.NOISE_MODEL_LIKE = None,
|
|
227
|
+
initial_state: cirq.STATE_VECTOR_LIKE = 0,
|
|
228
|
+
param_resolver: cirq.ParamResolverOrSimilarType = None,
|
|
229
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
228
230
|
dtype: Type[np.complexfloating] = np.complex64,
|
|
229
231
|
seed: Optional[Union[int, np.random.RandomState]] = None,
|
|
230
232
|
ignore_measurement_results: bool = True,
|
|
231
|
-
) ->
|
|
233
|
+
) -> np.ndarray:
|
|
232
234
|
"""Returns the density matrix resulting from simulating the circuit.
|
|
233
235
|
|
|
234
236
|
Note that, unlike `cirq.final_state_vector`, terminal measurements
|
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
15
17
|
from collections import abc
|
|
16
18
|
from typing import Any, Dict, Generic, Iterator, List, Mapping, Optional, Sequence, TYPE_CHECKING
|
|
17
19
|
|
|
@@ -32,10 +34,10 @@ class SimulationProductState(
|
|
|
32
34
|
|
|
33
35
|
def __init__(
|
|
34
36
|
self,
|
|
35
|
-
sim_states: Dict[Optional[
|
|
36
|
-
qubits: Sequence[
|
|
37
|
+
sim_states: Dict[Optional[cirq.Qid], TSimulationState],
|
|
38
|
+
qubits: Sequence[cirq.Qid],
|
|
37
39
|
split_untangled_states: bool,
|
|
38
|
-
classical_data: Optional[
|
|
40
|
+
classical_data: Optional[cirq.ClassicalDataStore] = None,
|
|
39
41
|
):
|
|
40
42
|
"""Initializes the class.
|
|
41
43
|
|
|
@@ -55,7 +57,7 @@ class SimulationProductState(
|
|
|
55
57
|
self._split_untangled_states = split_untangled_states
|
|
56
58
|
|
|
57
59
|
@property
|
|
58
|
-
def sim_states(self) -> Mapping[Optional[
|
|
60
|
+
def sim_states(self) -> Mapping[Optional[cirq.Qid], TSimulationState]:
|
|
59
61
|
return self._sim_states
|
|
60
62
|
|
|
61
63
|
@property
|
|
@@ -78,7 +80,7 @@ class SimulationProductState(
|
|
|
78
80
|
return merged_state.transpose_to_qubit_order(self.qubits, inplace=True)
|
|
79
81
|
|
|
80
82
|
def _act_on_fallback_(
|
|
81
|
-
self, action: Any, qubits: Sequence[
|
|
83
|
+
self, action: Any, qubits: Sequence[cirq.Qid], allow_decompose: bool = True
|
|
82
84
|
) -> bool:
|
|
83
85
|
gate_opt = (
|
|
84
86
|
action
|
|
@@ -136,9 +138,7 @@ class SimulationProductState(
|
|
|
136
138
|
self._sim_states[q] = op_args
|
|
137
139
|
return True
|
|
138
140
|
|
|
139
|
-
def copy(
|
|
140
|
-
self, deep_copy_buffers: bool = True
|
|
141
|
-
) -> 'cirq.SimulationProductState[TSimulationState]':
|
|
141
|
+
def copy(self, deep_copy_buffers: bool = True) -> cirq.SimulationProductState[TSimulationState]:
|
|
142
142
|
classical_data = self._classical_data.copy()
|
|
143
143
|
copies = {}
|
|
144
144
|
for sim_state in set(self.sim_states.values()):
|
|
@@ -152,9 +152,9 @@ class SimulationProductState(
|
|
|
152
152
|
|
|
153
153
|
def sample(
|
|
154
154
|
self,
|
|
155
|
-
qubits: List[
|
|
155
|
+
qubits: List[cirq.Qid],
|
|
156
156
|
repetitions: int = 1,
|
|
157
|
-
seed:
|
|
157
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
158
158
|
) -> np.ndarray:
|
|
159
159
|
columns = []
|
|
160
160
|
selected_order: List[ops.Qid] = []
|
|
@@ -170,11 +170,11 @@ class SimulationProductState(
|
|
|
170
170
|
index_order = [qubit_map[q] for q in qubits]
|
|
171
171
|
return stacked[:, index_order]
|
|
172
172
|
|
|
173
|
-
def __getitem__(self, item: Optional[
|
|
173
|
+
def __getitem__(self, item: Optional[cirq.Qid]) -> TSimulationState:
|
|
174
174
|
return self.sim_states[item]
|
|
175
175
|
|
|
176
176
|
def __len__(self) -> int:
|
|
177
177
|
return len(self.sim_states)
|
|
178
178
|
|
|
179
|
-
def __iter__(self) -> Iterator[Optional[
|
|
179
|
+
def __iter__(self) -> Iterator[Optional[cirq.Qid]]:
|
|
180
180
|
return iter(self.sim_states)
|
|
@@ -42,7 +42,7 @@ class EmptySimulationState(cirq.SimulationState):
|
|
|
42
42
|
super().__init__(state=EmptyQuantumState(), qubits=qubits, classical_data=classical_data)
|
|
43
43
|
|
|
44
44
|
def _act_on_fallback_(
|
|
45
|
-
self, action: Any, qubits: Sequence[
|
|
45
|
+
self, action: Any, qubits: Sequence[cirq.Qid], allow_decompose: bool = True
|
|
46
46
|
) -> bool:
|
|
47
47
|
return True
|
|
48
48
|
|
|
@@ -52,9 +52,9 @@ qs2 = cirq.LineQubit.range(2)
|
|
|
52
52
|
|
|
53
53
|
|
|
54
54
|
def create_container(
|
|
55
|
-
qubits: Sequence[
|
|
55
|
+
qubits: Sequence[cirq.Qid], split_untangled_states=True
|
|
56
56
|
) -> cirq.SimulationProductState[EmptySimulationState]:
|
|
57
|
-
state_map: Dict[Optional[
|
|
57
|
+
state_map: Dict[Optional[cirq.Qid], EmptySimulationState] = {}
|
|
58
58
|
log = cirq.ClassicalDataDictionaryStore()
|
|
59
59
|
if split_untangled_states:
|
|
60
60
|
for q in reversed(qubits):
|
cirq/sim/simulation_state.py
CHANGED
|
@@ -11,7 +11,11 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
+
|
|
14
15
|
"""Objects and methods for acting efficiently on a state tensor."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
15
19
|
import abc
|
|
16
20
|
import copy
|
|
17
21
|
from typing import (
|
|
@@ -49,8 +53,8 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
49
53
|
*,
|
|
50
54
|
state: TState,
|
|
51
55
|
prng: Optional[np.random.RandomState] = None,
|
|
52
|
-
qubits: Optional[Sequence[
|
|
53
|
-
classical_data: Optional[
|
|
56
|
+
qubits: Optional[Sequence[cirq.Qid]] = None,
|
|
57
|
+
classical_data: Optional[cirq.ClassicalDataStore] = None,
|
|
54
58
|
):
|
|
55
59
|
"""Inits SimulationState.
|
|
56
60
|
|
|
@@ -79,7 +83,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
79
83
|
|
|
80
84
|
def measure(
|
|
81
85
|
self,
|
|
82
|
-
qubits: Sequence[
|
|
86
|
+
qubits: Sequence[cirq.Qid],
|
|
83
87
|
key: str,
|
|
84
88
|
invert_mask: Sequence[bool],
|
|
85
89
|
confusion_map: Dict[Tuple[int, ...], np.ndarray],
|
|
@@ -106,10 +110,10 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
106
110
|
value.MeasurementKey.parse_serialized(key), corrected, qubits
|
|
107
111
|
)
|
|
108
112
|
|
|
109
|
-
def get_axes(self, qubits: Sequence[
|
|
113
|
+
def get_axes(self, qubits: Sequence[cirq.Qid]) -> List[int]:
|
|
110
114
|
return [self.qubit_map[q] for q in qubits]
|
|
111
115
|
|
|
112
|
-
def _perform_measurement(self, qubits: Sequence[
|
|
116
|
+
def _perform_measurement(self, qubits: Sequence[cirq.Qid]) -> List[int]:
|
|
113
117
|
"""Delegates the call to measure the `QuantumStateRepresentation`."""
|
|
114
118
|
if self._state is not None:
|
|
115
119
|
return self._state.measure(self.get_axes(qubits), self.prng)
|
|
@@ -118,7 +122,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
118
122
|
def _confuse_result(
|
|
119
123
|
self,
|
|
120
124
|
bits: List[int],
|
|
121
|
-
qubits: Sequence[
|
|
125
|
+
qubits: Sequence[cirq.Qid],
|
|
122
126
|
confusion_map: Dict[Tuple[int, ...], np.ndarray],
|
|
123
127
|
):
|
|
124
128
|
"""Applies confusion matrices to measured results.
|
|
@@ -138,9 +142,9 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
138
142
|
|
|
139
143
|
def sample(
|
|
140
144
|
self,
|
|
141
|
-
qubits: Sequence[
|
|
145
|
+
qubits: Sequence[cirq.Qid],
|
|
142
146
|
repetitions: int = 1,
|
|
143
|
-
seed:
|
|
147
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
144
148
|
) -> np.ndarray:
|
|
145
149
|
if self._state is not None:
|
|
146
150
|
return self._state.sample(self.get_axes(qubits), repetitions, seed)
|
|
@@ -166,7 +170,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
166
170
|
"""Creates a final merged state."""
|
|
167
171
|
return self
|
|
168
172
|
|
|
169
|
-
def add_qubits(self: Self, qubits: Sequence[
|
|
173
|
+
def add_qubits(self: Self, qubits: Sequence[cirq.Qid]) -> Self:
|
|
170
174
|
"""Add `qubits` in the `|0>` state to a new state space and take the kron product.
|
|
171
175
|
|
|
172
176
|
Args:
|
|
@@ -181,7 +185,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
181
185
|
return self
|
|
182
186
|
return NotImplemented
|
|
183
187
|
|
|
184
|
-
def remove_qubits(self: Self, qubits: Sequence[
|
|
188
|
+
def remove_qubits(self: Self, qubits: Sequence[cirq.Qid]) -> Self:
|
|
185
189
|
"""Remove `qubits` from the state space.
|
|
186
190
|
|
|
187
191
|
The qubits to be removed should be untangled from rest of the system and in the |0> state.
|
|
@@ -206,7 +210,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
206
210
|
return args
|
|
207
211
|
|
|
208
212
|
def factor(
|
|
209
|
-
self, qubits: Sequence[
|
|
213
|
+
self, qubits: Sequence[cirq.Qid], *, validate=True, atol=1e-07, inplace=False
|
|
210
214
|
) -> Tuple[Self, Self]:
|
|
211
215
|
"""Splits two state spaces after a measurement or reset."""
|
|
212
216
|
extracted = copy.copy(self)
|
|
@@ -223,7 +227,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
223
227
|
"""Subclasses that allow factorization should override this."""
|
|
224
228
|
return self._state.supports_factor if self._state is not None else False
|
|
225
229
|
|
|
226
|
-
def transpose_to_qubit_order(self, qubits: Sequence[
|
|
230
|
+
def transpose_to_qubit_order(self, qubits: Sequence[cirq.Qid], *, inplace=False) -> Self:
|
|
227
231
|
"""Physically reindexes the state by the new basis.
|
|
228
232
|
|
|
229
233
|
Args:
|
|
@@ -245,10 +249,10 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
245
249
|
return args
|
|
246
250
|
|
|
247
251
|
@property
|
|
248
|
-
def qubits(self) -> Tuple[
|
|
252
|
+
def qubits(self) -> Tuple[cirq.Qid, ...]:
|
|
249
253
|
return self._qubits
|
|
250
254
|
|
|
251
|
-
def swap(self, q1:
|
|
255
|
+
def swap(self, q1: cirq.Qid, q2: cirq.Qid, *, inplace=False):
|
|
252
256
|
"""Swaps two qubits.
|
|
253
257
|
|
|
254
258
|
This only affects the index, and does not modify the underlying
|
|
@@ -279,7 +283,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
279
283
|
args._set_qubits(qubits)
|
|
280
284
|
return args
|
|
281
285
|
|
|
282
|
-
def rename(self, q1:
|
|
286
|
+
def rename(self, q1: cirq.Qid, q2: cirq.Qid, *, inplace=False):
|
|
283
287
|
"""Renames `q1` to `q2`.
|
|
284
288
|
|
|
285
289
|
Args:
|
|
@@ -306,7 +310,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
306
310
|
args._set_qubits(qubits)
|
|
307
311
|
return args
|
|
308
312
|
|
|
309
|
-
def __getitem__(self, item: Optional[
|
|
313
|
+
def __getitem__(self, item: Optional[cirq.Qid]) -> Self:
|
|
310
314
|
if item not in self.qubit_map:
|
|
311
315
|
raise IndexError(f'{item} not in {self.qubits}')
|
|
312
316
|
return self
|
|
@@ -314,7 +318,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
314
318
|
def __len__(self) -> int:
|
|
315
319
|
return len(self.qubits)
|
|
316
320
|
|
|
317
|
-
def __iter__(self) -> Iterator[Optional[
|
|
321
|
+
def __iter__(self) -> Iterator[Optional[cirq.Qid]]:
|
|
318
322
|
return iter(self.qubits)
|
|
319
323
|
|
|
320
324
|
@property
|
|
@@ -323,7 +327,7 @@ class SimulationState(SimulationStateBase, Generic[TState], metaclass=abc.ABCMet
|
|
|
323
327
|
|
|
324
328
|
|
|
325
329
|
def strat_act_on_from_apply_decompose(
|
|
326
|
-
val: Any, args:
|
|
330
|
+
val: Any, args: cirq.SimulationState, qubits: Sequence[cirq.Qid]
|
|
327
331
|
) -> bool:
|
|
328
332
|
if isinstance(val, ops.Gate):
|
|
329
333
|
decomposed = protocols.decompose_once_with_qubits(val, qubits, flatten=False, default=None)
|
|
@@ -331,7 +335,7 @@ def strat_act_on_from_apply_decompose(
|
|
|
331
335
|
decomposed = protocols.decompose_once(val, flatten=False, default=None)
|
|
332
336
|
if decomposed is None:
|
|
333
337
|
return NotImplemented
|
|
334
|
-
all_ancilla: Set[
|
|
338
|
+
all_ancilla: Set[cirq.Qid] = set()
|
|
335
339
|
for operation in ops.flatten_to_ops(decomposed):
|
|
336
340
|
curr_ancilla = tuple(q for q in operation.qubits if q not in args.qubits)
|
|
337
341
|
args = args.add_qubits(curr_ancilla)
|
|
@@ -11,7 +11,11 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
+
|
|
14
15
|
"""An interface for quantum states as targets for operations."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
15
19
|
import abc
|
|
16
20
|
from types import NotImplementedType
|
|
17
21
|
from typing import (
|
|
@@ -47,8 +51,8 @@ class SimulationStateBase(Generic[TSimulationState], metaclass=abc.ABCMeta):
|
|
|
47
51
|
def __init__(
|
|
48
52
|
self,
|
|
49
53
|
*,
|
|
50
|
-
qubits: Sequence[
|
|
51
|
-
classical_data: Optional[
|
|
54
|
+
qubits: Sequence[cirq.Qid],
|
|
55
|
+
classical_data: Optional[cirq.ClassicalDataStore] = None,
|
|
52
56
|
):
|
|
53
57
|
"""Initializes the class.
|
|
54
58
|
|
|
@@ -61,19 +65,19 @@ class SimulationStateBase(Generic[TSimulationState], metaclass=abc.ABCMeta):
|
|
|
61
65
|
self._classical_data = classical_data or value.ClassicalDataDictionaryStore()
|
|
62
66
|
|
|
63
67
|
@property
|
|
64
|
-
def qubits(self) -> Tuple[
|
|
68
|
+
def qubits(self) -> Tuple[cirq.Qid, ...]:
|
|
65
69
|
return self._qubits
|
|
66
70
|
|
|
67
71
|
@property
|
|
68
|
-
def qubit_map(self) -> Mapping[
|
|
72
|
+
def qubit_map(self) -> Mapping[cirq.Qid, int]:
|
|
69
73
|
return self._qubit_map
|
|
70
74
|
|
|
71
|
-
def _set_qubits(self, qubits: Sequence[
|
|
75
|
+
def _set_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
72
76
|
self._qubits = tuple(qubits)
|
|
73
77
|
self._qubit_map = {q: i for i, q in enumerate(self.qubits)}
|
|
74
78
|
|
|
75
79
|
@property
|
|
76
|
-
def classical_data(self) ->
|
|
80
|
+
def classical_data(self) -> cirq.ClassicalDataStoreReader:
|
|
77
81
|
return self._classical_data
|
|
78
82
|
|
|
79
83
|
@abc.abstractmethod
|
|
@@ -82,7 +86,7 @@ class SimulationStateBase(Generic[TSimulationState], metaclass=abc.ABCMeta):
|
|
|
82
86
|
|
|
83
87
|
@abc.abstractmethod
|
|
84
88
|
def _act_on_fallback_(
|
|
85
|
-
self, action: Any, qubits: Sequence[
|
|
89
|
+
self, action: Any, qubits: Sequence[cirq.Qid], allow_decompose: bool = True
|
|
86
90
|
) -> Union[bool, NotImplementedType]:
|
|
87
91
|
"""Handles the act_on protocol fallback implementation.
|
|
88
92
|
|
|
@@ -94,7 +98,7 @@ class SimulationStateBase(Generic[TSimulationState], metaclass=abc.ABCMeta):
|
|
|
94
98
|
Returns:
|
|
95
99
|
True if the fallback applies, else NotImplemented."""
|
|
96
100
|
|
|
97
|
-
def apply_operation(self, op:
|
|
101
|
+
def apply_operation(self, op: cirq.Operation):
|
|
98
102
|
protocols.act_on(op, self)
|
|
99
103
|
|
|
100
104
|
@abc.abstractmethod
|
|
@@ -118,14 +122,14 @@ class SimulationStateBase(Generic[TSimulationState], metaclass=abc.ABCMeta):
|
|
|
118
122
|
@abc.abstractmethod
|
|
119
123
|
def sample(
|
|
120
124
|
self,
|
|
121
|
-
qubits: List[
|
|
125
|
+
qubits: List[cirq.Qid],
|
|
122
126
|
repetitions: int = 1,
|
|
123
|
-
seed:
|
|
127
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
124
128
|
) -> np.ndarray:
|
|
125
129
|
"""Samples the state value."""
|
|
126
130
|
|
|
127
131
|
@abc.abstractmethod
|
|
128
|
-
def __getitem__(self, item: Optional[
|
|
132
|
+
def __getitem__(self, item: Optional[cirq.Qid]) -> TSimulationState:
|
|
129
133
|
"""Gets the item associated with the qubit."""
|
|
130
134
|
|
|
131
135
|
@abc.abstractmethod
|
|
@@ -133,5 +137,5 @@ class SimulationStateBase(Generic[TSimulationState], metaclass=abc.ABCMeta):
|
|
|
133
137
|
"""Gets the number of items in the mapping."""
|
|
134
138
|
|
|
135
139
|
@abc.abstractmethod
|
|
136
|
-
def __iter__(self) -> Iterator[Optional[
|
|
140
|
+
def __iter__(self) -> Iterator[Optional[cirq.Qid]]:
|
|
137
141
|
"""Iterates the keys of the mapping."""
|
|
@@ -38,7 +38,7 @@ class ExampleSimulationState(cirq.SimulationState):
|
|
|
38
38
|
super().__init__(state=ExampleQuantumState(), qubits=qubits)
|
|
39
39
|
|
|
40
40
|
def _act_on_fallback_(
|
|
41
|
-
self, action: Any, qubits: Sequence[
|
|
41
|
+
self, action: Any, qubits: Sequence[cirq.Qid], allow_decompose: bool = True
|
|
42
42
|
) -> bool:
|
|
43
43
|
return True
|
|
44
44
|
|