Skip to content

Commit f696d40

Browse files
tonybruguierrht
authored andcommitted
Add missing code for ProjectorSum serialization in TFQ (quantumlib#4456)
For issue quantumlib#3288 I noticed that some code was missing when working on integrating with TFQ: tensorflow/quantum#623
1 parent 3b5dd63 commit f696d40

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

cirq-core/cirq/ops/linear_combinations.py

+5
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,11 @@ def __init__(
767767
def _value_equality_values_(self):
768768
return self._linear_dict
769769

770+
@property
771+
def qubits(self) -> Tuple[raw_types.Qid, ...]:
772+
qs = {q for k in self._linear_dict.keys() for q, _ in k}
773+
return tuple(sorted(qs))
774+
770775
def _json_dict_(self) -> Dict[str, Any]:
771776
linear_dict = []
772777
for projector_dict, scalar in dict(self._linear_dict).items():

cirq-core/cirq/ops/linear_combinations_test.py

+14
Original file line numberDiff line numberDiff line change
@@ -1947,3 +1947,17 @@ def test_projector_sum_division():
19471947

19481948
with pytest.raises(TypeError):
19491949
_ = zero_projector_sum / 'not_the_correct_type'
1950+
1951+
1952+
@pytest.mark.parametrize(
1953+
'terms, expected_qubits',
1954+
(
1955+
([], ()),
1956+
([cirq.ProjectorString({q0: 0})], (q0,)),
1957+
([cirq.ProjectorString({q0: 0}), cirq.ProjectorString({q1: 0})], (q0, q1)),
1958+
([cirq.ProjectorString({q0: 0, q1: 1})], (q0, q1)),
1959+
),
1960+
)
1961+
def test_projector_sum_has_correct_qubits(terms, expected_qubits):
1962+
combination = cirq.ProjectorSum.from_projector_strings(terms)
1963+
assert combination.qubits == expected_qubits

cirq-core/cirq/ops/projector.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _check_qids_dimension(qids):
2323
raise ValueError(f"Only qubits are supported, but {qid} has dimension {qid.dimension}")
2424

2525

26-
@value.value_equality
26+
@value.value_equality(approximate=True)
2727
class ProjectorString:
2828
def __init__(
2929
self,

0 commit comments

Comments
 (0)