Skip to content

Commit f48b5ec

Browse files
Remove *=
1 parent b8fb3a4 commit f48b5ec

File tree

8 files changed

+77
-48
lines changed

8 files changed

+77
-48
lines changed

algorithms/differential_equations/discrete_poisson_solver/discrete_poisson_solver.ipynb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@
282282
"source": [
283283
"@qfunc\n",
284284
"def matrix_inversion_HHL(\n",
285-
" prefactor: CReal,\n",
285+
" prefactor: float,\n",
286286
" my_unitary: QCallable[CInt, QArray],\n",
287287
" state: QArray,\n",
288288
" phase: QNum,\n",
@@ -295,7 +295,11 @@
295295
" unitary_with_power=lambda power: my_unitary(power, state),\n",
296296
" phase=phase,\n",
297297
" ),\n",
298-
" apply=lambda: assign_amplitude(prefactor / phase, indicator),\n",
298+
" apply=lambda: assign_amplitude_table(\n",
299+
" lookup_table(lambda p: 0 if p == 0 else prefactor / p, phase),\n",
300+
" phase,\n",
301+
" indicator,\n",
302+
" ),\n",
299303
" )"
300304
]
301305
},
@@ -474,7 +478,7 @@
474478
"\n",
475479
"\n",
476480
"qmod = create_model(main, constraints=Constraints(max_width=18))\n",
477-
"write_qmod(qmod, \"discrete_poisson_solver\", decimal_precision=12)\n",
481+
"write_qmod(qmod, \"discrete_poisson_solver\", decimal_precision=12, symbolic_only=False)\n",
478482
"qprog = synthesize(qmod)\n",
479483
"show(qprog)"
480484
]

algorithms/differential_equations/hhl_lanchester/hhl_lanchester.ipynb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,18 @@
571571
"\n",
572572
"@qfunc\n",
573573
"def simple_eig_inv(\n",
574-
" gamma: CReal,\n",
575-
" delta: CReal,\n",
576-
" c_param: CReal,\n",
574+
" gamma: float,\n",
575+
" delta: float,\n",
576+
" c_param: float,\n",
577577
" phase: QNum,\n",
578578
" indicator: Output[QBit],\n",
579579
"):\n",
580580
" allocate(indicator)\n",
581-
" indicator *= c_param / ((gamma * phase) + delta)"
581+
" assign_amplitude_table(\n",
582+
" lookup_table(lambda p: np.clip(c_param / ((gamma * p) + delta), -1, 1), phase),\n",
583+
" phase,\n",
584+
" indicator,\n",
585+
" )"
582586
]
583587
},
584588
{
@@ -840,7 +844,7 @@
840844
" preferences=preferences,\n",
841845
" execution_preferences=execution_preferences,\n",
842846
")\n",
843-
"write_qmod(qmod_hhl_basic, \"hhl_lanchester\", decimal_precision=12)\n",
847+
"write_qmod(qmod_hhl_basic, \"hhl_lanchester\", decimal_precision=12, symbolic_only=False)\n",
844848
"qprog_hhl_basic = synthesize(qmod_hhl_basic)\n",
845849
"show(qprog_hhl_basic)"
846850
]

algorithms/gibbs/quantum_thermal_state_preparation.ipynb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@
575575
"metadata": {},
576576
"outputs": [],
577577
"source": [
578-
"from classiq.qmod.symbolic import exp, min as qmin, pi, sqrt\n",
578+
"from classiq.qmod.symbolic import exp, pi, sqrt\n",
579579
"\n",
580580
"\n",
581581
"class LinbladianBlock(QStruct):\n",
@@ -598,13 +598,21 @@
598598
"\n",
599599
"@qfunc\n",
600600
"def apply_boltzmann_weight(\n",
601-
" beta: CReal, w0: CReal, bohr_freq: QNum, boltzmann_weight: QBit\n",
601+
" beta: float, w0: float, bohr_freq: QNum, boltzmann_weight: QBit\n",
602602
"):\n",
603603
" # glauber\n",
604-
" boltzmann_weight *= sqrt(1 / (exp(beta * w0 * bohr_freq) + 1))\n",
604+
" assign_amplitude_table(\n",
605+
" lookup_table(lambda bf: np.sqrt(1 / (np.exp(beta * w0 * bf) + 1)), bohr_freq),\n",
606+
" bohr_freq,\n",
607+
" boltzmann_weight,\n",
608+
" )\n",
605609
"\n",
606610
" # metropolis\n",
607-
" # boltzmann_weight *= qmin(1, exp(-beta * w0 * bohr_freq))\n",
611+
" # assign_amplitude_table(\n",
612+
" # lookup_table(lambda bf: min(1, np.exp(-beta * w0 * bf)), bohr_freq),\n",
613+
" # bohr_freq,\n",
614+
" # boltzmann_weight,\n",
615+
" # )\n",
608616
"\n",
609617
" # adjust conventions so that sqrt(gamma(w)) will be the amplitude of |0>\n",
610618
" X(boltzmann_weight)\n",
@@ -613,8 +621,8 @@
613621
"@qfunc\n",
614622
"def block_encode_linbladian(\n",
615623
" hamiltonian: CArray[PauliTerm],\n",
616-
" beta: CReal,\n",
617-
" w0: CReal,\n",
624+
" beta: float,\n",
625+
" w0: float,\n",
618626
" be: BlockEncodedLinbladianState,\n",
619627
"):\n",
620628
" operator_fourier_transform(\n",
@@ -679,8 +687,8 @@
679687
"@qfunc\n",
680688
"def delta_step(\n",
681689
" hamiltonian: CArray[PauliTerm],\n",
682-
" beta: CReal,\n",
683-
" w0: CReal,\n",
690+
" beta: float,\n",
691+
" w0: float,\n",
684692
" delta: CReal,\n",
685693
" delta_qbit: QBit,\n",
686694
" be: BlockEncodedLinbladianState,\n",

algorithms/hhl/hhl/hhl.ipynb

Lines changed: 9 additions & 10 deletions
Large diffs are not rendered by default.

applications/finance/option_pricing/option_pricing.ipynb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@
237237
"\n",
238238
"\n",
239239
"# translate from qubit space to price space\n",
240-
"def scale(val: QNum):\n",
240+
"def scale(val):\n",
241241
" return val * grid_step + min(grid_points)\n",
242242
"\n",
243243
"\n",
@@ -257,7 +257,7 @@
257257
"id": "13",
258258
"metadata": {},
259259
"source": [
260-
"For simplicity, use a general purpose amplitude loading method with the `*=` syntax. The calculation is exact, however it is not scalable for large variable sizes. More scalable methods are mentioned in [[4](#GS)] and [[6](#RAINBOW)].\n",
260+
"For simplicity, use a general purpose amplitude loading method with the `assign_amplitude_table` function. The calculation is exact, however it is not scalable for large variable sizes. More scalable methods are mentioned in [[4](#GS)] and [[6](#RAINBOW)].\n",
261261
"\n",
262262
"**Important**: So that all loaded amplitudes are not greater than 1, normalize the payoff by a `scaling_factor`, later multiplied in the post-process stage:"
263263
]
@@ -269,14 +269,16 @@
269269
"metadata": {},
270270
"outputs": [],
271271
"source": [
272-
"from classiq.qmod.symbolic import abs, sqrt\n",
273-
"\n",
274272
"scaling_factor = max(grid_points) - K\n",
275273
"\n",
276274
"\n",
277275
"@qfunc\n",
278276
"def payoff_linear(asset: Const[QNum], ind: QBit):\n",
279-
" ind *= sqrt(abs((scale(asset) - K) / scaling_factor))\n",
277+
" assign_amplitude_table(\n",
278+
" lookup_table(lambda n: np.sqrt(abs((scale(n) - K) / scaling_factor)), asset),\n",
279+
" asset,\n",
280+
" ind,\n",
281+
" )\n",
280282
"\n",
281283
"\n",
282284
"@qfunc\n",
@@ -347,7 +349,7 @@
347349
"outputs": [],
348350
"source": [
349351
"qmod = iqae.get_model()\n",
350-
"write_qmod(qmod, \"option_pricing\", decimal_precision=10)"
352+
"write_qmod(qmod, \"option_pricing\", decimal_precision=10, symbolic_only=False)"
351353
]
352354
},
353355
{

applications/finance/rainbow_options/rainbow_options_bruteforce_method.ipynb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,11 @@
314314
"metadata": {},
315315
"outputs": [],
316316
"source": [
317-
"from classiq.qmod.symbolic import exp, sqrt\n",
318-
"\n",
319-
"\n",
320317
"def get_payoff_expression(x, size, fraction_digits):\n",
321-
" payoff = sqrt(\n",
322-
" qmax(\n",
318+
" payoff = np.sqrt(\n",
319+
" max(\n",
323320
" S0[0]\n",
324-
" * exp(\n",
321+
" * np.exp(\n",
325322
" STEP_X / SCALING_FACTOR * (2 ** (size - fraction_digits)) * x\n",
326323
" + (MU[0] + MIN_X * CHOLESKY[0].sum())\n",
327324
" ),\n",
@@ -331,7 +328,7 @@
331328
" return payoff\n",
332329
"\n",
333330
"\n",
334-
"def get_payoff_expression_normalized(x: QNum, size, fraction_digits):\n",
331+
"def get_payoff_expression_normalized(x, size, fraction_digits):\n",
335332
" x_max = 1 - 1 / (2**size)\n",
336333
" payoff_max = get_payoff_expression(x_max, size, fraction_digits)\n",
337334
" payoff = get_payoff_expression(x, size, fraction_digits)\n",
@@ -344,8 +341,15 @@
344341
" size=max_reg.size, is_signed=False, fraction_digits=max_reg.size\n",
345342
" )\n",
346343
" bind(max_reg, max_reg_fixed)\n",
347-
" ind_reg *= get_payoff_expression_normalized(\n",
348-
" max_reg_fixed, max_reg.size, max_reg.fraction_digits\n",
344+
" assign_amplitude_table(\n",
345+
" lookup_table(\n",
346+
" lambda n: get_payoff_expression_normalized(\n",
347+
" n, max_reg.size, max_reg.fraction_digits\n",
348+
" ),\n",
349+
" max_reg_fixed,\n",
350+
" ),\n",
351+
" max_reg_fixed,\n",
352+
" ind_reg,\n",
349353
" )\n",
350354
" bind(max_reg_fixed, max_reg)"
351355
]
@@ -455,7 +459,7 @@
455459
],
456460
"source": [
457461
"qmod_2 = iqae.get_model()\n",
458-
"write_qmod(qmod_2, \"rainbow_options_bruteforce_method\")\n",
462+
"write_qmod(qmod_2, \"rainbow_options_bruteforce_method\", symbolic_only=False)\n",
459463
"\n",
460464
"print(\"Starting synthesis\")\n",
461465
"qprog_2 = iqae.get_qprog()\n",

tutorials/technology_demonstrations/hhl/hhl_example.ipynb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,16 @@
101101
"@qfunc\n",
102102
"def simple_eig_inv(phase: QNum, indicator: Output[QBit]):\n",
103103
" allocate(indicator)\n",
104-
" indicator *= (1 / 2**phase.size) / phase\n",
104+
" assign_amplitude_table(\n",
105+
" lookup_table(lambda p: 0 if p == 0 else (1 / 2**phase.size) / p, phase),\n",
106+
" phase,\n",
107+
" indicator,\n",
108+
" )\n",
105109
"\n",
106110
"\n",
107111
"@qfunc\n",
108112
"def my_hhl(\n",
109-
" precision: CInt,\n",
113+
" precision: int,\n",
110114
" b: CArray[CReal],\n",
111115
" unitary: QCallable[QArray],\n",
112116
" res: Output[QArray],\n",

tutorials/workshops/hhl_workshop/hhl_workshop.ipynb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@
326326
"C=1/2^{\\rm precision}.\n",
327327
"$$\n",
328328
"\n",
329-
"The built-in construct to define an amplitude loading is designated by `*=`, see [Expression Assignment Operations](https://docs.classiq.io/latest/qmod-reference/language-reference/statements/numeric-assignment/#example-3-in-place-assignment-of-a-logical-expression)."
329+
"The built-in construct to define an amplitude loading is the [`assign_amplitude_table`](https://docs.classiq.io/latest/qmod-reference/api-reference/functions/open_library/amplitude_loading/#classiq.open_library.functions.amplitude_loading.assign_amplitude_table) function."
330330
]
331331
},
332332
{
@@ -339,11 +339,15 @@
339339
"@qfunc\n",
340340
"def simple_eig_inv(phase: Const[QNum], indicator: Output[QBit]):\n",
341341
" # TODO allocate 1 qubit for indicator\n",
342-
" # TODO load its |1> state amplitude to be C/phase using the *= operator\n",
342+
" # TODO load its |1> state amplitude to be C/phase using the assign_amplitude_table function\n",
343343
"\n",
344344
" # Solution start\n",
345345
" allocate(indicator)\n",
346-
" indicator *= (1 / 2**phase.size) / phase\n",
346+
" assign_amplitude_table(\n",
347+
" lookup_table(lambda p: 0 if p == 0 else (1 / 2**phase.size) / p, phase),\n",
348+
" phase,\n",
349+
" indicator,\n",
350+
" )\n",
347351
" # Solution end"
348352
]
349353
},
@@ -404,7 +408,7 @@
404408
"source": [
405409
"@qfunc\n",
406410
"def my_hhl(\n",
407-
" fraction_digits: CInt,\n",
411+
" fraction_digits: int,\n",
408412
" b: CArray[CReal],\n",
409413
" unitary_with_matrix: QCallable[QArray],\n",
410414
" res: Output[QArray],\n",

0 commit comments

Comments
 (0)