110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225 | class LRQAOASetupNode(Node):
"""Sets up a Linear Ramp QAOA solver (non-variational).
Expects in problem_data:
- cost_operator OR qubo_matrix
- num_qubits (optional, derived from qubo_matrix)
- reps: int (number of QAOA layers)
- delta_gamma: float
- delta_beta: float
- backend: Backend instance (optional, defaults to Qrisp simulator)
- mixer: callable (optional, defaults to RX mixer)
- shots: int (optional, defaults to 128)
Creates:
- hybrid_algorithm: LRQAOA instance
- hybrid_inputs: dict with cost_operator and num_qubits
- ising_offset: float
Modifications at runtime: None
"""
_known_children = ["HybridAlgorithmExecuteNode"]
def __init__(self, children: list) -> None:
super().__init__(
requires=[
"reps",
"delta_gamma",
"delta_beta",
["cost_operator", "qubo_matrix"],
["optional:", "backend", "shots", "num_qubits", "mixer"],
],
creates=["hybrid_algorithm", "hybrid_inputs", "ising_offset"],
children=children,
)
def execute(self, problem_data: dict, path_info: dict) -> dict:
if "num_qubits" not in problem_data:
if "qubo_matrix" in problem_data:
problem_data["num_qubits"] = len(problem_data["qubo_matrix"])
else:
raise ValueError(
"Cannot determine num_qubits: neither num_qubits nor "
"qubo_matrix found in problem_data."
)
if "cost_operator" not in problem_data:
if "qubo_matrix" not in problem_data:
raise ValueError("Either cost_operator or qubo_matrix must be in problem_data.")
qubo_matrix = np.asarray(problem_data["qubo_matrix"], dtype=float)
cost_operator, ising_offset = qubo_to_qrisp_cost_operator(qubo_matrix)
problem_data["cost_operator"] = cost_operator
problem_data["ising_offset"] = ising_offset
else:
if "ising_offset" not in problem_data:
problem_data["ising_offset"] = 0.0
mixer = problem_data.get("mixer")
shots = problem_data.get("shots", 128)
problem_data["hybrid_algorithm"] = LRQAOA(
reps=problem_data["reps"],
delta_gamma=problem_data["delta_gamma"],
delta_beta=problem_data["delta_beta"],
mixer=mixer,
shots=shots,
)
problem_data["hybrid_inputs"] = {
"cost_operator": problem_data["cost_operator"],
"num_qubits": problem_data["num_qubits"],
}
return dict()
def interpret_result(
self,
result: dict,
problem_data: dict,
next_node_info: dict | None = None,
config: dict | None = None,
) -> dict:
counts = result["raw"]
cl_cost_function = problem_data.get("cl_cost_function")
if cl_cost_function is not None:
best_bitstring = None
best_energy = float("inf")
total_shots = sum(counts.values())
expectation = 0.0
for bitstring, count in counts.items():
energy = cl_cost_function(bitstring)
expectation += energy * count / total_shots
if energy < best_energy:
best_energy = energy
best_bitstring = bitstring
result["eigenvalue"] = expectation
result["best_energy"] = best_energy
result["best_bitstring"] = best_bitstring
result["eigenstate"] = counts
else:
best_bitstring = max(counts, key=counts.get)
result["best_bitstring"] = best_bitstring
result["eigenstate"] = counts
logger.warning(
"No cl_cost_function in problem_data; cannot compute energies. "
"Reporting most frequent bitstring."
)
result["solution_bitstring"] = result["best_bitstring"]
if config and config.get("saving_policy") == "discard_if_processed":
result.setdefault("_no_save", set()).add("raw")
return result
|