Execute Arithmetic Functions with Initial Values¶
It is possible to initialize registers with integer values, prior to the execution process. To do so, set the model inputs before synthesis (See Model inputs and outputs), then set the initial values for execution after synthesis.
Usage¶
You can set your initial values in the quantum program before the execution,
as in this example, which assumes a quantum program with two inputs (a
and b
)
and assigns their values (1
and 7
respectively):
from classiq.execution import set_initial_values
quantum_program = set_initial_values(quantum_program, a=1, b=7)
Examples¶
Adder¶
from classiq import Model, RegisterUserInput, QUInt, execute, synthesize
from classiq.builtin_functions import Adder
from classiq.execution import set_initial_values
# Define a model with an addition operation.
params = Adder(
left_arg=RegisterUserInput(size=3),
right_arg=RegisterUserInput(size=3),
)
model = Model()
in_wires = model.create_inputs(
{name: QUInt[reg.size] for name, reg in params.inputs.items()}
)
adder_out = model.Adder(params, in_wires=in_wires)
model.set_outputs({"sum": adder_out["sum"]})
model.sample()
# Synthesize the model
quantum_program = synthesize(model.get_model())
# Initialize the registers and execute
quantum_program = set_initial_values(quantum_program, left_arg=6, right_arg=4)
results = execute(quantum_program).result()
Arithmetic¶
from classiq import Model, RegisterUserInput, QUInt, execute, synthesize
from classiq.builtin_functions import Arithmetic
from classiq.execution import set_initial_values
# Define a model with an arithmetic operation.
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,
)
model = Model()
in_wires = model.create_inputs(
{name: QUInt[reg.size] for name, reg in params.inputs.items()}
)
arithmetic_out = model.Arithmetic(params, in_wires=in_wires)
model.set_outputs(arithmetic_out)
model.sample()
# Synthesize the model
quantum_program = synthesize(model.get_model())
# Initialize the registers and execute
quantum_program = set_initial_values(quantum_program, a=7, b=1, c=3)
results = execute(quantum_program).result()
Note
To get meaningful results, you must send a positive integer number as an initial condition.
In addition, RegisterUserInput
must have a default definition:
is_signed = False
fraction_places = 0
Moreover, ensure all the initialized registers are input registers.