Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise UserMessageError(
"""It looks like you've tried to run a program against a QPU but do
not currently have a reservation on one. To reserve time on Rigetti
QPUs, use the command line interface, qcs, which comes pre-installed
in your QMI. From within your QMI, type:
qcs reserve --lattice
For more information, please see the docs at
https://www.rigetti.com/qcs/docs/reservations or reach out to Rigetti
support at support@rigetti.com."""
)
logger.debug("QPU Client connecting to %s", endpoint)
return Client(endpoint, auth_config=self._get_client_auth_config())
def __init__(self, endpoint=None, timeout=None):
"""
Client to communicate with the benchmarking data endpoint.
:param endpoint: TCP or IPC endpoint of the Compiler Server
"""
self.client = Client(endpoint, timeout=timeout)
:param device: PyQuil Device object to use as compilation target
"""
if not endpoint.startswith("tcp://"):
raise ValueError(
f"PyQuil versions >= 2.4 can only talk to quilc "
f"versions >= 1.4 over network RPCQ. You've supplied the "
f"endpoint '{endpoint}', but this doesn't look like a network "
f"ZeroMQ address, which has the form 'tcp://domain:port'. "
f"You might try clearing (or correcting) your COMPILER_URL "
f"environment variable and removing (or correcting) the "
f"compiler_server_address line from your .forest_config file."
)
self.endpoint = endpoint
self.client = Client(endpoint, timeout=timeout)
self.target_device = TargetDevice(isa=device.get_isa().to_dict(), specs=None)
try:
self.connect()
except QuilcNotRunning as e:
warnings.warn(f"{e}. Compilation using quilc will not be available.")
self.timeout = timeout
_quilc_endpoint = quilc_endpoint or self.session.config.quilc_url
if not _quilc_endpoint.startswith("tcp://"):
raise ValueError(
f"PyQuil versions >= 2.4 can only talk to quilc "
f"versions >= 1.4 over network RPCQ. You've supplied the "
f"endpoint '{quilc_endpoint}', but this doesn't look like a network "
f"ZeroMQ address, which has the form 'tcp://domain:port'. "
f"You might try clearing (or correcting) your COMPILER_URL "
f"environment variable and removing (or correcting) the "
f"compiler_server_address line from your .forest_config file."
)
self.quilc_client = Client(_quilc_endpoint, timeout=timeout)
self.qpu_compiler_endpoint = qpu_compiler_endpoint
self._qpu_compiler_client = None
self._device = device
self.target_device = TargetDevice(isa=device.get_isa().to_dict(), specs=None)
self.name = name
try:
self.connect()
except QuilcNotRunning as e:
warnings.warn(f"{e}. Compilation using quilc will not be available.")
except QPUCompilerNotRunning as e:
warnings.warn(f"{e}. Compilation using the QPU compiler will not be available.")
def qpu_compiler_client(self) -> Client:
if not self._qpu_compiler_client:
endpoint = self.qpu_compiler_endpoint or self.session.config.qpu_compiler_url
if endpoint is not None:
if endpoint.startswith(("http://", "https://")):
device_endpoint = urljoin(
endpoint, f'devices/{self._device._raw["device_name"]}/'
)
self._qpu_compiler_client = HTTPCompilerClient(
endpoint=device_endpoint, session=self.session
)
elif endpoint.startswith("tcp://"):
self._qpu_compiler_client = Client(endpoint, timeout=self.timeout)
else:
raise UserMessageError(
"Invalid endpoint provided to QPUCompiler. Expected protocol in [http://, "
f"https://, tcp://], but received endpoint {endpoint}"
)
return self._qpu_compiler_client
def reset(self) -> None:
"""
Reset the state of the QVMCompiler quilc connection.
"""
timeout = self.client.timeout
self.client.close()
self.client = Client(self.endpoint, timeout=timeout)
from pyquil.api._devices import get_lattice, list_lattices
from pyquil.api._error_reporting import _record_call
from pyquil.api._qac import AbstractCompiler
from pyquil.api._qam import QAM
from pyquil.api._qpu import QPU
from pyquil.api._qvm import ForestConnection, QVM
from pyquil.device import AbstractDevice, NxDevice, gates_in_isa, ISA, Device
from pyquil.gates import RX, MEASURE
from pyquil.noise import decoherence_noise_with_asymmetric_ro, NoiseModel
from pyquil.pyqvm import PyQVM
from pyquil.quil import Program, validate_protoquil
from pyquil.quilbase import Measurement, Pragma, Gate, Reset
pyquil_config = PyquilConfig()
Executable = Union[BinaryExecutableResponse, PyQuilExecutableResponse]
def _get_flipped_protoquil_program(program: Program) -> Program:
"""For symmetrization, generate a program where X gates are added before measurement.
Forest 1.3 is really picky about where the measure instructions happen. It has to be
at the end!
"""
program = program.copy()
to_measure = []
while len(program) > 0:
inst = program.instructions[-1]
if isinstance(inst, Measurement):
program.pop()
to_measure.append((inst.qubit, inst.classical_reg))
else:
def native_quil_to_executable(self, nq_program: Program) -> PyQuilExecutableResponse:
return PyQuilExecutableResponse(
program=nq_program.out(),
attributes=_extract_attribute_dictionary_from_program(nq_program),
)
:param executable: An executable. See the above note for acceptable types.
"""
if self.requires_executable:
if isinstance(executable, PyQuilExecutableResponse):
executable = _extract_program_from_pyquil_executable_response(executable)
else:
raise TypeError(
"`executable` argument must be a `PyQuilExecutableResponse`. Make "
"sure you have explicitly compiled your program via `qc.compile` "
"or `qc.compiler.native_quil_to_executable(...)` for more "
"fine-grained control. This explicit step is required for running "
"on a QPU."
)
else:
if isinstance(executable, PyQuilExecutableResponse):
executable = _extract_program_from_pyquil_executable_response(executable)
elif isinstance(executable, Program):
pass
else:
raise TypeError(
"`executable` argument must be a `PyQuilExecutableResponse` or a "
"`Program`. You provided {}".format(type(executable))
)
return super().load(executable)
def load(self, executable):
if isinstance(executable, PyQuilExecutableResponse):
program = _extract_program_from_pyquil_executable_response(executable)
else:
program = executable
# initialize program counter
self.program = program
self.program_counter = 0
self._memory_results = None
# clear RAM, although it's not strictly clear if this should happen here
self.ram = {}
# if we're clearing RAM, we ought to clear the WF too
self.wf_simulator.reset()
# grab the gate definitions for future use
self._extract_defined_gates()