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
|
@@ -30,6 +30,8 @@ The quantum state is specified in two forms:
|
|
|
30
30
|
to state vector amplitudes.
|
|
31
31
|
"""
|
|
32
32
|
|
|
33
|
+
from __future__ import annotations
|
|
34
|
+
|
|
33
35
|
from typing import Any, Dict, List, Sequence, Union
|
|
34
36
|
|
|
35
37
|
import numpy as np
|
|
@@ -50,7 +52,7 @@ class CliffordSimulator(
|
|
|
50
52
|
"""An efficient simulator for Clifford circuits."""
|
|
51
53
|
|
|
52
54
|
def __init__(
|
|
53
|
-
self, seed:
|
|
55
|
+
self, seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None, split_untangled_states: bool = False
|
|
54
56
|
):
|
|
55
57
|
"""Creates instance of `CliffordSimulator`.
|
|
56
58
|
|
|
@@ -63,17 +65,17 @@ class CliffordSimulator(
|
|
|
63
65
|
super().__init__(seed=seed, split_untangled_states=split_untangled_states)
|
|
64
66
|
|
|
65
67
|
@staticmethod
|
|
66
|
-
def is_supported_operation(op:
|
|
68
|
+
def is_supported_operation(op: cirq.Operation) -> bool:
|
|
67
69
|
"""Checks whether given operation can be simulated by this simulator."""
|
|
68
70
|
# TODO: support more general Pauli measurements
|
|
69
71
|
return protocols.has_stabilizer_effect(op)
|
|
70
72
|
|
|
71
73
|
def _create_partial_simulation_state(
|
|
72
74
|
self,
|
|
73
|
-
initial_state: Union[int,
|
|
74
|
-
qubits: Sequence[
|
|
75
|
-
classical_data:
|
|
76
|
-
) ->
|
|
75
|
+
initial_state: Union[int, cirq.StabilizerChFormSimulationState],
|
|
76
|
+
qubits: Sequence[cirq.Qid],
|
|
77
|
+
classical_data: cirq.ClassicalDataStore,
|
|
78
|
+
) -> cirq.StabilizerChFormSimulationState:
|
|
77
79
|
"""Creates the StabilizerChFormSimulationState for a circuit.
|
|
78
80
|
|
|
79
81
|
Args:
|
|
@@ -101,15 +103,15 @@ class CliffordSimulator(
|
|
|
101
103
|
)
|
|
102
104
|
|
|
103
105
|
def _create_step_result(
|
|
104
|
-
self, sim_state:
|
|
106
|
+
self, sim_state: cirq.SimulationStateBase[clifford.StabilizerChFormSimulationState]
|
|
105
107
|
):
|
|
106
108
|
return CliffordSimulatorStepResult(sim_state=sim_state)
|
|
107
109
|
|
|
108
110
|
def _create_simulator_trial_result(
|
|
109
111
|
self,
|
|
110
|
-
params:
|
|
112
|
+
params: cirq.ParamResolver,
|
|
111
113
|
measurements: Dict[str, np.ndarray],
|
|
112
|
-
final_simulator_state:
|
|
114
|
+
final_simulator_state: cirq.SimulationStateBase[cirq.StabilizerChFormSimulationState],
|
|
113
115
|
):
|
|
114
116
|
return CliffordTrialResult(
|
|
115
117
|
params=params, measurements=measurements, final_simulator_state=final_simulator_state
|
|
@@ -121,16 +123,16 @@ class CliffordTrialResult(
|
|
|
121
123
|
):
|
|
122
124
|
def __init__(
|
|
123
125
|
self,
|
|
124
|
-
params:
|
|
126
|
+
params: cirq.ParamResolver,
|
|
125
127
|
measurements: Dict[str, np.ndarray],
|
|
126
|
-
final_simulator_state:
|
|
128
|
+
final_simulator_state: cirq.SimulationStateBase[cirq.StabilizerChFormSimulationState],
|
|
127
129
|
) -> None:
|
|
128
130
|
super().__init__(
|
|
129
131
|
params=params, measurements=measurements, final_simulator_state=final_simulator_state
|
|
130
132
|
)
|
|
131
133
|
|
|
132
134
|
@property
|
|
133
|
-
def final_state(self) ->
|
|
135
|
+
def final_state(self) -> cirq.CliffordState:
|
|
134
136
|
state = self._get_merged_sim_state()
|
|
135
137
|
clifford_state = CliffordState(state.qubit_map)
|
|
136
138
|
clifford_state.ch_form = state.state.copy()
|
|
@@ -152,7 +154,7 @@ class CliffordSimulatorStepResult(
|
|
|
152
154
|
"""A `StepResult` that includes `StateVectorMixin` methods."""
|
|
153
155
|
|
|
154
156
|
def __init__(
|
|
155
|
-
self, sim_state:
|
|
157
|
+
self, sim_state: cirq.SimulationStateBase[clifford.StabilizerChFormSimulationState]
|
|
156
158
|
):
|
|
157
159
|
"""Results of a step of the simulator.
|
|
158
160
|
Attributes:
|
|
@@ -199,7 +201,7 @@ class CliffordState:
|
|
|
199
201
|
Gates and measurements are applied to each representation in O(n^2) time.
|
|
200
202
|
"""
|
|
201
203
|
|
|
202
|
-
def __init__(self, qubit_map, initial_state: Union[int,
|
|
204
|
+
def __init__(self, qubit_map, initial_state: Union[int, cirq.StabilizerStateChForm] = 0):
|
|
203
205
|
self.qubit_map = qubit_map
|
|
204
206
|
self.n = len(qubit_map)
|
|
205
207
|
|
|
@@ -222,7 +224,7 @@ class CliffordState:
|
|
|
222
224
|
def _value_equality_values_(self) -> Any:
|
|
223
225
|
return self.qubit_map, self.ch_form
|
|
224
226
|
|
|
225
|
-
def copy(self) ->
|
|
227
|
+
def copy(self) -> cirq.CliffordState:
|
|
226
228
|
state = CliffordState(self.qubit_map)
|
|
227
229
|
state.ch_form = self.ch_form.copy()
|
|
228
230
|
|
|
@@ -241,7 +243,7 @@ class CliffordState:
|
|
|
241
243
|
def state_vector(self):
|
|
242
244
|
return self.ch_form.state_vector()
|
|
243
245
|
|
|
244
|
-
def apply_unitary(self, op:
|
|
246
|
+
def apply_unitary(self, op: cirq.Operation):
|
|
245
247
|
ch_form_args = clifford.StabilizerChFormSimulationState(
|
|
246
248
|
prng=np.random.RandomState(), qubits=self.qubit_map.keys(), initial_state=self.ch_form
|
|
247
249
|
)
|
|
@@ -253,7 +255,7 @@ class CliffordState:
|
|
|
253
255
|
|
|
254
256
|
def apply_measurement(
|
|
255
257
|
self,
|
|
256
|
-
op:
|
|
258
|
+
op: cirq.Operation,
|
|
257
259
|
measurements: Dict[str, List[int]],
|
|
258
260
|
prng: np.random.RandomState,
|
|
259
261
|
collapse_state_vector=True,
|
|
@@ -11,9 +11,12 @@
|
|
|
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
|
"""A protocol for implementing high performance clifford tableau evolutions
|
|
15
16
|
for Clifford Simulator."""
|
|
16
17
|
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
17
20
|
from typing import Optional, Sequence, TYPE_CHECKING
|
|
18
21
|
|
|
19
22
|
import numpy as np
|
|
@@ -30,10 +33,10 @@ class CliffordTableauSimulationState(StabilizerSimulationState[clifford_tableau.
|
|
|
30
33
|
|
|
31
34
|
def __init__(
|
|
32
35
|
self,
|
|
33
|
-
tableau:
|
|
36
|
+
tableau: cirq.CliffordTableau,
|
|
34
37
|
prng: Optional[np.random.RandomState] = None,
|
|
35
|
-
qubits: Optional[Sequence[
|
|
36
|
-
classical_data: Optional[
|
|
38
|
+
qubits: Optional[Sequence[cirq.Qid]] = None,
|
|
39
|
+
classical_data: Optional[cirq.ClassicalDataStore] = None,
|
|
37
40
|
):
|
|
38
41
|
"""Inits CliffordTableauSimulationState.
|
|
39
42
|
|
|
@@ -51,5 +54,5 @@ class CliffordTableauSimulationState(StabilizerSimulationState[clifford_tableau.
|
|
|
51
54
|
super().__init__(state=tableau, prng=prng, qubits=qubits, classical_data=classical_data)
|
|
52
55
|
|
|
53
56
|
@property
|
|
54
|
-
def tableau(self) ->
|
|
57
|
+
def tableau(self) -> cirq.CliffordTableau:
|
|
55
58
|
return self.state
|
|
@@ -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 typing import Optional, Sequence, TYPE_CHECKING, Union
|
|
16
18
|
|
|
17
19
|
import numpy as np
|
|
@@ -33,9 +35,9 @@ class StabilizerChFormSimulationState(
|
|
|
33
35
|
self,
|
|
34
36
|
*,
|
|
35
37
|
prng: Optional[np.random.RandomState] = None,
|
|
36
|
-
qubits: Optional[Sequence[
|
|
37
|
-
initial_state: Union[int,
|
|
38
|
-
classical_data: Optional[
|
|
38
|
+
qubits: Optional[Sequence[cirq.Qid]] = None,
|
|
39
|
+
initial_state: Union[int, cirq.StabilizerStateChForm] = 0,
|
|
40
|
+
classical_data: Optional[cirq.ClassicalDataStore] = None,
|
|
39
41
|
):
|
|
40
42
|
"""Initializes with the given state and the axes for the operation.
|
|
41
43
|
|
|
@@ -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 typing import Dict, List, Sequence
|
|
16
18
|
|
|
17
19
|
import numpy as np
|
|
@@ -26,7 +28,7 @@ from cirq.work import sampler
|
|
|
26
28
|
class StabilizerSampler(sampler.Sampler):
|
|
27
29
|
"""An efficient sampler for stabilizer circuits."""
|
|
28
30
|
|
|
29
|
-
def __init__(self, *, seed:
|
|
31
|
+
def __init__(self, *, seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None):
|
|
30
32
|
"""Inits StabilizerSampler.
|
|
31
33
|
|
|
32
34
|
Args:
|
|
@@ -36,8 +38,8 @@ class StabilizerSampler(sampler.Sampler):
|
|
|
36
38
|
self._prng = value.parse_random_state(seed)
|
|
37
39
|
|
|
38
40
|
def run_sweep(
|
|
39
|
-
self, program:
|
|
40
|
-
) -> Sequence[
|
|
41
|
+
self, program: cirq.AbstractCircuit, params: cirq.Sweepable, repetitions: int = 1
|
|
42
|
+
) -> Sequence[cirq.Result]:
|
|
41
43
|
results: List[cirq.Result] = []
|
|
42
44
|
for param_resolver in cirq.to_resolvers(params):
|
|
43
45
|
resolved_circuit = cirq.resolve_parameters(program, param_resolver)
|
|
@@ -45,7 +47,7 @@ class StabilizerSampler(sampler.Sampler):
|
|
|
45
47
|
results.append(cirq.ResultDict(params=param_resolver, measurements=measurements))
|
|
46
48
|
return results
|
|
47
49
|
|
|
48
|
-
def _run(self, circuit:
|
|
50
|
+
def _run(self, circuit: cirq.AbstractCircuit, repetitions: int) -> Dict[str, np.ndarray]:
|
|
49
51
|
|
|
50
52
|
measurements: Dict[str, List[np.ndarray]] = {
|
|
51
53
|
key: [] for key in protocols.measurement_key_names(circuit)
|
|
@@ -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
|
import abc
|
|
16
18
|
from types import NotImplementedType
|
|
17
19
|
from typing import Any, cast, Generic, Optional, Sequence, TYPE_CHECKING, TypeVar, Union
|
|
@@ -42,8 +44,8 @@ class StabilizerSimulationState(
|
|
|
42
44
|
*,
|
|
43
45
|
state: TStabilizerState,
|
|
44
46
|
prng: Optional[np.random.RandomState] = None,
|
|
45
|
-
qubits: Optional[Sequence[
|
|
46
|
-
classical_data: Optional[
|
|
47
|
+
qubits: Optional[Sequence[cirq.Qid]] = None,
|
|
48
|
+
classical_data: Optional[cirq.ClassicalDataStore] = None,
|
|
47
49
|
):
|
|
48
50
|
"""Initializes the StabilizerSimulationState.
|
|
49
51
|
|
|
@@ -65,7 +67,7 @@ class StabilizerSimulationState(
|
|
|
65
67
|
return self._state
|
|
66
68
|
|
|
67
69
|
def _act_on_fallback_(
|
|
68
|
-
self, action: Any, qubits: Sequence[
|
|
70
|
+
self, action: Any, qubits: Sequence[cirq.Qid], allow_decompose: bool = True
|
|
69
71
|
) -> Union[bool, NotImplementedType]:
|
|
70
72
|
strats = [self._strat_apply_gate, self._strat_apply_mixture]
|
|
71
73
|
if allow_decompose:
|
|
@@ -89,7 +91,7 @@ class StabilizerSimulationState(
|
|
|
89
91
|
self._state.apply_cx(target_axis, control_axis, exponent, global_shift)
|
|
90
92
|
self._state.apply_cx(control_axis, target_axis)
|
|
91
93
|
|
|
92
|
-
def _strat_apply_gate(self, val: Any, qubits: Sequence[
|
|
94
|
+
def _strat_apply_gate(self, val: Any, qubits: Sequence[cirq.Qid]) -> bool:
|
|
93
95
|
if not protocols.has_stabilizer_effect(val):
|
|
94
96
|
return NotImplemented
|
|
95
97
|
gate = val.gate if isinstance(val, ops.Operation) else val
|
|
@@ -117,7 +119,7 @@ class StabilizerSimulationState(
|
|
|
117
119
|
return NotImplemented
|
|
118
120
|
return True
|
|
119
121
|
|
|
120
|
-
def _strat_apply_mixture(self, val: Any, qubits: Sequence[
|
|
122
|
+
def _strat_apply_mixture(self, val: Any, qubits: Sequence[cirq.Qid]) -> bool:
|
|
121
123
|
mixture = protocols.mixture(val, None)
|
|
122
124
|
if mixture is None:
|
|
123
125
|
return NotImplemented
|
|
@@ -129,9 +131,7 @@ class StabilizerSimulationState(
|
|
|
129
131
|
matrix_gates.MatrixGate(unitaries[index]), qubits
|
|
130
132
|
)
|
|
131
133
|
|
|
132
|
-
def _strat_act_from_single_qubit_decompose(
|
|
133
|
-
self, val: Any, qubits: Sequence['cirq.Qid']
|
|
134
|
-
) -> bool:
|
|
134
|
+
def _strat_act_from_single_qubit_decompose(self, val: Any, qubits: Sequence[cirq.Qid]) -> bool:
|
|
135
135
|
if num_qubits(val) == 1:
|
|
136
136
|
if not has_unitary(val):
|
|
137
137
|
return NotImplemented
|
|
@@ -148,7 +148,7 @@ class StabilizerSimulationState(
|
|
|
148
148
|
|
|
149
149
|
return NotImplemented
|
|
150
150
|
|
|
151
|
-
def _strat_decompose(self, val: Any, qubits: Sequence[
|
|
151
|
+
def _strat_decompose(self, val: Any, qubits: Sequence[cirq.Qid]) -> bool:
|
|
152
152
|
gate = val.gate if isinstance(val, ops.Operation) else val
|
|
153
153
|
operations = protocols.decompose_once_with_qubits(gate, qubits, None)
|
|
154
154
|
if operations is None or not all(protocols.has_stabilizer_effect(op) for op in operations):
|
|
@@ -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 typing import Any, Dict, List, Sequence
|
|
16
18
|
|
|
17
19
|
import numpy as np
|
|
@@ -81,7 +83,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
81
83
|
def _value_equality_values_(self) -> Any:
|
|
82
84
|
return (self.n, self.G, self.F, self.M, self.gamma, self.v, self.s, self.omega)
|
|
83
85
|
|
|
84
|
-
def copy(self, deep_copy_buffers: bool = True) ->
|
|
86
|
+
def copy(self, deep_copy_buffers: bool = True) -> cirq.StabilizerStateChForm:
|
|
85
87
|
copy = StabilizerStateChForm(self.n)
|
|
86
88
|
|
|
87
89
|
copy.G = self.G.copy()
|
|
@@ -267,7 +269,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
267
269
|
|
|
268
270
|
self.update_sum(t, u, delta=delta)
|
|
269
271
|
|
|
270
|
-
def kron(self, other:
|
|
272
|
+
def kron(self, other: cirq.StabilizerStateChForm) -> cirq.StabilizerStateChForm:
|
|
271
273
|
n = self.n + other.n
|
|
272
274
|
copy = StabilizerStateChForm(n)
|
|
273
275
|
copy.G[: self.n, : self.n] = self.G
|
|
@@ -282,7 +284,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
282
284
|
copy.omega = self.omega * other.omega
|
|
283
285
|
return copy
|
|
284
286
|
|
|
285
|
-
def reindex(self, axes: Sequence[int]) ->
|
|
287
|
+
def reindex(self, axes: Sequence[int]) -> cirq.StabilizerStateChForm:
|
|
286
288
|
copy = StabilizerStateChForm(self.n)
|
|
287
289
|
copy.G = self.G[axes][:, axes]
|
|
288
290
|
copy.F = self.F[axes][:, axes]
|
|
@@ -387,7 +389,7 @@ class StabilizerStateChForm(qis.StabilizerState):
|
|
|
387
389
|
self.omega *= coefficient
|
|
388
390
|
|
|
389
391
|
def measure(
|
|
390
|
-
self, axes: Sequence[int], seed:
|
|
392
|
+
self, axes: Sequence[int], seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None
|
|
391
393
|
) -> List[int]:
|
|
392
394
|
return [self._measure(axis, random_state.parse_random_state(seed)) for axis in axes]
|
|
393
395
|
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
|
|
15
15
|
"""Objects and methods for acting efficiently on a density matrix."""
|
|
16
16
|
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
17
19
|
from typing import Any, Callable, List, Optional, Sequence, Tuple, Type, TYPE_CHECKING, Union
|
|
18
20
|
|
|
19
21
|
import numpy as np
|
|
@@ -55,7 +57,7 @@ class _BufferedDensityMatrix(qis.QuantumStateRepresentation):
|
|
|
55
57
|
def create(
|
|
56
58
|
cls,
|
|
57
59
|
*,
|
|
58
|
-
initial_state: Union[np.ndarray,
|
|
60
|
+
initial_state: Union[np.ndarray, cirq.STATE_VECTOR_LIKE] = 0,
|
|
59
61
|
qid_shape: Optional[Tuple[int, ...]] = None,
|
|
60
62
|
dtype: Optional[Type[np.complexfloating]] = None,
|
|
61
63
|
buffer: Optional[List[np.ndarray]] = None,
|
|
@@ -91,7 +93,7 @@ class _BufferedDensityMatrix(qis.QuantumStateRepresentation):
|
|
|
91
93
|
density_matrix = density_matrix.astype(dtype, copy=False)
|
|
92
94
|
return cls(density_matrix, buffer)
|
|
93
95
|
|
|
94
|
-
def copy(self, deep_copy_buffers: bool = True) ->
|
|
96
|
+
def copy(self, deep_copy_buffers: bool = True) -> _BufferedDensityMatrix:
|
|
95
97
|
"""Copies the object.
|
|
96
98
|
|
|
97
99
|
Args:
|
|
@@ -104,7 +106,7 @@ class _BufferedDensityMatrix(qis.QuantumStateRepresentation):
|
|
|
104
106
|
buffer=[b.copy() for b in self._buffer] if deep_copy_buffers else self._buffer,
|
|
105
107
|
)
|
|
106
108
|
|
|
107
|
-
def kron(self, other:
|
|
109
|
+
def kron(self, other: _BufferedDensityMatrix) -> _BufferedDensityMatrix:
|
|
108
110
|
"""Creates the Kronecker product with the other density matrix.
|
|
109
111
|
|
|
110
112
|
Args:
|
|
@@ -119,7 +121,7 @@ class _BufferedDensityMatrix(qis.QuantumStateRepresentation):
|
|
|
119
121
|
|
|
120
122
|
def factor(
|
|
121
123
|
self, axes: Sequence[int], *, validate=True, atol=1e-07
|
|
122
|
-
) -> Tuple[
|
|
124
|
+
) -> Tuple[_BufferedDensityMatrix, _BufferedDensityMatrix]:
|
|
123
125
|
"""Factors out the desired axes.
|
|
124
126
|
|
|
125
127
|
Args:
|
|
@@ -141,7 +143,7 @@ class _BufferedDensityMatrix(qis.QuantumStateRepresentation):
|
|
|
141
143
|
remainder = _BufferedDensityMatrix(density_matrix=remainder_tensor)
|
|
142
144
|
return extracted, remainder
|
|
143
145
|
|
|
144
|
-
def reindex(self, axes: Sequence[int]) ->
|
|
146
|
+
def reindex(self, axes: Sequence[int]) -> _BufferedDensityMatrix:
|
|
145
147
|
"""Transposes the axes of a density matrix to a specified order.
|
|
146
148
|
|
|
147
149
|
Args:
|
|
@@ -185,7 +187,7 @@ class _BufferedDensityMatrix(qis.QuantumStateRepresentation):
|
|
|
185
187
|
return True
|
|
186
188
|
|
|
187
189
|
def measure(
|
|
188
|
-
self, axes: Sequence[int], seed:
|
|
190
|
+
self, axes: Sequence[int], seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None
|
|
189
191
|
) -> List[int]:
|
|
190
192
|
"""Measures the density matrix.
|
|
191
193
|
|
|
@@ -205,10 +207,7 @@ class _BufferedDensityMatrix(qis.QuantumStateRepresentation):
|
|
|
205
207
|
return bits
|
|
206
208
|
|
|
207
209
|
def sample(
|
|
208
|
-
self,
|
|
209
|
-
axes: Sequence[int],
|
|
210
|
-
repetitions: int = 1,
|
|
211
|
-
seed: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
|
|
210
|
+
self, axes: Sequence[int], repetitions: int = 1, seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None
|
|
212
211
|
) -> np.ndarray:
|
|
213
212
|
"""Samples the density matrix.
|
|
214
213
|
|
|
@@ -248,10 +247,10 @@ class DensityMatrixSimulationState(SimulationState[_BufferedDensityMatrix]):
|
|
|
248
247
|
*,
|
|
249
248
|
available_buffer: Optional[List[np.ndarray]] = None,
|
|
250
249
|
prng: Optional[np.random.RandomState] = None,
|
|
251
|
-
qubits: Optional[Sequence[
|
|
252
|
-
initial_state: Union[np.ndarray,
|
|
250
|
+
qubits: Optional[Sequence[cirq.Qid]] = None,
|
|
251
|
+
initial_state: Union[np.ndarray, cirq.STATE_VECTOR_LIKE] = 0,
|
|
253
252
|
dtype: Type[np.complexfloating] = np.complex64,
|
|
254
|
-
classical_data: Optional[
|
|
253
|
+
classical_data: Optional[cirq.ClassicalDataStore] = None,
|
|
255
254
|
):
|
|
256
255
|
"""Inits DensityMatrixSimulationState.
|
|
257
256
|
|
|
@@ -285,7 +284,7 @@ class DensityMatrixSimulationState(SimulationState[_BufferedDensityMatrix]):
|
|
|
285
284
|
)
|
|
286
285
|
super().__init__(state=state, prng=prng, qubits=qubits, classical_data=classical_data)
|
|
287
286
|
|
|
288
|
-
def add_qubits(self, qubits: Sequence[
|
|
287
|
+
def add_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
289
288
|
ret = super().add_qubits(qubits)
|
|
290
289
|
return (
|
|
291
290
|
self.kronecker_product(type(self)(qubits=qubits), inplace=True)
|
|
@@ -293,7 +292,7 @@ class DensityMatrixSimulationState(SimulationState[_BufferedDensityMatrix]):
|
|
|
293
292
|
else ret
|
|
294
293
|
)
|
|
295
294
|
|
|
296
|
-
def remove_qubits(self, qubits: Sequence[
|
|
295
|
+
def remove_qubits(self, qubits: Sequence[cirq.Qid]):
|
|
297
296
|
ret = super().remove_qubits(qubits)
|
|
298
297
|
if ret is not NotImplemented:
|
|
299
298
|
return ret
|
|
@@ -302,9 +301,9 @@ class DensityMatrixSimulationState(SimulationState[_BufferedDensityMatrix]):
|
|
|
302
301
|
return remainder
|
|
303
302
|
|
|
304
303
|
def _act_on_fallback_(
|
|
305
|
-
self, action: Any, qubits: Sequence[
|
|
304
|
+
self, action: Any, qubits: Sequence[cirq.Qid], allow_decompose: bool = True
|
|
306
305
|
) -> bool:
|
|
307
|
-
strats: List[Callable[[Any, Any, Sequence[
|
|
306
|
+
strats: List[Callable[[Any, Any, Sequence[cirq.Qid]], bool]] = [
|
|
308
307
|
_strat_apply_channel_to_state
|
|
309
308
|
]
|
|
310
309
|
if allow_decompose:
|
|
@@ -346,7 +345,7 @@ class DensityMatrixSimulationState(SimulationState[_BufferedDensityMatrix]):
|
|
|
346
345
|
|
|
347
346
|
|
|
348
347
|
def _strat_apply_channel_to_state(
|
|
349
|
-
action: Any, args:
|
|
348
|
+
action: Any, args: cirq.DensityMatrixSimulationState, qubits: Sequence[cirq.Qid]
|
|
350
349
|
) -> bool:
|
|
351
350
|
"""Apply channel to state."""
|
|
352
351
|
if not args._state.apply_channel(action, args.get_axes(qubits)):
|
|
@@ -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
|
"""Simulator for density matrices that simulates noisy quantum circuits."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
15
19
|
from typing import Any, Dict, List, Optional, Sequence, Type, TYPE_CHECKING, Union
|
|
16
20
|
|
|
17
21
|
import numpy as np
|
|
@@ -116,8 +120,8 @@ class DensityMatrixSimulator(
|
|
|
116
120
|
self,
|
|
117
121
|
*,
|
|
118
122
|
dtype: Type[np.complexfloating] = np.complex64,
|
|
119
|
-
noise:
|
|
120
|
-
seed:
|
|
123
|
+
noise: cirq.NOISE_MODEL_LIKE = None,
|
|
124
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
121
125
|
split_untangled_states: bool = True,
|
|
122
126
|
):
|
|
123
127
|
"""Density matrix simulator.
|
|
@@ -147,12 +151,10 @@ class DensityMatrixSimulator(
|
|
|
147
151
|
|
|
148
152
|
def _create_partial_simulation_state(
|
|
149
153
|
self,
|
|
150
|
-
initial_state: Union[
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
classical_data: 'cirq.ClassicalDataStore',
|
|
155
|
-
) -> 'cirq.DensityMatrixSimulationState':
|
|
154
|
+
initial_state: Union[np.ndarray, cirq.STATE_VECTOR_LIKE, cirq.DensityMatrixSimulationState],
|
|
155
|
+
qubits: Sequence[cirq.Qid],
|
|
156
|
+
classical_data: cirq.ClassicalDataStore,
|
|
157
|
+
) -> cirq.DensityMatrixSimulationState:
|
|
156
158
|
"""Creates the DensityMatrixSimulationState for a circuit.
|
|
157
159
|
|
|
158
160
|
Args:
|
|
@@ -183,16 +185,16 @@ class DensityMatrixSimulator(
|
|
|
183
185
|
return not protocols.measurement_keys_touched(val)
|
|
184
186
|
|
|
185
187
|
def _create_step_result(
|
|
186
|
-
self, sim_state:
|
|
188
|
+
self, sim_state: cirq.SimulationStateBase[cirq.DensityMatrixSimulationState]
|
|
187
189
|
):
|
|
188
190
|
return DensityMatrixStepResult(sim_state=sim_state, dtype=self._dtype)
|
|
189
191
|
|
|
190
192
|
def _create_simulator_trial_result(
|
|
191
193
|
self,
|
|
192
|
-
params:
|
|
194
|
+
params: cirq.ParamResolver,
|
|
193
195
|
measurements: Dict[str, np.ndarray],
|
|
194
|
-
final_simulator_state:
|
|
195
|
-
) ->
|
|
196
|
+
final_simulator_state: cirq.SimulationStateBase[cirq.DensityMatrixSimulationState],
|
|
197
|
+
) -> cirq.DensityMatrixTrialResult:
|
|
196
198
|
return DensityMatrixTrialResult(
|
|
197
199
|
params=params, measurements=measurements, final_simulator_state=final_simulator_state
|
|
198
200
|
)
|
|
@@ -200,10 +202,10 @@ class DensityMatrixSimulator(
|
|
|
200
202
|
# TODO(#4209): Deduplicate with identical code in sparse_simulator.
|
|
201
203
|
def simulate_expectation_values_sweep(
|
|
202
204
|
self,
|
|
203
|
-
program:
|
|
204
|
-
observables: Union[
|
|
205
|
-
params:
|
|
206
|
-
qubit_order:
|
|
205
|
+
program: cirq.AbstractCircuit,
|
|
206
|
+
observables: Union[cirq.PauliSumLike, List[cirq.PauliSumLike]],
|
|
207
|
+
params: cirq.Sweepable,
|
|
208
|
+
qubit_order: cirq.QubitOrderOrList = ops.QubitOrder.DEFAULT,
|
|
207
209
|
initial_state: Any = None,
|
|
208
210
|
permit_terminal_measurements: bool = False,
|
|
209
211
|
) -> List[List[float]]:
|
|
@@ -242,7 +244,7 @@ class DensityMatrixStepResult(simulator_base.StepResultBase['cirq.DensityMatrixS
|
|
|
242
244
|
|
|
243
245
|
def __init__(
|
|
244
246
|
self,
|
|
245
|
-
sim_state:
|
|
247
|
+
sim_state: cirq.SimulationStateBase[cirq.DensityMatrixSimulationState],
|
|
246
248
|
dtype: Type[np.complexfloating] = np.complex64,
|
|
247
249
|
):
|
|
248
250
|
"""DensityMatrixStepResult.
|
|
@@ -353,9 +355,9 @@ class DensityMatrixTrialResult(
|
|
|
353
355
|
|
|
354
356
|
def __init__(
|
|
355
357
|
self,
|
|
356
|
-
params:
|
|
358
|
+
params: cirq.ParamResolver,
|
|
357
359
|
measurements: Dict[str, np.ndarray],
|
|
358
|
-
final_simulator_state:
|
|
360
|
+
final_simulator_state: cirq.SimulationStateBase[cirq.DensityMatrixSimulationState],
|
|
359
361
|
) -> None:
|
|
360
362
|
super().__init__(
|
|
361
363
|
params=params, measurements=measurements, final_simulator_state=final_simulator_state
|
cirq/sim/density_matrix_utils.py
CHANGED
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
|
|
15
15
|
"""Code to handle density matrices."""
|
|
16
16
|
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
17
19
|
from typing import List, Optional, Sequence, Tuple, TYPE_CHECKING
|
|
18
20
|
|
|
19
21
|
import numpy as np
|
|
@@ -31,7 +33,7 @@ def sample_density_matrix(
|
|
|
31
33
|
*, # Force keyword arguments
|
|
32
34
|
qid_shape: Optional[Tuple[int, ...]] = None,
|
|
33
35
|
repetitions: int = 1,
|
|
34
|
-
seed:
|
|
36
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
35
37
|
) -> np.ndarray:
|
|
36
38
|
"""Samples repeatedly from measurements in the computational basis.
|
|
37
39
|
|
|
@@ -96,7 +98,7 @@ def measure_density_matrix(
|
|
|
96
98
|
indices: Sequence[int],
|
|
97
99
|
qid_shape: Optional[Tuple[int, ...]] = None,
|
|
98
100
|
out: Optional[np.ndarray] = None,
|
|
99
|
-
seed:
|
|
101
|
+
seed: cirq.RANDOM_STATE_OR_SEED_LIKE = None,
|
|
100
102
|
) -> Tuple[List[int], np.ndarray]:
|
|
101
103
|
"""Performs a measurement of the density matrix in the computational basis.
|
|
102
104
|
|