Skip to content

Commit

Permalink
feat!: When running against a QPU, a qcs_sdk.api.ConnectionStrategy can
Browse files Browse the repository at this point in the history
be used to configure how a connection established.
  • Loading branch information
MarquessV committed Jun 23, 2023
1 parent 3200b43 commit 1cd524b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
9 changes: 4 additions & 5 deletions pyquil/api/_qam.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
##############################################################################
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Generic, Mapping, Optional, TypeVar, Sequence, Union
from typing import Any, Generic, Mapping, Optional, TypeVar, Sequence, Union

import numpy as np
from pyquil.api._abstract_compiler import QuantumExecutable
Expand Down Expand Up @@ -55,6 +55,7 @@ def execute(
self,
executable: QuantumExecutable,
memory_map: Optional[MemoryMap] = None,
**kwargs: Any,
) -> T:
"""
Run an executable on a QAM, returning a handle to be used to retrieve
Expand All @@ -74,11 +75,9 @@ def get_result(self, execute_response: T) -> QAMExecutionResult:
"""

def run(
self,
executable: QuantumExecutable,
memory_map: Optional[MemoryMap] = None,
self, executable: QuantumExecutable, memory_map: Optional[MemoryMap] = None, **kwargs: Any
) -> QAMExecutionResult:
"""
Run an executable to completion on the QAM.
"""
return self.get_result(self.execute(executable, memory_map))
return self.get_result(self.execute(executable, memory_map, **kwargs))
22 changes: 16 additions & 6 deletions pyquil/api/_qpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
MemoryReference,
)
from qcs_sdk import QCSClient
from qcs_sdk.qpu.api import submit, retrieve_results, ExecutionResult
from qcs_sdk.qpu.api import submit, retrieve_results, ExecutionResult, ConnectionStrategy
from qcs_sdk.qpu.rewrite_arithmetic import build_patch_values


Expand Down Expand Up @@ -102,6 +102,7 @@ def alloc(spec: ParameterSpec) -> np.ndarray:
class QPUExecuteResponse:
job_id: str
_executable: EncryptedProgram
connection_strategy: Optional[ConnectionStrategy]


class QPU(QAM[QPUExecuteResponse]):
Expand All @@ -113,7 +114,6 @@ def __init__(
timeout: float = 10.0,
client_configuration: Optional[QCSClient] = None,
endpoint_id: Optional[str] = None,
use_gateway: bool = True,
) -> None:
"""
A connection to the QPU.
Expand All @@ -136,18 +136,26 @@ def __init__(
self._quantum_processor_id = quantum_processor_id
self._endpoint_id = endpoint_id

self._use_gateway = use_gateway

@property
def quantum_processor_id(self) -> str:
"""ID of quantum processor targeted."""
return self._quantum_processor_id

def execute(self, executable: QuantumExecutable, memory_map: Optional[MemoryMap] = None) -> QPUExecuteResponse:
def execute(
self,
executable: QuantumExecutable,
memory_map: Optional[MemoryMap] = None,
connection_strategy: Optional[ConnectionStrategy] = None,
) -> QPUExecuteResponse:
"""
Enqueue a job for execution on the QPU. Returns a ``QPUExecuteResponse``, a
job descriptor which should be passed directly to ``QPU.get_result`` to retrieve
results.
:param:
connection_strategy: An optional `ConnectionStrategy` enum that can be used
to configure how the job is submitted and retrieved from the QPU. If unset
the public gateway is used.
"""
executable = executable.copy()

Expand All @@ -167,9 +175,10 @@ def execute(self, executable: QuantumExecutable, memory_map: Optional[MemoryMap]
quantum_processor_id=self.quantum_processor_id,
endpoint_id=self._endpoint_id,
client=self._client_configuration,
connection_strategy=connection_strategy,
)

return QPUExecuteResponse(_executable=executable, job_id=job_id)
return QPUExecuteResponse(_executable=executable, job_id=job_id, connection_strategy=connection_strategy)

def get_result(self, execute_response: QPUExecuteResponse) -> QAMExecutionResult:
"""
Expand All @@ -180,6 +189,7 @@ def get_result(self, execute_response: QPUExecuteResponse) -> QAMExecutionResult
job_id=execute_response.job_id,
quantum_processor_id=self.quantum_processor_id,
client=self._client_configuration,
connection_strategy=execute_response.connection_strategy,
)

ro_sources = execute_response._executable.ro_sources
Expand Down
8 changes: 3 additions & 5 deletions pyquil/api/_quantum_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ def to_compiler_isa(self) -> CompilerISA:
return self.compiler.quantum_processor.to_compiler_isa()

def run(
self,
executable: QuantumExecutable,
memory_map: Optional[MemoryMap] = None,
self, executable: QuantumExecutable, memory_map: Optional[MemoryMap] = None, **kwargs: Any
) -> QAMExecutionResult:
"""
Run a quil executable. All parameters in the executable must have values applied using
Expand All @@ -139,7 +137,7 @@ def run(
region for the run.
:return: execution result including readout data.
"""
return self.qam.run(executable, memory_map)
return self.qam.run(executable, memory_map, **kwargs)

def calibrate(self, experiment: Experiment) -> List[ExperimentResult]:
"""
Expand Down Expand Up @@ -794,7 +792,7 @@ def get_qc(
.. _QCS API Docs: https://docs.api.qcs.rigetti.com/#tag/endpoints
"""
client_configuration = QCSClient.load()
client_configuration = client_configuration or QCSClient.load()

# 1. Parse name, check for redundant options, canonicalize names.
prefix, qvm_type, noisy = _parse_name(name, as_qvm, noisy)
Expand Down

0 comments on commit 1cd524b

Please sign in to comment.