Skip to content

Commit

Permalink
Updates for 0.40.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Classiq Bot committed Apr 15, 2024
1 parent 8a507fe commit a66c416
Show file tree
Hide file tree
Showing 341 changed files with 6,369 additions and 3,164 deletions.
42 changes: 0 additions & 42 deletions CONTRIBUTING.md

This file was deleted.

12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ def main(res: Output[QBit]):
X(res)
```

The 1st line states that the function will be a quantum one. [Further documentation](https://docs.classiq.io/latest/user-guide/platform/qmod/python/functions/).
The 1st line states that the function will be a quantum one. [Further documentation](https://docs.classiq.io/latest/user-guide/platform/qmod/language-reference/functions/).

The 2nd line defines the type of the output. [Further examples on types](https://docs.classiq.io/latest/user-guide/platform/qmod/python/types/)
The 2nd line defines the type of the output. [Further examples on types](https://docs.classiq.io/latest/user-guide/platform/qmod/language-reference/quantum-types/)

The 3rd line allocates several qubits (in this example, only 1) in this quantum variable. [Further details on allocate](https://docs.classiq.io/latest/user-guide/platform/qmod/python/quantum-expressions/)
The 3rd line allocates several qubits (in this example, only 1) in this quantum variable. [Further details on allocate](https://docs.classiq.io/latest/user-guide/platform/qmod/language-reference/quantum-variables/)

The 4th line applies an `X` operator on the quantum variable. [Further details on quantum operators](https://docs.classiq.io/latest/user-guide/platform/qmod/python/operators/)
The 4th line applies an `X` operator on the quantum variable. [Further details on quantum operators](https://docs.classiq.io/latest/user-guide/platform/qmod/language-reference/operators/)

### More Examples

Expand Down Expand Up @@ -127,9 +127,7 @@ A part of a QML encoder (see the full algirthm [here](/algorithms/qml/quantum_au

```python
@qfunc
def angle_encoding(
exe_params: QParam[List[float]], qbv: Output[QArray[QBit, "len(exe_params)"]]
) -> None:
def angle_encoding(exe_params: CArray[CReal], qbv: Output[QArray[QBit]]) -> None:
allocate(exe_params.len, qbv)
repeat(
count=exe_params.len,
Expand Down
399 changes: 399 additions & 0 deletions algorithms/algebraic/discrete_log/discrete_log.ipynb

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions algorithms/algebraic/discrete_log/discrete_log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"friendly_name": "Discrete Logarithm",
"description": "Solving Discrete Logarithm Problem using Shor's Algorithm",
"qmod_type": ["algorithms"],
"level": ["advanced"]
}
24 changes: 24 additions & 0 deletions algorithms/algebraic/discrete_log/discrete_log.qmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
qfunc discrete_log_oracle<g: int, x: int, N: int, order: int>(x1: qbit[], x2: qbit[], output func_res: qbit[]) {
allocate<ceiling(log(N, 2))>(func_res);
inplace_prepare_int<1>(func_res);
modular_exp<N, x>(func_res, x1);
modular_exp<N, g>(func_res, x2);
}

qfunc discrete_log<g: int, x: int, N: int, order: int>(output x1: qbit[], output x2: qbit[], output func_res: qbit[]) {
allocate<ceiling(log(order, 2))>(x1);
allocate<ceiling(log(order, 2))>(x2);
hadamard_transform(x1);
hadamard_transform(x2);
discrete_log_oracle<g, x, N, order>(x1, x2, func_res);
invert {
qft(x1);
}
invert {
qft(x2);
}
}

qfunc main(output x1: qnum, output x2: qnum, output func_res: qnum) {
discrete_log<3, 2, 5, 4>(x1, x2, func_res);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"constraints": {
"max_width": 13
}
}
1 change: 0 additions & 1 deletion algorithms/algebraic/hidden_shift/hidden_shift.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
" QBit,\n",
" QCallable,\n",
" QNum,\n",
" QParam,\n",
" allocate,\n",
" bind,\n",
" create_model,\n",
Expand Down
12 changes: 6 additions & 6 deletions algorithms/algebraic/shor/doubly_controlled_modular_adder.qmod
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
qfunc phase_lad<value: int>(phi_b: qbit[]) {
repeat (index: len(phi_b)) {
PHASE<qft_const_adder_phase(index, value, len(phi_b))>(phi_b[index]);
repeat (index: phi_b.len) {
PHASE<qft_const_adder_phase(index, value, phi_b.len)>(phi_b[index]);
}
}

qfunc my_qft_step(qbv: qbit[]) {
H(qbv[0]);
repeat (index: len(qbv) - 1) {
repeat (index: qbv.len - 1) {
CPHASE<pi / (2 ** (index + 1))>(qbv[0], qbv[(index) + 1]);
}
}

qfunc qft_ns(qbv: qbit[]) {
repeat (index: len(qbv)) {
my_qft_step(qbv[index:len(qbv)]);
repeat (index: qbv.len) {
my_qft_step(qbv[index:qbv.len]);
}
}

qfunc ctrl_x<ref: int>(ctrl: qnum, aux: qbit) {
quantum_if (ctrl == ref) {
control (ctrl == ref) {
X(aux);
}
}
Expand Down
8 changes: 4 additions & 4 deletions algorithms/algebraic/shor/shor.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@
"outputs": [],
"source": [
"from classiq import (\n",
" CInt,\n",
" Output,\n",
" QArray,\n",
" QBit,\n",
" QParam,\n",
" X,\n",
" allocate,\n",
" control,\n",
Expand Down Expand Up @@ -140,7 +140,7 @@
"\n",
"@qfunc\n",
"def modular_exponentiation(\n",
" exponent: QParam[int], target: QArray[QBit, num_auxilliary_qubits]\n",
" exponent: CInt, target: QArray[QBit, num_auxilliary_qubits]\n",
") -> None:\n",
" power(2**exponent, lambda: unitary(elements=MODULAR_MUL_UNITARY, target=target))"
]
Expand Down Expand Up @@ -202,8 +202,8 @@
" repeat(\n",
" count=num_auxilliary_qubits,\n",
" iteration=lambda index: control(\n",
" operand=lambda: modular_exponentiation(index, qv_auxilliary),\n",
" ctrl=qv_counting[index],\n",
" operand=lambda: modular_exponentiation(index, qv_auxilliary),\n",
" ),\n",
" ) # ! not working with qv[a:]\n",
"\n",
Expand Down Expand Up @@ -470,7 +470,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.4"
}
},
"nbformat": 4,
Expand Down
40 changes: 20 additions & 20 deletions algorithms/algebraic/shor/shor_modular_exponentiation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@
" CPHASE,\n",
" CX,\n",
" PHASE,\n",
" CInt,\n",
" H,\n",
" Output,\n",
" Preferences,\n",
" QArray,\n",
" QBit,\n",
" QParam,\n",
" X,\n",
" allocate,\n",
" apply_to_all,\n",
Expand Down Expand Up @@ -188,7 +188,7 @@
"id": "28304e01-3f61-47a9-bf39-42ce35016ee1",
"metadata": {},
"source": [
"The function `ccmod_add` implements the modular adder which adds the (classical) number $a$ to the b-register modulo $N$ in the QFT space. the function recieves $a$ and $N$ as `QParams` of integers. The un-controlled, controlled and doubly controlled adders in the QFT space are implemented by the function `phase_lad`. The functions which check the msb of the b-register and conditionally flip the auxiliary qubit is `check_msb`. Notice that at this stage, as we don't use SWAP after the QFT, the msb is the first qubit."
"The function `ccmod_add` implements the modular adder which adds the (classical) number $a$ to the b-register modulo $N$ in the QFT space. The function receives $a$ and $N$ as `CInt`s: classical integers parameters. The un-controlled, controlled and doubly controlled adders in the QFT space are implemented by the function `phase_lad`. The functions which check the msb of the b-register and conditionally flip the auxiliary qubit is `check_msb`. Notice that at this stage, as we don't use SWAP after the QFT, the msb is the first qubit."
]
},
{
Expand All @@ -198,13 +198,13 @@
"metadata": {},
"outputs": [],
"source": [
"from classiq.qmod import QNum, bind, quantum_if, within_apply\n",
"from classiq.qmod import QNum, bind, control, within_apply\n",
"from classiq.qmod.builtins.classical_functions import qft_const_adder_phase\n",
"\n",
"\n",
"@qfunc\n",
"def phase_lad(\n",
" value: QParam[int],\n",
" value: CInt,\n",
" phi_b: QArray[QBit],\n",
") -> None:\n",
" repeat(\n",
Expand All @@ -216,32 +216,32 @@
"\n",
"\n",
"@qfunc\n",
"def ctrl_x(ref: QParam[int], ctrl: QNum, aux: QBit) -> None:\n",
" quantum_if(ctrl == ref, lambda: X(aux))\n",
"def ctrl_x(ref: CInt, ctrl: QNum, aux: QBit) -> None:\n",
" control(ctrl == ref, lambda: X(aux))\n",
"\n",
"\n",
"@qfunc\n",
"def check_msb(ref: QParam[int], x: QArray[QBit], aux: QBit) -> None:\n",
"def check_msb(ref: CInt, x: QArray[QBit], aux: QBit) -> None:\n",
" within_apply(lambda: invert(lambda: qft_ns(x)), lambda: ctrl_x(ref, x[0], aux))\n",
"\n",
"\n",
"@qfunc\n",
"def ccmod_add(\n",
" N: QParam[int],\n",
" a: QParam[int],\n",
" N: CInt,\n",
" a: CInt,\n",
" phi_b: QArray[QBit], # b in fourier basis\n",
" c1: QBit,\n",
" c2: QBit,\n",
" aux: QBit,\n",
") -> None:\n",
" ctrl = QArray(\"ctrl\")\n",
" bind([c1, c2], ctrl)\n",
" control(lambda: phase_lad(a, phi_b), ctrl)\n",
" control(ctrl, lambda: phase_lad(a, phi_b))\n",
" invert(lambda: phase_lad(N, phi_b))\n",
" check_msb(1, phi_b, aux)\n",
" control(lambda: phase_lad(N, phi_b), aux)\n",
" control(aux, lambda: phase_lad(N, phi_b))\n",
" within_apply(\n",
" lambda: invert(lambda: control(lambda: phase_lad(a, phi_b), ctrl)),\n",
" lambda: invert(lambda: control(ctrl, lambda: phase_lad(a, phi_b))),\n",
" lambda: check_msb(0, phi_b, aux),\n",
" )\n",
" bind(ctrl, [c1, c2])"
Expand Down Expand Up @@ -345,7 +345,7 @@
"id": "20ac31c9-e566-4149-a398-3a843c22538f",
"metadata": {},
"source": [
"We now can execute the synthesized circuit on a simulator (we use the default aer-simulator) and check the outcome. "
"We now can execute the synthesized circuit on a simulator (we use the default simulator) and check the outcome."
]
},
{
Expand Down Expand Up @@ -418,8 +418,8 @@
"source": [
"@qfunc\n",
"def cmod_mult(\n",
" N: QParam[int],\n",
" a: QParam[int],\n",
" N: CInt,\n",
" a: CInt,\n",
" b: QArray[QBit],\n",
" x: QArray[QBit],\n",
" ctrl: QBit,\n",
Expand Down Expand Up @@ -489,8 +489,8 @@
"\n",
"@qfunc\n",
"def cmod_mult_pair(\n",
" N: QParam[int],\n",
" a: QParam[int],\n",
" N: CInt,\n",
" a: CInt,\n",
" x: QArray[QBit],\n",
" ctrl: QBit,\n",
" aux: QBit,\n",
Expand All @@ -506,7 +506,7 @@
" ctrl,\n",
" aux,\n",
" )\n",
" control(lambda: multi_swap(x, b), ctrl)\n",
" control(ctrl, lambda: multi_swap(x, b))\n",
" pass\n",
" invert(\n",
" lambda: cmod_mult(\n",
Expand Down Expand Up @@ -540,8 +540,8 @@
"source": [
"@qfunc\n",
"def mod_exp_func(\n",
" N: QParam[int],\n",
" a: QParam[int],\n",
" N: CInt,\n",
" a: CInt,\n",
" x: QArray[QBit],\n",
" m: QArray[QBit],\n",
" aux: QBit,\n",
Expand Down
20 changes: 10 additions & 10 deletions algorithms/algebraic/shor/shor_modular_exponentiation.qmod
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
qfunc phase_lad<value: int>(phi_b: qbit[]) {
repeat (index: len(phi_b)) {
PHASE<qft_const_adder_phase(index, value, len(phi_b))>(phi_b[index]);
repeat (index: phi_b.len) {
PHASE<qft_const_adder_phase(index, value, phi_b.len)>(phi_b[index]);
}
}

qfunc my_qft_step(qbv: qbit[]) {
H(qbv[0]);
repeat (index: len(qbv) - 1) {
repeat (index: qbv.len - 1) {
CPHASE<pi / (2 ** (index + 1))>(qbv[0], qbv[(index) + 1]);
}
}

qfunc qft_ns(qbv: qbit[]) {
repeat (index: len(qbv)) {
my_qft_step(qbv[index:len(qbv)]);
repeat (index: qbv.len) {
my_qft_step(qbv[index:qbv.len]);
}
}

qfunc ctrl_x<ref: int>(ctrl: qnum, aux: qbit) {
quantum_if (ctrl == ref) {
control (ctrl == ref) {
X(aux);
}
}
Expand Down Expand Up @@ -62,21 +62,21 @@ qfunc cmod_mult<N: int, a: int>(b: qbit[], x: qbit[], ctrl: qbit, aux: qbit) {
within {
qft(b);
} apply {
repeat (index: len(x)) {
repeat (index: x.len) {
ccmod_add<N, (a * (2 ** index)) % N>(b, x[index], ctrl, aux);
}
}
}

qfunc multi_swap(x: qbit[], y: qbit[]) {
repeat (index: min(len(x), len(y))) {
repeat (index: min(x.len, y.len)) {
SWAP(x[index], y[index]);
}
}

qfunc cmod_mult_pair<N: int, a: int>(x: qbit[], ctrl: qbit, aux: qbit) {
b: qbit[];
allocate<len(x) + 1>(b);
allocate<x.len + 1>(b);
cmod_mult<N, a>(b, x, ctrl, aux);
control (ctrl) {
multi_swap(x, b);
Expand All @@ -88,7 +88,7 @@ qfunc cmod_mult_pair<N: int, a: int>(x: qbit[], ctrl: qbit, aux: qbit) {
}

qfunc mod_exp_func<N: int, a: int>(x: qbit[], m: qbit[], aux: qbit) {
repeat (index: len(m)) {
repeat (index: m.len) {
cmod_mult_pair<N, (a ** (2 ** index)) % N>(x, m[index], aux);
}
}
Expand Down
Loading

0 comments on commit a66c416

Please sign in to comment.