Esempio n. 1
0
 def test_equality_of_add(self):
     exp1 = Add(Qbit("a"), Qbit("b"))
     exp2 = Add(Qbit("b"), Qbit("a"))
     exp3 = Add(Qbit("a"), Qbit("a"))
     exp4 = Add(Qbit("a"), Qbit("b"))
     exp5 = Add(Qbit("a"), 1)
     self.assertTrue(exp1 == exp2)
     self.assertTrue(exp1 == exp4)
     self.assertTrue(hash(exp1) == hash(exp2))
     self.assertTrue(exp1 != exp3)
     self.assertFalse(exp1 == exp5)
     self.assertFalse(exp1 == Qbit("a"))
     self.assertFalse(exp1 == exp3)
     self.assertEqual(repr(exp1), "(Qbit(a)+Qbit(b))")
Esempio n. 2
0
 def test_compile_2nd_order(self):
     a, b = Qbit("a"), Qbit("b")
     exp = (Add(a, b) - 3)**2
     expected_qubo = {('a', 'a'): -5.0, ('a', 'b'): 2.0, ('b', 'b'): -5.0}
     expected_offset = 9.0
     expected_structure = {'a': ('a', ), 'b': ('b', )}
     self.compile_check(exp, expected_qubo, expected_offset,
                        expected_structure)
Esempio n. 3
0
with open('monroe_tract_dists.csv', mode='r') as inp:
    reader2 = csv.reader(inp)
    next(reader2)
    for row in reader2:
        t1_index = list(ccvi_dict.keys()).index(row[0])
        t2_index = list(ccvi_dict.keys()).index(row[2])
        edge_weight = 1/((float(ccvi_dict[row[0]])-float(ccvi_dict[row[2]]))*float(row[1]))
        W[t1_index, t2_index] = edge_weight

#Array of Tract Spins
T = Array.create('T', shape=n, vartype='SPIN')

#Sum of all Spins
sumT = T[0]
for i in range(1, len(T)):
    sumT = Add(sumT, T[i])

#Constraint Function
conFun = (sumT - (N - (n - N)))**2

#Objective Function
objFun = 0
for i in range(n):
    for j in range(n):
        objFun += W[i][j]*T[i]*T[j]

#Hamiltonian
H = objFun + conFun

#Create Ising Model
model = H.compile()
Esempio n. 4
0
 def test_express_error(self):
     self.assertRaises(ValueError, lambda: 2 / Qbit("a"))
     self.assertRaises(ValueError, lambda: Qbit("a") / Qbit("a"))
     self.assertRaises(ValueError, lambda: Qbit("a") ** 1.5)
     self.assertRaises(ValueError, lambda: Mul(1, Qbit("b")))
     self.assertRaises(ValueError, lambda: Add(1, Qbit("b")))
Esempio n. 5
0
 def test_express_error(self):
     self.assertRaises(ValueError, lambda: 2 / Binary("a"))
     self.assertRaises(ValueError, lambda: Binary("a") / Binary("a"))
     self.assertRaises(ValueError, lambda: Binary("a")**1.5)
     self.assertRaises(ValueError, lambda: Mul(1, Binary("b")))
     self.assertRaises(ValueError, lambda: Add(1, Binary("b")))