Skip to content

Execution

The Classiq platform allows you to easily execute quantum programs on quantum hardware or simulators of your choice.

Usage

The input for the execution is a Classiq quantum program, which is the result of synthesizing a quantum model (see Quantum Program Synthesis). When designing your model, don't forget to include execution primitives, such as sample. You may also execute alternative formats such as OpenQASM (see Alternative Formats).

When viewing a quantum program in the "Circuit" tab, after synthesizing your model or uploading your quantum program file, click the "Execute" button:

Execute a quantum program

In the next screen you can choose your execution preferences and run your quantum program.

from classiq import Model, execute, synthesize

model = Model()
#################################
# Design your quantum model here
#################################
model.sample()

# Synthesize a quantum program from the quantum model
quantum_program = synthesize(model.get_model())

# Execute the quantum program and access the result
job = execute(quantum_program)
results = job.result()

Execution Preferences

You can configure the execution process by modifying various execution preferences. The main execution preferences:

  • Backend preferences, such as provider, backend name, and credentials. See Cloud Providers.
  • Number of shots to use.
  • Transpilation options - You can set the transpilation level (and whether to transpile or not) in the Classiq executor by setting the transpile_to_hardware field (shown as the "Transpilation Option" field in the IDE execution page). For more information on the traspilation levels see quantum program transpilation.

First, choose backend preferences in the "Execute Quantum Circuit" window:

Choose backend preferences

You can select more than one backend to run on, but note that a maximum of five hardwares can be selected at a time.

Optionally configure more execution preferences in the "Execution Configuration" window:

Choose execution preferences

Finally, execute your program by clicking the "Run" button.

For full details on available execution preferences, refer to the manual.

You can set your execution preferences in the quantum model before the synthesis, as in this example:

from classiq import set_execution_preferences
from classiq.execution import ExecutionPreferences

# Define execution preferences
execution_preferences = ExecutionPreferences(
    num_shots=1000
)  # set your real preferences instead!

# Set the execution preferences
model = set_execution_preferences(model, execution_preferences)

Tip

If not specificed, the default backend is AER simulator, which doesn't require any provider credentials.

Results

The IDE shows a visualized view of each result returned from execution.

The most common result type is the measurements of your quantum program:

Sample results

It is possible to filter the results by specifying them:

Filter results Filtered results

The function execute returns an ExecutionJob object. The job's result can be accessed with the method result, which takes an optional argument timeout_sec specifying the polling timeout in seconds.

It is possible to access the execution job's id property and use it to retrieve the job's result in a later time or from a different device. In the same manner, it is also possible to retrieve results of jobs initiated from the IDE.

from classiq.execution import ExecutionJob

job = ExecutionJob(id="00000000-0000-0000-0000-000000000000")
result = job.result()

The execution job's result contains all of the saved results from the execution process as a list. Each result is a SavedResult object with these fields:

  • name
  • value: The result object.
  • value_type: The result type, which is one of the values of the enum SavedResultValueType.

Full Example

{
    "functions": [
        {
            "name": "main",
            "body": [
                {
                    "function": "StatePreparation",
                    "function_params": {
                        "probabilities": [0.05, 0.11, 0.13, 0.23, 0.27, 0.12, 0.03, 0.06],
                        "error_metric": {"KL": {"upper_bound": 0.01}}
                    },
                    "name": "state_preparation"
                }
            ]
        }
    ],
    "classical_execution_code": "result = sample()\nsave({'result': result})"
}
from classiq import Model, execute, synthesize
from classiq.builtin_functions import StatePreparation
from classiq.execution import ExecutionDetails

# Design a quantum model, include a `sample` primitive
model = Model()
probabilities = (0.5, 0.1, 0.2, 0.005, 0.015, 0.12, 0.035, 0.025)
sp_params = StatePreparation(
    probabilities=probabilities,
)
model.StatePreparation(sp_params)
model.sample()

# Optionally, set execution preferences:
# model = set_execution_preferences(model.get_model(), execution_preferences)
# Or with:
# model.execution_preferences = execution_preferences

# Synthesize and execute
quantum_program = synthesize(model.get_model())
job = execute(quantum_program)
results = job.result()

# Access the first `sample` result
res = results[0].value

all_qubit_counts = res.counts
first_two_qubit_counts = res.counts_of_qubits(0, 1)

# Retreive the parsed results
parsed_counts = res.parsed_counts
# When running a state vector simulator, retrieve the parsed state vector
# parsed_state_vector = res.parsed_state_vector