Skip to content

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).

NOTE: Some hardware providers may require an appropriate account.

Usage

To use the executor, pick an option:

  • Classiq's IDE
  • Classiq Python SDK

The "backend_preferences" field determines the hardware to execute on. There are four ways to get the measurement results of the execution.

  1. The counts attribute of the result allows access to the measurement results of all qubits.
  2. 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 the qubits argument of the method.
  3. The counts_of_output method is similar to counts_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.
  4. The counts_of_multiple_outputs is similar to counts_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"
        }
    }
}
Run the Classiq Execute command (Classiq: Execute Quantum Program), choose a file containing the program, and pick its instruction set (e.g., QASM or IonQ) and the output 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)
Note that the code input argument in the QuantumProgram class can accept any string in QASM or IonQ format and can be executed without generating it.

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, where qubit numbering starts with zero
"amplitude_estimation":{
    "alpha":0.05,
    "epsilon":0.01,
    "objective_qubits": [1]
}

Is equal to:

\[ P(|{a_{estimation} - a_{real}}| > epsilon) < alpha \]

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]
        }
    }
}
Run the Classiq Execute command (Classiq: execute Amplitude Estimation), choose a file containing the program, and pick its instruction set (e.g., QASM or IonQ) and the output 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 you can 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:

  • 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.