-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrw.py
40 lines (31 loc) · 1.02 KB
/
qrw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Example of a (Hadamard) quantum random walk
import math
import random
import sys
import time
from pyqrack import QrackSimulator
def main():
lattice_qb_count = 8
if len(sys.argv) > 1:
lattice_qb_count = int(sys.argv[1])
lattice_qubits = list(range(lattice_qb_count))
coin_qubit = lattice_qb_count
sign_qubit = lattice_qb_count - 1
sign_power = 1 << sign_qubit
step_count = sign_power - 1
if len(sys.argv) > 2:
step_count = int(sys.argv[2])
sim = QrackSimulator(lattice_qb_count + 1)
sim.x(sign_qubit)
for _ in range(step_count):
sim.h(coin_qubit)
sim.mcadd(1, [coin_qubit], lattice_qubits)
sim.x(coin_qubit)
sim.mcsub(1, [coin_qubit], lattice_qubits)
sim.x(coin_qubit)
exp_pos = sim.permutation_expectation(lattice_qubits) - sign_power
print("Expected position:", exp_pos)
obs_pos = (sim.m_all() & ~(1 << coin_qubit)) - sign_power
print("Observed position:", obs_pos)
if __name__ == '__main__':
sys.exit(main())