-
Notifications
You must be signed in to change notification settings - Fork 358
New: Experimental QCS Client #1368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7aa1e5
Upgrade: qcs-api-client
kalzoo f677c99
New(WIP): experimental subpackage
kalzoo baeaabb
Upgrade: qcs-api-client
kalzoo 0e84ce6
Fix: update qcs-api-client grpc API
notmgsk 3d78747
Merge branch 'rc' into experimental-api-client
notmgsk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from ._program import ExperimentalProgram |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from typing import Optional | ||
| from pyquil.quil import InstructionDesignator, Program | ||
|
|
||
|
|
||
| class ExperimentalProgram(Program): | ||
| """ | ||
| An ExperimentalProgram is identical to a Program except that ``num_shots`` is optional. | ||
| """ | ||
|
|
||
| num_shots: Optional[int] | ||
|
|
||
| def __init__(self, *instructions: InstructionDesignator): | ||
| super().__init__(*instructions) | ||
| self.num_shots = None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from ._compiler import ExperimentalQPUCompiler | ||
| from ._qpu import ExperimentalQPU | ||
| from ._quantum_computer import ExperimentalQuantumComputer, get_experimental_qc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from contextlib import contextmanager | ||
| from dataclasses import dataclass | ||
| import dataclasses | ||
| from pyquil.quilbase import Gate | ||
| from typing import Dict, Iterator, Optional, cast | ||
|
|
||
| from pyquil.api._abstract_compiler import AbstractCompiler, EncryptedProgram, QuantumExecutable | ||
| from pyquil.api._compiler import QPUCompiler, rewrite_arithmetic, _collect_memory_descriptors | ||
| from pyquil.experimental._program import ExperimentalProgram | ||
| from pyquil.quilatom import ExpressionDesignator, MemoryReference | ||
| from qcs_api_client.grpc.client import Client as GrpcClient | ||
| from qcs_api_client.grpc.models.controller import EncryptedControllerJob | ||
| from rpcq.messages import NativeQuilMetadata, ParameterAref, ParameterSpec | ||
| from pyquil.parser import parse_program, parse | ||
|
|
||
|
|
||
| @dataclass | ||
| class ExperimentalExecutable: | ||
| job: EncryptedControllerJob | ||
|
|
||
| recalculation_table: Dict[ParameterAref, ExpressionDesignator] | ||
| """A mapping from memory references to the original gate arithmetic.""" | ||
|
|
||
|
|
||
| class ExperimentalQPUCompiler: | ||
| quantum_processor_id: str | ||
| _timeout: Optional[int] = None | ||
|
|
||
| def __init__(self, *, quantum_processor_id: str, timeout: Optional[int] = None): | ||
| self.quantum_processor_id = quantum_processor_id | ||
| self._timeout = timeout | ||
|
|
||
| async def quil_to_native_quil(self, program: ExperimentalProgram): | ||
| raise NotImplementedError("compilation of quil to native quil is not yet supported for ExperimentalProgram") | ||
|
|
||
| async def native_quil_to_executable(self, native_quil_program: ExperimentalProgram) -> EncryptedControllerJob: | ||
| """ | ||
| Compile the provided native quil program to an executable suitable for use on the | ||
| experimental backend. | ||
| """ | ||
|
|
||
| # TODO: Expand calibrations within the program, and then remove calibrations and unused frames and waveforms | ||
|
|
||
| arithmetic_response = rewrite_arithmetic(native_quil_program) | ||
|
|
||
| with self._qcs_client() as client: | ||
| job = await client.translate_quil_to_encrypted_controller_job( | ||
| quantum_processor_id=self.quantum_processor_id, | ||
| quil_program=arithmetic_response.quil, | ||
| num_shots=native_quil_program.num_shots, | ||
| ) | ||
|
|
||
| return ExperimentalExecutable( | ||
| job=job, | ||
| recalculation_table={ | ||
| mref: _to_expression(rule) for mref, rule in arithmetic_response.recalculation_table.items() | ||
| }, | ||
| ) | ||
|
|
||
| @contextmanager | ||
| def _qcs_client(self) -> Iterator[GrpcClient]: | ||
| client = GrpcClient(url="https://grpc.qcs.rigetti.com") | ||
| try: | ||
| yield client | ||
| finally: | ||
| client.close() | ||
|
|
||
|
|
||
| def _to_expression(rule: str) -> ExpressionDesignator: | ||
| # We can only parse complete lines of Quil, so we wrap the arithmetic expression | ||
| # in a valid Quil instruction to parse it. | ||
| # TODO: This hack should be replaced after #687 | ||
| return cast(ExpressionDesignator, cast(Gate, parse(f"RZ({rule}) 0")[0]).params[0]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you not update
Programitself? Going from non-optional to optional should be backwards compatibleUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though I guess could result in a lot of refactoring to satisfy mypy and other use cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part of my intent in this PR is to not have to change anything in the main library for simplicity, with this being almost a "draft" for the next major version (after a long period of iteration & feedback)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes good sense 👍