The implementation is based on Algorithm 1 & Algorithm 2 in [1], with the intent of demistifying variables names
and simplifying the code flow.
Moreover, we separated the algorithm flow from quantum execution to allow migrating this code to any execution
interface and to improve its testability.Methods:
We want to find the largest K (with some lower and upper bounds) such that the K-scaled confidence interval
lies completely in the upper or lower half planes.
See Algorithm 2, [1].Parameters:
Handles the data storage for a single iteration of the Iterative Quantum Amplitude
Estimation algorithm.This class is intended to represent the results and state of a single Grover iteration
of the IQAE process.Attributes:
Name
Type
Description
grover_iterations
int
The iteration number of Grover’s algorithm.
sample_results
ExecutionDetails
The ExecutionDetails of Grover iteration. See ExecutionDetails.
Represents the result of an Iterative Quantum Amplitude Estimation (IQAE)
process.This class encapsulates the output of the IQAE algorithm, including the
estimated value, confidence interval, intermediate iteration data, and
any warnings generated during the computation.Attributes:
Name
Type
Description
estimation
float
Estimation of the amplitude.
confidence_interval
list[float]
The interval in which the amplitude is within, with a probability equal to epsilon.
iterations_data
list[IQAEIterationData]
List of IQAEIterationData of each Grover iteration.
warnings
list[str]
List of warnings generated during the IQAE process of each Grover iteration.
Represents the execution settings for running a quantum program.
Execution preferences for running a quantum program.For more details, refer to:
ExecutionPreferences example: ExecutionPreferences..Attributes:
Name
Type
Description
noise_properties
Optional[NoiseProperties]
Properties defining the noise in the quantum circuit. Defaults to None.
random_seed
int
The random seed used for the execution. Defaults to a randomly generated seed.
backend_preferences
BackendPreferencesTypes
Preferences for the backend used to execute the circuit. Defaults to the Classiq Simulator.
num_shots
Optional[pydantic.PositiveInt]
The number of shots (executions) to be performed.
transpile_to_hardware
TranspilationOption
Option to transpile the circuit to the hardware’s basis gates before execution. Defaults to TranspilationOption.DECOMPOSE.
job_name
Optional[str]
The name of the job, with a minimum length of 1 character.
transpile_to_hardware: TranspilationOption = pydantic.Field(default=(TranspilationOption.DECOMPOSE), description='Transpile the circuit to the hardware basis gates before execution', title='Transpilation Option')
include_zero_amplitude_outputs: bool = pydantic.Field(default=False, description='In state vector simulation, whether to include zero-amplitude states in the result')
Constraints for the quantum circuit synthesis engine.This class is used to specify constraints such as maximum width, depth,
gate count, and optimization parameters for the synthesis engine,
guiding the generation of quantum circuits that satisfy these constraints.Methods:
optimization_parameter: OptimizationParameterType = pydantic.Field(default=(OptimizationParameter.NO_OPTIMIZATION), description='If set, the synthesis engine optimizes the solution according to that chosen parameter')
Specifies the precision used for quantum operations. Defaults to DEFAULT_MACHINE_PRECISION.
backend_service_provider
str
The provider company or cloud service for the requested backend. Defaults to None.
backend_name
str
The name of the requested backend or target. Defaults to None.
custom_hardware_settings
CustomHardwareSettings
Defines custom hardware settings for optimization. This field is ignored if backend preferences are specified.
debug_mode
bool
If True, debug information is added to the synthesized result, potentially slowing down the synthesis. Useful for executing interactive algorithms. Defaults to True.
optimization_level
OptimizationLevel)
The optimization level used during synthesis (0-3);
output_format
List[QuantumFormat]
Lists the output format(s) for the quantum circuit. Defaults to [QuantumFormat.QASM]. QuantumFormat Options: - QASM = “qasm” - QSHARP = “qsharp” - QIR = “qir” - IONQ = “ionq” - CIRQ_JSON = “cirq_json” - QASM_CIRQ_COMPATIBLE = “qasm_cirq_compatible”
pretty_qasm
bool
If True, formats OpenQASM 2 outputs with line breaks inside gate declarations, improving readability. Defaults to True.
qasm3
Optional[bool]
If True, outputs OpenQASM 3.0 in addition to 2.0, applicable to relevant attributes in GeneratedCircuit. Defaults to None.
transpilation_option
TranspilationOption
Sets the transpilation option to optimize the circuit. Defaults to AUTO_OPTIMIZE. See TranspilationOption
solovay_kitaev_max_iterations
Optional[int]
Specifies the maximum number of iterations for the Solovay-Kitaev algorithm, if used. Defaults to None.
timeout_seconds
int
Timeout setting for circuit synthesis in seconds. Defaults to 300.
optimization_timeout_seconds
Optional[int]
Specifies the timeout for optimization in seconds, or None for no optimization timeout. This will still adhere to the overall synthesis timeout. Defaults to None.
backend_service_provider: Provider | ProviderVendor | str | None = pydantic.Field(default=None, description='Provider company or cloud for the requested backend.')
backend_name: PydanticBackendName | AllBackendsNameByVendor | None = pydantic.Field(default=None, description='Name of the requested backend or target.')
custom_hardware_settings: CustomHardwareSettings = pydantic.Field(default_factory=CustomHardwareSettings, description='Custom hardware settings which will be used during optimization. This field is ignored if backend preferences are given.')
debug_mode: bool = pydantic.Field(default=True, description='Add debug information to the synthesized result. Setting this option to False can potentially speed up the synthesis, and is recommended for executing iterative algorithms.')
synthesize_all_separately: bool = pydantic.Field(default=False, description='If true, a heuristic is used to determine if a function should be synthesized separately', deprecated=True)
optimization_level: OptimizationLevel = pydantic.Field(default=(OptimizationLevel.LIGHT), description='The optimization level used during synthesis; determines the trade-off between synthesis speed and the quality of the results')
qasm3: bool | None = pydantic.Field(None, description='Output OpenQASM 3.0 instead of OpenQASM 2.0. Relevant only for the qasmandtranspiled_circuit.qasmattributes ofGeneratedCircuit.')
transpilation_option: TranspilationOption = pydantic.Field(default=(TranspilationOption.AUTO_OPTIMIZE), description='If true, the returned result will contain a transpiled circuit and its depth')
optimization_timeout_seconds: pydantic.PositiveInt | None = pydantic.Field(default=None, description='Optimization timeout in seconds, or None for no optimization timeout (will still timeout when the generation timeout is over)')
A session for executing a quantum program.
ExecutionSession allows to execute the quantum program with different parameters and operations without the need to re-synthesize the model.
The session must be closed in order to ensure resources are properly cleaned up. It’s recommended to use ExecutionSession as a context manager for this purpose. Alternatively, you can directly use the close method.Methods:
Samples the quantum program with the given parameters, if any.Parameters:
Name
Type
Description
Default
self
“
required
parameters
ExecutionParams | None
The values to set for the parameters of the quantum program when sampling. Each key should be the name of a parameter in the quantum program (parameters of the main function), and the value should be the value to set for that parameter.
Initiates an execution job with the sample primitive.This is a non-blocking version of sample: it gets the same parameters and initiates the same execution job, but instead
of waiting for the result, it returns the job object immediately.Parameters:
Name
Type
Description
Default
self
“
required
parameters
ExecutionParams | None
The values to set for the parameters of the quantum program when sampling. Each key should be the name of a parameter in the quantum program (parameters of the main function), and the value should be the value to set for that parameter.
Samples the quantum program multiple times with the given parameters for each iteration. The number of samples is determined by the length of the parameters list.Parameters:
Name
Type
Description
Default
self
“
required
parameters
list[ExecutionParams]
A list of the parameters for each iteration. Each item is a dictionary where each key should be the name of a parameter in the quantum program (parameters of the main function), and the value should be the value to set for that parameter.
required
Returns:
Type:list[ExecutionDetails]
List[ExecutionDetails]: The results of all the sampling iterations.
Initiates an execution job with the batch_sample primitive.This is a non-blocking version of batch_sample: it gets the same parameters and initiates the same execution job, but instead
of waiting for the result, it returns the job object immediately.Parameters:
Name
Type
Description
Default
self
“
required
parameters
list[ExecutionParams]
A list of the parameters for each iteration. Each item is a dictionary where each key should be the name of a parameter in the quantum program (parameters of the main function), and the value should be the value to set for that parameter.
Estimates the expectation value of the given Hamiltonian using the quantum program.Parameters:
Name
Type
Description
Default
self
“
required
hamiltonian
Hamiltonian
The Hamiltonian to estimate the expectation value of.
required
parameters
ExecutionParams | None
The values to set for the parameters of the quantum program when estimating. Each key should be the name of a parameter in the quantum program (parameters of the main function), and the value should be the value to set for that parameter.
Initiates an execution job with the estimate primitive.This is a non-blocking version of estimate: it gets the same parameters and initiates the same execution job, but instead
of waiting for the result, it returns the job object immediately.Parameters:
Name
Type
Description
Default
self
“
required
hamiltonian
Hamiltonian
The Hamiltonian to estimate the expectation value of.
required
parameters
ExecutionParams | None
The values to set for the parameters of the quantum program when estimating. Each key should be the name of a parameter in the quantum program (parameters of the main function), and the value should be the value to set for that parameter.
Estimates the expectation value of the given Hamiltonian multiple times using the quantum program, with the given parameters for each iteration. The number of estimations is determined by the length of the parameters list.Parameters:
Name
Type
Description
Default
self
“
required
hamiltonian
Hamiltonian
The Hamiltonian to estimate the expectation value of.
required
parameters
list[ExecutionParams]
A list of the parameters for each iteration. Each item is a dictionary where each key should be the name of a parameter in the quantum program (parameters of the main function), and the value should be the value to set for that parameter.
required
Returns:
Type:list[EstimationResult]
List[EstimationResult]: The results of all the estimation iterations.
Initiates an execution job with the batch_estimate primitive.This is a non-blocking version of batch_estimate: it gets the same parameters and initiates the same execution job, but instead
of waiting for the result, it returns the job object immediately.Parameters:
Name
Type
Description
Default
self
“
required
hamiltonian
Hamiltonian
The Hamiltonian to estimate the expectation value of.
required
parameters
list[ExecutionParams]
A list of the parameters for each iteration. Each item is a dictionary where each key should be the name of a parameter in the quantum program (parameters of the main function), and the value should be the value to set for that parameter.
Minimizes the given cost function using the quantum program.Parameters:
Name
Type
Description
Default
self
“
required
cost_function
Hamiltonian | QmodExpressionCreator
The cost function to minimize. It can be one of the following: - A quantum cost function defined by a Hamiltonian. - A classical cost function represented as a callable that returns a Qmod expression. The callable should accept QVars as arguments and use names matching the Model outputs.
required
initial_params
ExecutionParams
The initial parameters for the minimization. Only Models with exactly one execution parameter are supported. This parameter must be of type CReal or CArray. The dictionary must contain a single key-value pair, where: - The key is the name of the parameter. - The value is either a float or a list of floats.
required
max_iteration
int
The maximum number of iterations for the minimization.
required
quantile
float
The quantile to use for cost estimation.
1.0
tolerance
float | None
The tolerance for the minimization.
None
Returns:
Type:list[tuple[float, ExecutionParams]]
A list of tuples, each containing the estimated cost and the corresponding parameters for that iteration. cost is a float, and parameters is a dictionary matching the execution parameter format.
Initiates an execution job with the minimize primitive.This is a non-blocking version of minimize: it gets the same parameters and initiates the same execution job, but instead
of waiting for the result, it returns the job object immediately.Parameters:
Name
Type
Description
Default
self
“
required
cost_function
Hamiltonian | QmodExpressionCreator
The cost function to minimize. It can be one of the following: - A quantum cost function defined by a Hamiltonian. - A classical cost function represented as a callable that returns a Qmod expression. The callable should accept QVars as arguments and use names matching the Model outputs.
required
initial_params
ExecutionParams
The initial parameters for the minimization. Only Models with exactly one execution parameter are supported. This parameter must be of type CReal or CArray. The dictionary must contain a single key-value pair, where: - The key is the name of the parameter. - The value is either a float or a list of floats.
required
max_iteration
int
The maximum number of iterations for the minimization.
When simulating on a statevector simulator, emulate the behavior of postprocessing
by discarding amplitudes for which their states are “undesirable”.Parameters:
Name
Type
Description
Default
self
“
required
output_name
str
The name of the register to filter
required
condition
Callable
Filter out values of the statevector for which this callable is False
A type representing a single qubit.QBit serves both as a placeholder for a temporary, non-allocated qubit
and as the type of an allocated physical or logical qubit.
Conceptually, a qubit is a two-level quantum system, described by the
superposition of the computational basis states:∣0⟩=(10),∣1⟩=(01)Therefore, a qubit state is a linear combination:∣ψ⟩=α∣0⟩+β∣1⟩,where ( \alpha ) and ( \beta ) are complex numbers satisfying:∣α∣2+∣β∣2=1.Typical usage includes:
Representing an unallocated qubit before its allocation.
Acting as the output type for a qubit or an allocated qubit in the main function after calling an allocation function.
Initialize a quantum variable to a new quantum object in the zero state: \left|\text{out}\right\rangle = \left|0\right\rangle^{\otimes \text{num_qubits}}If ‘num_qubits’ is not specified, it will be inferred according to the type of ‘out’.
In case the quantum variable is of type QNum, its numeric attributes can be specified as
well.Parameters:
Reassign qubit or arrays of qubits by redirecting their logical identifiers.This operation rewires the logical identity of the source qubits to new objects given in destination.
For example, an array of two qubits X can be mapped to individual qubits Y and Z.Parameters:
Name
Type
Description
Default
source
Input[QVar] | list[Input[QVar]]
A qubit or list of initialized qubits to reassign.
required
destination
Output[QVar] | list[Output[QVar]]
A qubit or list of target qubits to bind to. Must match the number of qubits in source.
Given two operations U and V, performs the sequence of operations U{−1}VU.This operation is used to represent a sequence where the inverse gate U^{-1} is applied, followed by another operation V, and then U is applied to uncompute. This pattern is common in reversible
computation and quantum subroutines.Parameters:
Name
Type
Description
Default
within
Callable[[], Statements]
The unitary operation U to be computed and then uncomputed.
Implementation of Iterative Quantum Amplitude Estimation [1].
Given A s.t. A‘∣0>‘n‘∣0>‘={1−a}∣ψ0>n‘∣0>‘+{a}∣ψ1>n‘∣1>‘, the algorithm estimates
a by iteratively sampling QkA, where Q=AS0A{†}S{ψ0}, and k is an integer variable.For estimating a, The algorithm estimates θa which is defined by a=sin2(θa), so it starts with a
confidence interval (0,π/2) and narrows down this interval on each iteration according to the sample results.Methods:
Executes IQAE’s quantum program with the provided epsilon, alpha, and execution
preferences.
If execution_preferences has been proved, or if it does not contain num_shot, then num_shot is set to 2048.Parameters:
Name
Type
Description
Default
self
“
required
epsilon
float
Target accuracy in therm of θa e.g a=sin2(θa±ϵ) .
required
alpha
float
Specifies the confidence level (1 - alpha)
required
execution_preferences
ExecutionPreferences | None
Preferences for the execution of the model. See ExecutionPreferences (Optional).
None
Members:
Name
Description
IQAEIterationData
Handles the data storage for a single iteration of the Iterative Quantum Amplitude Estimation algorithm.
IQAEResult
Represents the result of an Iterative Quantum Amplitude Estimation (IQAE) process.
Handles the data storage for a single iteration of the Iterative Quantum Amplitude
Estimation algorithm.This class is intended to represent the results and state of a single Grover iteration
of the IQAE process.Attributes:
Name
Type
Description
grover_iterations
int
The iteration number of Grover’s algorithm.
sample_results
ExecutionDetails
The ExecutionDetails of Grover iteration. See ExecutionDetails.
Represents the result of an Iterative Quantum Amplitude Estimation (IQAE)
process.This class encapsulates the output of the IQAE algorithm, including the
estimated value, confidence interval, intermediate iteration data, and
any warnings generated during the computation.Attributes:
Name
Type
Description
estimation
float
Estimation of the amplitude.
confidence_interval
list[float]
The interval in which the amplitude is within, with a probability equal to epsilon.
iterations_data
list[IQAEIterationData]
List of IQAEIterationData of each Grover iteration.
warnings
list[str]
List of warnings generated during the IQAE process of each Grover iteration.