|
38 | 38 | from cirq._compat import cached_property
|
39 | 39 | from cirq_google.cloud import quantum
|
40 | 40 | from cirq_google.engine.asyncio_executor import AsyncioExecutor
|
| 41 | +from cirq_google.engine import stream_manager |
41 | 42 |
|
42 | 43 | _M = TypeVar('_M', bound=proto.Message)
|
43 | 44 | _R = TypeVar('_R')
|
@@ -105,6 +106,10 @@ async def make_client():
|
105 | 106 |
|
106 | 107 | return self._executor.submit(make_client).result()
|
107 | 108 |
|
| 109 | + @cached_property |
| 110 | + def _stream_manager(self) -> stream_manager.StreamManager: |
| 111 | + return stream_manager.StreamManager(self.grpc_client) |
| 112 | + |
108 | 113 | async def _send_request_async(self, func: Callable[[_M], Awaitable[_R]], request: _M) -> _R:
|
109 | 114 | """Sends a request by invoking an asyncio callable."""
|
110 | 115 | return await self._run_retry_async(func, request)
|
@@ -697,6 +702,79 @@ async def get_job_results_async(
|
697 | 702 |
|
698 | 703 | get_job_results = duet.sync(get_job_results_async)
|
699 | 704 |
|
| 705 | + def run_job_over_stream( |
| 706 | + self, |
| 707 | + project_id: str, |
| 708 | + program_id: str, |
| 709 | + code: any_pb2.Any, |
| 710 | + job_id: str, |
| 711 | + processor_ids: Sequence[str], |
| 712 | + run_context: any_pb2.Any, |
| 713 | + program_description: Optional[str] = None, |
| 714 | + program_labels: Optional[Dict[str, str]] = None, |
| 715 | + priority: Optional[int] = None, |
| 716 | + job_description: Optional[str] = None, |
| 717 | + job_labels: Optional[Dict[str, str]] = None, |
| 718 | + ) -> duet.AwaitableFuture[Union[quantum.QuantumResult, quantum.QuantumJob]]: |
| 719 | + """Runs a job with the given program and job information over a stream. |
| 720 | +
|
| 721 | + Sends the request over the Quantum Engine QuantumRunStream bidirectional stream, and returns |
| 722 | + a future for the stream response. The future will be completed with a `QuantumResult` if |
| 723 | + the job is successful; otherwise, it will be completed with a QuantumJob. |
| 724 | +
|
| 725 | + Args: |
| 726 | + project_id: A project_id of the parent Google Cloud Project. |
| 727 | + program_id: Unique ID of the program within the parent project. |
| 728 | + code: Properly serialized program code. |
| 729 | + job_id: Unique ID of the job within the parent program. |
| 730 | + run_context: Properly serialized run context. |
| 731 | + processor_ids: List of processor id for running the program. |
| 732 | + program_description: An optional description to set on the program. |
| 733 | + program_labels: Optional set of labels to set on the program. |
| 734 | + priority: Optional priority to run at, 0-1000. |
| 735 | + job_description: Optional description to set on the job. |
| 736 | + job_labels: Optional set of labels to set on the job. |
| 737 | +
|
| 738 | + Returns: |
| 739 | + A future for the job result, or the job if the job has failed. |
| 740 | +
|
| 741 | + Raises: |
| 742 | + ValueError: If the priority is not between 0 and 1000. |
| 743 | + """ |
| 744 | + # Check program to run and program parameters. |
| 745 | + if priority and not 0 <= priority < 1000: |
| 746 | + raise ValueError('priority must be between 0 and 1000') |
| 747 | + |
| 748 | + project_name = _project_name(project_id) |
| 749 | + |
| 750 | + program_name = _program_name_from_ids(project_id, program_id) |
| 751 | + program = quantum.QuantumProgram(name=program_name, code=code) |
| 752 | + if program_description: |
| 753 | + program.description = program_description |
| 754 | + if program_labels: |
| 755 | + program.labels.update(program_labels) |
| 756 | + |
| 757 | + job = quantum.QuantumJob( |
| 758 | + name=_job_name_from_ids(project_id, program_id, job_id), |
| 759 | + scheduling_config=quantum.SchedulingConfig( |
| 760 | + processor_selector=quantum.SchedulingConfig.ProcessorSelector( |
| 761 | + processor_names=[ |
| 762 | + _processor_name_from_ids(project_id, processor_id) |
| 763 | + for processor_id in processor_ids |
| 764 | + ] |
| 765 | + ) |
| 766 | + ), |
| 767 | + run_context=run_context, |
| 768 | + ) |
| 769 | + if priority: |
| 770 | + job.scheduling_config.priority = priority |
| 771 | + if job_description: |
| 772 | + job.description = job_description |
| 773 | + if job_labels: |
| 774 | + job.labels.update(job_labels) |
| 775 | + |
| 776 | + return self._stream_manager.submit(project_name, program, job) |
| 777 | + |
700 | 778 | async def list_processors_async(self, project_id: str) -> List[quantum.QuantumProcessor]:
|
701 | 779 | """Returns a list of Processors that the user has visibility to in the
|
702 | 780 | current Engine project. The names of these processors are used to
|
|
0 commit comments