Skip to content

Python SDK

Classiq's Python SDK package, classiq, allows one to access Classiq's platform within Python.

Installation

To start using the Python SDK, you need to install Python (version 3.7+). The Classiq SDK package is hosted on PyPI and can be installed with pip:

pip install -U classiq

Running this command in the terminal installs the latest version of the classiq Python package.

Authentication

Authentication must be performed in order to use the SDK. It should be done only once, and calling it when the current device is already authenticated results in an error.

Authentication is simple, and requires asynchronously calling the single authentication function, as shown in the example below.

import classiq

classiq.authenticate()

Running this script will open the following confirmation window in your default browser.

Code confirmation screen

Make sure the user code shown in the window is the same one as printed by the script. After pressing confirm, the following login screen will appear,

Login screen

Complete your login using one of the available options.

Once the authentication process is complete, the script will conclude its run, and you would be able to freely use the SDK on your current device.

Note that the authentication process, and further automatic authentication attempts (that do not require user initiation), make use of your OS's password management system (e.g., macOS Keychain, Windows Credential Locket etc.). As a result, you will be prompted by the OS to approve access to the classiqTokenService for your IDE. See below for example (with macOS Keychain). The password required is the one you ordinarilly use to access your operating system, e.g., when unlocking your screen.

Access request window

Authentication on headless Linux systems

The authentication procedure uses Keyring for storing tokens locally. On headless Linux systems, Keyring can use the SecretService backend. In order to do so, follow Keyring's manual.

Usage Example

The following code example demonstrates using the SDK for circuit synthesis. The circuit implements preparing a quantum state and applying a quantum Fourier transform to the state. The obtained circuit is shown below. For more details and use cases, follow the User Guide and Reference Manual.

from classiq import ModelDesigner, QReg
from classiq.builtin_functions import StatePreparation, QFT
from classiq.interface.generator.state_preparation import (
    Metrics,
    NonNegativeFloatRange,
)

model_designer = ModelDesigner()

x = QReg(size=3)

probabilities = (0.5, 0.1, 0.2, 0.005, 0.015, 0.12, 0.035, 0.025)
sp_params = StatePreparation(
    probabilities=probabilities,
    num_qubits=x.size + 1,  # 1 extra for an auxillary qubit
    error_metric={Metrics.KL: NonNegativeFloatRange(upper_bound=0.3)},
)

model_designer.StatePreparation(sp_params, out_wires=x)
model_designer.preferences.draw_at_level = -1  # draw the circuit at gate level

qft_params = QFT(num_qubits=x.size)
model_designer.QFT(qft_params, in_wires=x)

circuit = model_designer.synthesize()
circuit.show()

 State preparation and QFT example

Updating Versions

The classiq package can be updated using pip, similarly to installing it. To update classiq to the latest version, run the following command:

pip install -U classiq

The classiq package can be updated to a specific version by running the following command:

pip install classiq=={DESIRED_VERSION}

You can validate the correct version is installed by executing:

pip show classiq