Skip to content

Commit

Permalink
Updates for 0.39.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Classiq Bot committed Apr 1, 2024
1 parent 69828b1 commit de18a18
Show file tree
Hide file tree
Showing 131 changed files with 5,368 additions and 1,221 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ A part of a QML encoder (see the full algirthm [here](/algorithms/qml/quantum_au
def angle_encoding(
exe_params: QParam[List[float]], qbv: Output[QArray[QBit, "len(exe_params)"]]
) -> None:
allocate(exe_params.len(), qbv)
allocate(exe_params.len, qbv)
repeat(
count=exe_params.len(),
count=exe_params.len,
iteration=lambda index: RY(pi * exe_params[index], qbv[index]),
)
```
Expand Down
52 changes: 26 additions & 26 deletions algorithms/algebraic/shor/doubly_controlled_modular_adder.qmod
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
qfunc phase_lad<value: int>(phi_b: qbit[]) {
repeat<len(phi_b), lambda<index>() {
PHASE<qft_const_adder_phase(index, value, len(phi_b))>(phi_b[0 + (index):(0 + (index)) + 1]);
}>();
repeat (index: len(phi_b)) {
PHASE<qft_const_adder_phase(index, value, len(phi_b))>(phi_b[index]);
}
}

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

qfunc qft_ns(qbv: qbit[]) {
repeat<len(qbv), lambda<index>() {
my_qft_step(qbv[0 + (index):0 + (len(qbv))]);
}>();
repeat (index: len(qbv)) {
my_qft_step(qbv[index:len(qbv)]);
}
}

qfunc ctrl_x<ref: int>(ctrl: qnum, aux: qbit) {
Expand All @@ -25,33 +25,33 @@ qfunc ctrl_x<ref: int>(ctrl: qnum, aux: qbit) {

qfunc check_msb<ref: int>(x: qbit[], aux: qbit) {
within {
invert<lambda() {
invert {
qft_ns(x);
}>();
}
} apply {
ctrl_x<ref>(x[0:1], aux);
ctrl_x<ref>(x[0], aux);
}
}

qfunc ccmod_add<N: int, a: int>(phi_b: qbit[], c1: qbit, c2: qbit, aux: qbit) {
ctrl: qbit[];
{c1, c2} -> ctrl;
control<lambda() {
control (ctrl) {
phase_lad<a>(phi_b);
}>(ctrl);
invert<lambda() {
}
invert {
phase_lad<N>(phi_b);
}>();
}
check_msb<1>(phi_b, aux);
control<lambda() {
control (aux) {
phase_lad<N>(phi_b);
}>(aux);
}
within {
invert<lambda() {
control<lambda() {
invert {
control (ctrl) {
phase_lad<a>(phi_b);
}>(ctrl);
}>();
}
}
} apply {
check_msb<0>(phi_b, aux);
}
Expand All @@ -63,11 +63,11 @@ qfunc main(output b: qnum, output ctrl: qbit[], output aux: qbit) {
allocate<2>(ctrl);
allocate<1>(aux);
inplace_prepare_int<8>(b);
X(ctrl[0:1]);
X(ctrl[1:2]);
X(ctrl[0]);
X(ctrl[1]);
within {
qft(b);
} apply {
ccmod_add<15, 9>(b, ctrl[0:1], ctrl[1:2], aux);
ccmod_add<15, 9>(b, ctrl[0], ctrl[1], aux);
}
}
18 changes: 9 additions & 9 deletions algorithms/algebraic/shor/shor.qmod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
qfunc modular_exponentiation<exponent: int>(target: qbit[4]) {
power<2 ** exponent, lambda() {
power (2 ** exponent) {
unitary<[
[
0.0,
Expand Down Expand Up @@ -290,22 +290,22 @@ qfunc modular_exponentiation<exponent: int>(target: qbit[4]) {
0.0
]
]>(target);
}>();
}
}

qfunc period_finding(output qv_counting: qbit[8], output qv_auxilliary: qbit[4]) {
allocate<8>(qv_counting);
hadamard_transform(qv_counting);
allocate<4>(qv_auxilliary);
X(qv_auxilliary[0:1]);
repeat<4, lambda<index>() {
control<lambda() {
X(qv_auxilliary[0]);
repeat (index: 4) {
control (qv_counting[index]) {
modular_exponentiation<index>(qv_auxilliary);
}>(qv_counting[0 + (index):(0 + (index)) + 1]);
}>();
invert<lambda() {
}
}
invert {
qft(qv_counting);
}>();
}
}

qfunc main(output qv_counting: qbit[8]) {
Expand Down
18 changes: 9 additions & 9 deletions algorithms/algebraic/shor/shor_modular_exponentiation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
"def my_qft_step(qbv: QArray[QBit]) -> None:\n",
" H(qbv[0])\n",
" repeat(\n",
" count=qbv.len() - 1,\n",
" count=qbv.len - 1,\n",
" iteration=lambda index: CPHASE(pi / 2 ** (index + 1), qbv[0], qbv[index + 1]),\n",
" )\n",
"\n",
Expand All @@ -178,8 +178,8 @@
"@qfunc\n",
"def qft_ns(qbv: QArray[QBit]) -> None:\n",
" repeat(\n",
" count=qbv.len(),\n",
" iteration=lambda index: my_qft_step(qbv[index : qbv.len()]),\n",
" count=qbv.len,\n",
" iteration=lambda index: my_qft_step(qbv[index : qbv.len]),\n",
" )"
]
},
Expand Down Expand Up @@ -208,9 +208,9 @@
" phi_b: QArray[QBit],\n",
") -> None:\n",
" repeat(\n",
" count=phi_b.len(),\n",
" count=phi_b.len,\n",
" iteration=lambda index: PHASE(\n",
" theta=qft_const_adder_phase(index, value, phi_b.len()), target=phi_b[index]\n",
" theta=qft_const_adder_phase(index, value, phi_b.len), target=phi_b[index]\n",
" ),\n",
" )\n",
"\n",
Expand Down Expand Up @@ -428,7 +428,7 @@
" within_apply(\n",
" lambda: qft(b),\n",
" lambda: repeat(\n",
" count=x.len(),\n",
" count=x.len,\n",
" iteration=lambda index: ccmod_add(\n",
" N, (a * (2**index)) % N, b, x[index], ctrl, aux\n",
" ),\n",
Expand Down Expand Up @@ -482,7 +482,7 @@
"@qfunc\n",
"def multi_swap(x: QArray[QBit], y: QArray[QBit]) -> None:\n",
" repeat(\n",
" count=min(x.len(), y.len()),\n",
" count=min(x.len, y.len),\n",
" iteration=lambda index: SWAP(x[index], y[index]),\n",
" )\n",
"\n",
Expand All @@ -496,7 +496,7 @@
" aux: QBit,\n",
") -> None:\n",
" b = QArray(\"b\")\n",
" allocate(x.len() + 1, b)\n",
" allocate(x.len + 1, b)\n",
"\n",
" cmod_mult(\n",
" N,\n",
Expand Down Expand Up @@ -547,7 +547,7 @@
" aux: QBit,\n",
") -> None:\n",
" repeat(\n",
" count=m.len(),\n",
" count=m.len,\n",
" iteration=lambda index: cmod_mult_pair(\n",
" N, (a ** (2**index)) % N, x, m[index], aux\n",
" ),\n",
Expand Down
76 changes: 38 additions & 38 deletions algorithms/algebraic/shor/shor_modular_exponentiation.qmod
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
qfunc phase_lad<value: int>(phi_b: qbit[]) {
repeat<len(phi_b), lambda<index>() {
PHASE<qft_const_adder_phase(index, value, len(phi_b))>(phi_b[0 + (index):(0 + (index)) + 1]);
}>();
repeat (index: len(phi_b)) {
PHASE<qft_const_adder_phase(index, value, len(phi_b))>(phi_b[index]);
}
}

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

qfunc qft_ns(qbv: qbit[]) {
repeat<len(qbv), lambda<index>() {
my_qft_step(qbv[0 + (index):0 + (len(qbv))]);
}>();
repeat (index: len(qbv)) {
my_qft_step(qbv[index:len(qbv)]);
}
}

qfunc ctrl_x<ref: int>(ctrl: qnum, aux: qbit) {
Expand All @@ -25,33 +25,33 @@ qfunc ctrl_x<ref: int>(ctrl: qnum, aux: qbit) {

qfunc check_msb<ref: int>(x: qbit[], aux: qbit) {
within {
invert<lambda() {
invert {
qft_ns(x);
}>();
}
} apply {
ctrl_x<ref>(x[0:1], aux);
ctrl_x<ref>(x[0], aux);
}
}

qfunc ccmod_add<N: int, a: int>(phi_b: qbit[], c1: qbit, c2: qbit, aux: qbit) {
ctrl: qbit[];
{c1, c2} -> ctrl;
control<lambda() {
control (ctrl) {
phase_lad<a>(phi_b);
}>(ctrl);
invert<lambda() {
}
invert {
phase_lad<N>(phi_b);
}>();
}
check_msb<1>(phi_b, aux);
control<lambda() {
control (aux) {
phase_lad<N>(phi_b);
}>(aux);
}
within {
invert<lambda() {
control<lambda() {
invert {
control (ctrl) {
phase_lad<a>(phi_b);
}>(ctrl);
}>();
}
}
} apply {
check_msb<0>(phi_b, aux);
}
Expand All @@ -62,35 +62,35 @@ qfunc cmod_mult<N: int, a: int>(b: qbit[], x: qbit[], ctrl: qbit, aux: qbit) {
within {
qft(b);
} apply {
repeat<len(x), lambda<index>() {
ccmod_add<N, (a * (2 ** index)) % N>(b, x[0 + (index):(0 + (index)) + 1], ctrl, aux);
}>();
repeat (index: len(x)) {
ccmod_add<N, (a * (2 ** index)) % N>(b, x[index], ctrl, aux);
}
}
}

qfunc multi_swap(x: qbit[], y: qbit[]) {
repeat<min(len(x), len(y)), lambda<index>() {
SWAP(x[0 + (index):(0 + (index)) + 1], y[0 + (index):(0 + (index)) + 1]);
}>();
repeat (index: min(len(x), len(y))) {
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);
cmod_mult<N, a>(b, x, ctrl, aux);
control<lambda() {
control (ctrl) {
multi_swap(x, b);
}>(ctrl);
invert<lambda() {
}
invert {
cmod_mult<N, mod_inverse(a, N)>(b, x, ctrl, aux);
}>();
}
free(b);
}

qfunc mod_exp_func<N: int, a: int>(x: qbit[], m: qbit[], aux: qbit) {
repeat<len(m), lambda<index>() {
cmod_mult_pair<N, (a ** (2 ** index)) % N>(x, m[0 + (index):(0 + (index)) + 1], aux);
}>();
repeat (index: len(m)) {
cmod_mult_pair<N, (a ** (2 ** index)) % N>(x, m[index], aux);
}
}

qfunc main(output x: qbit[], output power: qbit[], output aux: qbit) {
Expand All @@ -100,7 +100,7 @@ qfunc main(output x: qbit[], output power: qbit[], output aux: qbit) {
hadamard_transform(power);
inplace_prepare_int<1>(x);
mod_exp_func<6, 5>(x, power, aux);
invert<lambda() {
invert {
qft(power);
}>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
"source": [
"@qfunc\n",
"def my_grover_operator(state: QArray[QBit]):\n",
" io = QArray[QBit](\"io\", length=state.len() - 1)\n",
" io = QArray[QBit](\"io\", length=state.len - 1)\n",
" ind = QBit(\"ind\")\n",
" bind(state, [ind, io])\n",
" good_state_oracle(ind=ind)\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ qfunc my_grover_operator(state: qbit[]) {
state -> {ind, io};
good_state_oracle(ind);
within {
invert<lambda() {
invert {
state_loading(io, ind);
}>();
}
} apply {
zero_oracle(io, ind);
}
Expand Down
Loading

0 comments on commit de18a18

Please sign in to comment.