-
Notifications
You must be signed in to change notification settings - Fork 1k
/
engine_client.py
979 lines (835 loc) · 38.1 KB
/
engine_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
# Copyright 2020 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import sys
import time
from typing import Callable, Dict, List, Optional, Sequence, Set, TypeVar, Tuple, Union
import warnings
from google.api_core.exceptions import GoogleAPICallError, NotFound
from google.protobuf.timestamp_pb2 import Timestamp
from cirq_google.engine.client import quantum
from cirq_google.engine.client.quantum import types as qtypes
_R = TypeVar('_R')
class EngineException(Exception):
def __init__(self, message):
# Call the base class constructor with the parameters it needs
super().__init__(message)
RETRYABLE_ERROR_CODES = [500, 503]
class EngineClient:
"""Client for the Quantum Engine API that deals with the engine protos and
the gRPC client but not cirq protos or objects. All users are likely better
served by using the Engine, EngineProgram, EngineJob, EngineProcessor, and
Calibration objects instead of using this directly.
"""
def __init__(
self,
service_args: Optional[Dict] = None,
verbose: Optional[bool] = None,
max_retry_delay_seconds: int = 3600, # 1 hour
) -> None:
"""Engine service client.
Args:
service_args: A dictionary of arguments that can be used to
configure options on the underlying gRPC client.
verbose: Suppresses stderr messages when set to False. Default is
true.
max_retry_delay_seconds: The maximum number of seconds to retry when
a retryable error code is returned.
"""
self.max_retry_delay_seconds = max_retry_delay_seconds
if verbose is None:
verbose = True
self.verbose = verbose
if not service_args:
service_args = {}
# Suppress warnings about using Application Default Credentials.
with warnings.catch_warnings():
warnings.simplefilter('ignore')
self.grpc_client = quantum.QuantumEngineServiceClient(**service_args)
def _make_request(self, request: Callable[[], _R]) -> _R:
# Start with a 100ms retry delay with exponential backoff to
# max_retry_delay_seconds
current_delay = 0.1
while True:
try:
return request()
except GoogleAPICallError as err:
message = err.message
# Raise RuntimeError for exceptions that are not retryable.
# Otherwise, pass through to retry.
if err.code.value not in RETRYABLE_ERROR_CODES:
raise EngineException(message) from err
if current_delay > self.max_retry_delay_seconds:
raise TimeoutError(f'Reached max retry attempts for error: {message}')
if self.verbose:
print(message, file=sys.stderr)
print('Waiting ', current_delay, 'seconds before retrying.', file=sys.stderr)
time.sleep(current_delay)
current_delay *= 2
def create_program(
self,
project_id: str,
program_id: Optional[str],
code: qtypes.any_pb2.Any,
description: Optional[str] = None,
labels: Optional[Dict[str, str]] = None,
) -> Tuple[str, qtypes.QuantumProgram]:
"""Creates a Quantum Engine program.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
code: Properly serialized program code.
description: An optional description to set on the program.
labels: Optional set of labels to set on the program.
Returns:
Tuple of created program id and program
"""
parent_name = _project_name(project_id)
program_name = _program_name_from_ids(project_id, program_id) if program_id else ''
request = qtypes.QuantumProgram(name=program_name, code=code)
if description:
request.description = description
if labels:
request.labels.update(labels)
program = self._make_request(
lambda: self.grpc_client.create_quantum_program(parent_name, request, False)
)
return _ids_from_program_name(program.name)[1], program
def get_program(
self, project_id: str, program_id: str, return_code: bool
) -> qtypes.QuantumProgram:
"""Returns a previously created quantum program.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
return_code: If True returns the serialized program code.
"""
return self._make_request(
lambda: self.grpc_client.get_quantum_program(
_program_name_from_ids(project_id, program_id), return_code
)
)
def list_programs(
self,
project_id: str,
created_before: Optional[Union[datetime.datetime, datetime.date]] = None,
created_after: Optional[Union[datetime.datetime, datetime.date]] = None,
has_labels: Optional[Dict[str, str]] = None,
):
"""Returns a list of previously executed quantum programs.
Args:
project_id: the id of the project
created_after: retrieve programs that were created after this date
or time.
created_before: retrieve programs that were created after this date
or time.
has_labels: retrieve programs that have labels on them specified by
this dict. If the value is set to `*`, filters having the label
egardless of the label value will be filtered. For example, to
uery programs that have the shape label and have the color
label with value red can be queried using
{'color': 'red', 'shape':'*'}
"""
filters = []
if created_after is not None:
val = _date_or_time_to_filter_expr('created_after', created_after)
filters.append(f"create_time >= {val}")
if created_before is not None:
val = _date_or_time_to_filter_expr('created_before', created_before)
filters.append(f"create_time <= {val}")
if has_labels is not None:
for (k, v) in has_labels.items():
filters.append(f"labels.{k}:{v}")
return self._make_request(
lambda: self.grpc_client.list_quantum_programs(
_project_name(project_id), filter_=" AND ".join(filters)
)
)
def set_program_description(
self, project_id: str, program_id: str, description: str
) -> qtypes.QuantumProgram:
"""Sets the description for a previously created quantum program.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
description: The new program description.
Returns:
The updated quantum program.
"""
program_resource_name = _program_name_from_ids(project_id, program_id)
return self._make_request(
lambda: self.grpc_client.update_quantum_program(
program_resource_name,
qtypes.QuantumProgram(name=program_resource_name, description=description),
qtypes.field_mask_pb2.FieldMask(paths=['description']),
)
)
def _set_program_labels(
self, project_id: str, program_id: str, labels: Dict[str, str], fingerprint: str
) -> qtypes.QuantumProgram:
program_resource_name = _program_name_from_ids(project_id, program_id)
return self._make_request(
lambda: self.grpc_client.update_quantum_program(
program_resource_name,
qtypes.QuantumProgram(
name=program_resource_name, labels=labels, label_fingerprint=fingerprint
),
qtypes.field_mask_pb2.FieldMask(paths=['labels']),
)
)
def set_program_labels(
self, project_id: str, program_id: str, labels: Dict[str, str]
) -> qtypes.QuantumProgram:
"""Sets (overwriting) the labels for a previously created quantum
program.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
labels: The entire set of new program labels.
Returns:
The updated quantum program.
"""
program = self.get_program(project_id, program_id, False)
return self._set_program_labels(project_id, program_id, labels, program.label_fingerprint)
def add_program_labels(
self, project_id: str, program_id: str, labels: Dict[str, str]
) -> qtypes.QuantumProgram:
"""Adds new labels to a previously created quantum program.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
labels: New labels to add to the existing program labels.
Returns:
The updated quantum program.
"""
program = self.get_program(project_id, program_id, False)
old_labels = program.labels
new_labels = dict(old_labels)
new_labels.update(labels)
if new_labels != old_labels:
fingerprint = program.label_fingerprint
return self._set_program_labels(project_id, program_id, new_labels, fingerprint)
return program
def remove_program_labels(
self, project_id: str, program_id: str, label_keys: List[str]
) -> qtypes.QuantumProgram:
"""Removes labels with given keys from the labels of a previously
created quantum program.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
label_keys: Label keys to remove from the existing program labels.
Returns:
The updated quantum program.
"""
program = self.get_program(project_id, program_id, False)
old_labels = program.labels
new_labels = dict(old_labels)
for key in label_keys:
new_labels.pop(key, None)
if new_labels != old_labels:
fingerprint = program.label_fingerprint
return self._set_program_labels(project_id, program_id, new_labels, fingerprint)
return program
def delete_program(self, project_id: str, program_id: str, delete_jobs: bool = False) -> None:
"""Deletes a previously created quantum program.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
delete_jobs: If True will delete all the program's jobs, other this
will fail if the program contains any jobs.
"""
self._make_request(
lambda: self.grpc_client.delete_quantum_program(
_program_name_from_ids(project_id, program_id), delete_jobs
)
)
def create_job(
self,
project_id: str,
program_id: str,
job_id: Optional[str],
processor_ids: Sequence[str],
run_context: qtypes.any_pb2.Any,
priority: Optional[int] = None,
description: Optional[str] = None,
labels: Optional[Dict[str, str]] = None,
) -> Tuple[str, qtypes.QuantumJob]:
"""Creates and runs a job on Quantum Engine.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
job_id: Unique ID of the job within the parent program.
run_context: Properly serialized run context.
processor_ids: List of processor id for running the program.
priority: Optional priority to run at, 0-1000.
description: Optional description to set on the job.
labels: Optional set of labels to set on the job.
Returns:
Tuple of created job id and job.
Raises:
ValueError: If the priority is not betwen 0 and 1000.
"""
# Check program to run and program parameters.
if priority and not 0 <= priority < 1000:
raise ValueError('priority must be between 0 and 1000')
# Create job.
job_name = _job_name_from_ids(project_id, program_id, job_id) if job_id else ''
request = qtypes.QuantumJob(
name=job_name,
scheduling_config=qtypes.SchedulingConfig(
processor_selector=qtypes.SchedulingConfig.ProcessorSelector(
processor_names=[
_processor_name_from_ids(project_id, processor_id)
for processor_id in processor_ids
]
)
),
run_context=run_context,
)
if priority:
request.scheduling_config.priority = priority
if description:
request.description = description
if labels:
request.labels.update(labels)
job = self._make_request(
lambda: self.grpc_client.create_quantum_job(
_program_name_from_ids(project_id, program_id), request, False
)
)
return _ids_from_job_name(job.name)[2], job
def list_jobs(
self,
project_id: str,
program_id: Optional[str] = None,
created_before: Optional[Union[datetime.datetime, datetime.date]] = None,
created_after: Optional[Union[datetime.datetime, datetime.date]] = None,
has_labels: Optional[Dict[str, str]] = None,
execution_states: Optional[Set[quantum.enums.ExecutionStatus.State]] = None,
executed_processor_ids: Optional[List[str]] = None,
scheduled_processor_ids: Optional[List[str]] = None,
):
"""Returns the list of jobs for a given program.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Optional, a unique ID of the program within the parent
project. If None, jobs will be listed across all programs within
the project.
created_after: retrieve jobs that were created after this date
or time.
created_before: retrieve jobs that were created after this date
or time.
has_labels: retrieve jobs that have labels on them specified by
this dict. If the value is set to `*`, filters having the label
regardless of the label value will be filtered. For example, to
query programs that have the shape label and have the color
label with value red can be queried using
{'color': 'red', 'shape':'*'}
execution_states: retrieve jobs that have an execution state that
is contained in `execution_states`. See
`quantum.enums.ExecutionStatus.State` enum for accepted values.
executed_processor_ids: filters jobs by processor ID used for
execution. Matches any of provided IDs.
scheduled_processor_ids: filters jobs by any of provided
scheduled processor IDs.
"""
filters = []
if created_after is not None:
val = _date_or_time_to_filter_expr('created_after', created_after)
filters.append(f"create_time >= {val}")
if created_before is not None:
val = _date_or_time_to_filter_expr('created_before', created_before)
filters.append(f"create_time <= {val}")
if has_labels is not None:
for (k, v) in has_labels.items():
filters.append(f"labels.{k}:{v}")
if execution_states is not None:
state_filter = []
for execution_state in execution_states:
state_filter.append(f"execution_status.state = {execution_state.name}")
filters.append(f"({' OR '.join(state_filter)})")
if executed_processor_ids is not None:
ids_filter = []
for processor_id in executed_processor_ids:
ids_filter.append(f"executed_processor_id = {processor_id}")
filters.append(f"({' OR '.join(ids_filter)})")
if scheduled_processor_ids is not None:
ids_filter = []
for processor_id in scheduled_processor_ids:
ids_filter.append(f"scheduled_processor_ids: {processor_id}")
filters.append(f"({' OR '.join(ids_filter)})")
if program_id is None:
program_id = "-"
parent = _program_name_from_ids(project_id, program_id)
return self._make_request(
lambda: self.grpc_client.list_quantum_jobs(parent, filter_=" AND ".join(filters))
)
def get_job(
self, project_id: str, program_id: str, job_id: str, return_run_context: bool
) -> qtypes.QuantumJob:
"""Returns a previously created job.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
job_id: Unique ID of the job within the parent program.
return_run_context: If true then the run context will be loaded
from the job's run_context_location and set on the returned
QuantumJob.
"""
return self._make_request(
lambda: self.grpc_client.get_quantum_job(
_job_name_from_ids(project_id, program_id, job_id), return_run_context
)
)
def set_job_description(
self, project_id: str, program_id: str, job_id: str, description: str
) -> qtypes.QuantumJob:
"""Sets the description for a previously created quantum job.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
job_id: Unique ID of the job within the parent program.
description: The new job description.
Returns:
The updated quantum job.
"""
job_resource_name = _job_name_from_ids(project_id, program_id, job_id)
return self._make_request(
lambda: self.grpc_client.update_quantum_job(
job_resource_name,
qtypes.QuantumJob(name=job_resource_name, description=description),
qtypes.field_mask_pb2.FieldMask(paths=['description']),
)
)
def _set_job_labels(
self,
project_id: str,
program_id: str,
job_id: str,
labels: Dict[str, str],
fingerprint: str,
) -> qtypes.QuantumJob:
job_resource_name = _job_name_from_ids(project_id, program_id, job_id)
return self._make_request(
lambda: self.grpc_client.update_quantum_job(
job_resource_name,
qtypes.QuantumJob(
name=job_resource_name, labels=labels, label_fingerprint=fingerprint
),
qtypes.field_mask_pb2.FieldMask(paths=['labels']),
)
)
def set_job_labels(
self, project_id: str, program_id: str, job_id: str, labels: Dict[str, str]
) -> qtypes.QuantumJob:
"""Sets (overwriting) the labels for a previously created quantum job.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
job_id: Unique ID of the job within the parent program.
labels: The entire set of new job labels.
Returns:
The updated quantum job.
"""
job = self.get_job(project_id, program_id, job_id, False)
return self._set_job_labels(project_id, program_id, job_id, labels, job.label_fingerprint)
def add_job_labels(
self, project_id: str, program_id: str, job_id: str, labels: Dict[str, str]
) -> qtypes.QuantumJob:
"""Adds new labels to a previously created quantum job.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
job_id: Unique ID of the job within the parent program.
labels: New labels to add to the existing job labels.
Returns:
The updated quantum job.
"""
job = self.get_job(project_id, program_id, job_id, False)
old_labels = job.labels
new_labels = dict(old_labels)
new_labels.update(labels)
if new_labels != old_labels:
fingerprint = job.label_fingerprint
return self._set_job_labels(project_id, program_id, job_id, new_labels, fingerprint)
return job
def remove_job_labels(
self, project_id: str, program_id: str, job_id: str, label_keys: List[str]
) -> qtypes.QuantumJob:
"""Removes labels with given keys from the labels of a previously
created quantum job.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
job_id: Unique ID of the job within the parent program.
label_keys: Label keys to remove from the existing job labels.
Returns:
The updated quantum job.
"""
job = self.get_job(project_id, program_id, job_id, False)
old_labels = job.labels
new_labels = dict(old_labels)
for key in label_keys:
new_labels.pop(key, None)
if new_labels != old_labels:
fingerprint = job.label_fingerprint
return self._set_job_labels(project_id, program_id, job_id, new_labels, fingerprint)
return job
def delete_job(self, project_id: str, program_id: str, job_id: str) -> None:
"""Deletes a previously created quantum job.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
job_id: Unique ID of the job within the parent program.
"""
self._make_request(
lambda: self.grpc_client.delete_quantum_job(
_job_name_from_ids(project_id, program_id, job_id)
)
)
def cancel_job(self, project_id: str, program_id: str, job_id: str) -> None:
"""Cancels the given job.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
job_id: Unique ID of the job within the parent program.
"""
self._make_request(
lambda: self.grpc_client.cancel_quantum_job(
_job_name_from_ids(project_id, program_id, job_id)
)
)
def get_job_results(
self, project_id: str, program_id: str, job_id: str
) -> qtypes.QuantumResult:
"""Returns the results of a completed job.
Args:
project_id: A project_id of the parent Google Cloud Project.
program_id: Unique ID of the program within the parent project.
job_id: Unique ID of the job within the parent program.
Returns:
The quantum result.
"""
return self._make_request(
lambda: self.grpc_client.get_quantum_result(
_job_name_from_ids(project_id, program_id, job_id)
)
)
def list_processors(self, project_id: str) -> List[qtypes.QuantumProcessor]:
"""Returns a list of Processors that the user has visibility to in the
current Engine project. The names of these processors are used to
identify devices when scheduling jobs and gathering calibration metrics.
Args:
project_id: A project_id of the parent Google Cloud Project.
Returns:
A list of metadata of each processor.
"""
response = self._make_request(
lambda: self.grpc_client.list_quantum_processors(_project_name(project_id), filter_='')
)
return list(response)
def get_processor(self, project_id: str, processor_id: str) -> qtypes.QuantumProcessor:
"""Returns a quantum processor.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
Returns:
The quantum processor.
"""
return self._make_request(
lambda: self.grpc_client.get_quantum_processor(
_processor_name_from_ids(project_id, processor_id)
)
)
def list_calibrations(
self, project_id: str, processor_id: str, filter_str: str = ''
) -> List[qtypes.QuantumCalibration]:
"""Returns a list of quantum calibrations.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
filter_str: Filter string current only supports 'timestamp' with values
of epoch time in seconds or short string 'yyyy-MM-dd'. For example:
'timestamp > 1577960125 AND timestamp <= 1578241810'
'timestamp > 2020-01-02 AND timestamp <= 2020-01-05'
Returns:
A list of calibrations.
"""
response = self._make_request(
lambda: self.grpc_client.list_quantum_calibrations(
_processor_name_from_ids(project_id, processor_id), filter_=filter_str
)
)
return list(response)
def get_calibration(
self, project_id: str, processor_id: str, calibration_timestamp_seconds: int
) -> qtypes.QuantumCalibration:
"""Returns a quantum calibration.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
calibration_timestamp_seconds: The timestamp of the calibration in
seconds.
Returns:
The quantum calibration.
"""
return self._make_request(
lambda: self.grpc_client.get_quantum_calibration(
_calibration_name_from_ids(project_id, processor_id, calibration_timestamp_seconds)
)
)
def get_current_calibration(
self, project_id: str, processor_id: str
) -> Optional[qtypes.QuantumCalibration]:
"""Returns the current quantum calibration for a processor if it has one.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
Returns:
The quantum calibration or None if there is no current calibration.
Raises:
EngineException: If the request for calibration fails.
"""
try:
return self._make_request(
lambda: self.grpc_client.get_quantum_calibration(
_processor_name_from_ids(project_id, processor_id) + '/calibrations/current'
)
)
except EngineException as err:
if isinstance(err.__cause__, NotFound):
return None
raise
def create_reservation(
self,
project_id: str,
processor_id: str,
start: datetime.datetime,
end: datetime.datetime,
whitelisted_users: Optional[List[str]] = None,
):
"""Creates a quantum reservation and returns the created object.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
reservation_id: Unique ID of the reservation in the parent project,
or None if the engine should generate an id
start: the starting time of the reservation as a datetime object
end: the ending time of the reservation as a datetime object
whitelisted_users: a list of emails that can use the reservation.
"""
parent = _processor_name_from_ids(project_id, processor_id)
reservation = qtypes.QuantumReservation(
name='',
start_time=Timestamp(seconds=int(start.timestamp())),
end_time=Timestamp(seconds=int(end.timestamp())),
)
if whitelisted_users:
reservation.whitelisted_users.extend(whitelisted_users)
return self._make_request(
lambda: self.grpc_client.create_quantum_reservation(
parent=parent, quantum_reservation=reservation
)
)
def cancel_reservation(self, project_id: str, processor_id: str, reservation_id: str):
"""Cancels a quantum reservation.
This action is only valid if the associated [QuantumProcessor]
schedule not been frozen. Otherwise, delete_reservation should
be used.
The reservation will be truncated to end at the time when the request is
serviced and any remaining time will be made available as an open swim
period. This action will only succeed if the reservation has not yet
ended and is within the processor's freeze window. If the reservation
has already ended or is beyond the processor's freeze window, then the
call will return an error.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
reservation_id: Unique ID of the reservation in the parent project,
"""
name = _reservation_name_from_ids(project_id, processor_id, reservation_id)
return self._make_request(lambda: self.grpc_client.cancel_quantum_reservation(name=name))
def delete_reservation(self, project_id: str, processor_id: str, reservation_id: str):
"""Deletes a quantum reservation.
This action is only valid if the associated [QuantumProcessor]
schedule has not been frozen. Otherwise, cancel_reservation
should be used.
If the reservation has already ended or is within the processor's
freeze window, then the call will return a `FAILED_PRECONDITION` error.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
reservation_id: Unique ID of the reservation in the parent project,
"""
name = _reservation_name_from_ids(project_id, processor_id, reservation_id)
return self._make_request(lambda: self.grpc_client.delete_quantum_reservation(name=name))
def get_reservation(self, project_id: str, processor_id: str, reservation_id: str):
"""Gets a quantum reservation from the engine.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
reservation_id: Unique ID of the reservation in the parent project.
Raises:
EngineException: If the request to get the reservation failed.
"""
try:
name = _reservation_name_from_ids(project_id, processor_id, reservation_id)
return self._make_request(lambda: self.grpc_client.get_quantum_reservation(name=name))
except EngineException as err:
if isinstance(err.__cause__, NotFound):
return None
raise
def list_reservations(
self, project_id: str, processor_id: str, filter_str: str = ''
) -> List[qtypes.QuantumReservation]:
"""Returns a list of quantum reservations.
Only reservations owned by this project will be returned.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
filter_str: A string for filtering quantum reservations.
The fields eligible for filtering are start_time and end_time
Examples:
`start_time >= 1584385200`: Reservation began on or after
the epoch time Mar 16th, 7pm GMT.
`end_time >= 1483370475`: Reservation ends on
or after Jan 2nd 2017 15:21:15
Returns:
A list of QuantumReservation objects.
"""
response = self._make_request(
lambda: self.grpc_client.list_quantum_reservations(
_processor_name_from_ids(project_id, processor_id), filter_=filter_str
)
)
return list(response)
def update_reservation(
self,
project_id: str,
processor_id: str,
reservation_id: str,
start: Optional[datetime.datetime] = None,
end: Optional[datetime.datetime] = None,
whitelisted_users: Optional[List[str]] = None,
):
"""Updates a quantum reservation.
This will update a quantum reservation's starting time, ending time,
and list of whitelisted users. If any field is not filled, it will
not be updated.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
reservation_id: Unique ID of the reservation in the parent project,
start: the new starting time of the reservation as a datetime object
end: the new ending time of the reservation as a datetime object
whitelisted_users: a list of emails that can use the reservation.
The empty list, [], will clear the whitelisted_users while None
will leave the value unchanged.
"""
name = (
_reservation_name_from_ids(project_id, processor_id, reservation_id)
if reservation_id
else ''
)
reservation = qtypes.QuantumReservation(
name=name,
)
paths = []
if start:
reservation.start_time.seconds = int(start.timestamp())
paths.append('start_time')
if end:
reservation.end_time.seconds = int(end.timestamp())
paths.append('end_time')
if whitelisted_users is not None:
reservation.whitelisted_users.extend(whitelisted_users)
paths.append('whitelisted_users')
return self._make_request(
lambda: self.grpc_client.update_quantum_reservation(
name=name,
quantum_reservation=reservation,
update_mask=qtypes.field_mask_pb2.FieldMask(paths=paths),
)
)
def list_time_slots(
self, project_id: str, processor_id: str, filter_str: str = ''
) -> List[qtypes.QuantumTimeSlot]:
"""Returns a list of quantum time slots on a processor.
Args:
project_id: A project_id of the parent Google Cloud Project.
processor_id: The processor unique identifier.
filter_str: A string expression for filtering the quantum
time slots returned by the list command. The fields
eligible for filtering are `start_time`, `end_time`.
Returns:
A list of QuantumTimeSlot objects.
"""
response = self._make_request(
lambda: self.grpc_client.list_quantum_time_slots(
_processor_name_from_ids(project_id, processor_id), filter_=filter_str
)
)
return list(response)
def _project_name(project_id: str) -> str:
return f'projects/{project_id}'
def _program_name_from_ids(project_id: str, program_id: str) -> str:
return f'projects/{project_id}/programs/{program_id}'
def _job_name_from_ids(project_id: str, program_id: str, job_id: str) -> str:
return f'projects/{project_id}/programs/{program_id}/jobs/{job_id}'
def _processor_name_from_ids(project_id: str, processor_id: str) -> str:
return f'projects/{project_id}/processors/{processor_id}'
def _calibration_name_from_ids(
project_id: str, processor_id: str, calibration_time_seconds: int
) -> str:
return 'projects/%s/processors/%s/calibrations/%d' % (
project_id,
processor_id,
calibration_time_seconds,
)
def _reservation_name_from_ids(project_id: str, processor_id: str, reservation_id: str) -> str:
return 'projects/%s/processors/%s/reservations/%s' % (
project_id,
processor_id,
reservation_id,
)
def _ids_from_program_name(program_name: str) -> Tuple[str, str]:
parts = program_name.split('/')
return parts[1], parts[3]
def _ids_from_job_name(job_name: str) -> Tuple[str, str, str]:
parts = job_name.split('/')
return parts[1], parts[3], parts[5]
def _ids_from_processor_name(processor_name: str) -> Tuple[str, str]:
parts = processor_name.split('/')
return parts[1], parts[3]
def _ids_from_calibration_name(calibration_name: str) -> Tuple[str, str, int]:
parts = calibration_name.split('/')
return parts[1], parts[3], int(parts[5])
def _date_or_time_to_filter_expr(param_name: str, param: Union[datetime.datetime, datetime.date]):
"""Formats datetime or date to filter expressions.
Args:
param_name: The name of the filter parameter (for error messaging).
param: The value of the parameter.
Raises:
ValueError: If the supplied param is not a datetime or date.
"""
if isinstance(param, datetime.datetime):
return f"{int(param.timestamp())}"
elif isinstance(param, datetime.date):
return f"{param.isoformat()}"
raise ValueError(
f"Unsupported date/time type for {param_name}: got {param} of "
f"type {type(param)}. Supported types: datetime.datetime and"
f"datetime.date"
)