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
+ from dataclasses import dataclass
14
15
15
16
import cirq
16
17
import cirq_google as cg
17
18
import numpy as np
19
+ from cirq_google .workflow ._abstract_engine_processor_shim import AbstractEngineProcessorShim
18
20
from cirq_google .workflow .quantum_executable_test import _get_example_spec
19
21
20
22
23
+ @dataclass
24
+ class _MockEngineProcessor (AbstractEngineProcessorShim ):
25
+ def get_device (self ) -> cirq .Device :
26
+ return cg .Sycamore23
27
+
28
+ def get_sampler (self ) -> cirq .Sampler :
29
+ return cirq .ZerosSampler ()
30
+
31
+ def _json_dict_ (self ):
32
+ return cirq .obj_to_dict_helper (self , attribute_names = [], namespace = 'cirq.google.testing' )
33
+
34
+
21
35
def cg_assert_equivalent_repr (value ):
22
36
"""cirq.testing.assert_equivalent_repr with cirq_google.workflow imported."""
23
37
return cirq .testing .assert_equivalent_repr (
24
38
value ,
25
39
global_vals = {
26
40
'cirq_google' : cg ,
41
+ '_MockEngineProcessor' : _MockEngineProcessor ,
27
42
},
28
43
)
29
44
@@ -46,3 +61,62 @@ def test_executable_result():
46
61
raw_data = cirq .Result (params = cirq .ParamResolver (), measurements = {'z' : np .ones ((1_000 , 4 ))}),
47
62
)
48
63
cg_assert_equivalent_repr (er )
64
+
65
+
66
+ def _cg_read_json_gzip (fn ):
67
+ def _testing_resolver (cirq_type : str ):
68
+ if cirq_type == 'cirq.google.testing._MockEngineProcessor' :
69
+ return _MockEngineProcessor
70
+
71
+ return cirq .read_json_gzip (fn , resolvers = [_testing_resolver ] + cirq .DEFAULT_RESOLVERS )
72
+
73
+
74
+ def _assert_json_roundtrip (o , tmpdir ):
75
+ cirq .to_json_gzip (o , f'{ tmpdir } /o.json' )
76
+ o2 = _cg_read_json_gzip (f'{ tmpdir } /o.json' )
77
+ assert o == o2
78
+
79
+
80
+ def test_quantum_runtime_configuration ():
81
+ rt_config = cg .QuantumRuntimeConfiguration (
82
+ processor = _MockEngineProcessor (),
83
+ run_id = 'unit-test' ,
84
+ )
85
+
86
+ sampler = rt_config .processor .get_sampler ()
87
+ result = sampler .run (cirq .Circuit (cirq .measure (cirq .LineQubit (0 ), key = 'z' )))
88
+ assert isinstance (result , cirq .Result )
89
+
90
+ assert isinstance (rt_config .processor .get_device (), cirq .Device )
91
+
92
+
93
+ def test_quantum_runtime_configuration_serialization (tmpdir ):
94
+ rt_config = cg .QuantumRuntimeConfiguration (
95
+ processor = _MockEngineProcessor (),
96
+ run_id = 'unit-test' ,
97
+ )
98
+ cg_assert_equivalent_repr (rt_config )
99
+ _assert_json_roundtrip (rt_config , tmpdir )
100
+
101
+
102
+ def test_executable_group_result (tmpdir ):
103
+ egr = cg .ExecutableGroupResult (
104
+ runtime_configuration = cg .QuantumRuntimeConfiguration (
105
+ processor = _MockEngineProcessor (),
106
+ run_id = 'unit-test' ,
107
+ ),
108
+ shared_runtime_info = cg .SharedRuntimeInfo (run_id = 'my run' ),
109
+ executable_results = [
110
+ cg .ExecutableResult (
111
+ spec = _get_example_spec (name = f'test-spec-{ i } ' ),
112
+ runtime_info = cg .RuntimeInfo (execution_index = i ),
113
+ raw_data = cirq .Result (
114
+ params = cirq .ParamResolver (), measurements = {'z' : np .ones ((1_000 , 4 ))}
115
+ ),
116
+ )
117
+ for i in range (3 )
118
+ ],
119
+ )
120
+ cg_assert_equivalent_repr (egr )
121
+ assert len (egr .executable_results ) == 3
122
+ _assert_json_roundtrip (egr , tmpdir )
0 commit comments