Use this file to discover all available pages before exploring further.
View on GitHub
Open this notebook in GitHub to run it yourself
This tutorial demonstrates automatic arithmetic operation management by the synthesis engine. It synthesizes a complex arithmetic expression, where uncomputation procedure, together with initialization and reuse of auxiliary qubits, are all automated.Given different global width or depth constraints results in different circuits.Define a quantum model that applies some quantum arithmetic operation on QNum variables.
from classiq import *from classiq.qmod.symbolic import max@qfuncdef main(z: Output[QNum]): x = QNum() y = QNum() x |= 2 y |= 1 z |= (2 * x + y + max(3 * y, 2)) > 4 drop(x) drop(y)qmod = create_model(main)qmod = set_preferences(qmod, random_seed=424788457)
You can try different optimization scenarios, below we introduce two examples:
Optimizing over depth and constraining the maximal width to 9 qubits.
Optimizing over depth and constraining the maximal width to 12 qubits.
Optimizing over depth and constraining the maximal width to 9 qubits
NUM_QUBITS_1 = 9qmod_1 = set_constraints(qmod, optimization_parameter="depth", max_width=NUM_QUBITS_1)qprog_1 = synthesize(qmod_1)show(qprog_1)result = execute(qprog_1).result_value()print("The result of the arithmetic calculation: ", result.parsed_counts)
Output:
Quantum program link: https://platform.classiq.io/circuit/30eUHZJyfNuCdGpLdJhEcX9rCUp The result of the arithmetic calculation: [{'z': 1}: 2048]
Change the quantum model constraint to treat the second scenario for optimizing over depth and constraining the maximal width to 12 qubits:
NUM_QUBITS_2 = 12qmod_2 = set_constraints(qmod, optimization_parameter="depth", max_width=NUM_QUBITS_2)qprog_2 = synthesize(qmod_2)show(qprog_2)result = execute(qprog_2).result_value()print("The result of the arithmetic calculation: ", result.parsed_counts)
Output:
Quantum program link: https://platform.classiq.io/circuit/30eUJhEVwKFryUCOwnNzg7TvB9c The result of the arithmetic calculation: [{'z': 1}: 2048]
The given mathematical expression:z=(2⋅x+y+max(3⋅y,2))>4is solved by the automatic arithmetic operation management, optimizing over depth and constraining the maximal width to 9 and 12 qubits, which outputs the value for z.The parsed_counts result:
Optimizing over depth and constraining the maximal width to 9 qubits: [{'z': 1.0}: 2048]
Optimizing over depth and constraining the maximal width to 12 qubits: [{'z': 1.0}: 2048]
Both the result are same which verifies the arithmetic expression to be True.The expected result: Allocated values:x=2,y=1Expression Calculation:2⋅x+y=2⋅2+1=4+1=53⋅y=3⋅1=3max(3⋅y,2)=max(3,2)=3Therefore:2⋅x+y+max(3⋅y,2)=5+3=8As8>4⟹Truez is assigned the value True : z⟹1