@@ -49,80 +49,16 @@ def run_google(job: Job) -> Result:
49
49
Returns:
50
50
A Result after submission and execution of the job.
51
51
"""
52
- if type (job .device ) != GOOGLEDevice :
53
- raise ValueError ("Job device must be GOOGLEDevice" )
54
-
55
- if job .device .is_processor ():
56
- return run_processor (job )
57
- elif job .device .is_remote ():
58
- return run_google_remote (job )
59
- else :
60
- return run_local (job )
61
-
62
-
63
- @typechecked
64
- def run_processor (job : Job ) -> Result :
65
- """
66
- Executes the job locally on processor.
67
-
68
- Args:
69
- job : The job to be executed.
70
-
71
- Returns:
72
- Result: The result after submission and execution of the job..
73
- """
74
- cal = load_median_device_calibration (job .device .value )
75
- device = create_device_from_processor_id (job .device .value )
76
- # noise_props = noise_properties_from_calibration(cal)
77
- # noise_model = NoiseModelFromGoogleNoiseProperties(noise_props)
78
- sim = QSimSimulator (noise = None )
79
- sim_processor = SimulatedLocalProcessor (
80
- processor_id = job .device .value ,
81
- sampler = sim ,
82
- device = device ,
83
- calibrations = {cal .timestamp // 1000 : cal },
84
- )
85
- sim = SimulatedLocalEngine ([sim_processor ])
86
-
87
- job_cirq_circuit = job .circuit .to_other_language (
88
- Language .CIRQ , processor_id = job .device .value
89
- )
90
- if not isinstance (job_cirq_circuit , Cirq_circuit ):
91
- raise ValueError ("Circuit must be Cirq_circuit" )
92
-
93
- if job .job_type == JobType .STATE_VECTOR :
94
- raise NotImplementedError (
95
- f"Does not handle { job .job_type } for processor for the moment"
96
- )
97
- elif job .job_type == JobType .OBSERVABLE :
98
- raise NotImplementedError (
99
- f"Does not handle { job .job_type } for processor for the moment"
100
- )
101
- elif job .job_type == JobType .SAMPLE :
102
- assert isinstance (job .measure , BasisMeasure )
103
- if isinstance (job .measure .basis , ComputationalBasis ):
104
- result_sim = sim .get_sampler (job .device .value ).run (
105
- job_cirq_circuit , repetitions = job .measure .shots
106
- )
107
- else :
108
- raise NotImplementedError (
109
- "Does not handle other basis than the ComputationalBasis for the moment"
110
- )
111
- else :
112
- raise ValueError (f"Job type { job .job_type } not handled" )
113
-
114
- return extract_result (result_sim , job , GOOGLEDevice .CIRQ_LOCAL_SIMULATOR )
52
+ return run_local (job ) if not job .device .is_remote () else run_google_remote (job )
115
53
116
54
117
55
@typechecked
118
56
def run_google_remote (job : Job ) -> Result :
119
57
120
- if type (job .device ) != GOOGLEDevice :
121
- raise ValueError ("Job device must be GOOGLEDevice" )
58
+ assert type (job .device ) == GOOGLEDevice
122
59
123
60
job_cirq_circuit = job .circuit .to_other_language (Language .CIRQ )
124
- if not isinstance (job_cirq_circuit , Cirq_circuit ):
125
- raise ValueError ("Circuit must be Cirq_circuit" )
61
+ assert isinstance (job_cirq_circuit , Cirq_circuit )
126
62
127
63
if job .device .is_ionq ():
128
64
if job .job_type != JobType .SAMPLE :
@@ -151,8 +87,7 @@ def run_google_remote(job: Job) -> Result:
151
87
f" does not handle { job .device } for the moment only ionq is supported"
152
88
)
153
89
154
- result = extract_result (result_sim , job , GOOGLEDevice .CIRQ_LOCAL_SIMULATOR )
155
- return result
90
+ return extract_result (result_sim , job , GOOGLEDevice .CIRQ_LOCAL_SIMULATOR )
156
91
157
92
158
93
@typechecked
@@ -169,22 +104,22 @@ def run_local(job: Job) -> Result:
169
104
Raises:
170
105
ValueError: If the job device is not GOOGLEDevice.
171
106
"""
172
- if type (job .device ) != GOOGLEDevice :
173
- raise ValueError ("Job device must be GOOGLEDevice" )
107
+ assert type (job .device ) == GOOGLEDevice
174
108
175
- job_cirq_circuit = job .circuit .to_other_language (Language .CIRQ )
109
+ if job .device .is_processor ():
110
+ return run_local_processor (job )
176
111
177
- if not isinstance ( job_cirq_circuit , Cirq_circuit ):
178
- raise ValueError ( "Circuit must be Cirq_circuit" )
112
+ job_cirq_circuit = job . circuit . to_other_language ( Language . CIRQ )
113
+ assert isinstance ( job_cirq_circuit , Cirq_circuit )
179
114
180
- sim = Simulator ()
115
+ simulator = Simulator ()
181
116
182
117
if job .job_type == JobType .STATE_VECTOR :
183
- result_sim = sim .simulate (job_cirq_circuit )
118
+ result_sim = simulator .simulate (job_cirq_circuit )
184
119
elif job .job_type == JobType .SAMPLE :
185
120
assert isinstance (job .measure , BasisMeasure )
186
121
if isinstance (job .measure .basis , ComputationalBasis ):
187
- result_sim = sim .run (job_cirq_circuit , repetitions = job .measure .shots )
122
+ result_sim = simulator .run (job_cirq_circuit , repetitions = job .measure .shots )
188
123
else :
189
124
raise NotImplementedError (
190
125
"Does not handle other basis than the ComputationalBasis for the moment"
@@ -195,23 +130,77 @@ def run_local(job: Job) -> Result:
195
130
cirq_obs = job .measure .observable .to_other_language (
196
131
language = Language .CIRQ , circuit = job_cirq_circuit
197
132
)
198
-
199
- if type (cirq_obs ) != Cirq_PauliSum :
200
- raise ValueError ("cirq_obs must be a Cirq_PauliSum object" )
133
+ assert type (cirq_obs ) == Cirq_PauliSum
201
134
202
135
if job .measure .shots == 0 :
203
- result_sim = sim .simulate_expectation_values (
136
+ result_sim = simulator .simulate_expectation_values (
204
137
job_cirq_circuit , observables = cirq_obs
205
138
)
206
139
else :
207
140
result_sim = measure_observables (
208
141
job_cirq_circuit ,
209
142
cirq_obs , # type: ignore[reportArgumentType]
210
- sim ,
143
+ simulator ,
211
144
stopping_criteria = RepetitionsStoppingCriteria (job .measure .shots ),
212
145
)
213
146
else :
214
147
raise ValueError (f"Job type { job .job_type } not handled" )
148
+
149
+ return extract_result (result_sim , job , GOOGLEDevice .CIRQ_LOCAL_SIMULATOR )
150
+
151
+
152
+ @typechecked
153
+ def run_local_processor (job : Job ) -> Result :
154
+ """
155
+ Executes the job locally on processor.
156
+
157
+ Args:
158
+ job : The job to be executed.
159
+
160
+ Returns:
161
+ Result: The result after submission and execution of the job.
162
+ """
163
+ calibration = load_median_device_calibration (job .device .value )
164
+ device = create_device_from_processor_id (job .device .value )
165
+
166
+ # noise_props = noise_properties_from_calibration(cal)
167
+ # noise_model = NoiseModelFromGoogleNoiseProperties(noise_props)
168
+
169
+ simulator = QSimSimulator (noise = None )
170
+ sim_processor = SimulatedLocalProcessor (
171
+ processor_id = job .device .value ,
172
+ sampler = simulator ,
173
+ device = device ,
174
+ calibrations = {calibration .timestamp // 1000 : calibration },
175
+ )
176
+ simulator = SimulatedLocalEngine ([sim_processor ])
177
+
178
+ job_cirq_circuit = job .circuit .to_other_language (
179
+ Language .CIRQ , processor_id = job .device .value
180
+ )
181
+ assert isinstance (job_cirq_circuit , Cirq_circuit )
182
+
183
+ if job .job_type == JobType .STATE_VECTOR :
184
+ raise NotImplementedError (
185
+ f"Does not handle { job .job_type } for processor for the moment"
186
+ )
187
+ elif job .job_type == JobType .OBSERVABLE :
188
+ raise NotImplementedError (
189
+ f"Does not handle { job .job_type } for processor for the moment"
190
+ )
191
+ elif job .job_type == JobType .SAMPLE :
192
+ assert isinstance (job .measure , BasisMeasure )
193
+ if isinstance (job .measure .basis , ComputationalBasis ):
194
+ result_sim = simulator .get_sampler (job .device .value ).run (
195
+ job_cirq_circuit , repetitions = job .measure .shots
196
+ )
197
+ else :
198
+ raise NotImplementedError (
199
+ "Does not handle other basis than the ComputationalBasis for the moment"
200
+ )
201
+ else :
202
+ raise ValueError (f"Job type { job .job_type } not handled" )
203
+
215
204
return extract_result (result_sim , job , GOOGLEDevice .CIRQ_LOCAL_SIMULATOR )
216
205
217
206
@@ -246,19 +235,19 @@ def extract_result(
246
235
if job .job_type == JobType .SAMPLE :
247
236
if not isinstance (result , cirq_result ):
248
237
raise ValueError (
249
- f"result must be a cirq_result for job type { job .job_type } "
238
+ f"result: { type ( result ) } , must be a cirq_result for job type { job .job_type } "
250
239
)
251
240
return extract_result_SAMPLE (result , job , device )
252
241
elif job .job_type == JobType .STATE_VECTOR :
253
242
if not isinstance (result , StateVectorTrialResult ):
254
243
raise ValueError (
255
- f"result must be a cirq_result for job type { job .job_type } "
244
+ f"result: { type ( result ) } , must be a cirq_result for job type { job .job_type } "
256
245
)
257
246
return extract_result_STATE_VECTOR (result , job , device )
258
247
elif job .job_type == JobType .OBSERVABLE :
259
248
if isinstance (result , cirq_result ):
260
249
raise ValueError (
261
- f"result must be a list[float] | list[ObservableMeasuredResult] for job type { job .job_type } "
250
+ f"result: { type ( result ) } , must be a list[float] | list[ObservableMeasuredResult] for job type { job .job_type } "
262
251
)
263
252
return extract_result_OBSERVABLE (result , job , device )
264
253
else :
0 commit comments