Skip to content

Commit 6c0da89

Browse files
committed
Factor out ProjectorSum
1 parent 6027dbc commit 6c0da89

File tree

7 files changed

+0
-169
lines changed

7 files changed

+0
-169
lines changed

cirq-core/cirq/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@
251251
PhasedXZGate,
252252
PhaseFlipChannel,
253253
ProjectorString,
254-
ProjectorSum,
255254
RandomGateChannel,
256255
qft,
257256
Qid,

cirq-core/cirq/json_resolver_cache.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ def two_qubit_matrix_gate(matrix):
118118
'PhasedXPowGate': cirq.PhasedXPowGate,
119119
'PhasedXZGate': cirq.PhasedXZGate,
120120
'ProjectorString': cirq.ProjectorString,
121-
'ProjectorSum': cirq.ProjectorSum,
122121
'RandomGateChannel': cirq.RandomGateChannel,
123122
'QuantumFourierTransformGate': cirq.QuantumFourierTransformGate,
124123
'RepetitionsStoppingCriteria': cirq.work.RepetitionsStoppingCriteria,

cirq-core/cirq/ops/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@
133133

134134
from cirq.ops.projector import (
135135
ProjectorString,
136-
ProjectorSum,
137136
)
138137

139138
from cirq.ops.controlled_operation import (

cirq-core/cirq/ops/projector.py

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
from collections import defaultdict
2-
import numbers
31
from typing import (
42
Any,
5-
DefaultDict,
63
Dict,
7-
FrozenSet,
84
Iterable,
9-
List,
105
Mapping,
116
Optional,
12-
Tuple,
13-
Union,
147
)
158

169
import numpy as np
@@ -136,111 +129,3 @@ def _from_json_dict_(cls, projector_dict, **kwargs):
136129
def _value_equality_values_(self) -> Any:
137130
projector_dict = sorted(self._projector_dict.items())
138131
return tuple(projector_dict)
139-
140-
141-
def _projector_string_from_projector_dict(projector_dict):
142-
return ProjectorString(dict(projector_dict))
143-
144-
145-
@value.value_equality(approximate=True)
146-
class ProjectorSum:
147-
def __init__(self, linear_dict=None):
148-
self._linear_dict = linear_dict if linear_dict is not None else {}
149-
150-
def _value_equality_values_(self):
151-
return self._linear_dict
152-
153-
def _json_dict_(self) -> Dict[str, Any]:
154-
linear_dict = []
155-
for projector_dict, scalar in dict(self._linear_dict).items():
156-
key = [[k, v] for k, v in dict(projector_dict).items()]
157-
linear_dict.append([key, scalar])
158-
return {
159-
'cirq_type': self.__class__.__name__,
160-
'linear_dict': linear_dict,
161-
}
162-
163-
@classmethod
164-
def _from_json_dict_(cls, linear_dict, **kwargs):
165-
converted_dict = {}
166-
for projector_string in linear_dict:
167-
projector_dict = {x[0]: x[1] for x in projector_string[0]}
168-
scalar = projector_string[1]
169-
key = frozenset(projector_dict.items())
170-
converted_dict[key] = scalar
171-
return cls(linear_dict=value.LinearDict(converted_dict))
172-
173-
@classmethod
174-
def from_projector_strings(
175-
cls, terms: Union[ProjectorString, List[ProjectorString]]
176-
) -> 'ProjectorSum':
177-
if isinstance(terms, ProjectorString):
178-
terms = [terms]
179-
termdict: DefaultDict[FrozenSet[Tuple[raw_types.Qid, int]], value.Scalar] = defaultdict(
180-
lambda: 0.0
181-
)
182-
for pstring in terms:
183-
key = frozenset(pstring._projector_dict.items())
184-
termdict[key] += 1.0
185-
return cls(linear_dict=value.LinearDict(termdict))
186-
187-
def copy(self) -> 'ProjectorSum':
188-
return ProjectorSum(self._linear_dict.copy())
189-
190-
def matrix(self, projector_qids: Optional[Iterable[raw_types.Qid]] = None) -> np.ndarray:
191-
return sum(
192-
coeff * _projector_string_from_projector_dict(vec).matrix(projector_qids)
193-
for vec, coeff in self._linear_dict.items()
194-
)
195-
196-
def expectation_from_state_vector(
197-
self,
198-
state_vector: np.ndarray,
199-
qid_map: Mapping[raw_types.Qid, int],
200-
*,
201-
atol: float = 1e-7,
202-
check_preconditions: bool = True,
203-
) -> float:
204-
return sum(
205-
coeff
206-
* _projector_string_from_projector_dict(vec).expectation_from_state_vector(
207-
state_vector, qid_map
208-
)
209-
for vec, coeff in self._linear_dict.items()
210-
)
211-
212-
def expectation_from_density_matrix(
213-
self,
214-
state: np.ndarray,
215-
qid_map: Mapping[raw_types.Qid, int],
216-
*,
217-
atol: float = 1e-7,
218-
check_preconditions: bool = True,
219-
) -> float:
220-
return sum(
221-
coeff
222-
* _projector_string_from_projector_dict(vec).expectation_from_density_matrix(
223-
state, qid_map
224-
)
225-
for vec, coeff in self._linear_dict.items()
226-
)
227-
228-
def __iadd__(self, other: 'ProjectorSum'):
229-
result = self.copy()
230-
result._linear_dict += other._linear_dict
231-
return result
232-
233-
def __add__(self, other: 'ProjectorSum'):
234-
result = self.copy()
235-
result += other
236-
return result
237-
238-
def __imul__(self, other: numbers.Complex):
239-
result = self.copy()
240-
result._linear_dict *= other
241-
return result
242-
243-
def __rmul__(self, other: numbers.Complex):
244-
result = self.copy()
245-
result *= other
246-
return result

cirq-core/cirq/ops/projector_test.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -175,36 +175,3 @@ def test_expectation_from_density_matrix_basis_states_single_qubits():
175175
np.testing.assert_allclose(
176176
d.expectation_from_density_matrix(np.array([[0.0, 0.0], [0.0, 1.0]]), {q0: 0}), 0.0
177177
)
178-
179-
180-
def test_projector_sum_expectations():
181-
q0 = cirq.NamedQubit('q0')
182-
183-
zero_projector = cirq.ProjectorSum.from_projector_strings(cirq.ProjectorString({q0: 0}))
184-
one_projector = cirq.ProjectorSum.from_projector_strings(cirq.ProjectorString({q0: 1}))
185-
186-
proj_sum = 0.6 * zero_projector + 0.4 * one_projector
187-
np.testing.assert_allclose(proj_sum.matrix(), [[0.6, 0.0], [0.0, 0.4]])
188-
np.testing.assert_allclose(
189-
proj_sum.expectation_from_state_vector(np.array([1.0, 0.0]), {q0: 0}), 0.6
190-
)
191-
np.testing.assert_allclose(
192-
proj_sum.expectation_from_density_matrix(np.array([[1.0, 0.0], [0.0, 0.0]]), {q0: 0}), 0.6
193-
)
194-
195-
196-
def test_projector_sum_operations():
197-
q0 = cirq.NamedQubit('q0')
198-
199-
zero_projector = cirq.ProjectorSum.from_projector_strings(cirq.ProjectorString({q0: 0}))
200-
one_projector = cirq.ProjectorSum.from_projector_strings(cirq.ProjectorString({q0: 1}))
201-
202-
simple_addition = zero_projector + one_projector
203-
np.testing.assert_allclose(simple_addition.matrix(), [[1.0, 0.0], [0.0, 1.0]])
204-
205-
incrementation = zero_projector
206-
incrementation += one_projector
207-
np.testing.assert_allclose(incrementation.matrix(), [[1.0, 0.0], [0.0, 1.0]])
208-
209-
weighted_sum = 0.6 * zero_projector + 0.4 * one_projector
210-
np.testing.assert_allclose(weighted_sum.matrix(), [[0.6, 0.0], [0.0, 0.4]])

cirq-core/cirq/protocols/json_test_data/ProjectorSum.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

cirq-core/cirq/protocols/json_test_data/ProjectorSum.repr

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)