service

class parityos.services.service.InputDataT

A contravariant TypeVar representing input data of any type.

alias of TypeVar(‘InputDataT’, contravariant=True)

class parityos.services.service.OutputDataT

A covariant TypeVar representing output data of any type.

alias of TypeVar(‘OutputDataT’, covariant=True)

class parityos.services.service.PhysicalQubitT_co

A covariant TypeVar representing physical qubits.

alias of TypeVar(‘PhysicalQubitT_co’, bound=Qubit, covariant=True)

class parityos.services.service.LogicalQubitT_co

A covariant TypeVar representing 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 obj and 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 cls from 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 cls reconstructed from data.

class parityos.services.service.ResultExtractor(*args, **kwargs)

Bases: Protocol

Protocol for a Callable that extracts data of a desired target type from nested JSON data.

__call__(json_data: Mapping[str, JSONData] | Sequence[JSONData] | str | float | int | bool | None, target_type: type[T]) → T | None

Extract data of type target_type from given JSON data.

The representation of the desired response of type target_type can possibly lie deeply nested within json_data.

Parameters:
  • json_data – JSON data in python representation (dict, list, str, float, int, bool, None), see JSONData.

  • target_type – type of the desired extracted output.

Returns:

An deserialized object of type target_type if compatible serialized data is found in

json_data, None otherwise.

class parityos.services.service.DefaultResultExtractor(serializer: Serializer[TypedDataDict, NameClassTagger])

Bases: ResultExtractor

An implementation of ResultExtractor that recursively iterates through the nested tree structure of json_data and uses a Serializer to try to deserialize every node to desired type target_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, a dict can be deserialized to a list by returning the list of its keys).

serializer: Serializer[TypedDataDict, NameClassTagger]

The serializer used for extracting results of a particular target type.

__call__(json_data: Mapping[str, JSONData] | Sequence[JSONData] | str | float | int | bool | None, target_type: type[T]) → T | None

Extract data of type target_type from given JSON data.

The representation of the desired response of type target_type can possibly lie deeply nested within json_data.

Parameters:
  • json_data – JSON data in python representation (dict, list, str, float, int, bool, None), see JSONData.

  • target_type – type of the desired extracted output.

Returns:

An deserialized object of type target_type if compatible serialized data is found in

json_data, None otherwise.

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.run or AsyncService.submit.

input_data: InputDataT

Input data to start from as input for the service (e.g. problem representation)

configuration: str | dict[str, Any]

Parameters for the service itself (e.g. number of iterations)

description: str

Description of job data as string. Defaults to empty.

class parityos.services.service.Service

Bases: Generic[InputDataT, OutputDataT], ABC

Interface 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], ABC

Interface for services that can run asynchronously.

time_between_trials: float

Time between checks if the result is ready in seconds. Defaults to 2.

timeout: float

Maximum time for a single job in seconds. Defaults to 900 (15 minutes).

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:

  1. Submit job with passed parameters

  2. Query status of submitted job every time_between_trials seconds for at most

    timeout seconds.

  3. If the status of the job has turned to JobStatus.COMPLETED, fetch and return the result.

  4. 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], ABC

Interface for services that can connect to ParityAPI.

name: ClassVar[str]

Name of this service. Subclasses must implement this ClassVar.

input_data_type: ClassVar[type[InputDataT]]

Input data type for submit. Used for serialization. Subclasses must implement this ClassVar.

output_data_type: ClassVar[type[OutputDataT]]

Return type of submit. Used for deserialization. Subclasses must implement this ClassVar.

client: Client

API client that manages communication with ParityAPI.

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: object

Input for a LayoutService job.

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 ParityMapping for a given LayoutInput.

name: ClassVar[str] = 'layout'

Name of this service. Subclasses must implement this ClassVar.

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: object

Input for a CircuitService job.

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 OperatorPolynomial on a given device connectivity.

name: ClassVar[str] = 'circuit'

Name of this service. Subclasses must implement this ClassVar.

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: object

Input for a DriverService job.

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: object

Output from a DriverService job.

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 ProblemRepresentation on a given device connectivity.

name: ClassVar[str] = 'driver'

Name of this service. Subclasses must implement this ClassVar.

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: object

Input for a InitialStateService job.

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 Circuit that prepares the initial state on the given device.

Outputs of the DriverService can 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)

Bases: Generic[QubitT_co]

Base class for QAOA service input types.

problem: ProblemRepresentation[QubitT_co]

Problem to optimize using QAOA. Problem constraints are not supported.

number_of_rounds: int

Number of QAOA rounds.

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_circuit may contain ancilla qubits that do not correspond to logical problem qubits. It is therefore not safe to assume that all qubits in parameterized_circuit are also keys in relabeling_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 TwineQAOAService job.

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 DeviceModel in the context of a QAOA optimization.

name: ClassVar[str] = 'twine_qaoa'

Name of this service. Subclasses must implement this ClassVar.

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 TwineQFTService job.

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 TwineQFTService job.

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.

name: ClassVar[str] = 'twine_qft'

Name of this service. Subclasses must implement this ClassVar.

input_data_type

alias of TwineQFTInput

output_data_type

alias of TwineQFTOutput