-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
130 lines (106 loc) · 4.42 KB
/
functions.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import sympy
import numpy as np
import matplotlib.pyplot as plt
def read_file(file_name, instance):
"""Collect data from .txt file that characterizes the problem instance.
Args:
file_name (str): name of the file that contains the instance information.
instance (str): number of intance to use.
Returns:
control (list): important parameters of the instance.
[number of qubits, number of clauses, number of ones in the solution]
solution (list): list of the correct outputs of the instance for testing.
clauses (list): list of all clauses, with the qubits each clause acts upon.
"""
file = open('n{q}i{i}.txt'.format(q=file_name, i=instance), 'r')
control = list(map(int, file.readline().split()))
solution = list(map(str, file.readline().split()))
clauses = [list(map(int, file.readline().split())) for _ in range(control[1])]
return control, solution, clauses
def times(qubits, clauses):
"""Count the times each qubit appears in a clause to normalize H0.
Args:
qubits (int): # of total qubits in the instance.
clauses (list): clauses of the Exact Cover instance.
Returns:
times (list): number of times a qubit apears in all clauses.
"""
times = np.zeros(qubits)
for clause in clauses:
for num in clause:
times[num-1] += 1
return times
def h_problem(qubits, clauses):
"""Hamiltonian that satisfies all Exact Cover clauses.
Args:
qubits (int): # of total qubits in the instance.
clauses (list): clauses for an Exact Cover instance.
Returns:
sham (sympy.Expr): Symbolic form of the problem Hamiltonian.
smap (dict): Dictionary that maps the symbols that appear in the
Hamiltonian to the corresponding matrices and target qubits.
"""
z = sympy.symbols(" ".join((f"z{i}" for i in range(qubits))))
s = []
for x in z:
s.append((x**2, x))
smap = {s: i for i, s in enumerate(z)}
sham = sympy.expand(sum((sum(z[i - 1] for i in clause) - 1) ** 2 for clause in clauses))
sham = sham.subs(s)
return sham, smap
def h_weights(qubits, clauses, times):
"""Hamiltonian that satisfies all Exact Cover clauses.
Args:
qubits (int): # of total qubits in the instance.
clauses (list): clauses for an Exact Cover instance.
Returns:
sham (sympy.Expr): Symbolic form of the problem Hamiltonian.
smap (dict): Dictionary that maps the symbols that appear in the
Hamiltonian to the corresponding matrices and target qubits.
"""
z = sympy.symbols(" ".join((f"z{i}" for i in range(qubits))))
s = []
for x in z:
s.append((x**2, x))
smap = {s: i for i, s in enumerate(z)}
sham = sympy.expand(sum((sum(z[i - 1] for i in clause) - 1) ** 2 for clause in clauses))
sham = sham.subs(s)
return sham, smap
def symbolic_to_dwave(symbolic_hamiltonian, symbol_num):
"""Transforms a symbolic Hamiltonian to a dictionary of targets and matrices.
Works for Hamiltonians with one and two qubit terms only.
Args:
symbolic_hamiltonian: The full Hamiltonian written with symbols.
symbol_num: Dictionary that maps each symbol that appears in the
Hamiltonian to its target.
Returns:
Q (dict): Dictionary with the interactions to send to the DWAVE machine.
overall_constant (int): Constant that cannot be given to DWAVE machine.
"""
Q = {}
overall_constant = 0
for term in symbolic_hamiltonian.args:
if not term.args:
expression = (term,)
else:
expression = term.args
symbols = [x for x in expression if x.is_symbol]
numbers = [x for x in expression if not x.is_symbol]
if len(numbers) > 1:
raise ValueError("Hamiltonian must be expanded before using this method.")
elif numbers:
constant = float(numbers[0])
else:
constant = 1
if not symbols:
overall_constant += constant
elif len(symbols) == 1:
target = symbol_num[symbols[0]]
Q[(target, target)] = constant
elif len(symbols) == 2:
target1 = symbol_num[symbols[0]]
target2 = symbol_num[symbols[1]]
Q[(target1, target2)] = constant
else:
raise ValueError("Only one and two qubit terms are allowed.")
return Q, overall_constant