Skip to content

Commit f961f6b

Browse files
committed
Beautify quantum walks qmod's
1 parent 9b50bb9 commit f961f6b

File tree

4 files changed

+82
-28
lines changed

4 files changed

+82
-28
lines changed

tutorials/advanced_tutorials/discrete_quantum_walk/discrete_quantum_walk.ipynb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@
267267
"qmod_1 = create_model(\n",
268268
" main,\n",
269269
" execution_preferences=ExecutionPreferences(num_shots=NUM_SAMPLES),\n",
270-
" out_file=\"quantum_walk_circle\",\n",
271270
")\n",
272271
"\n",
273272
"qprog_1 = synthesize(qmod_1)\n",
@@ -461,7 +460,6 @@
461460
"qmod_2 = create_model(\n",
462461
" main,\n",
463462
" execution_preferences=ExecutionPreferences(num_shots=NUM_SHOTS),\n",
464-
" out_file=\"quantum_walk_circle_balanced_coin\",\n",
465463
")"
466464
]
467465
},
@@ -632,7 +630,6 @@
632630
" main,\n",
633631
" execution_preferences=ExecutionPreferences(num_shots=NUM_SHOTS),\n",
634632
" constraints=Constraints(optimization_parameter=\"width\"),\n",
635-
" out_file=\"quantum_walk_hypercube\",\n",
636633
")"
637634
]
638635
},

tutorials/advanced_tutorials/discrete_quantum_walk/quantum_walk_circle.qmod

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// Discrete quantum walk on a circle
2+
3+
// Perform a discrete quantum walk on a circle (regular 2**N polygon), for t steps
4+
5+
N: int = 7;
6+
circle_size: int = 2 ** N;
7+
8+
9+
// the coin is represented by a qubit and a "flip" is defined by some unitary operation on it.
10+
// Choosing the Hadamard gate, which sends the |0> state into an equal superposition of |0> and |1>.
111
qfunc quantum_coin_flip(coin: qbit) {
212
H(coin);
313
}
@@ -12,14 +22,15 @@ qfunc discrete_quantum_walk_circle(time: int, coin: qbit, x: qbit[]) {
1222
control (coin == 0) {
1323
quantum_step_clockwise(x);
1424
} else {
25+
// Step counter clock wise (equivalent to x += -1)
1526
invert {
1627
quantum_step_clockwise(x);
1728
}
1829
}
1930
}
2031
}
2132

22-
qfunc main(t: int, output x: qnum<floor(log(128, 2)), SIGNED, 0>, output coin: qbit) {
33+
qfunc main(t: int,output x: qnum<floor(log(circle_size, 2)), SIGNED, 0>, output coin: qbit) {
2334
allocate(x);
2435
allocate(coin);
2536
discrete_quantum_walk_circle(t, coin, x);
Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// Discrete quantum walk on a circle with a balanced coin
2+
3+
// Perform a discrete quantum walk on a circle (regular 2**N polygon), with a balanced coin, for t steps
4+
5+
N: int = 7;
6+
circle_size: int = 2 ** N;
7+
8+
// A generic function for a quantum walk
19
qfunc discrete_quantum_walk(time: int, coin_flip_qfunc: qfunc (qnum), walks_qfuncs: qfunc[] (), coin_state: qnum) {
210
power (time) {
311
coin_flip_qfunc(coin_state);
@@ -10,21 +18,34 @@ qfunc discrete_quantum_walk(time: int, coin_flip_qfunc: qfunc (qnum), walks_qfun
1018
}
1119

1220
qperm quantum_step_clockwise(x: qnum) {
13-
x += 1;
21+
x += 1;
1422
}
1523

16-
qfunc main(t: int, output x: qnum<floor(log(128, 2)), SIGNED, 0>, output coin: qbit) {
24+
qfunc main(t: int, output x: qnum<floor(log(circle_size, 2)), SIGNED, 0>, output coin: qbit) {
1725
allocate(x);
1826
allocate(coin);
27+
// Apply H and then S for a balanced initial condition for the coin
1928
H(coin);
2029
S(coin);
21-
discrete_quantum_walk(t, lambda(coin) {
22-
H(coin);
23-
}, [lambda() {
24-
quantum_step_clockwise(x);
25-
}, lambda() {
26-
invert {
27-
quantum_step_clockwise(x);
28-
}
29-
}], coin);
30+
31+
discrete_quantum_walk(
32+
t,
33+
// the coin is represented by a qubit and a "flip" is defined by some unitary operation on it.
34+
// Choosing the Hadamard gate, which sends the |0> state into an equal superposition of |0> and |1>.
35+
lambda(coin) {
36+
H(coin);
37+
},
38+
// Step clockwise or counter clockwise according the the coin's state
39+
[
40+
lambda() {
41+
quantum_step_clockwise(x);
42+
},
43+
lambda() {
44+
invert {
45+
quantum_step_clockwise(x);
46+
}
47+
}
48+
],
49+
coin
50+
);
3051
}
Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
// Discrete quantum walk on a hypercube
2+
3+
// Perform a discrete quantum walk on a 4-dimensional hypercube, for t steps.
4+
// the coin is represented by a qubit and a "flip" is defined by some unitary operation on it.
5+
6+
// Two nodes in the hypercube are connected to each other if their Hamming distance is 1.
7+
// Thus, a step along a hypercube is given by moving "1 Hamming distance away".
8+
// For a d-dimensional hypercube, at each node there are d possible directions to move.
9+
// Each of them is given by applying a bit flip on one of the bits.
10+
// This is obtained by applying an X gate on one of the d qubits.
11+
12+
113
qfunc discrete_quantum_walk(time: int, coin_flip_qfunc: qfunc (qnum), walks_qfuncs: qfunc[] (), coin_state: qnum) {
214
power (time) {
315
coin_flip_qfunc(coin_state);
@@ -17,17 +29,30 @@ qfunc main(t: int, output x: qbit[4], output coin: qbit[]) {
1729
allocate(x);
1830
allocate(2, coin);
1931
prepare_uniform_trimmed_state(4, coin);
20-
discrete_quantum_walk(t, lambda(coin) {
21-
grover_diffuser(lambda(coin) {
22-
prepare_uniform_trimmed_state(4, coin);
23-
}, coin);
24-
}, [lambda() {
25-
moving_one_hamming_dist(0, x);
26-
}, lambda() {
27-
moving_one_hamming_dist(1, x);
28-
}, lambda() {
29-
moving_one_hamming_dist(2, x);
30-
}, lambda() {
31-
moving_one_hamming_dist(3, x);
32-
}], coin);
32+
discrete_quantum_walk(
33+
t,
34+
lambda(coin) {
35+
// the coin is represented by a qubit and a "flip" is defined by some unitary operation on it.
36+
// Choosing the "Grover diffuser" function refers to a symmetric quantum walk.
37+
// The Grover diffuser operator is a reflection around a given state |psi>.
38+
grover_diffuser(lambda(coin) {
39+
prepare_uniform_trimmed_state(4, coin);
40+
}, coin);
41+
},
42+
[
43+
lambda() {
44+
moving_one_hamming_dist(0, x);
45+
},
46+
lambda() {
47+
moving_one_hamming_dist(1, x);
48+
},
49+
lambda() {
50+
moving_one_hamming_dist(2, x);
51+
},
52+
lambda() {
53+
moving_one_hamming_dist(3, x);
54+
}
55+
],
56+
coin
57+
);
3358
}

0 commit comments

Comments
 (0)