Python SDK¶
The Classiq Python SDK package, classiq
, allows access to the Classiq platform within
Python.
Installation¶
To start using the Python SDK, 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.
Updating Versions¶
You may use any of the active Classiq versions, as listed in Active Classiq Versions. It is strongly encouraged to always use the latest available version.
Update the classiq
package using pip
, similarly to installing it.
To update classiq
to the latest version, run this command:
pip install -U classiq
To update classiq
to a specific version:
pip install classiq=={DESIRED_VERSION}
To verify that the correct version is installed:
pip show classiq
Authentication¶
To use the SDK you must perform authentication. Do it only once. (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()
A confirmation window opens in your default browser:
Make sure the user code shown in the window is the same as printed by the script. Press confirm. This login screen appears:
Complete your login using one of the available options.
Once the authentication process is complete, the script concludes its run, and you can 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 password management system (e.g., macOS Keychain, Windows Credential Locker). As a result, you are prompted by the OS to approve access to the classiqTokenService for your IDE.The password required is the one you ordinarily use to access your operating system, e.g., when unlocking your screen. See below for an example using macOS Keychain.
Authentication on Headless Linux Systems¶
The authentication procedure on headless Linux systems stores the tokens locally in a credentials file. While you must still run the authentication once, it can be done on another system with a browser.
Imports Structure¶
The SDK package has an exposed imports structure. Import objects only from these public
subpackages. The public submodules of a certain module are encoded in its dir
entries.
For example, to view the submodules of the package main module:
import classiq
print(dir(classiq))
To use the SDK correctly, do not access any submodules that are not public.
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, first at the functional level, and then with the expanded state preparation function. For details and use cases, see the User Guide and Reference Manual.
from classiq import Model, QReg
from classiq.builtin_functions import StatePreparation, QFT
from classiq.builtin_functions.state_preparation import Metrics
from classiq.builtin_functions.range_types import NonNegativeFloatRange
model = Model()
num_qubits = 3
probabilities = (0.5, 0.1, 0.2, 0.005, 0.015, 0.12, 0.035, 0.025)
sp_params = StatePreparation(
probabilities=probabilities,
error_metric={Metrics.KL: NonNegativeFloatRange(upper_bound=0.3)},
)
x = model.StatePreparation(sp_params)["OUT"]
qft_params = QFT(num_qubits=num_qubits)
model.QFT(qft_params, in_wires=x)
circuit = model.synthesize()
circuit.show()