Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product/matanvax2/cad 25786 remove prepare int #687

Merged
merged 5 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions algorithms/algebraic/discrete_log/discrete_log.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@
" N_modulus: CInt,\n",
Copy link
Member

@TomerGoldfriend TomerGoldfriend Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add the preferences? do you know if the optimization level affects the results?


Reply via ReviewNB

" x1: QArray[QBit],\n",
" x2: QArray[QBit],\n",
" func_res: Output[QArray[QBit]],\n",
" func_res: Output[QNum],\n",
") -> None:\n",
" allocate(ceiling(log(N_modulus, 2)), func_res)\n",
"\n",
" inplace_prepare_int(1, func_res)\n",
" func_res ^= 1\n",
" modular_exp(N_modulus, x_element, func_res, x1)\n",
" modular_exp(N_modulus, g_generator, func_res, x2)"
]
Expand Down Expand Up @@ -403,10 +403,12 @@
],
"source": [
"constraints = Constraints(max_width=23)\n",
"preferences = Preferences(optimization_level=1)\n",
"execution_preferences = ExecutionPreferences(num_shots=10000)\n",
"qmod_Z13 = create_model(\n",
" main,\n",
" constraints=constraints,\n",
" preferences=preferences,\n",
" out_file=\"discrete_log_large\",\n",
" execution_preferences=execution_preferences,\n",
")\n",
Expand Down
2 changes: 1 addition & 1 deletion algorithms/algebraic/discrete_log/discrete_log.qmod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
qfunc discrete_log_oracle(g_generator: int, x_element: int, N_modulus: int, x1: qbit[], x2: qbit[], output func_res: qbit[]) {
allocate(ceiling(log(N_modulus, 2)), func_res);
inplace_prepare_int(1, func_res);
func_res ^= 1;
modular_exp(N_modulus, x_element, func_res, x1);
modular_exp(N_modulus, g_generator, func_res, x2);
}
Expand Down
2 changes: 1 addition & 1 deletion algorithms/algebraic/discrete_log/discrete_log_large.qmod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
qfunc discrete_log_oracle(g_generator: int, x_element: int, N_modulus: int, x1: qbit[], x2: qbit[], output func_res: qbit[]) {
allocate(ceiling(log(N_modulus, 2)), func_res);
inplace_prepare_int(1, func_res);
func_res ^= 1;
modular_exp(N_modulus, x_element, func_res, x1);
modular_exp(N_modulus, g_generator, func_res, x2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ qfunc main(output b: qnum, output ctrl: qbit[2], output aux: qbit) {
allocate(5, b);
allocate(2, ctrl);
allocate(1, aux);
inplace_prepare_int(8, b);
b ^= 8;
X(ctrl[0]);
X(ctrl[1]);
within {
Expand Down
6 changes: 3 additions & 3 deletions algorithms/algebraic/shor/shor_modular_exponentiation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
" allocate(1, aux)\n",
"\n",
" # set initial values for the addition\n",
" inplace_prepare_int(b_initial_value, b)\n",
" b ^= b_initial_value\n",
" X(ctrl[0])\n",
" X(ctrl[1])\n",
"\n",
Expand Down Expand Up @@ -544,7 +544,7 @@
"\n",
"@qfunc\n",
"def main(\n",
" x: Output[QArray[QBit]],\n",
" x: Output[QNum],\n",
" power: Output[QArray[2 * (reg_len - 1)]],\n",
" aux: Output[QBit],\n",
") -> None:\n",
Expand All @@ -553,7 +553,7 @@
" allocate(1, aux)\n",
"\n",
" hadamard_transform(power)\n",
" inplace_prepare_int(1, x)\n",
" x ^= 1\n",
"\n",
" mod_exp_func(\n",
" modulo_num,\n",
Expand Down
2 changes: 1 addition & 1 deletion algorithms/algebraic/shor/shor_modular_exponentiation.qmod
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ qfunc main(output x: qbit[], output power: qbit[6], output aux: qbit) {
allocate(6, power);
allocate(1, aux);
hadamard_transform(power);
inplace_prepare_int(1, x);
x ^= 1;
mod_exp_func(6, 5, x, power, aux);
invert {
qft(power);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@
"def long_time_integrator(\n",
" T: CReal, num_slices: CInt, qbe_full: FullBE # start from time 0\n",
"):\n",
" inplace_prepare_int(num_slices, qbe_full.counter)\n",
" qbe_full.counter ^= num_slices\n",
" repeat(\n",
" num_slices,\n",
" lambda i: long_time_integrator_step(\n",
Expand Down Expand Up @@ -887,7 +887,7 @@
"def long_time_integrator_naive(\n",
" T: CReal, num_slices: CInt, qbe_full: FullBE # start from time 0\n",
"):\n",
" inplace_prepare_int(num_slices, qbe_full.counter)\n",
" qbe_full.counter ^= num_slices\n",
" repeat(\n",
" num_slices,\n",
" lambda i: long_time_integrator_step_naive(\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ qfunc long_time_integrator_step(a: real, b: real, qbe_full: FullBE) {
}

qfunc long_time_integrator(T: real, num_slices: int, qbe_full: FullBE) {
inplace_prepare_int(num_slices, qbe_full.counter);
qbe_full.counter ^= num_slices;
repeat (i: num_slices) {
long_time_integrator_step((i * T) / num_slices, ((i + 1) * T) / num_slices, qbe_full);
}
Expand Down
2 changes: 1 addition & 1 deletion algorithms/dqi/dqi_max_xorsat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@
"@qfunc\n",
"def main(one_hot: Output[QArray]):\n",
" binary = QNum(\"binary\")\n",
" prepare_int(8, binary)\n",
" binary |= 8\n",
" binary_to_unary(binary, one_hot)\n",
"\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion community/QClass_2024/Assignments/HW1_QClass2024.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
"See also [control](https://docs.classiq.io/latest/qmod-reference/language-reference/operators/).\n",
"\n",
"1. Declare a `QNum` output argument using `Output[QNum]` and name it `x`.\n",
"2. Use the `prepare_int` function to initialize it to `9`. Note that you don't need to specify the `QNum` attributes - size, sign, and fraction digits, as they are inferred at the point of initialization.\n",
"2. Use numeric assignment (`|=`) to initialize it to `9`. Note that you don't need to specify the `QNum` attributes - size, sign, and fraction digits, as they are inferred at the point of initialization.\n",
"3. Execute the circuit and observe the results.\n",
"4. Declare another output argument of type `QBit` and perform a `control` such that under the condition that `x` is 9, the qubit is flipped. Execute the circuit and observe the results. Repeat for a different condition."
]
Expand Down
3 changes: 1 addition & 2 deletions community/QClass_2024/Assignments/HW2_QClass2024.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@
"3. Initialize variables `x=2`, `y=7`, `z=1` and computes `res = x * y - z`.\n",
"\n",
"Guidance:\n",
"* Use the operator `|=` to perform out-of-place assignment of arithmetic expression.\n",
"* To initialize the variables, use the function `prepare_int`.\n"
"* Use the operator `|=` to perform out-of-place assignment of an arithmetic expression (including classical numeric values).\n"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
"See also [control](https://docs.classiq.io/latest/qmod-reference/language-reference/operators/).\n",
"\n",
"1. Declare a `QNum` output argument using `Output[QNum]` and name it `x`.\n",
"2. Use the `prepare_int` function to initialize it to `9`. Note that you don't need to specify the `QNum` attributes - size, sign, and fraction digits, as they are inferred at the point of initialization.\n",
"2. Use numeric assignment (`|=`) to initialize it to `9`. Note that you don't need to specify the `QNum` attributes - size, sign, and fraction digits, as they are inferred at the point of initialization.\n",
"3. Execute the circuit and observe the results.\n",
"4. Declare another output argument of type `QBit` and perform a `control` such that under the condition that `x` is 9, the qubit is flipped. Execute the circuit and observe the results. Repeat for a different condition."
]
Expand Down Expand Up @@ -479,7 +479,7 @@
"@qfunc\n",
"def main(res: Output[QNum]) -> None:\n",
" x = QNum(\"x\")\n",
" prepare_int(7, x)\n",
" x |= 7\n",
"\n",
" y = QNum(\"y\")\n",
" prepare_state(probabilities=[1 / 3, 0, 0, 0, 1 / 3, 0, 0, 1 / 3], bound=0.01, out=y)\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@
"\n",
"@qfunc\n",
"def main(a: Output[QNum], b: Output[QNum], c: Output[QNum], res: Output[QNum]) -> None:\n",
" prepare_int(2, a)\n",
" prepare_int(1, b)\n",
" prepare_int(5, c)\n",
" a |= 2\n",
" b |= 1\n",
" c |= 5\n",
"\n",
" res |= (a + b + c & 15) % 8 ^ 3 & a ^ 10 == 4\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
qfunc main(output a: qnum, output b: qnum, output c: qnum, output res: qnum) {
prepare_int(2, a);
prepare_int(1, b);
prepare_int(5, c);
a = 2;
b = 1;
c = 5;
res = ((((((a + b) + c) & 15) % 8) ^ (3 & a)) ^ 10) == 4;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
qfunc main(output a: qnum, output b: qnum, output res: qnum) {
prepare_int(4, a);
prepare_int(5, b);
a = 4;
b = 5;
res = a & b;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"\n",
"@qfunc\n",
"def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
" prepare_int(4, a)\n",
" prepare_int(5, b)\n",
" a |= 4\n",
" b |= 5\n",
" res |= a & b\n",
"\n",
"\n",
Expand Down Expand Up @@ -133,7 +133,7 @@
"source": [
"@qfunc\n",
"def main(a: Output[QNum], res: Output[QNum]) -> None:\n",
" prepare_int(5, a)\n",
" a |= 5\n",
" res |= 3 & a\n",
"\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
qfunc main(output a: qnum, output res: qnum) {
prepare_int(5, a);
a = 5;
res = 3 & a;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"\n",
"@qfunc\n",
"def main(x: Output[QNum], y: Output[QNum]) -> None:\n",
" prepare_int(6, x)\n",
" x |= 6\n",
"\n",
" y |= ~x\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
qfunc main(output x: qnum, output y: qnum) {
prepare_int(6, x);
x = 6;
y = ~x;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
qfunc main(output a: qnum, output b: qnum, output res: qnum) {
allocate_num(5, True, 0, a);
allocate_num(3, False, 0, b);
inplace_prepare_int(4, a);
inplace_prepare_int(5, b);
a ^= 4;
b ^= 5;
res = a | b;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
"def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
" allocate_num(5, True, 0, a)\n",
" allocate_num(3, False, 0, b)\n",
" inplace_prepare_int(4, a)\n",
" inplace_prepare_int(5, b)\n",
" a ^= 4\n",
" b ^= 5\n",
" res |= a | b\n",
"\n",
"\n",
Expand Down Expand Up @@ -135,7 +135,7 @@
"source": [
"@qfunc\n",
"def main(a: Output[QNum], res: Output[QNum]) -> None:\n",
" prepare_int(4, a)\n",
" a |= 4\n",
" res |= 3 | a\n",
"\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
qfunc main(output a: qnum, output res: qnum) {
prepare_int(4, a);
a = 4;
res = 3 | a;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
qfunc main(output a: qnum, output b: qnum, output res: qnum) {
allocate_num(5, True, 0, a);
allocate_num(3, False, 0, b);
inplace_prepare_int(4, a);
inplace_prepare_int(5, b);
a ^= 4;
b ^= 5;
res = a ^ b;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
"def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
" allocate_num(5, True, 0, a)\n",
" allocate_num(3, False, 0, b)\n",
" inplace_prepare_int(4, a)\n",
" inplace_prepare_int(5, b)\n",
" a ^= 4\n",
" b ^= 5\n",
" res |= a ^ b\n",
"\n",
"\n",
Expand Down Expand Up @@ -135,7 +135,7 @@
"source": [
"@qfunc\n",
"def main(a: Output[QNum], res: Output[QNum]) -> None:\n",
" prepare_int(4, a)\n",
" a |= 4\n",
" res |= 3 ^ a\n",
"\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
qfunc main(output a: qnum, output res: qnum) {
prepare_int(4, a);
a = 4;
res = 3 ^ a;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"\n",
"@qfunc\n",
"def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
" prepare_int(4, a)\n",
" a |= 4\n",
" allocate(3, b)\n",
" hadamard_transform(b)\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
qfunc main(output a: qnum, output b: qnum, output res: qnum) {
prepare_int(4, a);
a = 4;
allocate(3, b);
hadamard_transform(b);
res = min(a, b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@qfunc\n",
"def main(power: Output[QNum], x: Output[QNum]) -> None:\n",
" allocate(ceiling(log(N, 2)), x)\n",
" inplace_prepare_int(1, x)\n",
" x ^= 1\n",
"\n",
" # initialize a uniform superposition of powers\n",
" allocate(3, power)\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
qfunc main(output power: qnum, output x: qnum) {
allocate(ceiling(log(5, 2)), x);
inplace_prepare_int(1, x);
x ^= 1;
allocate(3, power);
hadamard_transform(power);
modular_exp(5, 4, x, power);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
" allocate(5, a)\n",
" allocate(5, b)\n",
"\n",
" inplace_prepare_int(4, a)\n",
" inplace_prepare_int(7, b)\n",
" a ^= 4\n",
" b ^= 7\n",
"\n",
" res |= (a + b) % 4\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
qfunc main(output a: qnum, output b: qnum, output res: qnum) {
allocate(5, a);
allocate(5, b);
inplace_prepare_int(4, a);
inplace_prepare_int(7, b);
a ^= 4;
b ^= 7;
res = (a + b) % 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"\n",
"@qfunc\n",
"def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
" prepare_int(4, a)\n",
" prepare_int(5, b)\n",
" a |= 4\n",
" b |= 5\n",
" res |= a * b\n",
"\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
qfunc main(output a: qnum, output b: qnum, output res: qnum) {
prepare_int(4, a);
prepare_int(5, b);
a = 4;
b = 5;
res = a * b;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
qfunc main(output a: qnum, output b: qnum, output res: qnum) {
prepare_int(4, a);
prepare_int(5, b);
a = 4;
b = 5;
res = a - b;
}
Loading
Loading