|
39 | 39 | from cirq._compat import deprecated_parameter
|
40 | 40 | from cirq_google.cloud import quantum
|
41 | 41 | from cirq_google.engine.asyncio_executor import AsyncioExecutor
|
| 42 | +from cirq_google.engine import stream_manager |
42 | 43 |
|
43 | 44 | _M = TypeVar('_M', bound=proto.Message)
|
44 | 45 | _R = TypeVar('_R')
|
@@ -106,6 +107,10 @@ async def make_client():
|
106 | 107 |
|
107 | 108 | return self._executor.submit(make_client).result()
|
108 | 109 |
|
| 110 | + @cached_property |
| 111 | + def _stream_manager(self) -> stream_manager.StreamManager: |
| 112 | + return stream_manager.StreamManager(self.grpc_client) |
| 113 | + |
109 | 114 | async def _send_request_async(self, func: Callable[[_M], Awaitable[_R]], request: _M) -> _R:
|
110 | 115 | """Sends a request by invoking an asyncio callable."""
|
111 | 116 | return await self._run_retry_async(func, request)
|
@@ -736,6 +741,97 @@ async def get_job_results_async(
|
736 | 741 |
|
737 | 742 | get_job_results = duet.sync(get_job_results_async)
|
738 | 743 |
|
| 744 | + def run_job_over_stream( |
| 745 | + self, |
| 746 | + *, |
| 747 | + project_id: str, |
| 748 | + program_id: str, |
| 749 | + code: any_pb2.Any, |
| 750 | + run_context: any_pb2.Any, |
| 751 | + program_description: Optional[str] = None, |
| 752 | + program_labels: Optional[Dict[str, str]] = None, |
| 753 | + job_id: str, |
| 754 | + priority: Optional[int] = None, |
| 755 | + job_description: Optional[str] = None, |
| 756 | + job_labels: Optional[Dict[str, str]] = None, |
| 757 | + processor_id: str = "", |
| 758 | + run_name: str = "", |
| 759 | + device_config_name: str = "", |
| 760 | + ) -> duet.AwaitableFuture[Union[quantum.QuantumResult, quantum.QuantumJob]]: |
| 761 | + """Runs a job with the given program and job information over a stream. |
| 762 | +
|
| 763 | + Sends the request over the Quantum Engine QuantumRunStream bidirectional stream, and returns |
| 764 | + a future for the stream response. The future will be completed with a `QuantumResult` if |
| 765 | + the job is successful; otherwise, it will be completed with a QuantumJob. |
| 766 | +
|
| 767 | + Args: |
| 768 | + project_id: A project_id of the parent Google Cloud Project. |
| 769 | + program_id: Unique ID of the program within the parent project. |
| 770 | + code: Properly serialized program code. |
| 771 | + run_context: Properly serialized run context. |
| 772 | + program_description: An optional description to set on the program. |
| 773 | + program_labels: Optional set of labels to set on the program. |
| 774 | + job_id: Unique ID of the job within the parent program. |
| 775 | + priority: Optional priority to run at, 0-1000. |
| 776 | + job_description: Optional description to set on the job. |
| 777 | + job_labels: Optional set of labels to set on the job. |
| 778 | + processor_id: Processor id for running the program. If not set, |
| 779 | + `processor_ids` will be used. |
| 780 | + run_name: A unique identifier representing an automation run for the |
| 781 | + specified processor. An Automation Run contains a collection of |
| 782 | + device configurations for a processor. If specified, `processor_id` |
| 783 | + is required to be set. |
| 784 | + device_config_name: An identifier used to select the processor configuration |
| 785 | + utilized to run the job. A configuration identifies the set of |
| 786 | + available qubits, couplers, and supported gates in the processor. |
| 787 | + If specified, `processor_id` is required to be set. |
| 788 | +
|
| 789 | + Returns: |
| 790 | + A future for the job result, or the job if the job has failed. |
| 791 | +
|
| 792 | + Raises: |
| 793 | + ValueError: If the priority is not between 0 and 1000. |
| 794 | + ValueError: If `processor_id` is not set. |
| 795 | + ValueError: If only one of `run_name` and `device_config_name` are specified. |
| 796 | + """ |
| 797 | + # Check program to run and program parameters. |
| 798 | + if priority and not 0 <= priority < 1000: |
| 799 | + raise ValueError('priority must be between 0 and 1000') |
| 800 | + if not processor_id: |
| 801 | + raise ValueError('Must specify a processor id when creating a job.') |
| 802 | + if bool(run_name) ^ bool(device_config_name): |
| 803 | + raise ValueError('Cannot specify only one of `run_name` and `device_config_name`') |
| 804 | + |
| 805 | + project_name = _project_name(project_id) |
| 806 | + |
| 807 | + program_name = _program_name_from_ids(project_id, program_id) |
| 808 | + program = quantum.QuantumProgram(name=program_name, code=code) |
| 809 | + if program_description: |
| 810 | + program.description = program_description |
| 811 | + if program_labels: |
| 812 | + program.labels.update(program_labels) |
| 813 | + |
| 814 | + job = quantum.QuantumJob( |
| 815 | + name=_job_name_from_ids(project_id, program_id, job_id), |
| 816 | + scheduling_config=quantum.SchedulingConfig( |
| 817 | + processor_selector=quantum.SchedulingConfig.ProcessorSelector( |
| 818 | + processor=_processor_name_from_ids(project_id, processor_id), |
| 819 | + device_config_key=quantum.DeviceConfigKey( |
| 820 | + run_name=run_name, config_alias=device_config_name |
| 821 | + ), |
| 822 | + ) |
| 823 | + ), |
| 824 | + run_context=run_context, |
| 825 | + ) |
| 826 | + if priority: |
| 827 | + job.scheduling_config.priority = priority |
| 828 | + if job_description: |
| 829 | + job.description = job_description |
| 830 | + if job_labels: |
| 831 | + job.labels.update(job_labels) |
| 832 | + |
| 833 | + return self._stream_manager.submit(project_name, program, job) |
| 834 | + |
739 | 835 | async def list_processors_async(self, project_id: str) -> List[quantum.QuantumProcessor]:
|
740 | 836 | """Returns a list of Processors that the user has visibility to in the
|
741 | 837 | current Engine project. The names of these processors are used to
|
|
0 commit comments