File tree 2 files changed +31
-5
lines changed
cirq-google/cirq_google/engine
2 files changed +31
-5
lines changed Original file line number Diff line number Diff line change @@ -181,7 +181,7 @@ def calibration_results(self) -> Sequence['calibration_result.CalibrationResult'
181
181
"""
182
182
183
183
def __iter__ (self ) -> Iterator [cirq .Result ]:
184
- return iter ( self .results () )
184
+ yield from self .results ()
185
185
186
186
# pylint: disable=function-redefined
187
187
@overload
Original file line number Diff line number Diff line change 12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
from typing import Dict , List , TYPE_CHECKING
15
+ import pytest
16
+ import numpy as np
17
+ import cirq
15
18
from cirq_google .engine .abstract_job import AbstractJob
16
19
17
20
if TYPE_CHECKING :
@@ -83,17 +86,40 @@ def batched_results(self):
83
86
pass
84
87
85
88
def results (self ):
86
- return list (range (5 ))
89
+ return list (
90
+ cirq .ResultDict (params = {}, measurements = {'a' : np .asarray ([t ])}) for t in range (5 )
91
+ )
87
92
88
93
def calibration_results (self ):
89
94
pass
90
95
91
96
92
97
def test_instantiation_and_iteration ():
93
98
job = MockJob ()
99
+
100
+ # Test length
94
101
assert len (job ) == 5
95
- assert job [3 ] == 3
102
+
103
+ # Test direct indexing
104
+ assert job [3 ].measurements ['a' ][0 ] == 3
105
+
106
+ # Test iterating through for loop
96
107
count = 0
97
- for num in job :
98
- assert num == count
108
+ for result in job :
109
+ assert result . measurements [ 'a' ][ 0 ] == count
99
110
count += 1
111
+
112
+ # Test iterator using iterator
113
+ iterator = iter (job )
114
+ result = next (iterator )
115
+ assert result .measurements ['a' ][0 ] == 0
116
+ result = next (iterator )
117
+ assert result .measurements ['a' ][0 ] == 1
118
+ result = next (iterator )
119
+ assert result .measurements ['a' ][0 ] == 2
120
+ result = next (iterator )
121
+ assert result .measurements ['a' ][0 ] == 3
122
+ result = next (iterator )
123
+ assert result .measurements ['a' ][0 ] == 4
124
+ with pytest .raises (StopIteration ):
125
+ next (iterator )
You can’t perform that action at this time.
0 commit comments