Skip to main content
This section explains how to execute a quantum program using the ExecutionSession class. The class enables executing a quantum program with parameters and operations, eliminating the need to resynthesize the model.

Initializing

Initialize ExecutionSession with either:
  • A quantum program (the output of synthesize), or
  • OpenQASM 2.0 or 3.0 source as a string (same semantics as the first argument to sample).
It is recommended to use ExecutionSession as a context manager in order to ensure resources are cleaned up. Alternatively, you can call the close method directly.

Quantum program

OpenQASM string

When the first argument is OpenQASM text, do not use parameters on sample / submit_sample for Qmod-style main arguments; use a synthesized QuantumProgram instead.

Operations

ExecutionSession supports three types of operations:
  • sample: Executes the quantum program with the specified parameters.
  • estimate: Computes the expectation value of the specified Hamiltonian using the quantum program.
  • minimize: Runs a variational minimization loop, iteratively sampling the circuit to minimize a cost function using the COBYLA optimizer.
When estimating using a simulator, the ClassiqSimulatorBackendNames.SIMULATOR_STATEVECTOR backend calculates the expectation value directly from the computed state vector, as opposed to computing it from shots. Each invocation mode can use one of these options:
  • single
  • submit: When using submit as a prefix, the job object returns immediately, and the results should be polled. See ExecutionJob in the sdk reference.
For the sample method, pass a single parameter dict or a list for batch execution:
  • es.sample(parameters: Optional[ExecutionParams | List[ExecutionParams]])
  • es.submit_sample(parameters: Optional[ExecutionParams | List[ExecutionParams]])
The estimate method works the same way:
  • es.estimate(hamiltonian: SparsePauliOp, parameters: Optional[ExecutionParams | List[ExecutionParams]])
  • es.submit_estimate(hamiltonian: SparsePauliOp, parameters: Optional[ExecutionParams | List[ExecutionParams]])
The minimize method accepts a max_iteration parameter:
  • es.minimize(cost_function, initial_params, max_iteration)
max_iteration does not directly control the number of quantum jobs. It sets an upper bound on the number of classical optimizer (COBYLA) iterations; the optimizer may converge before reaching this limit. See the scipy COBYLA documentation for details on iteration and convergence behavior.

Examples

sample()

A simple example of how to use sample and its variations:

estimate()

An example that shows how to use estimate and its variations:

Handling Long Jobs

When handling long-running jobs (jobs that are submitted to HW providers with potentially very long queues) it is advisable to retrieve and save the job ID for future reference:
Alternatively, for scenarios requiring result polling, e.g., iterative hybrid algorithms, consider the following partial code example. This example runs a series of quantum estimation jobs, step by step. It automatically submits each job, collects the results, checks they meet certain criteria (for example, does the cost exceed a certain threshold), and adjusts the parameters for the next job based on those results.

@cfunc

Note that Classiq does not support algorithms utilizing @cfunc (or cscope in Qmod native) for long job execution.