Skip to content
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

Add support for sampling from Quokka devices #208

Merged
merged 3 commits into from
Sep 10, 2024

Conversation

dstrain115
Copy link
Collaborator

  • This sampler converts circuits to QASM and then sends them to a quokka endpoint for simulation.
  • Any parameterized circuits are resolved and sent point by point to the device.

- This sampler converts circuits to QASM and then
sends them to a quokka endpoint for simulation.
- Any parameterized circuits are resolved and sent point
by point to the device.
"Please install requests library to use Quokka"
"(e.g. pip install requests)"
)
raise e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nicer to augment the exception with a message:

raise ImportError("Please install requests library to use Quokka (e.g. pip install requests)") from e

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


def run_sweep(
self,
program: "cirq.AbstractCircuit",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cirq is already imported, are quotes needed for the three cirq types here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed quotes.

print(
"Warning! Keys can only be measured once in Quokka simulator"
)
print("Key {key} will only contain the last measured value")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add file=sys.stderr to prints if these are warnings?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to warnings.warn.

if self.endpoint is None:
self.endpoint = _REQUEST_ENDPOINT.format(self.quokka_name)
if self.post_function is None:
self.post_function = self._post
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this, combined with the _post method below, is a bit weird. A cleaner implementation would be to extract _post method into the module level function, and then pass it as a default value of post_function argument.

Maybe something like that:

class Quokka(object):
  def __init__(self, name=_DEFAULT_QUOKKA_NAME):
    self._end_point = _REQUEST_ENDPOINT.format(name)
  
  def __call__(self, json_request: JSON_TYPE) -> JSON_TYPE:
    result = requests.post(self._endpoint, json=json_request)
    return json.loads(result.content)

and then

class QuokkaSampler(cirq.Sampler):
  def __init__(self, post: Optional[Callable[[JSON_TYPE], JSON_TYPE]] = None):
    self._post = post or Quokka()

Now there's a clean way to instantiate sampler with different post methods:

s = QuokkaSampler()  # production instance
s = QuokkaSampler(post=_mock_post)  # unittest post function
s = QuokkaSampler(post=Quokka("staging"))  # staging quokka
s = QuokkaSampler(post=Quokka("foo"))  # foo quokka

in fact, you're already doing something very similar in test, so this refactoring makes even more sense :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Kept name as a parameter though since QuokkaSample("foo") seemed cleaner.

for key in measure_keys:
register_name = register_names[key]
if register_name not in json_results[_RESULT_KEY]:
raise RuntimeError(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuntimeError or KeyError?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.



class FakeQuokkaEndpoint:
def __init__(self, responses: Iterable[quokka_sampler.JSON_TYPE]):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for slightly nicer interface you can do *responses: quokka_sampler.JSON_TYPE and dispense with some square brackets below when passing expected responses. This would also remove the need for list conversion below

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Kept the list conversion so that I don't need to keep track of a counter or iterator.

@dstrain115 dstrain115 requested a review from solodov September 9, 2024 19:13
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Iterable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed anymore

@dstrain115 dstrain115 merged commit 378add5 into quantumlib:main Sep 10, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants