Execute Arithmetic Functions with Initial Conditions¶
You can execute arithmetic functions by initializing the function registers. This enables loading numbers (only integers) into each register of the arithmetic function (for example, adder), so this initial condition is considered as part of the circuit in the execution process.
Execute Adder¶
from classiq import Model, RegisterUserInput
from classiq.builtin_functions import Adder
from classiq import Executor
from classiq.execution import IBMBackendPreferences
# Define a circuit that generates a circuit including addition operations.
params = Adder(
left_arg=RegisterUserInput(size=3),
right_arg=RegisterUserInput(size=3),
output_name="sum",
)
model = Model()
model.Adder(params)
circuit = model.synthesize()
# Execute the circuit and initialize the registers
# as a dictionary (`initial_values` in the example). Note that the register
# names are the dictionary keys and the initial values are its values.
initial_values = dict(left_arg=6, right_arg=4)
result = Executor(
num_shots=10,
backend_preferences=IBMBackendPreferences(backend_name="aer_simulator"),
).execute(circuit, initial_values=initial_values)
Execute Arithmetic Expressions¶
You can execute any arithmetic expression with initial conditions as shown above. This example shows a circuit calculating an arithmetic expression with initial conditions:
from classiq import Model, RegisterUserInput
from classiq.builtin_functions import Arithmetic
from classiq import Executor
from classiq.execution 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 = Model()
model.Arithmetic(params)
circuit = model.synthesize()
initial_values = dict(a=7, b=1, c=3)
result = Executor(
num_shots=10,
backend_preferences=IBMBackendPreferences(backend_name="aer_simulator"),
).execute(circuit, initial_values=initial_values)
Note
To get meaningful results, you must send a real positive number as an initial condition. In addition, the 'RegisterUserInput' must have a default definition:
is_signed = False
fraction_places = 0
Moreover, ensure all the initialized registers are input registers.