Skip to content

Execute ARITHMETIC FUNCTION With Initial Conditions

We now support execution with initial conditions of arithmetic functions. This enables the loading of numbers (only integers) into each register of the arithmetic function (for example adder), and this initial condition will be considered as part of the circuit in the execution process.

EXECUTE ADDER

from classiq import ModelDesigner
from classiq.builtin_functions import Adder
from classiq.interface.generator.arith.arithmetic import RegisterUserInput
from classiq import Executor
from classiq.interface.backend.backend_preferences import IBMBackendPreferences

params = Adder(
    left_arg=RegisterUserInput(size=3),
    right_arg=RegisterUserInput(size=3),
    output_name="sum",
)
model_designer = ModelDesigner()
model_designer.Adder(params)
circuit = model_designer.synthesize()

initial_values = dict(left_arg=6, right_arg=4)
result = Executor(
    num_shots=10,
    backend_preferences=IBMBackendPreferences(backend_name="aer_simulator"),
).execute(arg=circuit, initial_values=initial_values)

register_data = circuit.get_registers(["sum"])[0]
register_output = result.register_output_from_result(register_data=register_data)

Where register_output is a dictionary where the results are the keys.

EXECUTE ARITHMETIC EXPRESSIONS

from classiq import ModelDesigner
from classiq.builtin_functions import Arithmetic
from classiq.interface.generator.arith.arithmetic import RegisterUserInput
from classiq import Executor
from classiq.interface.backend.backend_preferences import IBMBackendPreferences

params = Arithmetic(
    expression="(a + b -5) == c",
    definitions=dict(
        a=RegisterUserInput(size=3),
        b=RegisterUserInput(size=3),
        c=RegisterUserInput(size=3),
    ),
    uncomputation_method="optimized",
    qubit_count=25,
    output_name="expression_result",
)

model_designer = ModelDesigner()
model_designer.Arithmetic(params)
circuit = model_designer.synthesize()

initialize_registers = dict(a=7, b=1, c=3)
result = Executor(
    num_shots=10,
    backend_preferences=IBMBackendPreferences(backend_name="aer_simulator"),
).execute(arg=circuit, initial_values=initialize_registers)

register_data = circuit.get_registers(["expression_result"])[0]
register_output = result.register_output_from_result(register_data=register_data)
Note

You must send a real positive number as an initial condition to get meaningful results. In addition, the 'RegisterUserInput' must have a default definition: - is_signed = False, - fraction_places = 0. Moreover, all the initialized registers should be input registers.