QNLP  v1.0
QNLP_Python_MPI.py
Go to the documentation of this file.
1 from PyQNLPSimulator import PyQNLPSimulator as p
2 import QNLP as q
3 import numpy as np
4 from itertools import product
5 from tqdm import tqdm
6 
7 
8 # Import MPI4PY and set up MPI environment
9 
10 from mpi4py import MPI
11 #assert MPI.COMM_WORLD.Get_size() > 1
12 rank = MPI.COMM_WORLD.Get_rank()
13 
14 print("I am rank " , rank)
15 
16 
17 # Define basis tokens encoding and decoding dicts
18 encoding_dict = {"ns" : { "adult":0, "child":1, "smith":2, "surgeon":3 },
19  "v" : { "stand":0, "move":1, "sit":2, "sleep":3 },
20  "no" : { "inside":0,"outside":1 }
21  }
22 
23 decoding_dict = {"ns" : { 0:"adult", 1:"child", 2:"smith", 3:"surgeon"},
24  "v" : { 0:"stand", 1:"move", 2:"sit", 3:"sleep"},
25  "no" : { 0:"inside", 1:"outside"}
26  }
27 
28 len_reg_memory = 5
29 len_reg_auxiliary = len_reg_memory + 2
30 num_qubits = len_reg_memory + len_reg_auxiliary
31 num_bin_pattern = 8
32 
33 test_pattern = 0
34 
35 
36 # Create simulator object
37 
38 use_fusion = False
39 sim = p(num_qubits, use_fusion)
40 num_exps = 10000
41 normalise = True
42 
43 
44 # Set up registers to store indices
45 reg_memory = [0]*len_reg_memory;
46 for i in range(len_reg_memory):
47  reg_memory[i] = i
48 
49 reg_auxiliary = [0]*len_reg_auxiliary
50 for i in range(len_reg_auxiliary):
51  reg_auxiliary[i] = i + len_reg_memory;
52 
53 
54 # data for encoding using the given basis
55 "John rests outside, Mary walks inside"
56 sentences = [
57  [{"john" : [encoding_dict["ns"]["adult"], encoding_dict["ns"]["smith"]]},
58  {"rest" : [encoding_dict["v"]["sit"], encoding_dict["v"]["sleep"]]},
59  {"inside" : [encoding_dict["no"]["inside"]] }],
60 
61  [{"mary" : [encoding_dict["ns"]["child"], encoding_dict["ns"]["surgeon"]]},
62  {"walk" : [encoding_dict["v"]["stand"], encoding_dict["v"]["move"]]},
63  {"outside" :[encoding_dict["no"]["outside"]] }],
64 ]
65 if rank == 0:
66  print(sentences)
67 
68 # Create shift patterns for the encoding step
69 bit_shifts = [i[1] for i in q.utils.get_type_offsets(encoding_dict)]
70 bit_shifts.reverse()
71 
72 bit_shifts.insert(0,0)
73 bit_shifts = np.cumsum(bit_shifts)
74 
75 # Parse the sentences and generate the required bit-patterns
76 superpos_patterns = [list(sentences[0][i].values())[0] for i in range(3)]
77 
78 #Create list for the patterns to be encoded
79 vec_to_encode = []
80 
81 # Generate bit-patterns from sentences and store in vec_to_encode
82 for idx in range(len(sentences)):
83  superpos_patterns = [list(sentences[idx][i].values())[0] for i in range(3)]
84  # Generate all combinations of the bit-patterns for superpos states
85  for i in list(product(superpos_patterns[2], superpos_patterns[1], superpos_patterns[0])):
86  num = 0
87  for val in zip(i,bit_shifts):
88  num += (val[0] << val[1])
89  vec_to_encode.extend([num])
90 
91 print(vec_to_encode)
92 
93 # Init result counter
94 count_even = {}
95 for i in vec_to_encode:
96  count_even.update({i : 0})
97 
98 
99 # Test value to compare
100 #test_pattern = 0; # 00000 -> adult, stand, inside
101 test_pattern = 7; # 00111 -> adult, sleep, outside
102 
103 # Init result counter
104 count = {}
105 for i in vec_to_encode:
106  count.update({i : 0})
107 
108 # Repeated shots of experiment
109 for exp in tqdm(range(num_exps)):
110 
111  sim.initRegister()
112 
113  # Encode
114  sim.encodeBinToSuperpos_unique(reg_memory, reg_auxiliary, vec_to_encode, len_reg_memory)
115 
116  # Compute Hamming distance between test pattern and encoded patterns
117  sim.applyHammingDistanceRotY(test_pattern, reg_memory, reg_auxiliary, len_reg_memory)
118 
119  # Measure
120  sim.collapseToBasisZ(reg_auxiliary[len_reg_auxiliary-2], 1)
121 
122  val = sim.applyMeasurementToRegister(reg_memory, normalise)
123  if val in count:
124  count[val] += 1;
125  else:
126  count[val] = 1
127 
128 
129 if rank == 0 :
130  print("Experiments completed.")
131  print(count)
132