-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdevicePrep.py
109 lines (80 loc) · 3.7 KB
/
devicePrep.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# These functions can be used to create some of the required information for devices.py
import math
def makeExample ( num, pairs ):
# takes 'num' and 'pairs' for a device and returns 'example' (see devices.py for more on what these are)
example = [0]*num
from QuantumAwesomeness import getDisjointPairs
import random
matchingPairs = getDisjointPairs ( pairs, [], {} )
for pair in matchingPairs:
example[ pairs[pair][0] ] = random.random()/2
example[ pairs[pair][1] ] = example[ pairs[pair][0] ] + 0.05 * random.random()
return example
def makeLayout (pattern):
# The input is a string that specifies a type of pattern device ('line, 'ladder', 'square' or 'web') and the number of qubits
# This is done in the form: pattern = type + str(num)
# Given this information, this function determines the corresponding 'area', 'pairs, 'pos' and 'example for devices.py
# The type specifies the connectivity graph:
# - 'line' is a 1D, open-ended line connectivity
# - 'ladder' is a 2 X (num/2) open-ended square lattice (so num needs to be even)
# - 'square' is a sqrt(num) X sqrt(num) square lattice with open boundaries (so num needs to be square)
# - 'web' is all-to-all connectivity
if pattern[0:4]=="line":
num = int(pattern[4:])
if (num%2)==1: # we can continue only num is odd
area = [num,1]
entangleType = "CZ"
pairs = {}
for qubit in range(num-1):
pairs[chr(65+qubit)] = [qubit,qubit+1]
pos = {}
for qubit in range(num):
pos[qubit] = [qubit,0]
runs = {True:{'shots':[100],'move':['C','R'],'maxScore':20,'samples':100},False:{'shots':[],'move':[],'maxScore':0,'samples':0}}
else:
print("Try again with a valid number of qubits for this configuration")
elif pattern[0:6] in ["ladder","square"]:
num = int(pattern[6:])
if pattern[0:6]=="ladder":
Lx = int(num/2)
Ly = 2
good_num = ((num%2)==0) # we can continue only num is even
else:
Lx = int(math.sqrt(num))
Ly = Lx
good_num = (type(Lx) is int) # we can continue only num is square
if good_num:
area = [Lx,Ly]
entangleType = "CZ"
pairs = {}
pos = {}
char = 65
for y in range(Ly):
for x in range(Lx):
q = y*Lx+x
if x<(Lx-1): # pair for (x,y) and (x+1,y)
pairs[chr(char)] = [q,q+1]
char+=1
if y<(Ly-1): # pair for (x,y) and (x,y+Lx)
pairs[chr(char)] = [q,q+Lx]
char+=1
pos[q] = [x,y]
else:
print("Try again with a valid number of qubits for this configuration")
elif pattern[0:3]=="web":
num = int(pattern[3:])
L = int(math.sqrt(num))
area = [L,L]
pairs = {}
pos = {}
char = 65
for q0 in range(num-1):
for q1 in range(q0+1,num):
pairs[chr(char)] = [q0,q1]
char+=1
for q0 in range(num):
pos[q0] = [L*math.cos(q0*(2*math.pi)/num),L*math.sin(q0*(2*math.pi)/num)]
else:
print("\nWarning: " + str(device) + " is not a known device or pattern.\n")
example = makeExample ( num, pairs )
return area, pairs, pos, example