Skip to content

Commit 8d9ca9a

Browse files
committed
Beautify quantum walks qmod's
1 parent fe5b784 commit 8d9ca9a

File tree

3 files changed

+79
-25
lines changed

3 files changed

+79
-25
lines changed

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 a t number of 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+
// Choose 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,12 @@
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 a t number of steps
4+
5+
6+
N: int = 7;
7+
circle_size: int = 2 ** N;
8+
9+
// A generic function for a quantum walk
110
qfunc discrete_quantum_walk(time: int, coin_flip_qfunc: qfunc (qnum), walks_qfuncs: qfunc[] (), coin_state: qnum) {
211
power (time) {
312
coin_flip_qfunc(coin_state);
@@ -10,21 +19,33 @@ qfunc discrete_quantum_walk(time: int, coin_flip_qfunc: qfunc (qnum), walks_qfun
1019
}
1120

1221
qperm quantum_step_clockwise(x: qnum) {
13-
x += 1;
22+
x += 1;
1423
}
1524

16-
qfunc main(t: int, output x: qnum<floor(log(128, 2)), SIGNED, 0>, output coin: qbit) {
25+
qfunc main(t: int, output x: qnum<floor(log(circle_size, 2)), SIGNED, 0>, output coin: qbit) {
1726
allocate(x);
1827
allocate(coin);
28+
// Apply H and then S for a balanced initial condition for the coin
1929
H(coin);
2030
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);
31+
32+
discrete_quantum_walk(
33+
t,
34+
// Flip a coin
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: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// Discrete quantum walk on a hypercube
2+
3+
// Perform a discrete quantum walk on a 4-dimensional hypercube, for a t number of steps.
4+
// Two nodes in the hypercube are connected to each other if their Hamming distance is 1.
5+
// Thus, a step along a hypercube is given by moving "1 Hamming distance away".
6+
// For a d-dimensional hypercube, at each node there are d possible directions to move.
7+
// Each of them is given by applying a bit flip on one of the bits.
8+
// This is obtained by applying an X gate on one of the d qubits.
9+
10+
111
qfunc discrete_quantum_walk(time: int, coin_flip_qfunc: qfunc (qnum), walks_qfuncs: qfunc[] (), coin_state: qnum) {
212
power (time) {
313
coin_flip_qfunc(coin_state);
@@ -17,17 +27,29 @@ qfunc main(t: int, output x: qbit[4], output coin: qbit[]) {
1727
allocate(x);
1828
allocate(2, coin);
1929
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);
30+
discrete_quantum_walk(
31+
t,
32+
lambda(coin) {
33+
// For the coin operator, choosing the "Grover diffuser" function refers to a symmetric quantum walk.
34+
// The Grover diffuser operator is a reflection around a given state |psi>.
35+
grover_diffuser(lambda(coin) {
36+
prepare_uniform_trimmed_state(4, coin);
37+
}, coin);
38+
},
39+
[
40+
lambda() {
41+
moving_one_hamming_dist(0, x);
42+
},
43+
lambda() {
44+
moving_one_hamming_dist(1, x);
45+
},
46+
lambda() {
47+
moving_one_hamming_dist(2, x);
48+
},
49+
lambda() {
50+
moving_one_hamming_dist(3, x);
51+
}
52+
],
53+
coin
54+
);
3355
}

0 commit comments

Comments
 (0)