Skip to main content

synthesize

synthesize(
model: SerializedModel | BaseQFunc,
auto_show: bool = False,
constraints: Constraints | None = None,
preferences: Preferences | None = None
) -> QuantumProgram
Synthesize a model with the Classiq engine to receive a quantum program. More details Parameters:
NameTypeDescriptionDefault
modelSerializedModel | BaseQFuncThe entry point of the Qmod model - a qfunc named ‘main’ (or alternatively the output of ‘create_model’).required
auto_showboolWhether to ‘show’ the synthesized model (False by default).False
constraintsConstraints | NoneConstraints for the synthesis of the model. See Constraints (Optional).None
preferencesPreferences | NonePreferences for the synthesis of the model. See Preferences (Optional).None
Returns:

show

show(
quantum_program: QuantumProgram,
display_url: bool = True
) -> None
Displays the interactive representation of the quantum program in the Classiq IDE. Parameters:
NameTypeDescriptionDefault
quantum_programQuantumProgramThe quantum program to be displayed.required
display_urlboolWhether to print the urlTrue

QuantumProgram

Methods:
NameDescription
to_base_program
to_program
save_resultsSaves quantum program results as json into a file.
get_debug_info
raise_warningsRaises all warnings that were collected during synthesis.

hardware_data

hardware_data: SynthesisHardwareData

data

data: GeneratedCircuitData

model

model: ExecutionModel

transpiled_circuit

transpiled_circuit: TranspiledCircuitData | None = pydantic.Field(default=None)

creation_time

creation_time: str = pydantic.Field(default_factory=_get_formatted_utc_current_time)

compressed_debug_info

compressed_debug_info: bytes | None = pydantic.Field(default=None)

program_id

program_id: str = pydantic.Field(default_factory=get_uuid_as_str)

execution_primitives_input

execution_primitives_input: PrimitivesInput | None = pydantic.Field(default=None)

synthesis_warnings

synthesis_warnings: list[str] | None = pydantic.Field(default=None)

compiled_qmod

compiled_qmod: Model | None = pydantic.Field(default=None)

program_circuit

program_circuit: CircuitCodeInterface

save_results

save_results(
self: ,
filename: str | Path | None = None
) -> None
Saves quantum program results as json into a file. Parameters: filename (Union[str, Path]): Optional, path + filename of file. If filename supplied add .json suffix. Returns: None Parameters:
NameTypeDescriptionDefault
selfrequired
filenamestr | Path | NoneNone

raise_warnings

raise_warnings(
self:
) -> None
Raises all warnings that were collected during synthesis. Parameters:
NameTypeDescriptionDefault
selfrequired

Preferences

Preferences for synthesizing a quantum circuit. Methods: Attributes:
NameTypeDescription
machine_precisionintSpecifies the precision used for quantum operations. Defaults to DEFAULT_MACHINE_PRECISION.
backend_service_providerstrThe provider company or cloud service for the requested backend. Defaults to None.
backend_namestrThe name of the requested backend or target. Defaults to None.
custom_hardware_settingsCustomHardwareSettingsDefines custom hardware settings for optimization. This field is ignored if backend preferences are specified.
debug_modeboolIf True, debug information is added to the synthesized result, potentially slowing down the synthesis. Useful for executing interactive algorithms. Defaults to True.
optimization_levelOptimizationLevel)The optimization level used during synthesis (0-3);
output_formatList[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_qasmboolIf True, formats OpenQASM 2 outputs with line breaks inside gate declarations, improving readability. Defaults to True.
qasm3Optional[bool]If True, outputs OpenQASM 3.0 in addition to 2.0, applicable to relevant attributes in GeneratedCircuit. Defaults to None.
transpilation_optionTranspilationOptionSets the transpilation option to optimize the circuit. Defaults to AUTO_OPTIMIZE. See TranspilationOption
solovay_kitaev_max_iterationsOptional[int]Specifies the maximum number of iterations for the Solovay-Kitaev algorithm, if used. Defaults to None.
timeout_secondsintTimeout setting for circuit synthesis in seconds. Defaults to 300.
optimization_timeout_secondsOptional[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.
random_seedintRandom seed for circuit synthesis.

machine_precision

machine_precision: PydanticMachinePrecision = DEFAULT_MACHINE_PRECISION

backend_service_provider

backend_service_provider: Provider | ProviderVendor | str | None = pydantic.Field(default=None, description='Provider company or cloud for the requested backend.')

backend_name

backend_name: PydanticBackendName | AllBackendsNameByVendor | None = pydantic.Field(default=None, description='Name of the requested backend or target.')

custom_hardware_settings

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

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

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

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

output_format

output_format: PydanticConstrainedQuantumFormatList = pydantic.Field(default=[QuantumFormat.QASM], description='The quantum circuit output format(s). ')

pretty_qasm

pretty_qasm: bool = pydantic.Field(True, description='Prettify the OpenQASM2 outputs (use line breaks inside the gate declarations).')

qasm3

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

transpilation_option: TranspilationOption = pydantic.Field(default=(TranspilationOption.AUTO_OPTIMIZE), description='If true, the returned result will contain a transpiled circuit and its depth')

solovay_kitaev_max_iterations

solovay_kitaev_max_iterations: pydantic.PositiveInt | None = pydantic.Field(None, description='Maximum iterations for the Solovay-Kitaev algorithm (if applied).')

timeout_seconds

timeout_seconds: pydantic.PositiveInt = pydantic.Field(default=300, description='Generation timeout in seconds')

optimization_timeout_seconds

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

random_seed

random_seed: int = pydantic.Field(default_factory=create_random_seed, description='The random seed used for the generation')

symbolic_loops

symbolic_loops: bool = pydantic.Field(default=False)

backend_preferences

backend_preferences: BackendPreferences | None

set_preferences

set_preferences(
serialized_model: SerializedModel,
preferences: Preferences | None = None,
kwargs: Any = 
) -> SerializedModel
Overrides the preferences of a (serialized) model and returns the updated model. Parameters:
NameTypeDescriptionDefault
serialized_modelSerializedModelThe model in serialized form.required
preferencesPreferences | NoneThe new preferences to be set for the model. Can be passed as keyword arguments.None
kwargsAny
Returns:
  • Type: SerializedModel
  • The updated model with the new preferences applied.

set_execution_preferences

set_execution_preferences(
serialized_model: SerializedModel,
execution_preferences: ExecutionPreferences | None = None,
kwargs: Any = 
) -> SerializedModel
Overrides the execution preferences of a (serialized) model and returns the updated model. Parameters:
NameTypeDescriptionDefault
serialized_modelSerializedModelA serialization of the defined model.required
execution_preferencesExecutionPreferences | NoneThe new execution preferences to be set for the model. Can be passed as keyword arguments.None
kwargsAny

Constraints

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: Attributes:
NameTypeDescription
max_widthintMaximum number of qubits allowed in the generated quantum circuit. Defaults to None.
max_depthintMaximum depth of the generated quantum circuit. Defaults to None.
max_gate_countDict[TranspilerBasisGates, int]A dictionary specifying the maximum allowed count for each type of gate in the quantum circuit. Defaults to an empty dictionary.
optimization_parameterOptimizationParameterTypeDetermines if and how the synthesis engine should optimize the solution. Defaults to NO_OPTIMIZATION. See OptimizationParameterType

model_config

model_config = ConfigDict(extra='forbid')

max_width

max_width: pydantic.PositiveInt | None = pydantic.Field(default=None, description='Maximum number of qubits in generated quantum circuit')

max_depth

max_depth: pydantic.PositiveInt | None = pydantic.Field(default=None)

max_gate_count

max_gate_count: dict[TranspilerBasisGates, pydantic.NonNegativeInt] = pydantic.Field(default_factory=(lambda: defaultdict(int)))

optimization_parameter

optimization_parameter: OptimizationParameterType = pydantic.Field(default=(OptimizationParameter.NO_OPTIMIZATION), description='If set, the synthesis engine optimizes the solution according to that chosen parameter')

set_constraints

set_constraints(
serialized_model: SerializedModel,
constraints: Constraints | None = None,
kwargs: Any = 
) -> SerializedModel
Overrides the constraints of a (serialized) model and returns the updated model. Parameters:
NameTypeDescriptionDefault
serialized_modelSerializedModelThe model in serialized form.required
constraintsConstraints | NoneThe new constraints to be set for the model. Can be passed as keyword arguments.None
kwargsAny
Returns:
  • Type: SerializedModel
  • The updated model with the new constraints applied.

create_model

create_model(
entry_point: QFunc | GenerativeQFunc,
constraints: Constraints | None = None,
execution_preferences: ExecutionPreferences | None = None,
preferences: Preferences | None = None,
classical_execution_function: CFunc | None = None,
out_file: str | None = None
) -> SerializedModel
Create a serialized model from a given Qmod entry function and additional parameters. Parameters:
NameTypeDescriptionDefault
entry_pointQFunc | GenerativeQFuncThe entry point function for the model, which must be a QFunc named ‘main’.required
constraintsConstraints | NoneConstraints for the synthesis of the model. See Constraints (Optional).None
execution_preferencesExecutionPreferences | NonePreferences for the execution of the model. See ExecutionPreferences (Optional).None
preferencesPreferences | NonePreferences for the synthesis of the model. See Preferences (Optional).None
classical_execution_functionCFunc | NoneA function for the classical execution logic, which must be a CFunc (Optional).None
out_filestr | NoneFile path to write the Qmod model in native Qmod representation to (Optional).None
Returns:
  • Type: SerializedModel
  • A serialized model. Functions:
NameDescription
write_qmodCreates a native Qmod file from a serialized model and outputs the synthesis options (Preferences and Constraints) to a file.

write_qmod

write_qmod(
model: SerializedModel | QFunc | GenerativeQFunc,
name: str,
directory: Path | None = None,
decimal_precision: int = DEFAULT_DECIMAL_PRECISION,
symbolic_only: bool = True
) -> None
Creates a native Qmod file from a serialized model and outputs the synthesis options (Preferences and Constraints) to a file. The native Qmod file may be uploaded to the Classiq IDE. Parameters:
NameTypeDescriptionDefault
modelSerializedModel | QFunc | GenerativeQFuncThe entry point of the Qmod model - a qfunc named ‘main’ (or alternatively the output of ‘create_model’).required
namestrThe name to save the file by.required
directoryPath | NoneThe directory to save the files in. If None, the current working directory is used.None
decimal_precisionintThe number of decimal places to use for numbers, set to 4 by default.DEFAULT_DECIMAL_PRECISION
symbolic_onlyboolIf True keep function definitions un-expanded and symbolic (note that Qmod functions with parameters of Python types are not supported in this mode)True
Returns:
  • Type: None

qasm_to_qmod

qasm_to_qmod(
qasm: str,
qmod_format: QmodFormat
) -> str
Decompiles QASM to Native/Python Qmod. Returns Qmod code as a string. Native Qmod can be synthesized in the Classiq IDE, while Python Qmod can be copy-pasted to a Python file (.py) and synthesized by calling synthesize(main). Parameters:
NameTypeDescriptionDefault
qasmstrQASM 2 or QASM 3 coderequired
qmod_formatQmodFormatThe requested output formatrequired
Returns:
  • Type: str
  • The decompiled Qmod program