Python Interface¶
Introduction¶
The Python Interface provides users a way to run the Intel® Quantum SDK using
Python3, through the intelqsdk.cbindings
library. There are two modes for interacting with
Python:
Write quantum circuits in OpenQASM 2.0 – write a quantum circuit, and convert that to a .cpp file that has
quantum_kernel
functions, compile, and use theintelqsdk.cbindings
library to run thequantum_kernel
functions and call APIs, all from within Python.Write
quantum_kernel
functions in C++, compile to a.so
file, and call APIs from Python.
The Python Interface is installed in a virtual environment placed alongside the compiler
in the virtualenv
directory.
To run Python scripts using the intelqsdk.cbindings
library, use either
$ source <path to Intel Quantum SDK>/virtualenv/bin/activate
or call the script with python3
located at
$ <path to Intel Quantum SDK>/virtualenv/bin/python3
Python via OpenQASM 2.0¶
Procedure¶
Step 1: Write quantum programs¶
Using OpenQASM2.0, or alternatively, transpile the Python program into
OpenQASM2.0, with the user’s choice of quantum programming package. As long as the
program can be turned into a .qasm
file, the Bridge library will be able to
translate it to a C++ source file for the Intel® Quantum SDK.
At the beginning of the Python script, include the following lines:
from intelqsdk.cbindings import *
loadSdk("/path/to/file.so", sdk_name)
loadSdk``needs to be called before calling other functions or creating objects from
``intelqsdk.cbindings
library.
The sdk_name
is the user-created reference string given to this .so
library. Later on, when calling functions from this library or referencing
cbit
/qbit
variables, pass this identifier to indicate which library to
use. Users can also access multiple .so
libraries to call functions
or reference cbit
/qbit
variables from each library.
Step 2: Write the Python script¶
First, import several modules:
import intelqsdk.cbindings
from openqasm_bridge.v2 import translate
Next, use Bridge to translate the OpenQASM file to C++:
with open('example.qasm', 'r', encoding='utf8') as input_file:
input_string: str = input_file.read()
translated: list[str] = translate(input_string, kernel_name='my_kernel')
with open('example.cpp', 'w', encoding='utf8') as output_file:
for line in translated:
output_file.write(line + "\n")
Now, compile the translated C++ code:
compiler_path = "<path to Intel Quantum SDK>/intel-quantum-compiler"
intelqsdk.cbindings.compileProgram(compiler_path, "example.cpp", "-s", sdk_name)
From here, the user can start calling APIs to set up the simulator and run the quantum program. For example,
iqs_config = intelqsdk.cbindings.IqsConfig()
iqs_config.num_qubits = 5
iqs_config.simulation_type = "noiseless"
iqs_device = intelqsdk.cbindings.FullStateSimulator(iqs_config)
iqs_device.ready()
intelqsdk.cbindings.callCppFunction("my_kernel", sdk_name)
qbit_ref = intelqsdk.cbindings.RefVec()
for i in range(5):
qbit_ref.append(intelqsdk.cbindings.QbitRef("q", i, sdk_name).get_ref())
probabilities = iqs_device.getProbabilities(qbit_ref)
intelqsdk.cbindings.FullStateSimulator.displayProbabilities(probabilities, qbit_ref)
Using a Custom Backend with the Python Interface¶
To use a custom backend with the Python Interface,
create a Python class that derives from the CustomInterface
class
in the intelqsdk.cbindings
module. In this class,
implement the RXY
, RZ
, SwapA
, CPhase
, PrepZ
, and MeasZ
methods, and add
a constructor for the Python class.
The following example assumes the user has defined a Python class MySimulator
:
custom_device_id = "custom device"
CustomSimulator.registerCustomInterface(MySimulator, custom_device_id, <args>) // args is optional, depends on MySimulator's constructor
config = DeviceConfig(custom_device_id)
device = CustomSimulator(config)
Alternatively, if the user only intends to have one device, use the following shortcut:
device = CustomSimulator.createSimulator(MySimulator, "custom_device", <args>)
Note the difference relative to the C++ interface: instead of the class being a template argument of
registerCustomInterface
and createSimulator
, it is the first parameter in the Python Interface.
Call the function getCustomBackend()
to return the class that the user has created:
sim_object = device.getCustomBackend()
Known Limitations of the Python Interface¶
Any variables of
cbit
type must be global in order to access them.The C++ functions, including
quantum_kernel
, called from Python must returnvoid
and either take no parameters or take an single array ofdouble
.