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

Client side calibration expansion #1288

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions pyquil/_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ def quil(self, instructions):

@v_args(inline=True)
def def_gate_matrix(self, name, variables, matrix):
print(name)
print(matrix)
return DefGate(name, matrix=matrix, parameters=variables)

@v_args(inline=True)
Expand Down
38 changes: 29 additions & 9 deletions pyquil/api/_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,29 +340,35 @@ def quil_to_native_quil(self, program: Program, *, protoquil: Optional[bool] = N

@_record_call
def native_quil_to_executable(
self, nq_program: Program, *, debug: bool = False
self, nq_program: Program, *, debug: bool = False, expand_calibrations: bool = True,
) -> Optional[QuiltBinaryExecutableResponse]:
if not self.qpu_compiler_client:
raise UserMessageError(
"It looks like you're trying to compile to an executable, but "
"do not have access to the QPU compiler endpoint. Make sure you "
"are engaged to the QPU before trying to do this."
)
"""
Compile a native quil program to a binary executable.

:param nq_program: Native quil to compile
:param debug: Include debug information in the response
:param expand_calibrations: Expand calibrations locally,
rather than expanding calibrations on the translation service
:return: An (opaque) binary executable
"""
self._connect_qpu_compiler()

arithmetic_response = rewrite_arithmetic(nq_program)
nq_program_calibrated = (
self.expand_calibrations(nq_program) if expand_calibrations else nq_program
)
arithmetic_response = rewrite_arithmetic(nq_program_calibrated)

request = QuiltBinaryExecutableRequest(
quilt=arithmetic_response.quil, num_shots=nq_program.num_shots
)
response = cast(
QuiltBinaryExecutableResponse,
self.qpu_compiler_client.call(
self.qpu_compiler_client.call( # type: ignore
"native_quilt_to_binary", request, rpc_timeout=self.timeout
),
)

# TODO(notmgsk): use nq_program_calibrated?
response.recalculation_table = arithmetic_response.recalculation_table # type: ignore
response.memory_descriptors = _collect_memory_descriptors(nq_program)

Expand Down Expand Up @@ -425,6 +431,20 @@ def calibration_program(self) -> Program:
else:
return self._calibration_program

def expand_calibrations(self, program: Program, discard_defcals: bool = True) -> Program:
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO this logic should be on Program, or in a standalone function, for use where a QPUCompiler doesn't make sense

# Prepend the system's calibrations to the user's calibrations
calibrated_program = (
self.calibration_program + program.copy_everything_except_instructions()
)
for instruction in program:
calibrated_instruction = calibrated_program.calibrate(instruction)
calibrated_program.inst(calibrated_instruction)

if discard_defcals:
calibrated_program._calibrations = []

return calibrated_program

@_record_call
def reset(self) -> None:
"""
Expand Down
6 changes: 0 additions & 6 deletions pyquil/api/_qpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
##############################################################################
from collections import defaultdict
import uuid
import warnings
from typing import Any, Dict, List, Optional, Tuple, Union, cast

import numpy as np
Expand Down Expand Up @@ -259,11 +258,6 @@ def run(self, run_priority: Optional[int] = None) -> "QPU":
for name, array in extracted.items():
self._memory_results[name] = array
elif not ro_sources:
warnings.warn(
"You are running a QPU program with no MEASURE instructions. "
"The result of this program will always be an empty array. Are "
"you sure you didn't mean to measure some of your qubits?"
)
self._memory_results["ro"] = np.zeros((0, 0), dtype=np.int64)

self._last_results = results
Expand Down
2 changes: 1 addition & 1 deletion pyquil/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def test_quil_to_native_quil(compiler):


@pytest.mark.skip(reason="Deprecated.")
def test_native_quil_to_binary(server, mock_qpu_compiler):
def test_native_quil_to_executable(server, mock_qpu_compiler):
p = COMPILED_BELL_STATE.copy()
p.wrap_in_numshots_loop(10)
response = mock_qpu_compiler.native_quil_to_executable(p)
Expand Down