Client

This class provides a synchronous interface to qognitive’s API. It wraps the async client resolving the async calls to synchronous ones. It’s important that this client is used in a synchronous context, otherwise it will block the event loop.

class qcog_python_client.QcogClient

Bases: BaseQcogClient

Qcog Client.

await_async(async_callable: Coroutine[Any, Any, CallableReturnType]) CallableReturnType

Await an async callable.

classmethod create(*, token: str | None = None, hostname: str = 'dev.qognitive.io', port: int = 443, api_version: str = 'v1', safe_mode: bool = False, version: str = '0.0.123', httpclient: IRequestClient | None = None, dataclient: IDataClient | None = None) QcogClient

Instantiate a new Qcog client.

This client is meant to work in a synchronous context. It will raise an error if used in an async context.

Parameters:
  • token (str | None) – A valid API token granting access optional when unset (or None) expects to find the proper value as QCOG_API_TOKEN environment variable

  • hostname (str) – API endpoint hostname, currently defaults to dev.qognitive.io

  • port (int) – port value, default to https 443

  • api_version (str) – the “vX” part of the url for the api version

  • safe_mode (bool) – if true runs healthchecks before running any api call sequences

  • version (str) – the qcog version to use. Must be no smaller than OLDEST_VERSION and no greater than NEWEST_VERSION

  • httpclient (ABCRequestClient | None) – an optional http client to use instead of the default

  • dataclient (ABCDataClient | None) – an optional data client to use instead of the default

data(data: DataFrame) QcogClient

Upload a dataset.

Parameters:

data (pd.DataFrame) – the dataset as a DataFrame

Return type:

QcogClient

property dataset: dict

Return the dataset.

ensemble(*args: Any, **kwargs: Any) QcogClient

Select Ensemble model.

Parameters:
  • operators (list[str | int]) – List of operators

  • dim (int, default=16) – Dimension

  • num_axes (int, default=4) – Number of axes

  • sigma_sq (dict, default=None) – Sigma squared

  • sigma_sq_optimization (dict, default=None) – Sigma squared optimization

  • seed (int, default=42) – Seed

  • target_operator (list[str | int], default=None) – Target operator

Return type:

AsyncQcogClient

get_loss() list[list[int | float | Any]] | None

Return the loss matrix.

Returns:

the loss matrix

Return type:

Matrix

inference(data: DataFrame, parameters: InferenceParameters) DataFrame | Any

From a trained model query an inference.

Parameters:
  • data (pd.DataFrame) – the dataset as a DataFrame

  • parameters (dict) – inference parameters

Returns:

the predictions

Return type:

pd.DataFrame

property inference_result: dict

Return the inference result.

property model: ModelPauliParameters | ModelEnsembleParameters | ModelGeneralParameters | ModelPytorchParameters

Return the model.

pauli(*args: Any, **kwargs: Any) QcogClient

Pauli model.

Select Pauli model.

Parameters:
  • operators (list[str | int]) – List of operators

  • qbits (int, default=2) – Number of qbits

  • pauli_weight (int, default=2) – Pauli weight

  • sigma_sq (dict, default=None) – Sigma squared

  • sigma_sq_optimization (dict, default=None) – Sigma squared optimization

  • seed (int, default=42) – Seed

  • target_operator (list[str | int], default=None) – Target operator

Return type:

QcogClient

preloaded_data(guid: str) QcogClient

Retrieve a dataset that was previously uploaded from guid.

Parameters:

guid (str) – guid of a previously uploaded dataset

Return type:

QcogClient

preloaded_model(guid: str) QcogClient

Retrieve a preexisting model.

Parameters:

guid (str) – model guid

Return type:

QcogClient

preloaded_training_parameters(guid: str) QcogClient

Retrieve preexisting training parameters payload.

Parameters:
  • guid (str) – model guid

  • rebuild (bool) – if True, will initialize the class “model” (ex: pauli or ensemble) from the payload

Returns:

itself

Return type:

QcogClient

progress() dict

Return the current status of the training.

Returns:

the current status of the training guid : str training_completion : int current_batch_completion : int status : TrainingStatus

Return type:

dict

property pytorch_model: dict

Return the Pytorch model.

property pytorch_trained_models: list[dict]

Return the list of Pytorch trained models.

status() TrainingStatus

Return the current status of the training.

Returns:

the current status of the training

Return type:

TrainingStatus

train(batch_size: int, num_passes: int, weight_optimization: AnalyticOptimizationParameters | AdamOptimizationParameters | GradOptimizationParameters | EmptyDictionary, get_states_extra: LOBPCGFastStateParameters | PowerIterStateParameters | EIGHStateParameters | EIGSStateParameters | NPEIGHStateParameters | GradStateParameters | EmptyDictionary) QcogClient

Start a training job.

For a fresh “to train” model properly configured and initialized trigger a training request.

Parameters:
  • batch_size (int) – The number of samples to use in each training batch.

  • num_passes (int) – The number of passes through the dataset.

  • weight_optimization (NotRequiredWeightParams) – optimization parameters for the weights

  • get_states_extra (NotRequiredStateParams) – optimization parameters for the states

Return type:

AsyncQcogClient

property trained_model: dict

Return the trained model.

property training_parameters: dict

Return the training parameters.

property version: str

Qcog version.

wait_for_training(poll_time: int = 60) None

Wait for training to complete.

Note

This function is blocking

Parameters:

poll_time (int:) – status checks intervals in seconds

Returns:

itself

Return type:

QcogClient