Executor¶
Classiq executor allows you to easily execute quantum programs on different quantum hardware vendors. The executor gets as input a quantum program instruction set (e.g. OpenQASM), and a quantum hardware to execute on (e.g. IonQ hardware). In addition, the executor supports proprietary and generic execution schemes (e.g. VQE).
Please note that usage of some hardware providers may require an appropriate account.
Usage¶
In order to use the executor, you may pick one of the following options:
- Classiq python SDK.
- VS Code Classiq extension.
The "backend_preferences" field determines the hardware to execute on. There are four ways to get the measurement results of the execution.
- The
counts
attribute of the result allows access to the measurement results of all qubits. - The
counts_of_qubits
method allows access to results of specific qubits. The order of qubits in the measurement result is determined by their order in thequbits
argument of the method. - The
counts_of_output
method is similar tocounts_of_qubits
, but receives an output name as an argument. Note it may only be used if the generated model has outputs. See Model Inputs & Outputs for output setting instructions. - The
counts_of_multiple_outputs
is similar tocounts_of_output
. It receives a tuple of output names, and returns the counts of all specified outputs, keyed by a tuple of states matching the requested outputs.
Circuit Example¶
Create the following file: execution_preferences.exct
{
"preferences": {
"num_shots": 1000,
"backend_preferences": {
"backend_service_provider": "IBM Quantum",
"backend_name": "aer_simulator"
}
}
}
Classiq: Execute Quantum Program
), choose a file containing the program and pick it's instruction set (e.g. QASM or ionq) and the outptut path.
from classiq import Model, Executor
from classiq.builtin_functions import StatePreparation
from classiq.builtin_functions.state_preparation import Metrics
from classiq.builtin_functions.range_types import NonNegativeFloatRange
from classiq.execution import IBMBackendPreferences
# circuit generation
model = Model()
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.01)},
)
model.StatePreparation(sp_params)
circuit = model.synthesize()
res = Executor(
num_shots=1000,
backend_preferences=IBMBackendPreferences(backend_name="aer_simulator"),
).execute(circuit)
all_qubit_counts = res.counts
first_two_qubit_counts = res.counts_of_qubits(0, 1)
Amplitude Estimation Example¶
In the execution preferences, add "amplitude_estimation" with three fields:
-alpha, confidence level of the AE algorithm
-epsilon: estimation precision
-objective_qubits: list specifying on which qubits to perform the amplitude estimation, qubits numbering starts with zero
"amplitude_estimation":{
"alpha":0.05,
"epsilon":0.01,
"objective_qubits": [1]
}
Is equal to:
where \(a_{real} = P(Qubit_1 = \ket{1})\).
Create the following file: execution_preferences.exct
{
"preferences": {
"backend_preferences": {
"backend_service_provider": "IBM Quantum",
"backend_name": "aer_simulator"
},
"amplitude_estimation":{
"alpha":0.05,
"epsilon":0.01,
"objective_qubits": [1]
}
}
}
Classiq: execute Amplitude Estimation
), choose a file containing the program and pick it's instruction set (e.g. QASM or ionq) and the outptut path.
from classiq import Executor
from classiq.execution import AmplitudeEstimation, IBMBackendPreferences
qasm = 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\nx q[1];\n'
res = Executor(
amplitude_estimation=AmplitudeEstimation(
alpha=0.05, epsilon=0.01, objective_qubits=[1]
),
backend_preferences=IBMBackendPreferences(
backend_name="aer_simulator_density_matrix"
),
).execute(qasm)
Note, in the SDK it is also possible to generate multiple backend preferences from a single provider using the 'batch_preferences' command.
from classiq.execution import IBMBackendPreferences
backends = IBMBackendPreferences.batch_preferences(
backend_names=["aer_simulator", "aer_simulator_density_matrix"],
access_token=None,
)
Supported Backend Providers¶
The different executions types (and commands) for textual model are:
- Execute Quantum Program: Executes a quantum program object, i.e. simply run the a given circuit.
- Execute Generated Circuit: Execute a generated circuit, the result of a generation call. This can be used for finance and grover applications. See Finance.