service¶
- class parityos.services.service.InputDataT¶
A contravariant
TypeVarrepresenting input data of any type.alias of TypeVar(‘InputDataT’, contravariant=True)
- class parityos.services.service.OutputDataT¶
A covariant
TypeVarrepresenting output data of any type.alias of TypeVar(‘OutputDataT’, covariant=True)
- class parityos.services.service.PhysicalQubitT_co¶
A covariant
TypeVarrepresenting physical qubits.alias of TypeVar(‘PhysicalQubitT_co’, bound=
Qubit, covariant=True)
- class parityos.services.service.LogicalQubitT_co¶
A covariant
TypeVarrepresenting logical qubits.alias of TypeVar(‘LogicalQubitT_co’, bound=
Qubit, covariant=True)
- parityos.services.service.to_raw_serialized_data(obj: Any, serializer: TypedDataT, ~parityos_serialization.utils.class_tagger.ClassTaggerT]=<parityos_serialization.serializer.DeterministicJsonSerializer object>) Any¶
Serialize
objand only return the raw serialized data without injected class information.This is used for objects that are sent in json format directly over the API, which only handles raw serialized data without injected class tags.
- Parameters:
obj – Object to serialize.
serializer – Serializer to use for serialization. Defaults to
DETERMINISTIC_SERIALIZER.
- Returns:
Serialized data without class tag.
- parityos.services.service.from_raw_serialized_data(data: ~typing.Any, cls: type[~parityos.services.service.T], serializer: ~parityos_serialization.serializer.Serializer[~parityos_serialization.utils.typedefs.TypedDataT, ~parityos_serialization.utils.class_tagger.ClassTaggerT] = <parityos_serialization.serializer.DeterministicJsonSerializer object>) T¶
Construct an instance of class
clsfrom its raw serialized data dict representation that has no class tag injected. The class to deserialize to needs to be given explicitly.This is used for objects that are sent in json format directly over the API, which only handles raw serialized data without injected class tags.
- Parameters:
data – Serialized data.
cls – Type of the class to construct.
serializer – Serializer to use for deserialization. Defaults to
DETERMINISTIC_SERIALIZER.
- Returns:
Instance of class
clsreconstructed fromdata.
- class parityos.services.service.ResultExtractor(*args, **kwargs)¶
Bases:
ProtocolProtocol for a Callable that extracts data of a desired target type from nested JSON data.
- class parityos.services.service.DefaultResultExtractor(serializer: Serializer[TypedDataDict, NameClassTagger])¶
Bases:
ResultExtractorAn implementation of
ResultExtractorthat recursively iterates through the nested tree structure ofjson_dataand uses aSerializerto try to deserialize every node to desired typetarget_type.Returns the first successfully deserialized object it can create.
Warning
Use broad built-in target types (such as e.g.
list) with caution, as many things can be successfully deserialized to them (for example, adictcan be deserialized to alistby returning the list of its keys).- serializer: Serializer[TypedDataDict, NameClassTagger]¶
The serializer used for extracting results of a particular target type.
- parityos.services.service.DEFAULT_EXTRACTOR = DefaultResultExtractor(serializer=<parityos_serialization.serializer.DeterministicJsonSerializer object>)¶
The data extractor used as default for
APIService.result_extractor
- class parityos.services.service.ServiceJobData(input_data: InputDataT, configuration, description: str = '')¶
Bases:
Generic[InputDataT]Data passed to the service for a service job.
Fields correspond to input parameters of
Service.runorAsyncService.submit.- input_data: InputDataT¶
Input data to start from as input for the service (e.g. problem representation)
- class parityos.services.service.Service¶
Bases:
Generic[InputDataT,OutputDataT],ABCInterface for running services in ParityOS.
- abstractmethod run(input_data: InputDataT, configuration: str | dict[str, Any] | None = None, description: str = '') OutputDataT¶
Runs the full service synchronously, from input to output.
- class parityos.services.service.AsyncService(*, time_between_trials: float = 2, timeout: float = 900)¶
Bases:
Service[InputDataT,OutputDataT],ABCInterface for services that can run asynchronously.
- run(input_data: InputDataT, configuration: str | dict[str, Any] | None = None, description: str = '') OutputDataT¶
A synchronous blocking wrapper around the asynchronous functionality of this class.
It performs the following steps:
Submit job with passed parameters
- Query status of submitted job every
time_between_trialsseconds for at most timeoutseconds.
- Query status of submitted job every
If the status of the job has turned to
JobStatus.COMPLETED, fetch and return the result.Raise if job failed or didn’t finish before timeout.
- Parameters:
input_data – Input data to start from as input for the service (e.g. problem representation).
configuration – preset string or configuration dict with concrete configuration parameters (if license permits). Optional.
description – Description of the job. Defaults to empty string.
- Returns:
The result returned by the service.
- Raises:
ParityOSTimeoutException – if the maximum time for a job has elapsed.
- abstractmethod submit(input_data: InputDataT, configuration: str | dict[str, Any] | None = None, description: str = '') str¶
Submit input data and return a job id string that can be used to get results.
- abstractmethod get_info(job_id: str) JobInfo¶
Get information on the current status of a submission.
- Parameters:
job_id – The unique id of the job for which to get information.
- Returns:
Current status of the job.
- abstractmethod get_result(job_id: str) OutputDataT¶
Get the output for a submission, the caller is responsible for making sure the job is complete (e.g. by calling
get_info).- Parameters:
job_id – The unique id of the job for which to get results.
- Returns:
Current status of the job.
- Raises:
ParityOSJobError – If no results are available for the job.
- class parityos.services.service.APIService(client: Client, result_extractor: ResultExtractor = DefaultResultExtractor(serializer=<parityos_serialization.serializer.DeterministicJsonSerializer object>), *, time_between_trials: float = 2, timeout: float = 900)¶
Bases:
AsyncService[InputDataT,OutputDataT],ABCInterface for services that can connect to ParityAPI.
- input_data_type: ClassVar[type[InputDataT]]¶
Input data type for
submit. Used for serialization. Subclasses must implement thisClassVar.
- output_data_type: ClassVar[type[OutputDataT]]¶
Return type of
submit. Used for deserialization. Subclasses must implement thisClassVar.
- result_extractor: ResultExtractor¶
Callable that extracts
- submit(input_data: InputDataT, configuration: str | dict[str, Any] | None = None, description: str = '') str¶
Asynchronously submit a request and return a submission id which can be used to fetch the results.
- Parameters:
input_data – Input data to start from as input for the service (e.g. problem
representation).
configuration – Parameters for the service itself (e.g. number of iterations).
description – Description of the job.
- Returns:
The ID of the job returned by the API.
- get_info(job_id: str) JobInfo¶
Get information on the current status of a submission.
- Parameters:
job_id – The unique id of the job for which to get information.
- Returns:
Current status of the job.
- get_result(job_id: str) OutputDataT¶
Get the output for a submission, the caller is responsible for making sure the job is complete (e.g. by calling
get_info).- Parameters:
job_id – The unique id of the job for which to get results.
- Returns:
Current status of the job.
- Raises:
ParityOSJobError – If no results are available for the job.
- class parityos.services.service.LayoutInput(problem: ProblemRepresentation[Qubit[int] | Qubit[str]], device: DeviceConnectivity[Qubit[Point2D[int]]])¶
Bases:
objectInput for a
LayoutServicejob.- problem: ProblemRepresentation[Qubit[int] | Qubit[str]]¶
Optimization problem which to map to a device. Inequality sum constraints are not supported.
- device: DeviceConnectivity[Qubit[Point2D[int]]]¶
Device onto which to map the problem.
- final class parityos.services.service.LayoutService(client: Client, result_extractor: ResultExtractor = DefaultResultExtractor(serializer=<parityos_serialization.serializer.DeterministicJsonSerializer object>), *, time_between_trials: float = 2, timeout: float = 900)¶
Bases:
APIService[LayoutInput,ParityMapping[Qubit,Qubit]]Service for generating an optimal
ParityMappingfor a givenLayoutInput.- input_data_type¶
alias of
LayoutInput
- output_data_type¶
alias of
ParityMapping
- class parityos.services.service.CircuitInput(operator_polynomial: OperatorPolynomial[Z[Qubit[Point2D[int]]]] | OperatorPolynomial[X[Qubit[Point2D[int]]]], device: DeviceConnectivity[Qubit[Point2D[int]]])¶
Bases:
objectInput for a
CircuitServicejob.- operator_polynomial: OperatorPolynomial[Z[Qubit[Point2D[int]]]] | OperatorPolynomial[X[Qubit[Point2D[int]]]]¶
Operator for which to create the circuit.
- device: DeviceConnectivity[Qubit[Point2D[int]]]¶
Device on which to create the circuit.
- final class parityos.services.service.CircuitService(client: Client, result_extractor: ResultExtractor = DefaultResultExtractor(serializer=<parityos_serialization.serializer.DeterministicJsonSerializer object>), *, time_between_trials: float = 2, timeout: float = 900)¶
Bases:
APIService[CircuitInput,FrozenCircuit[Qubit[Point2D[int]]]]Service for preparing a circuit for applying (the exponential of) an
OperatorPolynomialon a given device connectivity.- input_data_type¶
alias of
CircuitInput
- output_data_type¶
alias of
FrozenCircuit
- class parityos.services.service.DriverInput(compiled_problem: ProblemRepresentation[Qubit[Point2D[int]]], device: DeviceConnectivity[Qubit[Point2D[int]]])¶
Bases:
objectInput for a
DriverServicejob.- compiled_problem: ProblemRepresentation[Qubit[Point2D[int]]]¶
Optimization problem (already mapped to device via a
ParityMapping). Inequality sum constraints are not supported.
- device: DeviceConnectivity[Qubit[Point2D[int]]]¶
Device on which the QAOA optimization is performed.
- class parityos.services.service.DriverOutput(constraint_hamiltonian: OperatorPolynomial[Z[Qubit[Point2D[int]]]], x_driver_hamiltonian: OperatorPolynomial[X[Qubit[Point2D[int]]]], exchange_driver_hamiltonian: OperatorPolynomial[ExchangeTerm[Qubit[Point2D[int]]]])¶
Bases:
objectOutput from a
DriverServicejob.- constraint_hamiltonian: OperatorPolynomial[Z[Qubit[Point2D[int]]]]¶
Hamiltonian representing the constraints in the Pauli Z Basis.
- x_driver_hamiltonian: OperatorPolynomial[X[Qubit[Point2D[int]]]]¶
Constraint preserving single qubit QAOA driver Hamiltonian in Pauli X Basis.
- exchange_driver_hamiltonian: OperatorPolynomial[ExchangeTerm[Qubit[Point2D[int]]]]¶
Constraint preserving exchange QAOA driver Hamiltonian.
- final class parityos.services.service.DriverService(client: Client, result_extractor: ResultExtractor = DefaultResultExtractor(serializer=<parityos_serialization.serializer.DeterministicJsonSerializer object>), *, time_between_trials: float = 2, timeout: float = 900)¶
Bases:
APIService[DriverInput,DriverOutput]Service for for generating constraint preserving QAOA driver Hamiltonians from a given compiled
ProblemRepresentationon a given device connectivity.- input_data_type¶
alias of
DriverInput
- output_data_type¶
alias of
DriverOutput
- class parityos.services.service.InitialStateInput(x_driver_hamiltonian: OperatorPolynomial[X[Qubit[Point2D[int]]]], sum_constraints: frozenset[SumConstraint[Qubit[Point2D[int]]]], device: DeviceConnectivity[Qubit[Point2D[int]]])¶
Bases:
objectInput for a
InitialStateServicejob.- x_driver_hamiltonian: OperatorPolynomial[X[Qubit[Point2D[int]]]]¶
QAOA driver Hamiltonian in Pauli X Basis.
- sum_constraints: frozenset[SumConstraint[Qubit[Point2D[int]]]]¶
Explicit sum constraints of the problem. Inequality sum constraints are not supported.
- device: DeviceConnectivity[Qubit[Point2D[int]]]¶
Device on which to generate the initial state.
- final class parityos.services.service.InitialStateService(client: Client, result_extractor: ResultExtractor = DefaultResultExtractor(serializer=<parityos_serialization.serializer.DeterministicJsonSerializer object>), *, time_between_trials: float = 2, timeout: float = 900)¶
Bases:
APIService[InitialStateInput,FrozenCircuit[Qubit[Point2D[int]]]]Service for preparing a suitable initial state for a constraint preserving QAOA optimization from a constraint preserving driver Hamiltonian and explicit constraints on a given device connectivity.
This service returns a
Circuitthat prepares the initial state on the given device.Outputs of the
DriverServicecan be used as inputs for this service.- name: ClassVar[str] = 'initial_state'¶
Name of this service. Subclasses must implement this
ClassVar.
- input_data_type¶
alias of
InitialStateInput
- output_data_type¶
alias of
FrozenCircuit
- class parityos.services.service.QAOAInput(problem: ProblemRepresentation[QubitT_co], *, number_of_rounds: int = 1)¶
-
Base class for QAOA service input types.
- problem: ProblemRepresentation[QubitT_co]¶
Problem to optimize using QAOA. Problem constraints are not supported.
- class parityos.services.service.QAOAOutput(parameterized_circuit: FrozenCircuit[PhysicalQubitT_co], relabeling_map: Mapping[KeyT, ValueT])¶
Bases:
Generic[LogicalQubitT_co,PhysicalQubitT_co]Base class for QAOA service output types.
Warning
The returned
parameterized_circuitmay contain ancilla qubits that do not correspond to logical problem qubits. It is therefore not safe to assume that all qubits inparameterized_circuitare also keys inrelabeling_map.- parameterized_circuit: FrozenCircuit[PhysicalQubitT_co]¶
The QAOA circuit with parameterized gates for classical optimization.
- relabeling_map: MappingProxyType¶
A mapping from physical circuit qubits to logical problem qubits.
- property physical_to_logical_qubit: MappingProxyType¶
An alias for
relabeling_map.
- logical_to_physical_qubit: MappingProxyType¶
The inverse mapping to
physical_to_logical_qubit.
- class parityos.services.service.TwineQAOAInput(problem: ProblemRepresentation[QubitT_co], device: DeviceModel[PhysicalQubitT_co, BasicQubitProperties | None, BasicGateProperties | None], *, number_of_rounds: int = 1)¶
Bases:
QAOAInput[LogicalQubitT_co],Generic[LogicalQubitT_co,PhysicalQubitT_co]Input for a
TwineQAOAServicejob.- device: DeviceModel[PhysicalQubitT_co, BasicQubitProperties | None, BasicGateProperties | None]¶
The device on which to perform the QAOA optimization.
- class parityos.services.service.TwineQAOAOutput(parameterized_circuit: FrozenCircuit[PhysicalQubitT_co], relabeling_map: Mapping[KeyT, ValueT])¶
Bases:
QAOAOutput[LogicalQubitT_co,PhysicalQubitT_co]
- final class parityos.services.service.TwineQAOAService(client: Client, result_extractor: ResultExtractor = DefaultResultExtractor(serializer=<parityos_serialization.serializer.DeterministicJsonSerializer object>), *, time_between_trials: float = 2, timeout: float = 900)¶
Bases:
APIService[TwineQAOAInput[Qubit,Qubit],TwineQAOAOutput[Qubit,Qubit]]Service for generating a Parity Twine circuit for applying (the exponential of) a problem Hamiltonian on a given
DeviceModelin the context of a QAOA optimization.- input_data_type¶
alias of
TwineQAOAInput
- output_data_type¶
alias of
TwineQAOAOutput
- class parityos.services.service.TwineQFTInput(qubits: list[LogicalQubitT_co], device: DeviceModel[PhysicalQubitT_co, BasicQubitProperties | None, BasicGateProperties | None])¶
Bases:
Generic[LogicalQubitT_co,PhysicalQubitT_co]Input for a
TwineQFTServicejob.- qubits: list[LogicalQubitT_co]¶
The sequence of qubits on which to implement the QFT.
- device: DeviceModel[PhysicalQubitT_co, BasicQubitProperties | None, BasicGateProperties | None]¶
The device on which to construct the QFT circuit.
- class parityos.services.service.TwineQFTOutput(physical_circuit: FrozenCircuit[PhysicalQubitT_co], initial_qubit_mapping: frozenset[tuple[LogicalQubitT_co, PhysicalQubitT_co]], final_qubit_mapping: frozenset[tuple[LogicalQubitT_co, PhysicalQubitT_co]])¶
Bases:
Generic[LogicalQubitT_co,PhysicalQubitT_co]Output from a
TwineQFTServicejob.- physical_circuit: FrozenCircuit[PhysicalQubitT_co]¶
The circuit in terms of physical device qubits implementing the QFT.
- initial_qubit_mapping: frozenset[tuple[LogicalQubitT_co, PhysicalQubitT_co]]¶
A mapping between logical qubits and physical device qubits at the beginning of the circuit.
- final_qubit_mapping: frozenset[tuple[LogicalQubitT_co, PhysicalQubitT_co]]¶
A mapping between logical qubits and physical device qubits at the end of the circuit.
- property initial_logical_to_physical_mapping: MappingProxyType¶
The mapping from logical qubits to physical device qubits at the beginning of the circuit.
- property initial_physical_to_logical_mapping: MappingProxyType¶
The inverse mapping to
initial_logical_to_physical_mapping.
- property final_logical_to_physical_mapping: MappingProxyType¶
The mapping from logical qubits to physical device qubits at the end of the circuit.
- property final_physical_to_logical_mapping: MappingProxyType¶
The inverse mapping to
final_logical_to_physical_mapping.
- final class parityos.services.service.TwineQFTService(client: Client, result_extractor: ResultExtractor = DefaultResultExtractor(serializer=<parityos_serialization.serializer.DeterministicJsonSerializer object>), *, time_between_trials: float = 2, timeout: float = 900)¶
Bases:
APIService[TwineQFTInput[Qubit,Qubit],TwineQFTOutput[Qubit,Qubit]]Service for generating a Parity Twine circuit for QFT.
- input_data_type¶
alias of
TwineQFTInput
- output_data_type¶
alias of
TwineQFTOutput