Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

Execution Tutorial

  • Part 1
This tutorial covers the basics of executing a quantum program using Classiq directly through the Python SDK. It is also possible to use the Classiq Platform to execute quantum algorithms. For this, we will start by synthesizing the following example from the synthesis tutorial:
from classiq import *


@qfunc
def main(x: Output[QNum[3]], y: Output[QNum]) -> None:
    allocate(x)
    hadamard_transform(x)
    y |= x**2 + 1


qprog = synthesize(main)
show(qprog)
Output:

Quantum program link: https://platform.classiq.io/circuit/30HuOnn0LwUGqLIKMlUETImaIfc
  

This quantum program evaluates the function y(x)=x2+1y(x) = x^2 + 1, for all integers x[0,7]x \in [0,7]. To execute a quantum program and save its results in the Python SDK, create an ExecutionSession. To sample the states using this object, one can use sample:
with ExecutionSession(qprog) as es:
    results = es.sample()
Output:
gio: https://platform.classiq.io/circuit/30HuOnn0LwUGqLIKMlUETImaIfc?login=True&version=0.86.1: Operation not supported
  

The information from the outputs of the quantum program can be obtained in the form of a dataframe using the dataframe attribute:
results.dataframe
xycountprobabilitybitstring
0122850.139160000010001
17502620.127930110010111
25262600.126953011010101
34172540.124023010001100
4252530.123535000101010
56372510.122559100101110
63102490.121582001010011
7012340.114258000001000
The information displayed in the dataframe is:
  • counts shows the number of times each state was measured.
  • bitstring is the bitstring that represents each state measured.
  • x and y are the numerical representation of the states associated with the measurement.
  • probability is the probability associated with each measured state.
By default, the number of executions of the quantum program is 20482048. This quantity, called the number of shots, can be modified using ExecutionPreferences. For instance, if we want to execute the same circuit with 10,00010{,}000 shots:
prefs_more_shots = ExecutionPreferences(num_shots=10000)

with ExecutionSession(qprog, execution_preferences=prefs_more_shots) as es:
    results_more_shots = es.sample()
The number of counts for each state will grow proportionally with the number of shots:
results_more_shots.dataframe
xycountprobabilitybitstring
075012740.1274110010111
12512730.1273000101010
21212670.1267000010001
363712660.1266100101110
441712540.1254010001100
552612260.1226011010101
631012240.1224001010011
70112160.1216000001000

Backend selection

The backend of an execution is the hardware or simulator where the quantum program is executed. To select a specific backend, it is necessary to use its correct Backend Preferences. Check the different Cloud Providers and their backend preferences for execution. In this section, we will explore two different examples for clarification:

First example: Execution using the state vector simulator from Classiq

A state vector simulator outputs the amplitudes of a quantum program. On real hardware, obtaining these amplitudes requires quantum tomography — the process of measuring in different bases to reconstruct the output state. Since Classiq provides its own state vector simulator backend, we will use ClassiqBackendPreferences to define it as the state vector simulator. This information is provided on the Cloud Providers page. To define the quantum program’s execution preferences, use execution_preferences under ExecutionSession. In this example, we will perform a simulation with num_shots=1 since the state vector simulator performs an exact simulation of the quantum program. If no backend is defined in the preferences, then the Classiq simulator is selected by default.
backend_preferences = ClassiqBackendPreferences(
    backend_name="simulator_statevector"
)  # Always check the Cloud Providers to correctly define the backend.

execution_preferences = ExecutionPreferences(
    num_shots=1, backend_preferences=backend_preferences
)
Now, execute the quantum program using execute.
with ExecutionSession(qprog, execution_preferences=execution_preferences) as es:
    results_statevector = es.sample()
The outputs of the quantum program can be displayed using the dataframe property:
results_statevector.dataframe.head()
xyamplitudeprobabilitybitstring
07183.923208e-15+2.775558e- 17j1.539233e-29010010111
10333.014272e-15+0.000000e+ 00j9.085836e-30100001000
24491.538630e-15-5.551115e- 17j2.370465e-30110001100
33421.303117e-15+2.775558e- 17j1.698883e-30101010011
45581.185360e-15+1.387779e- 16j1.424337e-30111010101
The outputs from the execution obtained via statevector simulator will differ from the default simulator:
  • state_vector will output a dict containing the bitstrings followed by its numerically evaluated amplitudes.
  • parsed_state_vector will output a list of SimulatedState, each containing the values of x and y followed by its bitstrings and its numerically evaluated amplitudes.